Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex digits smaller then 82

Tags:

regex

I'm trying to write a simple regex but I don't know why it is not working.

User enter 2 digits number like 01, 09, 23, 55, until 82. After 82 system will refuse.

Here is my regex, 2 digits must be smaller than 82.

0[1-9]|[1-8][0-9]|8[0-2]
like image 777
Kadir Avatar asked Dec 15 '22 23:12

Kadir


2 Answers

You should have [1-7] for the range 10-79, not [1-8]. Don't forget the ^ and $ to specify the start and ending of the string:

^(0[1-9]|[1-7]\d|8[0-2])$
like image 68
Guffa Avatar answered Jan 08 '23 01:01

Guffa


Why not cast to an integer and then just test x < 82?

like image 44
Gareth Avatar answered Jan 07 '23 23:01

Gareth