1

我有一个带有可选端口号的 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,但我现在可以接受。

4

4 回答 4

1

尝试这个:

^(?:(?: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-01T13:46:14.237 回答
1

我发现的正则表达式数字范围生成器为0 .. 255

0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])

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])

首先,您的 IP 地址正则表达式可以缩短为

(?:0*(?:[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])(? :\.|\b)){4}

所以表达式的一般结构是

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
  )?
)

给出两组(IP 和端口,如果适用)。

^((?: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

如果可以,请使用小型解析器。在任何编程语言中,拆分.:检查结果数字范围的有效性都是很重要的。

于 2013-08-01T14:17:01.393 回答
0

由于每个八位字节或任何你称之为的最大值为 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;
}
于 2013-08-01T13:54:22.450 回答
0

只需|在末尾添加一个:

当前正则表达式|

这匹配current_regex或空字符串。

于 2013-08-01T13:47:24.050 回答