RegEx匹配表达式或什么也没有

时间:2013-08-01 13:43:56

标签: regex

您好我有一个带有可选portnumber的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 个答案:

答案 0 :(得分: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) «$»

答案 1 :(得分:1)

我找到的regex number range generator0 .. 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

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

答案 2 :(得分:0)

最后添加|

current_regex|

这匹配 current_regex 或空字符串。

答案 3 :(得分: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;
}