
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract 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 [email protected] and my friend's email is [email protected]
We should get the output −
[email protected] [email protected]
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 [email protected] and my friend's email is [email protected]" 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 −
[email protected] [email protected]
Advertisements