我的要求是一个正则表达式,它接受0到100之间的十进制值(如1,2,3,....,99,0.1,0.2,0.3,.....,99.9,0.01,0.02, 0.03,.....,99.99,00.01至99.99)。我找到了一个解决方案
/^(?!0?0\.00$)\d{1,2}\.\d{2}$/
但它只接受小数值,如00.01到99.99。
答案 0 :(得分:2)
怎么样:
^(?!0+(?:\.0+)?$)\d?\d(?:\.\d\d?)?$
<强>解释强>
^ : begining of string
(?! : negative lookahead, assumes there is no:
0+ : 1 or more zero
(?: : non capture group
\.0+ : a dot then 1 or more zeros
)?$ : end of group, optional, until end of string
) : end of lookahead
\d?\d : 1 or 2 digit
(?: : non capture group
\.\d\d? : a dot followed by 1 or 2 digit
)? : end of group, optional
$ : end of string
答案 1 :(得分:0)
试试这个正则表达式。
/^(\d{1,2}\.\d{1,2}|\d{1,2})$/
答案 2 :(得分:0)
以下内容如何:
^(100|([0-9][0-9]?(\.[0-9]+)?))$
首先处理100号码的外壳,然后倾向于剩余的组合。
确保在Java中使用它时转义反斜杠。