我有一个带有可选端口号的IP地址的正则表达式。我现在需要更改它,以便它将匹配,如果没有,或者如果有什么东西,那么它必须是一个有效的IP和可选的端口号。
我的判罚是..。
\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]?注意:我检查的可选端口号并不理想,我知道,因为输入99999是可能的,而最大端口号是65535,但我现在可以接受。
发布于 2013-08-01 13:46:14
尝试如下:
^(?:(?: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]))?$解释
# ^(?:(?: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) «$»发布于 2013-08-01 13:54:22
既然每个八进制或任何你所称的值都是255,一个IP地址要么是4组,要么是6组,为什么不把字符串解析成一个数组,然后检查一下呢?
例如:
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;
}https://stackoverflow.com/questions/17995784
复制相似问题