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A123 1234 1234 1234 1234 123\n"
"IT12A1231234123412341234123\n"
"CY12 A123 1234 1234 1234 1234 1234\n"
"CY12A12312341234123412341234\n"
"NL12 ABCD 1234 1234 12\n"
"NL12ABCD1234123412\n"
"LV12 ABCD 1234 1234 1234 1\n"
"LV12ABCD1234123412341\n"
"BG12 ABCD 1234 1234 1234 12\n"
"BG12ABCD12341234123412\n"
"GI12 ABCD 1234 1234 1234 123\n"
"GI12ABCD123412341234123\n"
"RO12 ABCD 1234 1234 1234 1234\n"
"RO12ABCD1234123412341234\n"
"KW12 ABCD 1234 1234 1234 1234 1234 12\n"
"KW12ABCD1234123412341234123412\n"
"MT12 ABCD 1234 1234 1234 1234 1234 123\n"
"MT12ABCD12341234123412341234123\n"
"NO12 1234 1234 1234 1234\n"
"NO121234123412341234\n"
"DK12 1234 1324 1234 12\n"
"DK1212341324123412\n"
"MK12 1234 1234 1234 123\n"
"MK12123412341234123\n"
"AT12 1234 1234 1234 1234\n"
"AT121234123412341234\n"
"BA12 1234 1234 1234 1234 1\n"
"BA1212341234123412341\n"
"DE12 1234 1234 1234 1234 12\n"
"DE12123412341234123412\n"
"IL12 1234 1234 1234 1234 123\n"
"IL121234123412341234123\n"
"CZ12 1234 1234 1234 1234 1234\n"
"CZ1212341234123412341234\n"
"PT12 1234 1234 1234 1234 1234 1\n"
"PT12123412341234123412341\n"
"BE12 1234 1234 1234 1234 1234 12\n"
"BE121234123412341234123412\n"
"FR12 1234 1234 1234 1234 1234 123\n"
"FR1212341234123412341234123\n"
"AL12 1234 1234 1234 1234 1234 1234\n"
"AL12123412341234123412341234\n"
"AZ12 1234 1234 1234 1234 1234 1234 1\n"
"AZ121234123412341234123412341\n"
"GR12 1234 1234 1234 1234 1234 1234 12\n"
"GR1212341234123412341234123412")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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