Giridharan
Giridharan

Reputation: 4462

Regular expression matches maximum and minimum values

I am facing an issue of matching expected values using regular expressions. My expected minimum value is 0.01 and the maximum value is 15.99.

Regular expression:

^(1[0-5]|0[0-9]|[0-9])(?:\.([0-9]{1,2}))$

Failure scenarios:

0.00

Upvotes: 0

Views: 157

Answers (2)

Mustofa Rizwan
Mustofa Rizwan

Reputation: 10466

You can try this:

^(?!(0.00|00.00))(1[0-5]|0[0-9]|[0-9])(?:\.([0-9]{1,2}))$

Explanation

Although it is already accepted, the regex can be simplified a bit by:

^(?!(0.00|00.00))((0\d?|1[0-5]?).\d{1,2})$

Upvotes: 1

Abdullah Mallik
Abdullah Mallik

Reputation: 817

Just add (?!0.00?) before your pattern.

^(?!0.00?)(1[0-5]|0[0-9]|[0-9])(?:\.([0-9]{1,2}))$

Upvotes: 1

Related Questions