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 |

`# 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) ` |

*chevron_right*

*filter_none*

**Output:**

['2', '7', '10']

`# 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) ` |

*chevron_right*

*filter_none*

**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

## Recommended Posts:

- Regular Expressions in Python | Set 2 (Search, Match and Find All)
- Regular Expressions in Python
- Python Flags to Tune the Behavior of Regular Expressions
- Extracting MAC address using Python
- Python | Pandas Extracting rows using .loc[]
- Python | Extracting rows using Pandas .iloc[]
- Using CountVectorizer to Extracting Features from Text
- Python - Extracting Kth Key in Dictionary
- Python - Extracting keys not in values
- Python - Extracting Key from Value Substring
- Python - Extracting Priority Elements in Tuple List
- Extracting an attribute value with beautifulsoup in Python
- Program to generate all possible valid IP addresses from given string
- NLP | Extracting Named Entities
- Extracting the real and imaginary parts of an NumPy array of complex numbers
- Plot Mathematical Expressions in Python using Matplotlib
- Evaluate the Mathematical Expressions using Tkinter in Python
- Send PDF File through Email using pdf-mail module
- How to extract Email column from Excel file and find out the type of mail using Pandas?
- Python program to extract Email-id from URL text file

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.