import re
regex = re.compile(r"^(?<year>\d{4})(?:(?<month>\d{2})(?:(?<day>\d{2})(?:(?<hour>\d{2})(?:(?<minute>\d{2})(?:(?<second>\d{2})(?:.(?<millis>\d{3})(?:\[(?:(?<offsetSign>[+-]?)(?<offsetHour>\d{1,2}))(?:.(?<offsetMin>\d{2}))?(?::(?<tz>.*)?)?\])?)?)?)?)?)?)?$", flags=re.MULTILINE)
test_str = ("1996\n"
"199610\n"
"19961005\n"
"1996100513\n"
"199610051322\n"
"19961005132200\n"
"19961005132200.124\n"
"19961005132200.124[-5]\n"
"19961005132200.124[-5:EST]\n"
"19961005132200.124[-5.30:EST]\n"
"19961005132200.124[-5.30:]\n"
"19961005132200.124[-5.30]\n"
"19961005132200.124[+5]\n"
"19961005132200.124[+5.30]\n"
"19961005132200.124[+5:EST]\n"
"19961005132200.124[+5.30:EST]\n"
"19961005132200.124[+5.30:]\n"
"19961005132200.124[5]\n"
"19961005132200.124[5.30]\n"
"19961005132200.124[5:EST]\n"
"19961005132200.124[5.30:EST]\n"
"19961005132200.124[5.30:]\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