RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$
Description
RegExp for validating the format of IP Addresses. This works great with the ASP.NET RegularExpressionValidator server control.
Matches
127.0.0.1 | 255.255.255.0 | 192.168.0.1
Non-Matches
1200.5.4.3 | abc.def.ghi.jkl | 255.foo.bar.1
Author Rating: The rating for this expression. G. Andrew Duthie
Source Cobbled this together, primarily from a message on the PHPBuilder forum, with my own mods to allow matches on 127.0.0.1, etc.
Your Rating
Bad Good

Enter New Comment

Title

Name

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: regex that does not allow values prefix with zero
Name: avi
Date: 11/26/2014 3:14:25 AM
Comment:
can i have an regular expression that does not allowed values prefix with 0 like 01.090.08.02 or 000.000.000.000 or 002.01.000.098


Title: regex that does not allow values prefix with zero
Name: avi
Date: 11/26/2014 3:12:12 AM
Comment:
can i have an regular expression that does not allowed values prefix with 0 like 01.090.08.02 or 000.000.000.000 or 002.01.000.098


Title: regex that does not allow values prefix with zero
Name: avi
Date: 11/26/2014 3:11:06 AM
Comment:
can i have an regular expression that does not allowed values prefix with 0 like 01.090.08.02 or 000.000.000.000 or 002.01.000.098


Title: 10.169.x.x/16
Name: Dhiyaa
Date: 10/30/2014 3:01:33 AM
Comment:
if any one can help me, I need expression for 10.169.x.x/16


Title: 10.169.x.x/16
Name: Dhiyaa
Date: 10/30/2014 2:18:25 AM
Comment:
if any one can help me, I need expression for 10.169.x.x/16


Title: 10.169.x.x/16
Name: Dhiyaa
Date: 10/30/2014 2:17:40 AM
Comment:
if any one can help me, I need expression for 10.169.x.x/16


Title: 10.169.x.x/16
Name: Dhiyaa
Date: 10/29/2014 10:14:41 AM
Comment:
if any one can help me, I need expression for 10.169.x.x/16


Title: Pattern Based on IP Classes
Name: Murat Uzundede
Date: 1/1/2014 1:49:17 AM
Comment:
"^(2[0-2][0-3]|1\d{2}|\d((?<!0)\d)|[1-9])\.((25[0-5]|2[0-4]\d|1\d{2}|\d((?<!0)\d)|\d)\.){2}(25[0-4]|2[0-4]\d|1\d{2}|\d((?<!0)\d)|[1-9])$" This regular expression validates if an ip address A(First octet 1-126), B (First octet 127-191) and C (First octet 192-223) ip classes. It will turn false for subnet addresses and broadcast addresses Class A ip address sample 1.0.0.1 Valid 01.0.0.1 not valid 1.0.0.01 not valid 10.0.0.0 Not valid 10.0.0.255 Not valid Class B ip address sample 128.0.0.1 valid ip address 128.00.0.1 not valid 128.0.0.0 not valid 191.255.255.1 valid 191.255.255.255 not valid Class C ip address 192.0.0.1 valid 192.255.255.1 valid 192.255.0.255 not valid 223.0.0.1 valid All ip addresses between 224.0.0.0 to 255.255.255.255 will result as not valid ip address


Title: Pattern Based on IP Classes
Name: Murat Uzundede
Date: 1/1/2014 12:40:38 AM
Comment:
"^(2[0-2][0-3]|1\d{2}|\d((?<!0)\d)|[1-9])\.((25[0-5]|2[0-4]\d|1\d{2}|\d((?<!0)\d)|\d)\.){2}(25[0-4]|2[0-4]\d|1\d{2}|\d((?<!0)\d)|[1-9])$" This regular expression validates if an ip address A(First octet 1-126), B (First octet 127-191) and C (First octet 192-223) ip classes. It will turn false for subnet addresses and broadcast addresses Class A ip address sample 1.0.0.1 Valid 01.0.0.1 not valid 1.0.0.01 not valid 10.0.0.0 Not valid 10.0.0.255 Not valid Class B ip address sample 128.0.0.1 valid ip address 128.00.0.1 not valid 128.0.0.0 not valid 191.255.255.1 valid 191.255.255.255 not valid Class C ip address 192.0.0.1 valid 192.255.255.1 valid 192.255.0.255 not valid 223.0.0.1 valid All ip addresses between 224.0.0.0 to 255.255.255.255 will result as not valid ip address


