Extracting email addresses using regular expressions in Python Last Updated : 29 Dec, 2020 Comments Improve Suggest changes Like Article Like Report Let suppose a situation in which you have to read some specific data like phone numbers, email addresses, dates, a collection of words etc. How can you do this in a very efficient manner?The Best way to do this by Regular Expression. Let take an example in which we have to find out only email from the given input by Regular Expression. Examples: Input : Hello shubhamg199630@gmail.com Rohit neeraj@gmail.com Output : shubhamg199630@gmail.com neeraj@gmail.com Here we have only selected email from the given input string. Input : My 2 favourite numbers are 7 and 10 Output :2 7 10 Here we have selected only digits. Regular Expression- Regular expression is a sequence of character(s) mainly used to find and replace patterns in a string or file. So we can say that the task of searching and extracting is so common that Python has a very powerful library called regular expressions that handles many of these tasks quite elegantly. Symbol Usage $ Matches the end of the line \s Matches whitespace \S Matches any non-whitespace character * Repeats a character zero or more times \S Matches any non-whitespace character *? Repeats a character zero or more times (non-greedy) + Repeats a character one or more times +? Repeats a character one or more times (non-greedy) [aeiou] Matches a single character in the listed set [^XYZ] Matches a single character not in the listed set [a-z0-9] The set of characters can include a range ( Indicates where string extraction is to start ) Indicates where string extraction is to end Python3 # Python program to extract numeric digit # from A string by regular expression... # Importing module required for regular # expressions import re # Example String s = 'My 2 favourite numbers are 7 and 10' # find all function to select all digit from 0 # to 9 [0-9] for numeric Letter in the String # + for repeats a character one or more times lst = re.findall('[0-9]+', s) # Printing of List print(lst) Output: ['2', '7', '10'] Python3 # Python program to extract emails From # the String By Regular Expression. # Importing module required for regular # expressions import re # Example string s = """Hello from shubhamg199630@gmail.com to priya@yahoo.com about the meeting @2PM""" # \S matches any non-whitespace character # @ for as in the Email # + for Repeats a character one or more times lst = re.findall('\S+@\S+', s) # Printing of List print(lst) Output: ['shubhamg199630@gmail.com', 'priya@yahoo.com'] For more details: Regular Expression in Python with Examples | Set 1 Regular Expressions in Python | Set 2 (Search, Match and Find All) Python Docs for Regular Expression Comment More infoAdvertise with us S shubhamg199630 Follow Improve Article Tags : Python python-regex Python Regex-programs Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 6 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 8 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like