import re
regex = re.compile(r"(?<type>[A-Z]{1})(?<subtype>[A-Z]{1})(?<country>[A-Z]{3})(?<last_name>[A-Z<]{25})(?<department>[A-Z0-9<]{3})(?<office>[0-9<]{3})\n(?<id_card>[A-Z0-9]{12})(?<id_check>[0-9]{1})(?<first_name>[A-Z<]{14})(?<birth_date>[0-9]{6})(?<birth_check>[0-9]{1})(?<gender>[MF]{1})(?<full_check>[0-9]{1})")
test_str = ("IDFRALOISEAU<<<<<<<<<<<<<<<<<<<<<<<<\n"
"970675K002774HERVE<<DJAMEL<7303216M4\n\n"
"IDFRAPALERMO<<<<<<<<<<<<<<<<<<061773\n"
"1309061227772BIANCA<<HELLY<8911205F7\n\n"
"IDFRAPETE<<<<<<<<<<<<<<<<<<<<<952042\n"
"0509952018746NICOLAS<<PAUL<8206152M3")
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