import re
regex = re.compile(r"^[a-z]{1}[a-z0-9!#$%&'*+-\/=?^_`{|}~]{0,}@[a-z]{1}[a-z.0-9-]{0,}[a-z0-9]{1}\.[a-z0-9]{2,}$", flags=re.IGNORECASE | re.MULTILINE)
test_str = ("opinionated email validation based on https://en.wikipedia.org/wiki/Email_address\n\n"
"Valid email addresses\n"
"simple@example.com\n\n"
"werw@mydomain.gov.us\n"
"very.common@example.com\n"
"very.co34mmon@example.com\n"
"disposable.style.email.with+symbol@example.com\n"
"other.email-with-hyphen@example.com\n"
"other.email-with-hyphen@ex12ample.com\n"
"other.email-with-hyphen@exa-mple.com\n"
"fully-qualified-domain@example.com\n"
"example-indeed@strange-example.com\n\n\n\n\n"
"Invalid email addresses\n"
"user-@example.org (local part ending with non-alphanumeric character from the list of allowed printable characters)\n"
"x@example.com (one-letter local-part)\n"
"admin@mailserver1 (local domain name with no TLD, although ICANN highly discourages dotless email addresses[10])\n"
"user.name+tag+sorting@example.com (may go to user.name@example.com inbox depending on mail server)\n"
"user%example.com@example.org (% escaped mail route to user@example.com via example.org)\n"
"mailhost!username@example.org (bangified host route used for uucp mailers)\n\n"
"example@s.example (see the List of Internet top-level domains)\n"
" @example.org (space between the quotes)\n"
"john..doe@example.org (quoted double dot)\n"
"Abc.example.com (no @ character)\n"
"A@b@c@example.com (only one @ is allowed outside quotation marks)\n"
"a\"b(c)d,e:f;g<h>i[j\\k]l@example.com (none of the special characters in this local-part are allowed outside quotation marks)\n"
"just\"not\"right@example.com (quoted strings must be dot separated or the only element making up the local-part)\n"
"this is\"not\\allowed@example.com (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash)\n"
"this\\ still\\\"not\\\\allowed@example.com (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes)\n"
"1234567890123456789012345678901234567890123456789012345678901234+x@example.com (local-part is longer than 64 characters)\n"
"i_like_underscore@but_its_not_allowed_in_this_part.example.com (Underscore is not allowed in domain part)")
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