Wednesday, January 8, 2020

Python - Extracting Email Addresses Using Regular Expressions

As a python developers, we have to accomplished a lot of jobs such as data cleansing from a file before processing the other business operations. 

For an example, you have a raw data text file and you have to read some specific data like email addresses by  to performing the actual Regular Expression matching.

What is a Regular Expression and which module is used in Python?
Regular expression is a sequence of special character(s) mainly used to find and replace patterns in a string or file, using a specialized syntax held in a pattern. 
The Python module re provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression.
Example -
# Python program to extract emails from the String By Regular Expression.
  
# Importing module required for regular
# expressions
import re 
  
# Example string 
txt = "Ryan has sent an invoice email to john.d@yahoo.com by using his email id ryan.arjun@gmail.com and he also shared a copy to his boss rosy.gray@amazon.co.uk on the cc part."
  
# \w matches any non-whitespace character
# @ for as in the Email
# + for Repeats a character one or more times
findEmail = re.findall(r'[\w\.-]+@[\w\.-]+', txt)  

# Printing findEmail of List
print(findEmail)


Output - 
['john1.d@yahoo.com', 'ryan.arjun@gmail.com', 'rosy.gray@amazon.co.uk']


To learn more, please follow us -

To Learn more, please visit our YouTube channel at - 
To Learn more, please visit our Instagram account at -
To Learn more, please visit our twitter account at -

Popular Posts

Get Sponsored by Big Brands