import re
regex = re.compile(r"^(02|03|04|06|08|09|072|074|076|077|078|079|050|051|052|053|054|055|056|058|059)((?:(?![1,0]{1}))\d{7})$", flags=re.MULTILINE)
test_str = ("Valid: \n"
"022345677\n"
"032345677\n"
"042345677\n"
"062345677\n"
"082345677\n"
"092345677\n"
"0722345677\n"
"022345677\n"
"0742345677\n"
"0762345677\n"
"0742345677\n"
"0762345677\n"
"0772345677\n"
"0782345677\n"
"0792345677\n"
"0502345677\n"
"0512345677\n"
"0522345677\n"
"0532345677\n"
"0542345677\n"
"0552345677\n"
"0562345677\n"
"0582345677\n"
"0592345677\n\n"
"NotValid : \n"
"021345677\n"
"030345677\n"
"142345677\n"
"362345677\n"
"282345677\n"
"492345677\n"
"5722345677\n"
"622345677\n"
"7742345677\n"
"8762345677\n"
"9742345677\n\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