Regular expression where part of string must be number between 0-100

后端 未结 7 752
有刺的猬
有刺的猬 2020-12-02 02:23

I need to validate serial numbers. For this we use regular expressions in C#, and a certain product, part of the serial number is the \"seconds since midnight\". There are

7条回答
  •  情深已故
    2020-12-02 02:43

    Generate a Regular Expression to Match an Arbitrary Numeric Range http://utilitymill.com/utility/Regex_For_Range

    yields the following regex expression:

    \b0*([0-9]{1,4}|[1-7][0-9]{4}|8[0-5][0-9]{3}|86[0-3][0-9]{2}|86400)\b
    

    Description of output:

    First, break into equal length ranges:
      0 - 9
      10 - 99
      100 - 999
      1000 - 9999
      10000 - 86400
    
    Second, break into ranges that yield simple regexes:
      0 - 9
      10 - 99
      100 - 999
      1000 - 9999
      10000 - 79999
      80000 - 85999
      86000 - 86399
      86400 - 86400
    
    Turn each range into a regex:
      [0-9]
      [1-9][0-9]
      [1-9][0-9]{2}
      [1-9][0-9]{3}
      [1-7][0-9]{4}
      8[0-5][0-9]{3}
      86[0-3][0-9]{2}
      86400
    
    Collapse adjacent powers of 10:
      [0-9]{1,4}
      [1-7][0-9]{4}
      8[0-5][0-9]{3}
      86[0-3][0-9]{2}
      86400
    
    Combining the regexes above yields:
      0*([0-9]{1,4}|[1-7][0-9]{4}|8[0-5][0-9]{3}|86[0-3][0-9]{2}|86400)
    

    Tested here: http://osteele.com/tools/rework/

提交回复
热议问题