0 到 1 之间的正则表达式十进制值最多 4 个小数位 [英] Regex decimal values between 0 and 1 up to 4 decimal places
本文介绍了0 到 1 之间的正则表达式十进制值最多 4 个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个 C# Web 应用程序,并使用此正则表达式验证文本框中的数据,该正则表达式仅接受 0 和 1 之间的正十进制值:
I am writing a C# web application and verify data in a textbox using this regular expression that only accepts positive decimal values between 0 and 1:
^(0(\.\d+)?|1(\.0+)?)$
我会像正则表达式一样进行调整,将条目限制为小数点后 4 位的精度.
I would to adapt like the regex to restrict entries to 4 decimal places of precision.
允许
0
0.1
0.12
0.123
0.1234
1
不允许
-0.1
-1
1.1
2
我发现以下正则表达式最多只能保留 4 位小数,但我不确定如何将两者结合起来.
I have found the following regex that only allows up to 4 decimal places, but I am unsure on how to combine the two.
^(?!0\d|$)\d*(\.\d{1,4})?$
非常感谢任何帮助,谢谢.
Any help is greatly appreciated, thanks.
推荐答案
您需要设置用限制{1,4}
替换+
量词:>
You need to set replace the +
quantifier with the limiting {1,4}
:
^(0(\.[0-9]{1,4})?|1(\.0{1,4})?)$
^^^^^ ^^^^^
查看正则表达式演示
详情:
^
- 字符串的开始(
- 外组开始0
- 一个零(\.[0-9]{1,4})?
-.
后跟 1 到 4 位数字的可选序列|
- 或1
-1
(\.0{1,4})?)
-.
的可选序列,后跟 1 到 4 个零
^
- start of string(
- Outer group start0
- a zero(\.[0-9]{1,4})?
- an optional sequence of a.
followed with 1 to 4 digits|
- or1
- a1
(\.0{1,4})?)
- an optional sequence of.
followed with 1 to 4 zeros
这篇关于0 到 1 之间的正则表达式十进制值最多 4 个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文