Regular Expression To Match IPv4 Addresses

I needed a regex pattern to match IPv4 addresses today. I thought that this is quite common thing and it will be easy to find one on the Net. I have found a lot of them indeed. Unfortunately no one was good enough. Most of them was too simple and could match a string which is not IPv4 address. On the other hand, those more specific didn't match some fully legal IPv4 addresses (for example the one from regexlib.com). So I have developed my own:
This pattern matches all (I hope) IPv4 addresses in dot-decimal notation (first line) and network blocks in CIDR notation (when you join second line).


Anonymous said...

Thanks for the regex!

Anonymous said...

I made a small change....

see with regular IPv4 your regex matched an address like 400.123.34.56

but I liked your thinking... so I reversed your . and added the beginning marker.

This seems to make sure that you don't have the one I mentioned above and/or one like

(your regexp matched both of them.)

Hope this helps someone!

Shane said...

Thanks for the regex, it is good. It can be shortened very slightly:


Here we recognize that:


Can matches a lot of cases we don't care about, and simplify to:


This approach was also taken in one of the comments. (I do not recommend the regex there, as it allows leading zeros in some cases.)

We also convert:




This new regex is 6 characters shorter and matches an identical set. No idea about the performance. ;)