views:

53

answers:

2

What is the regular expression for validating a month with the leading zero?

Passes regular expression:

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

Fails regular expression:

1, 00, 13 and up.
+7  A: 
/^(0[1-9]|1[0-2])$/
Seb
Getting rid of the parentheses can improve the performance of this expression.
Daniel Trebbien
A: 
/^01|02|03|04|05|06|07|08|09|10|11|12$/
Daniel Trebbien
Expressed more succinctly in [Seb's answer](http://stackoverflow.com/questions/2878010/regular-expression-for-validating-month/2878016#2878016)
Daniel DiPaolo
Not a very sexy solution... =(
Mark Canlas
It may not be the more succinct, but it is faster than /^(0[1-9]|1[0-2])$/, even /^0[1-9]|1[0-2]$/, when used with grep against 1,000,000 test data that I generated.
Daniel Trebbien