Home > Net >  Regex Number with decimals over 60
Regex Number with decimals over 60

Time:10-12

I need a RegEx for a numeric value with up to 2 decimals places that allows for all numbers over 60 but none below.

I have used the below RegEx for numbers between 0 and 60, but now I need a similar expression for all numbers over 60

^(?=.)(?:(?:(?:0|[1-5]?\d)?(?:[,.]\d{1,2})?)|60(?:[.,]00?)?)$

CodePudding user response:

You can use

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

Or, if you cannot afford lookaheads:

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

See the regex demo #1 / regex demo #2.

Details:

  • ^ - start of string
  • (?!60(?:\.00?)?$) - a negative lookahead that fails the match if the string is 60, 60.0 or 60.00
  • (?: - start of a non-capturing group:
    • [6-9][0-9] - 60 to 99 number
    • | - or
    • [1-9][0-9]{2,} - a digit from 1 to 9 and then two or more digits (100 and more)
  • ) - end of the group
  • (?:\.[0-9]{1,2})? - an optional sequence of a . and one or two digits
  • $ - end of string.
  • Related