import re
regex = re.compile(r"^((?<atu>ATU)|(?<be>BE)|(?<bg>BG)|(?<cy>CY)|(?<cz>CZ)|(?<dk>DK)|(?<de>DE)|(?<ee>EE)|(?<el>EL)|(?<es>ES)|(?<fi>FI)|(?<fr>FR)|(?<hr>HR)|(?<hu>HU)|(?<ie>IE)|(?<lt>LT)|(?<lu>LU)|(?<lv>LV)|(?<mt>MT)|(?<nl>NL)|(?<pl>PL)|(?<pt>PT)|(?<ro>RO)|(?<se>SE)|(?<si>SI)|(?<sk>SK)|(?<xi>XI))((?(atu)[0-9]{8})|(?(be)[01][0-9]{9})|(?(bg)[0-9]{9,10})|(?(cy)[A-Z0-9]{9})|(?(cz)[0-9]{8,10})|(?(dk)[0-9]{8})|(?(de)[0-9]{9})|(?(ee)[0-9]{9})|(?(el)[0-9]{9})|(?(es)[A-Z]{1}[0-9]{8})|(?(fi)[0-9]{8})(?(fr)[0-9A-Z]{2}[0-9]{9})|(?(hr)[0-9]{11})|(?(hu)[0-9]{8})|(?(ie)(?:[0-9]{7}[A-Z]{1,2}|[0-9][A-Z][0-9]{5}[A-Z]))|(?(lt)(?:[0-9]{9}|[0-9]{12}))|(?(lu)[0-9]{8})|(?(lv)[0-9]{11})(?(mt)[0-9]{8})(?(nl)[0-9]{9}B[0-9]{2})|(?(pl)[0-9]{10})|(?(pt)[0-9]{9})|(?(ro)[0-9]{2,10})|(?(se)[0-9]{12})|(?(si)[0-9]{8})|(?(sk)[0-9]{10})|(?(xi)(?:[0-9]{3}[0-9]{4}[0-9]{2}(?:[0-9]{3})?|[A-Z0-9]{5})))$", flags=re.MULTILINE)
test_str = ("ATU99999999\n"
"BE0999999999\n"
"BE1999999999\n"
"BG999999999\n"
"BG9999999999\n"
"CY99999999L\n"
"CZ99999999\n"
"CZ999999999\n"
"CZ9999999999\n"
"DE999999999\n"
"DK99999999\n"
"EE999999999\n"
"EL999999999\n"
"ESX12345678\n"
"FI99999999\n"
"FRXX999999999\n"
"HR99999999999\n"
"HU99999999\n"
"IE9S99999L\n"
"IE9999999WI\n"
"LT999999999\n"
"LT999999999999\n"
"LU99999999\n"
"LV99999999999\n"
"MT99999999\n"
"NL000099998B57\n"
"PL9999999999\n"
"PT999999999\n"
"RO999999999\n"
"SE999999999999\n"
"SI99999999\n"
"SK9999999999\n"
"XI999999999\n"
"XI999999999999\n"
"XIGD999\n"
"XIHA999")
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