
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Extracting email addresses using regular expressions in Python
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
- Related Articles
- Checking for valid email address using regular expressions in Java
- How to validate an email address using Java regular expressions.
- Select all email addresses beginning with 5 numeric characters (regular expression) in MySQL
- How to match whitespace in python using regular expressions
- How to validate an email id using regular expression in Python?
- How to extract email id from text using Python regular expression?
- Extracting MAC address using Python
- Date validation using Java Regular Expressions
- Name validation using Java Regular Expressions
- How to Convert Multiple Email Addresses to Hyperlinks in Excel?
- Extracting rows using Pandas .iloc[] in Python
- How to match whitespace but not newlines using Python regular expressions?
- How do backslashes work in Python Regular Expressions?
- Extracting Regular Expression from the Slice in Golang
- Phone Number validation using Java Regular Expressions

Advertisements