You are currently viewing Python regex replace all

In Python, you can use the regex re module to perform regular expression operations like replace all occurrences of strings. The re.sub() function is commonly used to replace all occurrences of a pattern with a specified replacement.

Advertisements

Have you ever think how applications like document viewers and editors enable you to search for a word and replace it with a new one? What happens in the background seems to be complex but have you ever asked yourself how this is made possible? What if I tell you that with Python you can build an application having these very same features where you can search for a word and replace it? Python is library-rich and it has a library for text processing called re library, which you can use for tasks like text searching, matching, replacing, etc.

In this Python article, we will explore how to use the re.sub() method with clear-cut examples to replace all instances of a pattern in a given text by using regex expressions. We will also cover the various flags available and how to use them to make the replacement more powerful.

Table of contents-

  • The re.sub() method
  • The re.sub() method syntax
  • Replacing matched patterns with re.sub() method
  • Using flags with re.sub() method
    • re.IGNORECASE flag
    • re.DOTALL flag
    • re.MULTILINE flag
  • Conclusion

1. Python regex replace all using re.sub() method

One of the most used methods in the Python re library is the re.sub() which is used to replace a new string for all instances of a pattern in a string (Python regex replaces all). The pattern, the string, and the input string are the three arguments required by this method.

1.1 The re.sub() method syntax

Here let us look at the re.sub() method syntax and what arguments it takes to fully function. Below is the syntax:


# Syntax
re.sub(pattern, replace, string, count=0, flags=0)

The below table summarizes the arguments:

ArgumentDescription
patternThis is the string pattern we want to match and replace
replaceThe replacement string that we want to replace the matched pattern with
stringThe input string that we want to search and replace
count(optional)The maximum number of pattern occurrences to be replaced. If count is not provided or is 0, all occurrences will be replaced
flags(optional)Flags simply modify the behavior of the regex
re.sub() method arguments

2. Replacing matched patterns with re.sub() method

We will look at a basic example demonstrating how to replace a string with a given regex pattern. Here is an example:


# Import re library
import re

# Replace matched pattern with another text
text = "SparkByExamples. One Stop For All Code Examples."
pattern = r"Code Examples"
new_text = re.sub(pattern, "Data Enthusiasts", text)

print(new_text)

The above Python code uses the re.sub() method to search for the pattern Code Examples in the text SparkByExamples. One Stop For All Code Examples, and replaces it with Data Enthusiasts. The resulting string is then stored in the new_text variable.

Output:


# Output:
SparkByExamples. One Stop For All Data Enthusiasts.

3. Using flags with re.sub() method

To make the re.sub() method more powerful, you can use it with flags, as mentioned earlier, flags just help you modify the behavior of your regex. In the below sections, we will look at some of the flags used with the re.sub() method.

3.1 re.IGNORECASE flag

The first flag we will look at is the re.IGNORECASE, this helps you to ignore any case sensitivity in the regex. Here is an example:


# Import re library
import re

# Using e.IGNORECASE flag
text = "SPARKBYEXAMPLES"
pattern = r"spark"
new_text = re.sub(pattern, "Data Enthusiasts", text, flags=re.IGNORECASE)

print(new_text)

In the code snippet, the re.sub() method replaces the text spark (case-insensitive) with Data Enthusiasts in the given string SPARKBYEXAMPLES. The flags=re.IGNORECASE parameter is used to ignore the case of the pattern while searching for a match in the text.

Output:


# Output:
Data EnthusiastsBYEXAMPLES

3.2 re.DOTALL flag

If you want to match patterns across multiple lines including newlines then the re.DOTALL flag is the solution. Here is a code example:


# Import re library
import re

# Using re.DOTALL  flag
text = "SparkByExamples.\nOne Stop For All Code Examples."
pattern = r"Spark.*Examples"
new_text = re.sub(pattern, "Data Enthusiasts", text, flags=re.DOTALL)

print(new_text) 

In this example, we are using the re.sub() method to replace the text SparkByExamples.\nOne Stop For All Code Examples. that matches the pattern Spark.*Examples with Data Enthusiasts. The re.DOTALL flag is used to include newline characters in the match.

Output:


# Output:
Data Enthusiasts.

3.3 re.MULTILINE flag

In scenarios where you want to match patterns across multiple lines, the re.MULTILINE is the go-to flag. Here is how you can use it in your code:


# Import re library
import re

# Using re.MULTILINE flag
text = "SparkByExamples\nOne Stop For All Code Examples.\nData Enthusiasts"
pattern = r"^One.*Examples"
new_text = re.sub(pattern, "Data Enthusiasts", text, flags=re.MULTILINE)

print(new_text)

In this example, we used the re.MULTILINE flag to replace all occurrences of a pattern that match across multiple lines. The ^ character matches the start of the line. Therefore, the pattern ^One.*Examples matches all lines that start with One and end with Examples.

Output:


# Output:
SparkByExamples
Data Enthusiasts.
Data Enthusiasts

4. Conclusion

In conclusion, this tutorial on Python regex replace all has equipped you with essential skills for replacing all matches of a regular expression in a given string. You have learned about the powerful re.sub() method, which is a flexible way of finding and replacing substrings in a string using regular expressions.

The tutorial has also covered the syntax of the method and the usage of flags to make it more effective. I believe that the knowledge gained from this tutorial will come in handy when you encounter similar scenarios in your future Python projects. With the ability to use regular expressions to manipulate strings in Python, you have gained a valuable tool that will help you solve complex string manipulation problems more efficiently.