Jeremy
Jeremy

Reputation: 9293

Regex to match month name followed by year

Is it possible to use a regex to match "February 2009", for example?

Upvotes: 41

Views: 70862

Answers (6)

d0mpsy
d0mpsy

Reputation: 1

January\s*\d{4}|February\s*\d{4}|March\s*\d{4}....and so on ...

Upvotes: 0

Sarah Reynolds
Sarah Reynolds

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

Todd
Todd

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

Pedro Machin
Pedro Machin

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

Beerswiller
Beerswiller

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

Tomalak
Tomalak

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

Related Questions