Reputation: 9293
Is it possible to use a regex to match "February 2009", for example?
Upvotes: 41
Views: 70862
Reputation: 1
The regex below will take into account the max number of days for the relevant month and also take into account leap years for February.
^(((0[1-9]|[12][0-9]|3[01])[ ]\b(?:Jan(?:uary)?|Mar(?:ch)?|May|Jul(?:y)?|Aug(?:ust)?|Oct(?:ober)?|Dec(?:ember)?)|(0[1-9]|[12][0-9]|30)[ ]\b(?:Apr(?:il)?|Jun(?:e)?|Sep(?:tember)?|Nov(?:ember)?)|(0[1-9]|1\d|2[0-8])[ ]\b(?:Feb(?:ruary)?))[ ]\d{4}|29[ ]\b(?:Feb(?:ruary)?)[ ](\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))$
Upvotes: 0
Reputation: 41
This regex accounts for some spacing around the comma.
Sometimes it's not always in the right place.
((\b\d{1,2}\D{0,3})?\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)\D?)(\d{1,2}(st|nd|rd|th)?)?((\s*[,.\-\/]\s*)\D?)?\s*((19[0-9]\d|20\d{2})|\d{2})*
Upvotes: 3
Reputation: 51
Modifying Beerswiller's answer, if you want "st"/"nd"/"rd" variations:
(\b\d{1,2}\D{0,3})?\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)\D?(\d{1,2}(st|nd|rd|th)?)?(([,.\-\/])\D?)?((19[7-9]\d|20\d{2})|\d{2})*
Upvotes: 4
Reputation: 679
I had to work on this to match a few fringe examples, but I ended up using
(\b\d{1,2}\D{0,3})?\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)\D?(\d{1,2}\D?)?\D?((19[7-9]\d|20\d{2})|\d{2})
to capture dates with word months in them
Upvotes: 34
Reputation: 338208
Along the lines of
\b(?:Jan(?:uary)?|Feb(?:ruary)?|...|Dec(?:ember)?) (?:19[7-9]\d|2\d{3})(?=\D|$)
that's
\b # a word boundary (?: # non-capturing group Jan(?:uary)? # Jan(uary) |Feb(?:ruary)? # |... # and so on |Dec(?:ember)? # Dec(ember) ) # end group # a space (?: # non-capturing group 19[7-9]\d|2\d{3} # 1970-2999 ) # end group (?=\D|$) # followed by: anything but a digit or the end of string
Upvotes: 62