经过多次尝试和研究,我无法找到解决此问题的正确方法:
需要匹配等于或大于1; 并且.5的增量也是有效的; 只有一个小数位。
本质上我需要捕获1 1.5 2 2.5等等。
环顾四周,找到以下帮助: perl regex to find any number that is a multiple of 5
Regex greater than zero with 2 decimal places
How to match two strings with integers greater than zero using regex?
我试过了:
(?!\d[0.5])\d+|\.|\d*[05]\d{1}$
没有成功。
你能帮帮忙吗?
答案 0 :(得分:2)
您可以使用此正则表达式:
^[1-9]\d*(?:\.[05])?$
<强>解体:强>
^ # Start of input
[1-9] # match digit 1 to 9
\d* # match 0 or more of any digits
(?: # start of non-capturing group
\. # match a decimal
[05] # followed by digit 0 or 5
)? # end non-capturing group. (? makes this group optional)
$ # End of input