import re
regex = re.compile(r"^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$", flags=re.MULTILINE)
test_str = ("###################### Valid Emails ########################################################\n"
"######################## German, Austrian, international ###################################\n\n"
"janedoe@web.de\n"
"jane.doe@web.de\n"
"jdoe@web.de\n"
"jane-doe@web.de\n"
"jane@web.de\n"
"doe@web.de\n"
"janed@web.de\n"
"doej@web.de\n"
"jane_doe@web.de\n"
"j.doe@web.de\n"
"jane.d@web.de\n\n"
"janedoe@t-online.de\n"
"jane.doe@t-online.de\n"
"jdoe@t-online.de\n"
"jane-doe@t-online.de\n"
"jane@t-online.de\n"
"janed@t-online.de\n"
"doej@t-online.de\n"
"jane_doe@t-online.de\n"
"j.doe@t-online.de\n"
"jane.d@t-online.de\n\n"
"janedoe@hotmail.de\n"
"jane.doe@hotmail.de\n"
"jdoe@hotmail.de\n"
"jane-doe@hotmail.de\n\n"
"janedoe@gmx.de\n"
"jane.doe@gmx.de\n"
"jdoe@gmx.de\n"
"jane-doe@gmx.de\n\n\n"
"jane.doe@post.at\n"
"jane-doe@post.at\n"
"janedoe@post.at\n"
"jane@post.at\n"
"john.doe@austrian.com\n"
"horneaudrey@fulbright.at\n\n\n"
"#####################################################################################\n"
"########################## Not valid email ##########################################\n"
".test@gmail.com\n"
"-test@gmail.com\n"
"t.-.est@gmail.com\n"
".test.@gmail.com\n"
"test.@gmail.com\n"
"test.@.gmail.com\n"
"test@.gmail.com\n"
"test@.gmail.com.\n"
"test@gmail.com.\n\n"
"\"email\"@example.com\n\n"
"#@%^%#$@#$@#.com\n"
"@example.com\n"
"Joe Smith <email@example.com>\n"
"email.example.com\n"
"email@example@example.com\n"
".email@example.com\n"
"email.@example.com\n"
"email..email@example.com\n"
"あいうえお@example.com\n"
"email@example.com (Joe Smith)\n"
"email@example\n"
"email@-example.com\n"
"email@example.web\n"
"email@111.222.333.44444\n"
"email@example..com\n"
"Abc..123@example.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