import re
regex = re.compile(r"^((http|https):\/\/)?([a-zA-Z0-9_][-_a-zA-Z0-9]{0,62}\.)+([a-zA-Z0-9]{1,10})$", flags=re.MULTILINE)
test_str = ("xor\n"
"xor.com\n"
"ww2.xor.co\n"
"www.xor.com\n"
"val-id.com\n"
"-invalid.com\n"
"http://xor.com\n"
"https://xour.cu\n"
"www.http://r.com\n"
"https://ww2.xour.cu\n"
"s2._domainkey.lixtil.com.au\n"
"3426127.group27-.hsbspot.co\n"
"hs2._domainkey.lixtil.com.au\n"
"3426127.grou p27-.hu3spot.co\n"
"little_com_au.xor.tinzy.330.net\n"
"3426127.group27.sites.hubseot.net\n"
"littil-com-au.hs01a.dkim.hubspoteeail.net\n"
"sg2.v8143875.c308460036.e.marketingfutomation.services")
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