import re
regex = re.compile(r"(?<day>0?[1-9]|[12][0-9]|3[01])(?>.{1})(?(?=0?[469]|11)(?<!31(?>.{1}))|)(?(?=0?2)(?<!3[01](?>.{1})))(?(?=0?2(?>.{1})\d\d(?!00)(?:[02468][048]|[13579][26]))|(?<!29(?>.{1})))(?<month>0?[1-9]|1[12])(?>.{1})(?<year>19[0-9][0-9]|20[0-9][0-9]|[0-9]{2,4})(?!\d)", flags=re.MULTILINE)
test_str = ("29.02.1704\n\n"
"01.01.1987 // ok\n"
"101.01.19870 // bad- too many numbers\n"
"1.1.1987 // ok\n"
"32.02.1987 // bad- 32 feb\n"
"29.02.2001 // bad\n"
"0.0.1987 // bad\n"
"0.13.1987 // bad\n"
"8.8.8.8 // bad\n"
"31.04.1987 // bad\n"
"31.06.1987 // bad\n"
"31.09.1987 // bad\n"
"31.11.1987 // bad\n"
"30.02.1987 // bad\n"
"29.02/1987 // bad\n"
"29.02.1988 // ok\n"
"29.02.2000 // bad\n"
"29.02.2408 \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