Matching IPv4 addresses with RegExp
Published at 2023-07-04
Last update over 365 days ago
Licensed under CC BY-NC-SA 4.0
python
regex
software-engineering
programming-language
ipv4
While solving the NUM3RS problem of CS50P, I was asked to match IPv4 addresses with RegExp.
A valid IPv4 address should be like this:
where
RegExp doesn’t support the direct expression of a number range. We have to do it some other way.
The 0-255 Part
After some analysis, we can easily see that we can do the 0-255 part this way:
and the RegExp should be like this:
[0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|25[0-5]
or
\d|([1-9]\d)|(1\d\d)|(2[0-4]\d)|(25[0-5])
The 0-65525 Part
With similar method, we can write the 0-65535 part like this:
[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]
Concatenate Them
^((([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|25[0-5])\.){3}([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|25[0-5]))((:([0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?)$