Reputation: 2553
I have a regular expression for IP address with optional portnumber. I now need to change it so that it will match if nothing or if something then it must be a valid IP with optional portnumber.
My regex is...
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b[.: ]?[0-9]?[0=9]?[0-9]?[0-9]?[0=9]?
Note: My check for optional port number is not ideal, I know, as it is possible to enter 99999 whereas the max port number is 65535, but I can live with that at the moment.
Upvotes: 1
Views: 959
Reputation: 338148
The regex number range generator I found creates for 0 .. 255
0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])
and for 1 .. 65535
:
0*([1-9][0-9]{0,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])
Taking the first bit, your IP address regex can be shortened to
(?:0*(?:[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?:\.|\b)){4}
So the general structure of the expression would be
OCTET_EXPRESSION ::= 0*(?:[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]) PORT_EXPRESSION ::= 0*(?:[1-9][0-9]{0,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]) ( (?: OCTET_EXPRESSION (?:\.|\b) ){4} ) ( (?: : PORT_EXPRESSION )? )
giving two groups (IP and port, if applicable).
^((?:0*(?:[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?:\.|\b)){4})(?::(0*(?:[1-9][0-9]{0,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])))?$
http://rubular.com/r/IPNJrTp06y
If you can, use a small parser. Splitting on .
and :
and checking the validity of the resulting numeric ranges is tivial in any programming language.
Upvotes: 1
Reputation: 7433
Since each octet or whatever you call it maxes out at 255, and an IP address is either 4 [or 6] sets, why not parse the string into an array and just check that way?
For example:
var ipAddress = "111.11.0.255:1234";
var ipAndPort = ipAddress.Split(':');
var ipArray = ipAndPort[0].Split('.');
var port = ipAndPort.Length == 2 ? ipAndPort[1] : 80;
for (var i = 0; i < 4; i++)
{
switch (i)
{
case 0:
if (ipArray[i] <= 0 || ipArray[i] > 255)
return false;
break;
default:
if (ipArray[i] < 0 || ipArray[i] > 255)
return false;
break;
}
if (port > 65535 || port < 1)
return false;
return true;
}
Upvotes: 0
Reputation: 129497
Just add a |
at the end:
current_regex|
This matches current_regex
or the empty string.
Upvotes: 0
Reputation: 11182
Try this:
^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:[.:]\b([1-9][0-9]{0,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]))?$
Explanation
# ^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:[.:]\b([1-9][0-9]{0,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]))?$
#
# Assert position at the beginning of the string «^»
# Match the regular expression below «(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}»
# Exactly 3 times «{3}»
# Match the regular expression below «(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])»
# Match either the regular expression below (attempting the next alternative only if this one fails) «25[0-5]»
# Match the characters “25” literally «25»
# Match a single character in the range between “0” and “5” «[0-5]»
# Or match regular expression number 2 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
# Match the character “2” literally «2»
# Match a single character in the range between “0” and “4” «[0-4]»
# Match a single character in the range between “0” and “9” «[0-9]»
# Or match regular expression number 3 below (attempting the next alternative only if this one fails) «1[0-9][0-9]»
# Match the character “1” literally «1»
# Match a single character in the range between “0” and “9” «[0-9]»
# Match a single character in the range between “0” and “9” «[0-9]»
# Or match regular expression number 4 below (the entire group fails if this one fails to match) «[1-9]?[0-9]»
# Match a single character in the range between “1” and “9” «[1-9]?»
# Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
# Match a single character in the range between “0” and “9” «[0-9]»
# Match the character “.” literally «\.»
# Match the regular expression below «(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])»
# Match either the regular expression below (attempting the next alternative only if this one fails) «25[0-5]»
# Match the characters “25” literally «25»
# Match a single character in the range between “0” and “5” «[0-5]»
# Or match regular expression number 2 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
# Match the character “2” literally «2»
# Match a single character in the range between “0” and “4” «[0-4]»
# Match a single character in the range between “0” and “9” «[0-9]»
# Or match regular expression number 3 below (attempting the next alternative only if this one fails) «1[0-9][0-9]»
# Match the character “1” literally «1»
# Match a single character in the range between “0” and “9” «[0-9]»
# Match a single character in the range between “0” and “9” «[0-9]»
# Or match regular expression number 4 below (the entire group fails if this one fails to match) «[1-9]?[0-9]»
# Match a single character in the range between “1” and “9” «[1-9]?»
# Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
# Match a single character in the range between “0” and “9” «[0-9]»
# Match the regular expression below «(?:[.:]\b([1-9][0-9]{0,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]))?»
# Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
# Match a single character present in the list “.:” «[.:]»
# Assert position at a word boundary «\b»
# Match the regular expression below and capture its match into backreference number 1 «([1-9][0-9]{0,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])»
# Match either the regular expression below (attempting the next alternative only if this one fails) «[1-9][0-9]{0,3}»
# Match a single character in the range between “1” and “9” «[1-9]»
# Match a single character in the range between “0” and “9” «[0-9]{0,3}»
# Between zero and 3 times, as many times as possible, giving back as needed (greedy) «{0,3}»
# Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[1-5][0-9]{4}»
# Match a single character in the range between “1” and “5” «[1-5]»
# Match a single character in the range between “0” and “9” «[0-9]{4}»
# Exactly 4 times «{4}»
# Or match regular expression number 3 below (attempting the next alternative only if this one fails) «6[0-4][0-9]{3}»
# Match the character “6” literally «6»
# Match a single character in the range between “0” and “4” «[0-4]»
# Match a single character in the range between “0” and “9” «[0-9]{3}»
# Exactly 3 times «{3}»
# Or match regular expression number 4 below (attempting the next alternative only if this one fails) «65[0-4][0-9]{2}»
# Match the characters “65” literally «65»
# Match a single character in the range between “0” and “4” «[0-4]»
# Match a single character in the range between “0” and “9” «[0-9]{2}»
# Exactly 2 times «{2}»
# Or match regular expression number 5 below (attempting the next alternative only if this one fails) «655[0-2][0-9]»
# Match the characters “655” literally «655»
# Match a single character in the range between “0” and “2” «[0-2]»
# Match a single character in the range between “0” and “9” «[0-9]»
# Or match regular expression number 6 below (the entire group fails if this one fails to match) «6553[0-5]»
# Match the characters “6553” literally «6553»
# Match a single character in the range between “0” and “5” «[0-5]»
# Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
Upvotes: 1