How to write regexp, matches date
-format:
String of the form
YYYY-MM-DD
where MM<12
, DD<31
How to write regexp, matches date
-format:
String of the form
YYYY-MM-DD
where MM<12
, DD<31
Fast and readable, but not precise:
^\d{4}-\d\d-\d\d$
MM=00..12 DD=00..31? A little bit more precise ( e.g. 2015-00-00 is still possible), but hard to read:
^\d{4}-(0\d|1[0-2])-([0-2]\d|3[01])$
MM=01..12 DD=01..31? To cut out YYYY-00-00, a bit longer:
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
But you asked for MM<12 (not <= 12) and DD < 31, so: MM=00..11 DD=00..30? Right? Then use this:
^\d{4}-(0\d|1[01])-([0-2]\d|30)$
The ^ checks for the start and $ for the end of the string. You can drop it, if the surrounding characters are not interested.