taji01 taji01 - 1 year ago 90
C# Question

Day Validation using Regex

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


Answer Source

Following will work for you.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download