简体   繁体   English

正则表达式不限制小数点后的值

[英]Regex not limiting values after decimal point

My text field should accept values between 0.00 and 0.99 and I have the following regex 我的文本字段应接受介于0.00和0.99之间的值,并且我有以下正则表达式

^0.\.*[0,9]{0,2}$

But its not limiting the values after decimal point.Can someone guide me to fix it. 但是它不限制小数点后的值。有人可以指导我修复它。

The correct code for that is 正确的代码是

^0\.[0-9]{1,2}$

The errors in your expression are that 您表达中的错误是

1) [0,9] is not a range. 1) [0,9]不是范围。 It means that the characters 0 , , and 9 can be matched at the end of the string. 这意味着字符0,9可以在字符串的末尾匹配。

2) 0.\\.* means that any single-character may be matched if it comes after a 0 and before a dot . 2) 0.\\.*表示任何单个字符都可以匹配,如果它在0dot之前。 Including alphabetic characters. 包括字母字符。

To debug in an easier way, try online testers, such as http://www.regextester.com/ 要以更简单的方式进行调试,请尝试使用在线测试仪,例如http://www.regextester.com/

This should work: 这应该工作:

^0\.\d{0,2}$
  • ^ Look for start of line ^寻找行首
  • 0\\. Look for 0. 寻找0.
  • \\d{0,2} Look for a digit between 0 and 2 times \\d{0,2}寻找0到2次之间的数字
  • $ Look for end of line $寻找行尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM