Python Regex (Regular Expression or RE) | Operations

Regular expressions  (REs, or regexes, or regex patterns) are used to identify whether a pattern exists in a given sequence of strings or not. For example validation of Email, phone number, and passwords, etc. Python Regex is basic a tiny, highly specialized programming language embedded inside Python, which is available through the re module.

Python Regex (Regular Expression or RE) | Operations | Examples

The regular expressions are extremely powerful and useful, you will learn how to use them in Python in this tutorial.

Basically used at the server side to validate the format of email addresses or phone numbers or passwords during registration. Another use of the Python re module is parsing text data files to find, replace or delete certain string, etc.

Python Regex Syntax

In Python, regular expressions are supported by the re module. It’s required to import a module in your code to use Python Regex.

Simple String matching Regular Expression Example :

Here is importing a python re module and matching the strings.

Output: Matched!

Python regular expression methods

In python “re” module provides a several inbuilt functions to perform an option.

  • re.match() – Determine if the RE matches at the beginning of the string.
  • – Scan through a string, looking for any location where this RE matches.
  • re.findall() – Find all substrings where the RE matches, and returns them as a list.

Python re.match() Function Example :

Matching the word in a string.

Output : <re.Match object; span=(0, 5), match=’split‘>

Python Function Example :

Search normal word in a string.

Output : <re.Match object; span=(14, 16), match=’in‘>

Python re.findall() Function Example :

Email example – Using regex to find the email pattern in a string.

Output : [‘[email protected]’]

Matching Characters

\dMatches any decimal digit; this is equivalent to the class [0-9].

\DMatches any non-digit character; this is equivalent to the class [^0-9].

\sMatches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].

\SMatches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v].

\wMatches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].

\WMatches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_].

Modifying Strings Functions in Python re

Regular expressions are also commonly used to modify strings in various ways, using the following pattern functions:

  • split() – Split the string into a list, splitting it wherever the RE matches
  • sub() – Find all substrings where the RE matches, and replace them with a different string
  • subn() – Does the same thing as sub(), but returns the new string and the number of replacements

Python re – re.split function

\s – Matches whitespace. Equivalent to [\t\n\r\f].

Output : [‘split’, ‘the’, ‘string’, ‘in’, ‘python’]

Python Search and Replace – sub() and subn()Function 

The sub() method takes a replacement value, which can be either a string or a function, and the string to be processed.

Output: search the number in python

The subn() method does the same work, but returns a 2-tuple containing the new string value and the number of replacements that were performed:

Output : (‘search the number in python’, 1)

Reference website:

Do comment if you have nay doubts and suggestion on this tutorial.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Python Regex Examples are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.