Title: IP Validation Pattern
Name: Murat Uzundede
Date: 6/21/2013 4:43:39 PM
Comment:
This is what I came up with "^(((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2})|([0-9]{1,2}))$"


Title: Thank you
Name: Ashraf Sabry
Date: 11/20/2012 2:24:09 AM
Comment:
It seems to work well


Title: short variant
Name: abz
Date: 10/26/2011 5:39:52 AM
Comment:
^(((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{2}|[\d])[.]){3}((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{2}|[\d])))$


Title: short variant
Name: abz
Date: 10/26/2011 5:31:41 AM
Comment:
^(((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{2}|[\d])[.]){3}((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{2}|[\d])))$


Title: Fixed some bugs
Name: Lin.Lu.China
Date: 7/25/2009 4:57:12 AM
Comment:
Thanks to Duthie's work. I have fixed some bugs,023.44.33.22 Here is my code: ^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$ If someone wants to allow 0.0.0.0, try this one, ^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$


Title: Fixed some bugs
Name: Lin.Lu.China
Date: 7/24/2009 11:27:45 PM
Comment:
Thanks to Duthie's work. I have fixed some bugs,023.44.33.22 Here is my code: ^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$ If someone wants to allow 0.0.0.0, try this one, ^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$


Title: Fixed some bugs
Name: Lin.Lu.China
Date: 7/24/2009 10:51:50 PM
Comment:
Thanks to Duthie's work. I have fixed some bugs,023.44.33.22 Here is my code: ^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$ If someone wants to allow 0.0.0.0, try this one, ^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$


Title: Where is the source code.........
Name: Ankit
Date: 9/8/2008 9:24:51 AM
Comment:
Where is the source code.........How can I get the source code of this Validation..........


Title: Pattern Title
Name: miksa
Date: 11/6/2006 6:34:08 AM
Comment:
Very good. Checks also that there is no extra marks after the last part of the IP address (many times our users entered dot after last part).


Title: Alternative
Name: snabbi
Date: 8/15/2006 7:52:36 AM
Comment:
The regex could be shorter than that: /^(?:(?:[01]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d?\d|2[0-4]\d|25[0-5])$/


Title: error tip to right pattern
Name: peta
Date: 8/13/2006 3:24:59 AM
Comment:
(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]) 255-250 249-200 199-100 99-10 9-0 ^(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]))$


Title: error
Name: peta
Date: 8/13/2006 3:16:54 AM
Comment:
Non-Matching Text 1200.5.4.3|||abc.def.ghi.jkl|||255.foo.bar.1 BUT MATCH: 192.068.0.1


Title: Suggestion ?
Name: Jaymes B
Date: 7/5/2006 12:34:40 PM
Comment:
What about 192.168.1.0 or 192.168.1.255 Technically those are not assignable IP addresses. Would it be possible to modify the regex to account for that? Maybe a separate regex for subnets ? Or are we just checking for form :)


Title: Buggy
Name: Jrf
Date: 9/21/2005 3:28:26 PM
Comment:
BEWARE: Regex is buggy as it wrongly allows 023.44.33.22 and doesn't allow 0.0.0.0.


Title: RE: Quick Question
Name: G. Andrew Duthie
Date: 8/28/2003 8:52:15 PM
Comment:
I honestly don't know. As I mentioned in the Source comment, I put this together from prior art, so I'm not sure whether allowing leading zeros was intentional or not. Sorry I can't offer better insight.


Title: Quick Question
Name: micah
Date: 8/28/2003 8:42:32 PM
Comment:
Do some systems pad octets with zeros? You are allowing 3 digit octets to have leading zeros but not 2 digit octets in such a way that it doesn't look like a mistake, but like your intention. Only reason I can come up with is if systems pad. Otherwise I would change the [0-1]{1}[0-9]{2} section to 1[0-9]{2}


Title: perl regexp
Name: tassos
Date: 5/28/2003 8:44:06 AM
Comment:
excellent!!!!


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials