使用双精度值将正则表达式限制在一定范围内
Restricting Regular expression up to certain limit using double values
I have been facing an issue of restricting double values upto certain
limit. But my regular expression does not matches the expected
result.Kindly guide me how to rectify it.
正则表达式
^[0-1]?([0-5]{1,2})?(\.[0-9][0-9])?$
预期结果:
Values should pass the condition ranges from range 00.00 to 15.99
失败场景
6.12
7.12
8.21
9.21
要将 0
到 15
之间的数字与可选的前导零和强制性的 1 或 2 位小数部分匹配,您可以使用
^(?:0?[0-9]|1[0-5])\.[0-9]{1,2}$
详情:
^
- 字符串开头
(?:0?[0-9]|1[0-5])
- 两种选择之一:
0?[0-9]
- 一个可选的 0
后跟一位数字
|
- 或
1[0-5]
- 10
到 15
(1
后跟从 0
到 5
的 1 个数字)
\.
- 一个点
[0-9]{1,2}
- 1 位或 2 位数字
$
- 字符串结尾(可以替换为 \z
以匹配字符串的结尾)。
这会起作用:^(1[0-5]|0[0-9]|[0-9])(?:\.([0-9]{1,2}))$
您可以通过将 {1,2} 更改为您想要的最小值和最大值来调整小数的数量。完整匹配您将获得 $0,$1 为整数部分,$2 为小数部分。
I have been facing an issue of restricting double values upto certain limit. But my regular expression does not matches the expected result.Kindly guide me how to rectify it.
正则表达式
^[0-1]?([0-5]{1,2})?(\.[0-9][0-9])?$
预期结果:
Values should pass the condition ranges from range 00.00 to 15.99
失败场景
6.12
7.12
8.21
9.21
要将 0
到 15
之间的数字与可选的前导零和强制性的 1 或 2 位小数部分匹配,您可以使用
^(?:0?[0-9]|1[0-5])\.[0-9]{1,2}$
详情:
^
- 字符串开头(?:0?[0-9]|1[0-5])
- 两种选择之一:0?[0-9]
- 一个可选的0
后跟一位数字|
- 或1[0-5]
-10
到15
(1
后跟从0
到5
的 1 个数字)
\.
- 一个点[0-9]{1,2}
- 1 位或 2 位数字$
- 字符串结尾(可以替换为\z
以匹配字符串的结尾)。
这会起作用:^(1[0-5]|0[0-9]|[0-9])(?:\.([0-9]{1,2}))$
您可以通过将 {1,2} 更改为您想要的最小值和最大值来调整小数的数量。完整匹配您将获得 $0,$1 为整数部分,$2 为小数部分。