Brilliant
Staff member
- Joined
- Dec 31, 2024
- Messages
- 380
- Reaction score
- 7
- Points
- 18
- User icon
- <svg xmlns="http://www.w3.org/2000/svg" height="14" width="15.75" viewBox="0 0 576 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path fill="#63E6BE" d="M309 106c11.4-7 19-19.7 19-34c0-22.1-17.9-40-40-40s-40 17.9-40 40c0 14.4 7.6 27 19 34L209.7 220.6c-9.1 18.2-32.7 23.4-48.6 10.7L72 160c5-6.7 8-15 8-24c0-22.1-17.9-40-40-40S0 113.9 0 136s17.9 40 40 40c.2 0 .5 0 .7 0L86.4 427.4c5.5 30.4 32 52.6 63 52.6l277.2 0c30.9 0 57.4-22.1 63-52.6L535.3 176c.2 0 .5 0 .7 0c22.1 0 40-17.9 40-40s-17.9-40-40-40s-40 17.9-40 40c0 9 3 17.3 8 24l-89.1 71.3c-15.9 12.7-39.5 7.5-48.6-10.7L309 106z"/></svg>
To match a unicast IPv4 address (without specifying a port) using a regular expression (regex), here’s a solution that considers valid unicast IPv4 addresses.
---
Regex Rule
Explanation
1. Match each octet of an IPv4 address:
25[0-5]: Matches numbers from 250 to 255.
2[0-4][0-9]: Matches numbers from 200 to 249.
1[0-9]{2}: Matches numbers from 100 to 199.
[1-9]?[0-9]: Matches numbers from 0 to 99.
Combine with dots (\.) for all four octets.
2. Match unicast IPv4:
Unicast IPv4 addresses range from 1.0.0.0 to 223.255.255.255, excluding multicast and special addresses:
0.x.x.x is invalid (reserved).
127.x.x.x is reserved for loopback.
224.x.x.x and above are multicast or reserved.
However, the regex above does not enforce strict unicast validation—it simply ensures valid IPv4 format. To enforce unicast rules programmatically, filter the matches in your code.
3. Avoid ports:
Ensure the regex does not capture ports 1234) by using \b (word boundaries).
---
Usage in Code
JavaScript Example
Strict Unicast Validation
If you want to strictly enforce unicast IPv4 ranges, add an additional programmatic check after matching:
1. Ensure the first octet is between 1 and 223.
2. Exclude 127.x.x.x.
JavaScript Example with Unicast Check
Conclusion
Use the regex to match valid IPv4 addresses.
Add programmatic logic for stricter rules like unicast validation.
The
regex avoids ports by using \b and does not capture strings like 192.168.1.1:8080.
---
Regex Rule
Explanation
1. Match each octet of an IPv4 address:
25[0-5]: Matches numbers from 250 to 255.
2[0-4][0-9]: Matches numbers from 200 to 249.
1[0-9]{2}: Matches numbers from 100 to 199.
[1-9]?[0-9]: Matches numbers from 0 to 99.
Combine with dots (\.) for all four octets.
2. Match unicast IPv4:
Unicast IPv4 addresses range from 1.0.0.0 to 223.255.255.255, excluding multicast and special addresses:
0.x.x.x is invalid (reserved).
127.x.x.x is reserved for loopback.
224.x.x.x and above are multicast or reserved.
However, the regex above does not enforce strict unicast validation—it simply ensures valid IPv4 format. To enforce unicast rules programmatically, filter the matches in your code.
3. Avoid ports:
Ensure the regex does not capture ports 1234) by using \b (word boundaries).
---
Usage in Code
JavaScript Example
Strict Unicast Validation
If you want to strictly enforce unicast IPv4 ranges, add an additional programmatic check after matching:
1. Ensure the first octet is between 1 and 223.
2. Exclude 127.x.x.x.
JavaScript Example with Unicast Check
Conclusion
Use the regex to match valid IPv4 addresses.
Add programmatic logic for stricter rules like unicast validation.
The
regex avoids ports by using \b and does not capture strings like 192.168.1.1:8080.