加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式大于0,小数位为2位

发布时间:2020-12-14 06:30:07 所属栏目:百科 来源:网络整理
导读:我需要一个数字值的RegEx,最多两个小数位数大于零,并且在一列中可能没有零.我也应该添加….整数都可以.看到下面的一部分,但可能有前导或尾随的空格 Good values:.10.11.12123.1292092092.13Error values:00.00.00001.234-1-1.2Anything less than zero 这个
我需要一个数字值的RegEx,最多两个小数位数大于零,并且在一列中可能没有零.我也应该添加….整数都可以.看到下面的一部分,但可能有前导或尾随的空格
Good values:
.1
0.1
1.12
123.12
92
092
092.13

Error values:
0
0.0
0.00
00
1.234
-1
-1.2
Anything less than zero
这个怎么样:
^s*(?=.*[1-9])d*(?:.d{1,2})?s*$

说明:

^            # Start of string
s*          # Optional whitespace
(?=.*[1-9])  # Assert that at least one digit > 0 is present in the string
d*          # integer part (optional)
(?:          # decimal part:
 .          # dot
 d{1,2}     # plus one or two decimal digits
)?           # (optional)
s*          # Optional whitespace
$           # End of string

在Python中测试:

>>> import re
>>> test = [".1","0.1","1.12","123.12","92","092","092.13","0","0.0","0.00","00","1.234","-1","-1.2"]
>>> r = re.compile(r"^s*(?=.*[1-9])d*(?:.d{1,2})?s*$")
>>> for item in test:
...     print(item,"matches" if r.match(item) else "doesn't match")
...
.1 matches
0.1 matches
1.12 matches
123.12 matches
92 matches
092 matches
092.13 matches
0 doesn't match
0.0 doesn't match
0.00 doesn't match
00 doesn't match
1.234 doesn't match
-1 doesn't match
-1.2 doesn't match

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读