CuriousGeorge
CuriousGeorge

Reputation: 311

Regex for range 7.2-80.0, decimal length not required

Anyone see what's wrong with the below query? Trying to confirm a number is between or equal to 7.2 and 80 in regex. Multiple decimals don't matter for example 8.999 is fine.

^(?:80(?:\.0)?|[8-79](?:\.[0-9])?|7?:\.[2-9])$

Upvotes: 0

Views: 35

Answers (1)

Josh Withee
Josh Withee

Reputation: 11336

Your character class [8-79] is not a valid way of matching integers 8 through 79. An integer range in a character class must be a range of one-digit integers. A proper way to match integers 8 through 79 would be:

(?:[89]|[1-7][0-9])

Also, you are only matching up to one decimal place. For example,

80(?:\.0)? 

will match 80 and 80.0, but not 80.00. If this could cause a problem for your application, you would instead want to use

80(?:\.0+)?

Using these concepts, I think this regex should do what you want:

^(?:80(?:\.0+)?|(?:[89]|[1-7][0-9])(?:\.[0-9]+)?|7(?:\.[2-9][0-9]*)?)$

Upvotes: 1

Related Questions