Your question is not 100% clear, but it looks like you're trying to parse a date from an incoming string. If so, use the datetime module rather than a regex. It is more likely to handle locales etc. The datetime.datetime.strptime()
method is designed to read dates from strings, so try something like the following:
import datetime
def myDate(raw):
# Try to match a date with a year.
try:
dt = datetime.datetime.strptime(raw, '%B %d, %Y')
# Make sure its the year we want.
if dt.year != 2012:
return None
# Error, try to match without a year.
except ValueError:
try:
dt = datetime.datetime.strptime(raw, '%B %d')
except ValueError:
return None
# Add in the year information - by default it says 1900 since
# there was no year details in the string.
dt = dt.replace(year=2012)
# Strip away the time information and return just the date information.
return dt.date()
The strptime()
method returns a datetime
object i.e., date and time information. Hence the last line calls the date()
method to return just the date. Also note that the function returns None
when there is no valid input - you can easily change this to do whatever you situation requires. See the documentation of the strptime() method for details of what the different format codes.
A few examples of its use:
>>> myDate('March 29, 2012')
datetime.date(2012, 3, 29)
>>> myDate('March 29, 2011')
>>> myDate('March 29, 2011') is None
True
>>> myDate('March 29')
datetime.date(2012, 3, 29)
>>> myDate('March 39')
>>> myDate('March 39') is None
True
You'll notice this catches and refuses to accept illegal dates (e.g., March 39) which can be tricky to handle with a regex.