How to use Regex in Python | Email Regex in Python | 2022

After reading this article. You will be familiar with the fundamentals of regular expression & how to use regex in python.

A regex in or Regular Expression in Python is a sequence of characters that forms a search pattern.RegEx can be used to check if a string contains the specified search pattern.

  • Regular Expression are symbols in python that make your job in extracting much easy
  •  Before you can use regular expressions in your program, you must import the library using “ import re
  • There are some functions that you can use by this library that makes extracting data easier to code
  1. re.search()
    • To see if a string matches a regular expression, similar to the find() method for strings
  2. re.findall()
    • To see if a string matches a regular expression, similar to “find()+slicing

Regular Expressions in python:

  1. ( ^ )               Matches the beginning of a line
  2. ( . )               Matches any character
  3. ( $ )               Matches The End of a line
  4. ( \s )             Matches any whitespace character
  5. ( \S )             Matches any non-whitespace character
  6. ( * )               Repeats a character zero or more time
  7. ( *? )             Repeats a character zero or more times (Nongreedy)
  8. ( + )               Repeats a character one or more time
  9. ( +? )             Repeats a character one or more times (Nongreedy)
  10.  [aeiou]         Matches a single character in the listed set
  11.  [^XYZ]          Matches a single character, not in the listed set
  12.  [a-z0-9]        the set of characters can include a range
  13.  (                   indicates where string extracting is to start
  14.  )                   indicates where string extracting is to End
  15.  [  ]  nonblank character

How to Use Regex in Python for Email Validation

In this example, we will give a simple code to find any emails in a text file

Step 1:

  • When you use a regular expression you should import its library first which called ‘re’ that library gives you all the regular expressions that you need to use in the program.
  • You can import this library by writing at the beginning of the code (import re)
import namespace in python for regex

Step 2:

  • Make an input function for the user to let him enter the name of the text file
  • Also, add to the name the text file extension (.txt)
input from console to test regex

Step 3:

  • You should use open function now to open the file and read it, using open() and an attribute ‘r’
opne file in python

Step 4:

  • Now we should start dealing with each line separately, so you should make a for loop
  • inside that loop, you should add a statement block to find each line that has an email
  • if you were about to use anything else as the previous tutorials, it would take many lines from you to find an email inside that line but using regular expression it only needs two lines
  • you must use re.findall() and a regular expression ( ‘\S+@\S+’), that regular expression means you have to find any word that has ‘@’ and before and after the ‘@’ any word with no white spaces
search text in file using python

Step 5:

  • Run the code to see how the regular expression code find the emails through the file
Console output in python

In this tutorial, We tried to understand the basics of regular expressions or regex in Python with example. Let us know about your thoughts or any concern in the below comments section.

Scroll to Top