import re
regex = re.compile(r"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
test_str = ("Please contact us at: support@github.com.\n\n"
"Just send email to s.miller@mit.edu and j.hopking@york.ac.uk for more information.\n\n"
"Many users @ SoftUni confuse email addresses. We @ Softuni.BG provide \n"
"high-quality training @ home or @ class. –- steve.parker@soft.de.\n\n"
"• Examples of valid emails: info@company-hotels.org, kiki@hotmail.co.uk, no-reply@github.com, s.peterson@mail.uu.net, info-bg@software-software.software.academy. \n\n"
"• Examples of invalid emails: --123@gmail.com, …@mail.bg, .info@info.info, _steve@yahoo.cn, mike@helloworld, mike@.unknown.soft., s.johnson@invalid-.\n\n\n"
"\\b(\\b[\\w.\\w]+\\b)@(\\b[\\w.\\w]+\\b)\\b\n\n"
"\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")
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