Python Regex Date -
i'm trying create regex looks dates in format mm-dd-yyyy, , came far:
dateregex = re.compile(r''' (0[1-9]|1[0-2]) # month - (10|20|[0-2][1-9]|3[01]) # day: not [0-2][0-9]|3[01] avoid 00 matching - ((198[0-9]|20(0[0-9]|1[0-6])''' # year: matches 1980 - 2016 , re.verbose)
is there easier way allows me create range of numbers? , wanted create 1 allows legal dates (for example, june shouldn't have 31 days), easiest way match months different days, like:
((01|03|05|07|08|10|12)-(31 day regex pattern)-(year regex) # 31-day months | (04|06|09|11)-(30 day regex pattern)-(year regex) # 30-day months | 02-(regex depending on leap year)) # 28 or 29 days
not sure how february besides putting leaps years , 29 days together, , remaining years 28 days.
i agree jonrsharpe way combine regex datetime. used simple regex going match could date in format, try parse them datetime.
import re import datetime def yield_valid_dates(datestr): match in re.finditer(r"\d{1,2}-\d{1,2}-\d{4}", datestr): try: date = datetime.datetime.strptime(match.group(0), "%m-%d-%y") yield date # or can yield match.group(0) if want # yield date string found 05-04-1999 except valueerror: # date couldn't parsed datetime... invalid date pass teststr = """05-04-1999 here filler text in between 2 dates 4-5-2016 invalid date 32-2-2016 here invalid date, there no 32d day of month 6-32-2016. can not include leading zeros 4-2-2016 , still detected""" date in yield_valid_dates(teststr): print(date)
this prints 3 valid dates:
1999-05-04 00:00:00 2016-04-05 00:00:00 2016-04-02 00:00:00
Comments
Post a Comment