import re
regex = re.compile(r"(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})\b(?! is variably)")
test_str = ("D 10.50.80.0/24 [90/3072] via 10.10.10.1, 3w6d, Vlan10\n"
"C 10.10.140.0/24 is directly connected, Vlan240\n"
"10.10.140.0/2\n"
"10.10.140.0/16\n"
"2.2.2.2/24\n"
"5.5.5.5.5/24\n\n"
"// Negative\n"
"10.0.0.0\n"
" 10.10.60.0/16 is variably subnetted, 58 subnets, 4 masks\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