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 Next Article Extracting email addresses using regular expressions in Python S shubhamg199630 Follow Improve Article Tags : Misc Pattern Searching Python DSA python-regex Python Regex-programs +2 More Practice Tags : MiscPattern Searchingpython Similar Reads Extracting all Email Ids in any given String using Regular Expressions Given a string str, the task is to extract all the Email ID's from the given string.Example:Input: "Please send your resumes to Hr@Iwillgetbacktoyou@gmail.com for any business inquiry please mail us at business@enquiry@gmail.com"Output: Hr@Iwillgetbacktoyou@gmail.combusiness@enquiry@gmail.comApproac 4 min read How to validate an IP address using Regular Expressions in Java Given an IP address, the task is to validate this IP address with the help of Regular Expressions.The IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.Examples: Inpu 3 min read Python Flags to Tune the Behavior of Regular Expressions Python offers some flags to modify the behavior of regular expression engines. Let's discuss them below: Case InsensitivityDot Matching NewlineMultiline ModeVerbose ModeDebug ModeCase Insensitivity The re.IGNORECASE allows the regular expression to become case-insensitive. Here, the match is returne 3 min read Parsing and Processing URL using Python - Regex Prerequisite: Regular Expression in Python URL or Uniform Resource Locator consists of many information parts, such as the domain name, path, port number etc. Any URL can be processed and parsed using Regular Expression. So for using Regular Expression we have to use re library in Python. Example: U 3 min read How Can I Find All Matches to a Regular Expression in Python? In Python, regular expressions (regex) are a powerful tool for finding patterns in text. Whether we're searching through logs, extracting specific data from a document, or performing complex string manipulations, Python's re module makes working with regular expressions straightforward. In this arti 3 min read Extract IP address from file using Python Let us see how to extract IP addresses from a file using Python. Algorithm :  Import the re module for regular expression.Open the file using the open() function.Read all the lines in the file and store them in a list.Declare the pattern for IP addresses. The regex pattern is :  r'(\d{1,3}\.\d{1,3 2 min read How to validate a domain name using Regular Expression Given string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression.The valid domain name must satisfy the following conditions: The domain name should be a-z or A-Z or 0-9 and hyphen (-).The domain name should be between 1 and 63 characters long 6 min read How to read Emails from Gmail using Gmail API in Python ? In this article, we will see how to read Emails from your Gmail using Gmail API in Python. Gmail API is a RESTful API that allows users to interact with your Gmail account and use its features with a Python script. So, let's go ahead and write a simple Python script to read emails. RequirementsPytho 6 min read Rule-Based Data Extraction in HTML using Python textminer Module While working with HTML, there are various requirements to extract data from plain HTML tags in an ordered manner in form of python data containers such as lists, dict, integers, etc. This article deals with a library that helps to achieve this using a rule-based approach. Features of Python - textm 3 min read Sending Email using FastAPI Framework in Python Before jumping into the topic directly, let's have a small intro about the technologies we are going to use. As the name suggests, we will be using FastAPI, a Python language framework. FastAPI: FastAPI is a python framework to develop REST Apis. It is very easy to build,  high performance, easy to 3 min read Like