- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 email@example.com and my friend's email is firstname.lastname@example.org
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 email@example.com and my friend's email is firstname.lastname@example.org" 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 −
- 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
- Name validation using Java Regular Expressions
- Date validation using Java Regular Expressions
- Extracting MAC address using Python
- How to validate an email id using regular expression in Python?
- How to extract email id from text using Python regular expression?
- Phone Number validation using Java Regular Expressions
- Zip Code validation using Java Regular Expressions
- Extracting rows using Pandas .iloc in Python
- How do backslashes work in Python Regular Expressions?
- Java Regular Expressions Tutorial
- What is Regular Expressions?