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

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$
Description
This matches an IP address, putting each number in its own group that can be retrieved by number. If you do not care about capturing the numbers, then you can make this shorter by putting everything after ^ until immediately after the first \. in a group ( ) with a {3} after it. Then put the number matching regex in once more. It only permits numbers in the range 0-255.
Matches
0.0.0.0 | 255.255.255.02 | 192.168.0.136
Non-Matches
256.1.3.4 | 023.44.33.22 | 10.57.98.23.
Author Rating: The rating for this expression. Andrew Polshaw
Source
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: Got a shortened version working for Linux Bash scripts
Name: Thomas
Date: 8/9/2019 10:15:54 AM
Comment:
I could not get the regex to work in my bash script, so I had to update it. It is the sortened version that James entered but with \d replaced by []. I also included what Eger had suggested, there can not be a leading 0 in any octet. ^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\.([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}$


Title: Your Shortened version is so good you should list it separately
Name: James Glenski
Date: 1/4/2018 6:42:57 PM
Comment:
I like the shortened version. ^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$ Works like a champ.


Title: Thanks
Name: John
Date: 7/3/2010 7:12:39 PM
Comment:
Thanks for the regex Andrew and thanks to eger for the updated copy (thanks 5 years later, hehe). :)


Title: updated version
Name: eger
Date: 5/6/2005 4:54:07 PM
Comment:
I was having similar problems as sergeya suggested. so i went ahead and modified Andrews a bit to be more strict. This one will not allow 0 as the first octect, 0 in front of any octet number, or multiple 0's: ^([1-9]{1}\d{1}|[1-9]{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1}|[1-9]{1}\d{1}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1}|[1-9]{1}\d{1}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1}|[1-9]{1}\d{1}|1\d\d|2[0-4]\d|25[0-5])$


Title: prob with this regexp
Name: sergeya
Date: 6/10/2004 8:21:18 PM
Comment:
allows 01.01.01.01 - does not check for 0 in 2-digit groups


Title: Shortening?
Name: Ri
Date: 2/15/2004 3:53:45 AM
Comment:
Could you be so kind as to give a snippet of this shortening you speak of in the description?


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