Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match month name followed by year

Tags:

regex

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

like image 458
Jeremy Avatar asked Apr 16 '10 19:04

Jeremy


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do you match periods in regex?

Use the escape character \ to match a period with regex within a regular expression to match a literal period since, by default, the dot . is a metacharacter in regex that matches any character except a newline.

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


2 Answers

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 
like image 146
Tomalak Avatar answered Oct 02 '22 01:10

Tomalak


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

like image 41
Beerswiller Avatar answered Oct 02 '22 02:10

Beerswiller