如何匹配小于或等于100的数字? [英] how to match a number which is less than or equal to 100?

查看:299
本文介绍了如何匹配小于或等于100的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配一个小于或等于100的数字,它可以是0到100之间的任何数字,但是正则表达式不应该匹配大于100的数字,例如120、130、150、999,等

I want to match a number which is less than or equal to 100, it can be anything within 0-100, but the regex should not match for a number which is greater than 100 like 120, 130, 150, 999, etc.

推荐答案

尝试一下

\b(0*(?:[1-9][0-9]?|100))\b

说明

"
\b                # Assert position at a word boundary
(                 # Match the regular expression below and capture its match into backreference number 1
   0              # Match the character "0" literally
      *           # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   (?:            # Match the regular expression below
                  # Match either the regular expression below (attempting the next alternative only if this one fails)
         [1-9]    # Match a single character in the range between "1" and "9"
         [0-9]    # Match a single character in the range between "0" and "9"
            ?     # Between zero and one times, as many times as possible, giving back as needed (greedy)
      |           # Or match regular expression number 2 below (the entire group fails if this one fails to match)
         100      # Match the characters "100" literally
   )
)
\b                # Assert position at a word boundary
"

这篇关于如何匹配小于或等于100的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