Python regex replace


A regular expression specifies a set of strings that matches it; The re module in Python provides methods to check if a string matches with a regular expression or not and is used to perform different operations based on this.

For example sub() and subn() methods are used to find and replace a string in Python.

Today in this article, I will discuss using Python regular expressions to perform search and replace operations on strings.

How to replace a string in Python using regular expression

Both re.sub() and re.subn() methods can be used to perform search and replace operations on a string in Python. The re.subn() is a new method that performs the same task but returns the result a little bit differently.

Python re.sub() method

The re.sub() is a built-in method of the re module in Python that accepts five arguments maximum and returns replaced string.

Syntax of Python re.sub() method is given below:

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

The details of these arguments are given below.

  • pattern– This is a substring or regular expression pattern that we are going to search in the targeted string.
  • replacement– The replacement is a string or function that we are going to insert for each occurrence of a pattern in the targeted string.
  • string-This is the variable that points to the target string.
  • count-This is the maximum number of occurrences of a pattern that is to be replaced in the targeted string. By default, it is set to zero which means it relace all occurrences in the targeted string.
  • flags– This is the optional flag which is by default set to zero means no flags are applied. Some of these flags are re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode matching), and re.X (verbose), etc. You can find details of each flag here.

This method will return a string with replaced occurrences if the pattern does not found then it will return the original string unchanged.

Example 1:

Let’s say we have a string “Rain is a strong determinant for good agriculture in many countries of the world.” and we want to replace all occurrences of whitespace with an underscore then we can do this by using re.sub() method as you can see in the given example.

import re

target_str = "Rain is a strong determinant for good agriculture in many countries of the world."
updated_str = re.sub(r"\s", "_", target_str)
# String after replacing all whitespaces
print(updated_str)

When you execute this code it will display the given output.

Rain_is_a_strong_determinant_for_good_agriculture_in_many_countries_of_the_world.

Example 2:

You can also replace multiple regex patterns in a string. You can see this in the example below.

import re

target_str = "Rain_is_a_strong_determinant for good agriculture in many countries of the world."
# replace multiple patterns
res_str = re.sub(r"(\s)|(_)", "-", target_str)
print(res_str)

The above code will replace all the occurrences of underscore and whitespace from the targeted string with a dash. You can see the output which is given below.

Rain-is-a-strong-determinant-for-good-agriculture-in-many-countries-of-the-world.

Python re.subn() method

It is very much similar to re.sub() method, but it has a little bit different returned result. The method returns a tuple of two elements –

  • First element is the modified string after the replacement of all the occurrences of given pattern
  • Second element is the count of replacements it has made

Example :

import re

target_str = "Rain is a strong determinant for good agriculture in many countries of the world."
# Using re.subn() method
updated_str = re.subn(r"\s", "_", target_str)
# String after replacing all whitespaces
print(updated_str)

When you execute this code it will display the given output.

('Rain_is_a_strong_determinant_for_good_agriculture_in_many_countries_of_the_world.', 13)

To know more about re module and its functions you can visit its official documentation.

Conclusion

I hope now you are able to use sub() and subn() methods of Python re module to replace occurrence of a pattern in a string.

Now if you have a query or suggestions then write us in the comments below.

Leave a Comment

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