import re
regex = re.compile(r"^((25[0-5]|(2[0-4]|1\d|[1-9])?\d)(\.(?!$)|$)){4}$", flags=re.MULTILINE)
test_str = ("192.168.0.12\n"
"255.255.255.0\n"
"256.178.222.345\n"
"218.255.1.0\n"
" 1.1.1.1 e ip 192.168.10.3\n"
"0.0.0.0\n"
"192.0.9.25\n"
"33.1.1.1\n"
"123.1.5.4\n"
"1.10.100.3\n"
"3.\n\n\n"
"http://192.168.255.111/teste\n\n\n"
"30.168.1.255.1\n"
"127.1\n"
"192.168.1.256\n"
"-1.2.3.4 \n"
"1.1.1.1.\n"
"3...3")
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