Regex stands for Regular Expressions. It is used to find out information based on complex patterns in text. Regular Expressions has the support of native library function as well as add-on library.
Python supports Regex as it is a part of Python’s standard library. Regular Expression Library serves in many of the operations. One of its exciting usages is to substitute/replace patterns in the text.
In this article, we will learn to replace patterns in a Python String using Regex with the help of different examples. First, we will go through the basics of Regex in Python. The replacement of patterns will be demonstrated with the use of code snippets and output.
Table of Contents
Python Regex Basics:
You have to import Regex Module/ Library of Python for Regular Expression syntax.
To learn how to replace Patterns in a string using Regex, we first need to understand the re.sub () method of Regular Expressions. It is Regex Replacement Function.
This is the Regex string replace method. It is used as a search function for the patterns and then replaces a specific pattern in a string. The syntax is given as:
re.sub (pattern, repl, string, count=0, flags=0)
- pattern:the patterns to be searched and substituted
- repl:the string that will replace the Pattern
- string:variable name that stores the Pattern
- count=0:the characters up to which the string will be replaced
- flags:modifies the regex pattern meaning
Let’s have a look at different examples to understand the concept.
Example No. 01: A set of characters with only one specific character
The following example will implement how to replace a range of characters with only one single character.
import re def substitutor(): mysentence = "He loves fish tacos." print(re.sub(r"[a-z]", "2", mysentence)) substitutor()
Example No. 02: Specific Text Pattern
This example will cover the primary use of re.sub () and string match replaces the string with only 3 of the parameters.
import re def substitutor(): mysentence = "Visit Codeleaks for more Articles.” print(re.sub (r"Articles", "Information", mysentence)) substitutor()
Example No. 03: Using count parameter
The below example is implementing string substitution up to a certain number of characters.
import re def substitutor(): mysentence = ‘Salad is for rabbits.’ print(re.sub(r"[a-z]", "1", mysentence, 10, flags = re.I)) substitutor()
Example No. 04: Replacing a case-insensitive set of characters
We will use a regex module flag to carry out the replacement of case-insensitive match characters. This will ignore the lowercase letter and uppercase letter in the entire string. This example will demonstrate the case-insensitive approach of regular expression function.
import re def substitutor(): mysentence = "I love my new pets." print(re.sub(r"[a-z]", "6", mysentence, flags = re.I)) substitutor()
The above examples have demonstrated distinct ways of replacing a pattern in a string using Regular Expression in Python. I hope these examples are pretty clear to have a proper understanding of the Regex Python substitution technique.