-1

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])
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
taji01
  • 2,527
  • 8
  • 36
  • 80

1 Answers1

1

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:

  1. [12]\d
  2. 3[01]
  3. 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.

Jithin Pavithran
  • 1,250
  • 2
  • 16
  • 41