Ilya
Ilya

Reputation: 113

Regex catch 0 or 0.0 or 0.00

I have a tough question: I use (^[1-9]\d*|\d+\.\d*[1-9])$, I want to find zero-sum like 0 or 0.0 or 0.00 or 0000, but that regex says to me 60. or 60.00 or 62.0 etc not valid (equal zero). What I do wrong?

These examples for valid or not valid sum

Thanks!

Upvotes: 1

Views: 2496

Answers (1)

Allan
Allan

Reputation: 12438

Can you try the following regex it does respect your criteria (valid, not valid)

^(?:0?[1-9]\d*(?:\.\d*[0-9]*)?)|(:?^0.[1-9]+0)$

https://regex101.com/r/dqqkqy/6

After change in requirements to accept 0.1, 0.2, 0.3:

^(?:0?[1-9]\d*(?:\.\d*[0-9]*)?)|(:?^0.[1-9]+0?)$

https://regex101.com/r/dqqkqy/8

Upvotes: 1

Related Questions