RegEx:如何匹配大于X100.00的所有数字?

时间:2018-05-15 00:27:50

标签: regex

RegEx:如何匹配大于X100.00的所有数字?

1 个答案:

答案 0 :(得分:1)

要匹配大于100.00且最多两位小数的数字,您可以使用以下正则表达式模式:

(?<!\.)(?:1(?!00(?!\.?\d))|[2-9])[0-9]\d+(?:\.\d{1,2})?

正则表达式模式的说明:

(?<!                        # Start of negative Lookbehind.
    \.                      # Matches the character '.' literally.
)                           # End of the negative Lookbehind.
(?:                         # Start of first non-capturing group.
    1                       # Matches the character '1' literally.
    (?!                     # Start of first negative Lookahead.
        00                  # Matches the characters '00' literally.
        (?!                 # Start of second negative Lookahead.
            \.              # Matches the character '.' literally.
            ?               # Matches between zero and one times of the previous char.
            \d              # Matches any numeric character.
        )                   # End of first negative Lookahead.
    )                       # End of second negative Lookahead.
    |[2-9]                  # Or any number between 2 and 9.
)                           # End of the non-capturing group.
[0-9]                       # Matches any number between 0 and 9.
\d+                         # Matches one or more numeric characters.
(?:                         # Start of second non-capturing group.
    \.                      # Matches the character '.' literally.
    \d                      # Matches any numeric character.
    {1,2}                   # Matches between one and two times of the previous char.
)                           # End of the second non-capturing group.
?                           # Matches between zero and one times of the previous group.

表示:

  

查找[未跟随00 的数字1,除非他们后跟更多数字] [2到9之间的任何数字] < strong>后跟 0到9之间的数字,然后是任意数量的数字,包括可选小数点,后跟最多两位小数。 但等等,请确保整个事件前面没有小数点(即点)。

Try it online

请注意,这将与123.45中的123.4567匹配。

  • 如果您想匹配两个以上的小数位,可以删除最后的{1,2}
  • 如果您想要阻止匹配数字,如果它有两个以上的小数位,您可以在(?!\d)之后添加额外的否定前瞻{1,2}

希望有所帮助。