import re
regex = re.compile(r"^(?!\.)((^|\.)([1-9]?\d|1\d\d|2(5[0-5]|[0-4]\d))){4}$", flags=re.MULTILINE)
test_str = ("115.42.150.37\n"
"192.168.0.1\n"
"110.234.52.124\n\n"
"01.01.01.01\n"
"1.1.1.01\n"
"123123123213\n"
"210.110\n"
"255\n"
"y.y.y.y\n"
"255.0.0.y\n"
"666.10.10.20\n"
"4444.11.11.11\n"
"33.3333.33.3\n"
"365.1.1.1\n"
"123.123.123.123.123\n"
".123.123.123.123\n"
"123.123.123.123.\n"
"1.2.3.256\n"
"1.2.260.1\n"
"1.333.1.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