0 到 1 之间的正则表达式十进制值最多 4 个小数位

编程入门 行业动态 更新时间:2024-10-27 18:31:32
本文介绍了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 start
      • 0 - a zero
      • (\.[0-9]{1,4})? - an optional sequence of a . followed with 1 to 4 digits
      • | - or
      • 1 - a 1
      • (\.0{1,4})?) - an optional sequence of . followed with 1 to 4 zeros

更多推荐

0 到 1 之间的正则表达式十进制值最多 4 个小数位

本文发布于:2023-11-10 23:02:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数位   最多   正则表达式   十进制值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!