import re
regex = re.compile(r"^(((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|((([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])))$", flags=re.MULTILINE)
test_str = ("foo\n"
"foo.bar\n"
"www.foo.bar\n\n"
".foo\n"
"foo.\n"
"foo.com.\n"
".com.bar\n\n"
"foo-bar.com\n"
"foo-bar.com-eu\n"
"foo.bar.com\n\n"
"0.foo.bar\n"
"foo.1.bar\n"
"foo.bar.2\n"
".1.bar.com\n\n"
"foo.bar1.com\n"
"foo.2bar.com\n"
"foo.bar3.com\n"
"foo.bar.4com\n\n"
"127\n"
"127.0\n"
"127.0.0\n"
"127.0.0.1\n\n"
".127\n"
".127.0\n"
".127.0.0\n"
".127.0.0.1\n\n"
"8.8.8.8\n"
"8.8.4.4\n\n"
"c:/\n"
"c:/foo/\n"
"c:/foo/bar/\n\n"
"\\\\\n"
"\\\\foo\n"
"\\\\foo.bar\n"
"\\\\foo\\bar\n\n"
"http://regexp101.com\n"
"regexp101.com\n"
"www.regexp101.com\n"
"www.regexp101.com/")
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