import re
regex = re.compile(r"^([A-Za-z0-9]{2})([0-9]{2})((?:[A-R][0-9]{2}){0,10})((?:[0-9]{4}){0,12})((?:[A-Z])?)$", flags=re.MULTILINE)
test_str = ("0101110112011301210122012301310132013301410142014301B\n"
"0101B\n"
"0101B12B\n"
"01011011B\n"
"0101B121111B\n"
"010110111011B\n"
"0101B1210111011B\n"
"0520D01D53D\n"
"0620E01E53D\n"
"0724\n"
"0803E23J54B\n"
"0903\n"
"1006E25J56B\n"
"1103B01\n"
"1407\n"
"1504\n"
"1807E30J61B\n"
"1904K24R\n"
"2003K41M54R\n"
"2104K06R\n"
"2203H37M68B\n"
"72B6P271531\n"
"05BRE010941")
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