简体   繁体   English

检查正则表达式中的最小值

[英]check for minimum amount in regular expression

I have a regular expression that checks for a value between the range of 0 and 999999. 我有一个正则表达式,用于检查0到999999之间的值。

/^((?:\d{1,3},)?\d{1,3})(\.\d{2})$/

The problem is that 0.00 is valid. 问题是0.00是有效的。 I would like to set 0.01 as the smallest amount that is valid. 我想将0.01设置为有效的最小数量。 So, putting in 0.00 would me invalid. 因此,输入0.00将使我无效。

I'm terrible with the black art of regex. 我对正则表达式的黑艺术感到恐惧。 Can anyone help? 有人可以帮忙吗?

Thanks. 谢谢。

Don't put logic into regular expressions. 不要将逻辑放入正则表达式中。 They are for matching patterns, not doing numerical comparisons. 它们用于匹配模式,而不是进行数值比较。

In Perl, this would be: 在Perl中,这将是:

if ( $s =~ /^((?:\d{1,3},)?\d{1,3})(\.\d{2})$/ && ( $s > 0 ) ) {
    # acceptable number
}

Regexes aren't a black art if you use them properly. 如果正确使用正则表达式,并不是一件黑事。 Trying to do numeric calculations with them is not using them properly. 尝试对它们进行数值计算不能正确使用它们。

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

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