import re
regex = re.compile(r"(?i)^(?:NPO|PRO|PCR|REQ|8)+(\d{7,8})(?!(\d))", flags=re.MULTILINE)
test_str = ("NPO1234567\n"
"NPO12345678\n"
"NPO123456\n"
"NPO123456789\n"
"npo1234567\n"
"npo12345678\n"
"npo123456\n"
"npo123456789\n"
"PRO1234567\n"
"PRO12345678\n"
"PRO123456\n"
"PRO123456789\n"
"pro1234567\n"
"pro12345678\n"
"pro123456\n"
"pro123456789\n"
"PCR1234567\n"
"PCR12345678\n"
"PCR123456\n"
"PCR123456789\n"
"pcr1234567\n"
"pcr12345678\n"
"pcr123456\n"
"pcr123456789\n"
"REQ1234567\n"
"REQ12345678\n"
"REQ123456\n"
"REQ123456789\n"
"req1234567\n"
"req12345678\n"
"req123456\n"
"req123456789\n"
"81234567\n"
"812345678\n"
"8123456\n"
"8123456789\n")
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