import re
regex = re.compile(r"(?<type>[A-Za-z]{1})(?<subtype>[A-Za-z<]{1})(?<expcountry>[A-Za-z]{3})(?<docnumber>[0-9A-Za-z<]{9})(?<docnumbercheck>[0-9]{1})(?<complement>[0-9a-zA-Z<]{15})\n(?<birthdate>[0-9]{6})(?<birthcheck>[0-9]{1})(?<sex>[mfMF]{1})(?<expiredate>[0-9]{6})(?<expirecheck>[0-9]{1})(?<nacionality>[A-Za-z]{3})(?<optional>[a-zA-Z0-9<]{11})(?<linecheck>[0-9]{1})\n(?<lname>[A-Z]+)(?<lname2><[A-Z]+){0,}<<(?<spacing>[<]{0,})(?<fname>[A-Z]+)(?<mname1><[A-Z]+){0,}(?<namecomplement>[a-zA-Z<]+){0,}")
test_str = ("IDCHES0002068<8<<<<<<<<<<<<<<<\n"
"8102287F1301014CHE<<<<<<<<<<<4\n"
"VADIS<<QUO<<<<<<<<<<<<<<<<<<<<\n\n"
"I<UTOD231458907<<<<<<<<<<<<<<<\n"
"7408122F1204159UTO<<<<<<<<<<<6\n"
"ERIKSSON<<ANNA<MARIA<<<<<<<<<<\n\n"
"I<NLDXI020DF236123456783<<<<<<\n"
"7208148F1108268NLD<<<<<<<<<<<9\n"
"VAN<DER<STEEN<<MARIANNE<LOUISE")
match = regex.search(test_str)
if match:
print(f"Match 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