简体   繁体   English

正则表达式接受 0 到 100 之间的十进制数

[英]Regular expression to accept decimal numbers between 0 to 100

My requirement is a regular expression it accepts decimal values between 0 to 100 (like 1,2,3,....,99, 0.1,0.2,0.3,.....,99.9, 0.01,0.02,0.03,.....,99.99, 00.01 to 99.99).我的要求是一个正则表达式,它接受 0 到 100 之间的十进制值(如 1,2,3,....,99, 0.1,0.2,0.3,.....,99.9, 0.01,0.02,0.03,. .....,99.99, 00.01 至 99.99)。 I found one solution我找到了一个解决方案

/^(?!0?0\.00$)\d{1,2}\.\d{2}$/ 

but it accepts only decimal values like 00.01 to 99.99.但它只接受像 00.01 到 99.99 这样的十进制值。

How about: 怎么样:

^(?!0+(?:\.0+)?$)\d?\d(?:\.\d\d?)?$

Explanation: 说明:

^           : begining of string
  (?!       : negative lookahead, assumes there is no:
    0+      : 1 or more zero
    (?:     : non capture group
      \.0+  : a dot then 1 or more zeros
    )?$     : end of group, optional, until end of string
  )         : end of lookahead
  \d?\d     : 1 or 2 digit
  (?:       : non capture group
    \.\d\d? : a dot followed by 1 or 2 digit
  )?        : end of group, optional
$           : end of string

How about the following: ^(100|([0-9][0-9]?(\\.[0-9]+)?))$ 怎么样: ^(100|([0-9][0-9]?(\\.[0-9]+)?))$

First handle the outer case of the 100 number, then tend to the remaining combinations. 首先处理100号的外壳,然后处理剩余的组合。

Make sure to escape the backward slash when using this in Java. 在Java中使用时,请确保避免使用反斜杠。

试试这个正则表达式。

/^(\d{1,2}\.\d{1,2}|\d{1,2})$/
/^(100|([0-9][0-9]?(\.\d{1,2})?))/

this will return the decimal number between 0 to 100 with match 2 digits and optional two decimal.这将返回 0 到 100 之间的十进制数,匹配 2 位数字和可选的两位小数。 eg out put would be like one of these例如,输出就像其中之一

3
33
33.3
33.33

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

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