import re
regex = re.compile(r"^(?:(?=(\d)..\1(?!0*$)|(?:0..1|1..2|2..3|3..4|4..5|5..6|6..7|7..8|8..9|9..0)0*$)\d){3}\d{3}$", flags=re.MULTILINE)
test_str = ("Pass:\n"
"000001\n"
"009010\n"
"010011\n"
"019020\n"
"057058\n"
"099100\n"
"100101\n"
"299300\n"
"398399\n"
"404405\n"
"500501\n"
"665666\n"
"666667\n"
"899900\n"
"998999\n\n"
"Edge Case:\n"
"999000\n\n"
"Not Pass:\n"
"000000\n"
"123456\n"
"123234\n"
"000002\n"
"010002\n"
"010012\n"
"050060\n"
"397399\n"
"0071072\n"
"89090\n"
"0890900\n"
"1234\n"
"200210\n"
"123123124")
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