import re
regex = re.compile(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$", flags=re.MULTILINE)
test_str = ("Format: minimum 2 characters @ minimum 3 characters . 2 - 63 characters\n\n"
"the g modifier let's us match multiple times in one string. the m modifier is useful here because it lets us treat each line as a seperate entry. Though in practice you probably dont need it for a simple one line email field\n\n"
"Jim@stuff.co.uk\n\n"
"Jim@google.com\n\n"
"Henry@h.com\n\n"
"Joe @m.net\n\n"
"Joe@m.net\n\n"
"Joe@mo.net\n\n"
"Joe@sam.net\n\n"
"a@sam.net\n\n"
"mo@sam.org\n\n"
"12@sam.org\n\n"
"Jon@sam.c\n\n"
"Jon@sam.co\n\n")
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