Drakkainen
Drakkainen

Reputation: 1142

How to use Regular Expression to get the dates from this string?

I have a string that looks like this:

<some_text> February 19, 2009 through March 17, 2009 <some_text>

How can I pick up the dates using a regular expression, working with python.

I tried this to see if I can at least match the string, but it doesn't find anything:

r'\w*\d{1,2},\w+\d{4}\w+through\w+\d{1,2},\w+\d{4}'

Any help would be appreciated.

Upvotes: 0

Views: 115

Answers (2)

joel3000
joel3000

Reputation: 1319

You will need to use re.search to do this.

Because this will be a long regexp I suggest you compile it, just for clarity.

The basic regexp will look like ths:

date_finder = re.compile("(\w+) through (\w+)")

This will find two strings separated by 'through'.

To access them you will use

out = data_finder.search(input_str)

out.group(1) # first paren match
out.group(2) # second paren match group

Next you will have to check to see if your groups are actually date strings or not.

date_finder = re.compile("([JFMASOND][a-z]+\s+\d{1,2}[\s,]+\d{4}) through")

This is accessed from:

out = date_finder.search(input_str)
out.group(1) # date string before through

To get the second just repeat that regexp on the other side of 'through'. The regexp might take a little tweaking depending on your input data but you should get the idea.

Hope that helps.

Upvotes: 1

Toto
Toto

Reputation: 91415

How about:

(\w+ \d\d?, \d{4})\b.+?\b(\w+ \d\d?, \d{4})\b

Upvotes: 1

Related Questions