automate-boring-stuff/exercises/ex07/ex07b.py

40 lines
741 B
Python

"""
Project: Date Detection
Page 186
"""
import re
import datetime
tests = """
Sí 1/1/1911
Sí 02/1/2000
Sí 12/12/2012
No 123/1/1911
No 1/123/1911
No 1/1/11
Inválido 32/12/1212
Inválido 3/13/1212
Inválido 3/12/3000
Inválido 3/12/00
Inválido 29/02/2023
"""
def find_date(string):
rgx = re.compile(r"\b(\d{1,2})/(\d{1,2})/(\d{4})")
finds = rgx.findall(string)
dates = []
for find in finds:
d, m, y = int(find[0]), int(find[1]), int(find[2])
if y > 999 and y < 3000:
try:
dates.append(datetime.date(y, m, d).isoformat())
except Exception:
pass
return dates
dates = find_date(tests)
print(dates)