I have several strings, and have identified some formats of date on them, and would like to recognize date on each string
an_2011_02_12_azar.mp3 ->this is yyyy_mm_dd
20121112_Marcel.mp3 ->this is yyyymmdd
cdani_270607.mp3 ->this is ddmmyy
lica_07_03_15.mp3 ->this is dd_mm_yy
to do so I have:
foo = """
an_2011_02_12_azar.mp3
20121112_Marcel.mp3
cdani_270607.mp3
lica_07_03_15.mp3
"""
try:
lines = foo.split('\n')
except AttributeError:
lines = x
for line in lines:
print(line)
#deals with 2011_02_12 format
match = re.search(r'\d{4}_\d{2}_\d{2}', line)
date = datetime.datetime.strptime(match.group(), '%Y_%m_%d').date()
print(date)
How to apply several regular expressions so it can recognize dates?
%Y_%m_%dI geterrordate = datetime.datetime.strptime(match.group(), '%Y_%m_%d').date() AttributeError: 'NoneType' object has no attribute 'group'Noneor handle the error and move on, andbreak/returnwhen one parses successfully.ORon regular expression?redocs, or just use a loop outside of regex.