The Python re.sub() method is used to substitute occurrences of a pattern in a string with another string. It searches for all non-overlapping matches of the pattern in the input string and replaces them with the specified replacement string.
This method is helpful for performing global search and replace operations on strings using regular expressions. It provides flexibility in pattern matching and replacement allowing for complex transformations of text data.
With optional parameters for count and flags re.sub() method offers control over the number of replacements and the matching behavior by making it a versatile tool for text processing tasks in Python.
SyntaxFollowing is the syntax and parameters of Python re.sub() method −
re.sub(pattern, repl, string, count=0, flags=0)Parameter
The Following are the parameters of the python re.sub() method −
This method returns an iterator of match objects
Example 1Following is the basic example of python re.sub() method, in which all numeric sequences in the string are replaced with the string 'number' −
import re result = re.sub(r'\d+', 'number', 'Welcome to Tutorialspoint learning 2024') print(result)Output
Welcome to Tutorialspoint learning numberExample 2
This example uses capturing groups in both the pattern and the replacement string to rearrange a date format.
import re result = re.sub(r'(\d+)-(\d+)-(\d+)', r'\3/\2/\1', 'Date: 2022-01-01') print(result)Output
Date: 01/01/2022Example 3
Here in this example a output of the function 'square' is used as the replacement. It squares each numeric match found in the string −
import re def square(match): num = int(match.group()) return str(num ** 2) result = re.sub(r'\d+', square, 'Numbers: 1 2 3 4 5') print(result)Output
Numbers: 1 4 9 16 25Example 4
In this example the count=1 argument limits the number of substitutions to 1, so only the first numeric sequence is replaced −
import re result = re.sub(r'\d+', 'number', 'There are 123 apples and 456 oranges.', count=1) print(result)Output
There are number apples and 456 oranges.
python_modules.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4