import re
regex = re.compile(r"^([a-f\d]{1,4}:){7}[a-f\d]{1,4}$", flags=re.MULTILINE)
test_str = ("1050:1000:1000:a000:5:600:300c:326b\n"
"1050:1000:2000:ab00:5:600:300c:326a\n"
"1050:1000:3000:abc0:5:600:300c:326c\n"
"1051:1000:4000:abcd:5:600:300c:326b\n"
"1050:0:0:0:5:600:300c:326b\n"
"1050:0:0:0:5:600:300c:326a\n"
"1050:0:0:0:5:600:300c:326c\n"
"1051:0:0:0:5:600:300c:326b\n"
"1050:10:0:0:5:600:300c:326b\n"
"1050:10:0:0:5:600:300c:326a\n"
"1050:10:0:0:5:600:300c:326c\n"
"1051:10:0:0:5:600:300c:326b\n"
"1050:10:0:0:15:600:300c:326b\n"
"1050:10:0:10:5:600:300c:326a\n"
"1050:10:10:0:5:600:300c:326c\n"
"1051:110:0:0:5:600:300c:326b\n\n"
"1050:10:0:0:15:600:300c:326k\n"
"1050:10:0:0:15:600:300c:326g\n"
"1050:10:0:0:15:600:300c:326h\n"
"1050:10:0:0:15:600:300c:326i\n"
"not an ip address\n"
"not an ipv4 Address\n"
"Not an IPv5 Address")
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