import re
regex = re.compile(r"^([a-z][a-z0-9]{1,13}[_.\-]?[a-z0-9]{1,13})@(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|([a-z0-9]{1,10}[\-]?[a-z0-9]{1,10}[.]?[a-z0-9-]{1,10}[.]{1}[a-z]{2,4}))$", flags=re.MULTILINE)
test_str = ("//Valid\n"
"john.doe@mail.com\n"
"johndoe@mail.com\n"
"john-doe@mail.com\n"
"john_doe@mail.com\n"
"john123doe@mail.com\n"
"john123.doe@mail.com\n"
"johndoe456@mail.com\n"
"abc-d@mail.com\n"
"test@test.domain.com\n"
"test@test.domain.shop\n"
"test@127.1.1.1\n"
"test@178.117.125.185\n\n"
"// Invalid\n"
"abc-@mail.com\n"
"abc..def@mail.com\n"
".abc@mail.com\n"
"abc#def@mail.com\n"
"123abc-d@mail.com\n"
"ABc@mail.com\n"
"test@sub.test.domain.com\n"
"test@test.domain.c\n"
"test@300.1.1.1\n"
"test@1.256.1.1\n"
"test@1.127.268.1\n"
"test@1.127.1.485\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