I need to validate an IP range that is in format 000000000 to 255255255 without any delimiters between the 3 groups of numbers. Each of the three groups that the final IP consists of should be 000 (yes, 0 padded) to 255.
As this is my 1st stackoverflow entry, please be lenient if I did not follow etiquette correctly.
^([01]\d{2}|2[0-4]\d|25[0-5]){3}$
Which breaks down in the following parts:
If you decide you want 4 octets instead of 3, just change the last {3}
to {4}
. Also, you should be aware of IPv6 too.
I would personally not use regex for this. I think it's easier to ensure that the string consists of 9 digits, split up the string into 3 groups of 3-digit numbers, and then check that each number is between 0 and 255, inclusive.
If you really insist on regex, then you could use something like this:
"([0-1][0-9][0-9]|2[0-4][0-9]|25[0-5]){3}"
The expression comprises an alternation of three terms: the first matches 000-199
, the second 200-249
, the third 250-255
. The {3}
requires the match exactly three times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With