Thursday, July 4, 2013

Regular Expression To Validate Months.


Solution 1 - The solution is ^(0[1-9]|1[012])$

Result - The above regular expression allows 

01
02
03
04
05
06
07
08
09
10
11
12

Solution 2 - The solution is ^([1-9]|1[012])$

Result - The above regular expression allows 

1
2
3
4
5
6
7
8
9
10
11
12


Solution 3 -
 This integrated solution 1 and solution 2 ^(0?[1-9]|1[012])$

Result - The above regular expression allows 

1
2
3
4
5
6
7
8
9
10
11
12

01
02
03
04
05
06
07
08
09
10
11
12

Hint - ? symbol in regular expression is used to match whether the character before question mark (?) is present or absent.. both will return true..

No comments:

Post a Comment