import re
regex = re.compile(r"(?im)^(?=.{1,64}@)(?:(\"[^\"\\]*(?:\\.[^\"\\]*)*\"@)|((?:(?:\([^)]*\)|[^\W_])(?:\.(?!\.)|[-!#\$%&'\*\+\/=\?\^`\{\}\|~\w]|\([^)]*\))*)?[^\W_](?:\([^)]*\))?@))(?=.{1,255}$)(?:(\[[^\[\]]+\])|((?:(?=.{1,63}\.)[^\W_][-\w]*[^\W_]*\.)+[^\W_](?:[^\W_]|-){0,22}[^\W_])|((?=.{1,63}$)[^\W_][-\w]*))$")
test_str = ("info@kamikaze.uk\n"
"alfred@live.nl\n"
"dubai@organon.it\n"
"karel.de.koning@hetnet.nl\n"
"decock@upc.nl\n"
"jiazhe_xu@sina.cn\n"
"xfunkblasterx@yahoomail.org\n"
"super-duper-email@hotmail.com\n\n"
"\"John..Doe\"@example.com\n"
"john.smith(comment)@example.com\n"
"(comment)john.smith@example.com\n"
"john.smith@worldwideweb.123com_org\n"
"\"John.\"(),:;<>@[\\].Doe\"@.123radio_tv\n"
"\"much.more unusual\"@example.com\n"
"other.email-with-dash@example.com\n"
"disposable.style.email.with+symbol@example.com\n"
"\"()<>[]:,;@\\\\\"!#$%&'-/=?^_`{}| ~.a\"@example.org\n\n"
"smith@[IPv6:2001:db8::1]\n"
"jsmith@[192.168.2.1]")
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