精度为2的十进制的简单正则表达式精度为2的十进制的正则表达式是什么?有效实例:123.1225675492929292929292.120.213.1无效示例:12.12322.23332e666.76小数点可以是可选的,整数也可以包括在内。
3 回答
冉冉说
TA贡献1877条经验 获得超1个赞
^[0-9]+(\.[0-9]{1,2})?$
^ # Start of string [0-9]+ # Require one or more numbers ( # Begin optional group \. # Point must be escaped or it is treated as "any character" [0-9]{1,2} # One or two numbers )? # End group--signify that it's optional with "?" $ # End of string
[0-9]
\d
[0-9]
import re deci_num_checker = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""")valid = ["123.12", "2", "56754", "92929292929292.12", "0.21", "3.1"] invalid = ["12.1232", "2.23332", "e666.76"]assert len([deci_num_checker.match(x) != None for x in valid]) == len(valid)assert [deci_num_checker.match(x) == None for x in invalid].count(False) == 0
- 3 回答
- 0 关注
- 657 浏览
添加回答
举报
0/150
提交
取消