Kadir
Kadir

Reputation: 3224

Regex digits smaller then 82

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]

Upvotes: 4

Views: 302

Answers (3)

Gareth
Gareth

Reputation: 138110

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

Upvotes: 4

Mattias Buelens
Mattias Buelens

Reputation: 20179

Your second part is wrong. It'll match from 10 to 89, whereas you want it to match from 10 to 79 and let the third part handle 80 to 82.

0[1-9]|[1-7][0-9]|8[0-2]

Upvotes: 1

Guffa
Guffa

Reputation: 700592

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])$

Upvotes: 7

Related Questions