【问题标题】:Regex statement for only numbers between 0 and 255 in C#C# 中仅适用于 0 到 255 之间的数字的正则表达式语句
【发布时间】:2012-05-21 09:33:34
【问题描述】:

如何只为 0 到 255 之间的数字编写正则表达式语句? 0 和 255 对语句有效。

【问题讨论】:

    标签: c# regex


    【解决方案1】:

    您可以在这里找到一些数字范围:

    http://www.regular-expressions.info/numericranges.html

    你的例子是:

    ^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$ 
    

    【讨论】:

      【解决方案2】:
      ^([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
      

      This tool 对这些事情很有帮助。一点点搜索也不会伤害任何人。

      如果您想允许前导零,则需要调整模式。例如:

      ^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$
      

      【讨论】:

        【解决方案3】:

        尝试消极地看待背后:

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

        说明

        <!--
        (?<!\-)\b0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b
        
        Options: ^ and $ match at line breaks
        
        Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!\-)»
           Match the character “-” literally «\-»
        Assert position at a word boundary «\b»
        Match the character “0” literally «0*»
           Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
        Match the regular expression below and capture its match into backreference number 1 «([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])»
           Match either the regular expression below (attempting the next alternative only if this one fails) «[0-9]{1,2}»
              Match a single character in the range between “0” and “9” «[0-9]{1,2}»
                 Between one and 2 times, as many times as possible, giving back as needed (greedy) «{1,2}»
           Or match regular expression number 2 below (attempting the next alternative only if this one fails) «1[0-9]{2}»
              Match the character “1” literally «1»
              Match a single character in the range between “0” and “9” «[0-9]{2}»
                 Exactly 2 times «{2}»
           Or match regular expression number 3 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 4 below (the entire group fails if this one fails to match) «25[0-5]»
              Match the characters “25” literally «25»
              Match a single character in the range between “0” and “5” «[0-5]»
        Assert position at a word boundary «\b»
        -->
        

        【讨论】:

          【解决方案4】:

          试试

          ([01]?\d\d?|2[0-4]\d|25[0-5])
          

          【讨论】:

            猜你喜欢
            • 2011-09-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-26
            • 1970-01-01
            相关资源
            最近更新 更多