import re
regex = re.compile(r"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-9])|(2[0-3]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])))?))?$", flags=re.MULTILINE)
test_str = ("16/10/1991\n"
"16-10-1991\n"
"16.10.1991\n"
"1991/16/10\n"
"1991-16-10\n"
"1991.16.10\n"
"10/16/1991\n"
"10-16-1991\n"
"10.16.1991\n"
"1991-10-16\n"
"1991/10/16\n"
"2016-2-29\n"
"2020-2-29\n"
"2024-2-29\n"
"2028-2-29\n"
"2032-2-29\n"
"2036-2-29\n"
"2040-2-29\n"
"5304-2-29\n\n"
"1991.10.16\n"
"91-10-16\n"
"91/10/16\n"
"91.10.16\n\n"
"01/01/2000\n"
"31/01/2000\n"
"32/01/2000\n"
"01/1/2000\n"
"01/1/01\n\n"
"29/02/2000\n"
"28/02/2001\n"
"29/02/2003\n\n"
"30/04/2000\n"
"31/04/2000\n\n"
"31/07/2000\n\n"
"31/08/2000\n\n"
"31/09/2000\n\n"
"01/12/2000\n\n"
"01/15/2000\n")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html