I'm trying to use Regex to validate the days in a month. With this i am able to validate 01 to 31. How can i also validate 1-31, so that way i can either have 01 to 31 or 1 to 31
(0[1-9]|[12]\d|3[01])
I'm trying to use Regex to validate the days in a month. With this i am able to validate 01 to 31. How can i also validate 1-31, so that way i can either have 01 to 31 or 1 to 31
(0[1-9]|[12]\d|3[01])
Following will work for you.
(([12]\d)|(3[01])|(0?[1-9]))
I think you didn't understand the way your expression works.
Here it is:
|
stands for OR
.
Thus you have 3 cases here:
[12]\d
3[01]
0?[1-9]
1 - match either 1
or 2
as the 1st character and \d
match any digit (same as [0-9]) as the second character.
2 - match 3
as the 1st digit. And either 0
or 1
as the second one.
3 - match with any digit between 1 to 9 (both included). 0?
add an optional 0
as the 1st character.