Python re.sub() - Replace using Regular Expression


Python re.sub() Function

re.sub() function replaces one or many matches with a string in the given text. The search and replacement happens from left to right.

In this tutorial, we will learn how to use re.sub() function with the help of example programs.

Syntax - re.sub()

The syntax of re.sub() function is

re.sub(pattern, repl, string, count=0, flags=0)

where

ParameterDescription
pattern[Mandatory] The pattern which has to be found in the string.
repl[Mandatory] The value which has to be replaced in the string in place of matched pattern.
string[Mandatory] The string in which the replacement has to be done.
count[Optional] The maximum number of pattern occurrences to be replaced.
flags[Optional] Optional flags like re.IGNORECASE, etc.

Return Value

The function returns a List object.

Example 1: re.sub() - Replace Pattern Matchings with Replacement String

In this example, we will take a string and replace patterns that contains a continuous occurrence of numbers with the string NN. We will do the replacement using re.sub() function.

Python Program

import re

pattern = '[0-9]+'
string = 'Account Number - 12345, Amount - 586.32'
repl = 'NN'

print('Original string')
print(string)

result = re.sub(pattern, repl, string)

print('After replacement')
print(result)

Output

Original string
Account Number - 12345, Amount - 586.32
After replacement
Account Number - NN, Amount - NN.NN

Example 2: re.sub() - Limit Maximum Number of Replacement

We can limit the maximum number of replacements by re.sub() function by specifying count optional argument.

In this example, we will take the same pattern, string, and replacement as in the previous example. But, we shall limit the maximum number of replacements to 2.

Python Program

import re

pattern = '[0-9]+'
string = 'Account Number - 12345, Amount - 586.32'
repl = 'NN'

print('Original string')
print(string)

result = re.sub(pattern, repl, string, count=2)

print('After replacement')
print(result)

Output

Account Number - 12345, Amount - 586.32
After replacement
Account Number - NN, Amount - NN.32

Only two pattern matching are replaced with the replacement string. Rest of the matchings are not replaced.

Example 3: re.sub() - Optional Flags

In this example, we will pass optional flags argument re.IGNORECASE to re.sub() function. This flag tells re.sub() function to ignore case while matching the pattern in the string.

Python Program

import re

pattern = '[a-z]+'
string = 'Account Number - 12345, Amount - 586.32'
repl = 'AA'

print('Original string')
print(string)

result = re.sub(pattern, repl, string, flags=re.IGNORECASE)

print('After replacement')
print(result)

Output

Original string
Account Number - 12345, Amount - 586.32
After replacement
AA AA - 12345, AA - 586.32

Summary

In this tutorial of Python Examples, we learned how to use re.sub() function to replace or substitute all the matchings for a given pattern in a string with the replacement string, with the help of example programs.