import re
regex = re.compile(r"(?<newfield>AB\d{2}-)")
test_str = ("1 1401812.AQWEAB02-TCPL02.1G\n"
"2 1356292.QWERAB04-ANCA02\n"
"3 1234OAB05-PLAIN02 reserved ||\n"
"4 1405252.AB07-SBCC01\n"
"5 1409325-ARDRAB05-GENIV02.22\n"
"6 1304030.ARDRAB07-TECEL02.10333\n"
"7 1389621.ABFDBC01-COGDS02333\n"
"8 1349222.ABFDBC01-MOH29.5MJJ\n"
"9 1313513.ABFDBC01-BPRSS\n"
"10 1393599.ABFDBC01-WGELP\n"
"11 1375957.ABFDBC01-BREQL01.0M222\n"
"12 1332348.ABFDBC01-MANNG01.10M1WW1\n"
"13 1321017.ABFDBC01-BLJCW01.3MQQ")
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