Extracting email addresses using regular expressions in Python

PythonProgrammingServer Side Programming

Email addresses are pretty complex and do not have a standard being followed all over the world which makes it difficult to identify an email in a regex. The RFC 5322 specifies the format of an email address. We'll use this format to extract email addresses from the text.

For example, for a given input string −

Hi my name is John and email address is john.doe@somecompany.co.uk and my friend's email is jane_doe124@gmail.com

We should get the output −

john.doe@somecompany.co.uk
jane_doe124@gmail.com

We can use the following regex for exatraction −

[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+

We can extract the email addresses using the find all method from re module. For example,

Example

import re

my_str = "Hi my name is John and email address is john.doe@somecompany.co.uk and my friend's email is jane_doe124@gmail.com"
emails = re.findall("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", my_str)

for mail in an email:
print(mail)

Output

This will give the output −

john.doe@somecompany.co.uk
jane_doe124@gmail.com
raja
Published on 03-Aug-2018 06:10:42
Advertisements