Python re.sub() Method | Replace Strings Using Regex

In Python, many built-in methods are used to replace or remove the string, such as the “replace()” method and “string slicing”, etc. The “re.sub()” method uses the regex module to replace the string in Python. The built-in “re module” provides various programming operations like replacing string, splitting string and joining string, etc.

This write-up will briefly explain the working/usage of the Python “re.sub()” method to replace strings using regex in Python with multiple examples. The below-listed learning outcomes are discussed one by one:

So, let’s begin!

What is Python re.sub() Method?

In Python, the “re.sub()” method of re-module can search and replace substrings from the given string. The replacement of specific strings operates from left to right. This method can replace more than one pattern of a given string. The syntax of the “re.sub()” method is shown below:

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

In the above syntax:

  • The “pattern” parameter specifies the target pattern inside the string to search and replace.
  • The “repl” parameter indicates the value that must be replaced in the input string.
  • The “string” parameter takes a string as an input and performs a replacement operation on that string.
  • The “count” parameter specifies the multiple numbers of occurrences that have to be done on the given string. The default value of count is “zero”, meaning it can replace all the patterns in the given string.
  • The “flags” parameter is not necessary to define (optional). The default value of flags is “zero” which means no flag is raised.

Let’s practice this method to replace substrings in Python:

Example 1: Replace Specific Pattern From The String

The “re.sub()” method replaces some specific substring from the given string. Let’s understand it via the following code:

Code

import re
string_value = 'Hello, Welcome and Good Morning. How are you?'

pattern = 'Good Morning'
replacement = 'Good Evening'

#replace Good Morning with Good Evening
replaced_string = re.sub(pattern, replacement, string_value)
print(replaced_string)

In the above code:

  • Firstly, the “re” module is imported at the beginning of the program.
  • The string variable named “string_value” is initialized.
  • The “pattern” variable is initialized with the matched pattern of the string, and the “replacement” variable is initialized with the value to be replaced.
  • The “re.sub()” function takes the “pattern” and “replacement” variables as input. As a third parameter, the string variable is also placed inside the “re.sub()” function.
  • The value after replacement will be stored in the variable “replaced_string”.

Output

The output shows the replacement of “Good Morning” with “Good Evening”.

Example 2: Limit the Replacement Count

The “re.sub()” method can also replace a limited pattern from the string by taking the input value in the “count” parameter. The count value will specify the occurrences of the replacement that is performed on the given string. Let’s understand it via the following code:

Code

import re
string_value = "Ali is Python Developer and machine learning Expert"
# replace only first occurrence
replaced_string = re.sub("\s", "-", string_value, count=1)
print(replaced_string)

# replace first three occurrence
replaced_string1 = re.sub("\s", "-", string_value, count=3)
print(replaced_string1)

In the above code:

  • The “re” module is imported at the beginning of the program.
  • The string variable named “string_value” is initialized.
  • The “re.sub()” function takes the four input parameters. The first and second arguments take that string’s replacement value. The third parameter takes the “string_value” as an input in which three replacements are performed.
  • The “count” occurrences parameter takes the integer value and limits the replacement in the string.
  • The “re.sub()” function parameter “count” takes the “1” value to replace the first occurrence and the “3” value to replace the first three occurrences.

Output

The output shows that the “” hyphen replaced blank space one time when the count value became “1” and three times when the count value became “3”.

Example 3: Replace Multiple Patterns

The “re.sub()” method can replace multiple patterns from the string. Let’s understand it with an example of code  given below:

Code

import re

string_value = '[email protected] [email protected] [email protected]'

print(re.sub('gmail|yahoo|facebook', 'itslinuxfoss', string_value))

In the above code:

  • The “re” module is imported at the beginning of the program.
  • The string variable named “string_value” is initialized.
  • The “re.sub()” function takes three patterns from the given string and replaces them with a string named “itslinuxfoss”.

Output

In the above output, three different “substrings” are replaced with a string named “itslinuxfoss”.

That’s all from this guide!

Conclusion

In Python, the “re.sub()” method of the “re” module is used to replace the strings using regular expressions. The “re.sub()” method replaces multiple patterns with some specific string and also limits the replacement by taking integer values in the “count” parameter. This post has briefly explained  Python’s “re.sub()” method with numerous examples.