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 [email protected] Rohit [email protected] Output : [email protected] [email protected] 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 [email protected] to [email protected] 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: ['[email protected]', '[email protected]'] 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@[email protected] for any business inquiry please mail us at business@[email protected]"Output: Hr@[email protected]@[email protected] 4 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 Like