import re
regex = re.compile(r"^(?!-)(xn--)?[a-zA-Z0-9][a-zA-Z0-9-_]{0,61}[a-zA-Z0-9]{0,1}\.(?!-)(xn--)?([a-zA-Z0-9\-]{1,50}|[a-zA-Z0-9-]{1,30}\.[a-zA-Z]{2,})$", flags=re.MULTILINE)
test_str = ("#Valid\n"
"xn-fsqu00a.xn-0zwm56d\n"
"xn-fsqu00a.xn--vermgensberatung-pwb\n"
"xn--stackoverflow.com\n"
"stackoverflow.xn--com\n"
"stackoverflow.co.uk\n"
"google.com.au\n"
"i.oh1.me\n"
"wow.british-library.uk\n"
"xn--stackoverflow.com\n"
"stackoverflow.xn--com\n"
"stackoverflow.co.uk\n"
"0-0O_.COM\n"
"a.net\n"
"0-0O.COM\n"
"0-OZ.CO.uk\n"
"0-TENSION.COM.br\n"
"0-WH-AO14-0.COM-com.net\n"
"a-1234567890-1234567890-1234567890-1234567890-1234567890-1234-z.eu.us\n"
"#Invalid\n"
"-0-0O.COM\n"
"0-0O.-COM\n"
"-a.dot\n"
"a-1234567890-1234567890-1234567890-1234567890-1234567890-12345-z.eu.us")
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