import re
regex = re.compile(r"((http(s)?[:\/\/]{1,}))?([a-z0-9\-]{1,}[.])([a-z0-9\-]{1,}[.])?([a-z]{2,3})(.{1,})?", flags=re.IGNORECASE)
test_str = ("http://example.com\n\n"
"http://subdom.example.com\n\n"
"http://example.uk\n\n"
"https://example.com\n\n"
"https://subdom.example.com\n\n"
"https://example.uk\n\n"
"http://example.com/?query=butt\n\n"
"http://subdom.example.com/?query=butt\n\n"
"http://example.uk/?query=butt\n\n"
"https://example.com/?query=butt\n\n"
"https://subdom.example.com/?query=butt\n\n"
"https://example.uk/?query=butr\n\n"
"tasdfa .ss")
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