import re
regex = re.compile(r"^(?<srcNet>\d{1,3}\.\d{1,3}\.\d{1,3}|[0-9a-f]{1,4}:[0-9a-f]{1,4}:[0-9a-f]{1,4}:[0-9a-f]{1,4})[.:]", flags=re.IGNORECASE | re.MULTILINE)
test_str = ("218.65.30.30\n"
"218.65.30.107\n"
"58.218.198.172\n"
"2001:468:1900::34\n"
"2001:468:4043:6343:1011:6604:3200:2883\n"
"::1\n"
"2001:0468:1900:1010:4030:3898:3021:aabC\n"
"103.3022.349.349\n"
".1\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