import re
regex = re.compile(r"(?<ipv6_full>(?:^(?:f[c-f][0-9a-f]{2}|200[1-2]|0?100):).+|(?:.+:0{0,3}1$|(?:.+:ffff:(?:0:)?|^0{0,2}64:ff9b::)(?<ipv4_map>(?:25[0-5]|2[0-4]\d|[0-1]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d{1,2})){3})$))", flags=re.MULTILINE)
test_str = ("::1\n"
"100::\n"
"2001::\n"
"2001:20::\n"
"2001:db8::\n"
"2002::\n"
"fc00::\n"
"fe80::\n"
"ff00::\n"
"64:ff9b::0.0.0.0\n"
"100::\n"
"::ffff:0.0.0.0\n"
"::ffff:0:255.255.255.255")
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