Therefore, please consider the following solution ...
1. Identify the sets to be matched:
- Days with the prefix "0":
{01,...,09},{10,...,31}
- The subset
{10,...,31}
can be divided into => {10,...,29},{30,31}
- Without prefix:
{1,...,31} => {1,...,9},{10,...,31}
2. Corresponding regular expressions for each subset:
--------------------------------- Sub-Set | Regular-Expression --------------------------------- {01,...,09} | [0][1-9] {10,...,29} | [1-2][0-9] {30,31} | 3[01] {1,...,9} | [1-9] ---------------------------------
Now we can group ([0][1-9])
and ([1-9])
together as ([0]?[1-9])
. Where ?
means 0 or 1 occurrence of pattern / symbol. [UPDATE] - Thanks, @MattFrear, for pointing this out.
As a result, RegEx looks like this: ^(([0]?[1-9])|([1-2][0-9])|(3[01]))$
Checked here: http://regexr.com/?383k1 [UPDATE]
Kent Pawar
source share