简体   繁体   中英

check for minimum amount in regular expression

I have a regular expression that checks for a value between the range of 0 and 999999.

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

The problem is that 0.00 is valid. I would like to set 0.01 as the smallest amount that is valid. So, putting in 0.00 would me invalid.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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