Reputation: 31
'\b\[a-bA-B]{4,10}\s\w{4}\b'
is this the regular expression for 'may 2013' or any such format date in python?
import re
regex=re.compile('\b\[a-z]{4-10}\s\w{4}\b')
for line in text:
list = regex.findall(line)
for w in list:
print w
Upvotes: 0
Views: 7501
Reputation: 644
Extending last answer further. Regex below is able to find month in digit too.
(?P<month>([a-zA-Z]+)|(\d{2}))\s+(?P<year>\d{4})
Like these two:
Upvotes: 0
Reputation: 61225
What about this
re.compile(r'\w{3}\s+[0-9]{4}')
\w{3}
match any word character [a-zA-Z0-9_]
three times\s+
match any white space character [\r\n\t\f ]
[0-9]{4}
match a single character present in the list below Exactly 4 timesUpvotes: 1
Reputation: 1683
The regex pattern for a date format like may 2013
is the following:
re.compile('[a-zA-Z]+\s+\d{4}')
Also, you can retrieve the month or year, by using group names, with the following code snippet:
regex = re.compile('(?P<month>[a-zA-Z]+)\s+(?P<year>\d{4})')
m = regex.search('may 2013')
print(m.group('month'), m.group('year'))
Upvotes: 4