正则表达式范围小数 0.1

编程入门 行业动态 更新时间:2024-10-15 14:19:24
本文介绍了正则表达式范围小数 0.1 - 7.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个正则表达式来验证小数点和范围.总共应存在 3 个数字,包括点,并且值必须大于 0.0.这意味着有效范围是从 0.1 到 7.0.

I need a regular expression that should validate decimal point as well as range. Totally 3 number should be present including dot and the value must be greater than 0.0. That means the valid range is from 0.1 to 7.0.

我使用了以下正则表达式:^\d{1,1}(\.\d{1,2})?$

I used the following regex: ^\d{1,1}(\.\d{1,2})?$

除了范围验证外,它工作正常.我需要改变什么?

It works fine except for the range validation. What do I need to change?

推荐答案

正则表达式在验证数字范围方面是出了名的糟糕.但这是可能的.您必须将数字范围分解为这些数字的预期文本表示:

Regexes are notoriously bad at validating number ranges. But it's possible. You have to break down the number range into the expected textual representations of those numbers:

^ # Start of string (?: # Either match... 7(?:.0)? # 7.0 (or 7) | # or [1-6](?:.[0-9])? # 1.0-6.9 (or 1-6) | # or 0?.[1-9] # 0.1-0.9 (or .1-.9) ) # End of alternation $ # End of string

作为单行:

^(?:7(?:.0)?|[1-6](?:.[0-9])?|0?.[1-9])$

在 Java 中:

Pattern regex = Patternpile("^(?:7(?:\.0)?|[1-6](?:\.[0-9])?|0?\.[1-9])$");

更多推荐

正则表达式范围小数 0.1

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

发布评论

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

>www.elefans.com

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