- 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
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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 firstname.lastname@example.org and my friend's email is email@example.com
We should get the output −
We can use the following regex for exatraction −
We can extract the email addresses using the find all method from re module. For example,
import re my_str = "Hi my name is John and email address is firstname.lastname@example.org and my friend's email is email@example.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)
This will give the output −
- 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