正则表达式 十进制范围 0.1

编程入门 行业动态 更新时间:2024-10-23 11:30:30
本文介绍了正则表达式 十进制范围 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 06:32:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562945.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   十进制

发布评论

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

>www.elefans.com

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