Regular Expression Details

Accurate IP address verification...
Each numeric part of an IP address must be between 0 and 255. All patterns I have seen on the net cover this range but they allow leading zeros. The valid cases are:
* 0
* 1, 10-19 and 100-199 aka 1[0-9]{0,2}
* 2 and 20-29 aka 2[0-9]?
* 200-249 aka 2[0-4][0-9]
* 250-255 aka 25[0-5]
* 3-9 and 30-99 aka [3-9][0-9]?

Enter New Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Allows invalid IP addressesName: Dan RDate: 2/24/2010 12:59:09 PMComment: Allows 0.1.1.1 and 1.0.0.0. While 1.1.0.0 may be a valid IP (depending on mask) 1.0.0.0 will never be valid, nor would 0.1.1.1.

Title: General concept for long version patternsName: Vassilis PetrouliasDate: 12/30/2005 4:46:04 AMComment: Thank you for your comment. In general I use long version and syntax when I distribute patterns while this way regex newbies can easily visualize the logic of a pattern. On the other hand experience developers can short them at dead time.

Title: A shorter versionName: Jesse HouwingDate: 12/29/2005 7:13:45 PMComment: This does the same, but is a little shorter. I personally find it easier to read (though the part covering 0-99 is arguably harder).
^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$