Search code examples
regexpreg-matchregular-language

Restricting Regular expression up to certain limit using double values


I have been facing an issue of restricting double values upto certain limit. But my regular expression does not matches the expected result.Kindly guide me how to rectify it.

Regular Expression

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

Expected Result:

Values should pass the condition ranges from range 00.00 to 15.99

Failure Scenarios

6.12
7.12
8.21
9.21

Solution

  • This will work : ^(1[0-5]|0[0-9]|[0-9])(?:\.([0-9]{1,2}))$ You can tweak the number of fractional numbers by changing the {1,2} to the min and max you want. You will get $0 for the full match, $1 is the integer part and $2 is the decimal part.

    See example here : https://regex101.com/r/XC1lff/7