import re
regex = re.compile(r"\(20(\d{2}|x{2})((\.|-)([0-9]{2}|x{2}|KW(\d{2}|x{2})|Q(x|[1-4])|(([0-9]{2}|x{2})(\.|-)([0-9]{2}|x{2}))))?\)")
test_str = ("20000002000\n"
"200 0\n"
"(2022.xx2000.xx)\n"
"(2022.xx) 2000.xx\n"
"(2020-20)\n"
"(20xx-12)\n"
"(20xx.xx-11)\n"
"(2002-KW00.00)\n"
"(2002-KW00)\n"
"(2022.KWxx)\n"
"(2021.KW09)\n"
"(2021.KW09)\n"
"(2021.02)\n"
"2022.Qx\n"
"2032.Q4\n"
"(2020.xx)\n"
"2022-Q1\n"
"2022 Test\n"
"Test 20xx\n\n"
"20202020\n"
".Qx\n"
"KW23\n"
"KWxx\n"
"(20xx)\n"
"(.-Q4)\n\n"
"2000-\n"
"2000.\n\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