As you are working with the re module, you might find yourself in a situation where you want to replace multiple patterns in a string. This task may seem complicated in the first place but with the help of the re.sub() method in Python regex, it is all possible.

Advertisements

In this tutorial, we will explore the versatility of the re.sub() method and how you can leverage it to perform multiple pattern replacements.

Here is what you will learn:

  • Basic Syntax for Multiple Pattern Replacement
  • Using a Pattern Group for Multiple Pattern Replacements
  • Using a Dictionary for Multiple Pattern Replacements
  • Handling overlapping patterns
  • Conclusion

Basic Syntax for Multiple Pattern Replacement

First things first, let us get to understand the syntax that is used to perform multiple pattern replacements. Here is it below:


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

Below is the breakdown of the re.sub() method arguments:

ArgumentDescription
pattern_groupA pattern group is created by combining multiple patterns using the | operator and enclosed in parentheses. Each pattern within the group represents a separate pattern to search for
replacementThe string to replace the matched patterns with
stringThe input string in which the patterns are searched for
count(optional)Specifies the maximum number of replacements to be made. By default, all occurrences are replaced
flags(optional)Modify the behavior of the regex pattern matching, such as case insensitivity or multiline matching
re.sub() method arguments

Using a Pattern Group for Multiple Pattern Replacements

Now having looked at the syntax for performing multiple pattern replacements, let us now look at our first example. We will see how you can use a pattern group to do multiple replacements. Here is an example:


import re

# input text
text = "spark by example"

# pattern group combining multiple patterns to search for
pattern_group = r"(spark|by|example)"

# peplace the matched patterns with "hello world"
new_text = re.sub(pattern_group, "hello world", text)

# print the modified text
print(new_text)

In this code, we have an input text "spark by example" that we want to modify. We define a pattern group using the pattern_group variable, which contains three separate patterns "spark", "by", and "example". These patterns are combined using the | operator within parentheses to create the pattern group. The re.sub() method is then used to replace all occurrences of any of these patterns with the string "hello world".

Using a Dictionary for Multiple Pattern Replacements

Another method of doing multiple pattern replacement is by using a dictionary. With this method, we create a dictionary with pattern replacement pairs. To understand this better let us look at the following example:


import re

# the text to search and replace patterns in
text = "Hello $name! Welcome to $website where you will learn programming."

# define the pattern-replacement dictionary
replacements = {
    r"\$name": "Programmers",
    r"\$website": "http://www.sparkbyexamples.com"
}

# perform the multiple replacements using re.sub()
pattern = re.compile("|".join(replacements.keys()))
new_text = pattern.sub(lambda match: replacements[re.escape(match.group())], text)

# print the modified text
print(new_text)

The code defines a dictionary, replacementswhere each key represents a pattern to search for and the corresponding value is the replacement string. The <strong>re.compile()</strong> method is used to compile a regular expression pattern from the keys of the replacements dictionary. The re.sub() function is then applied to the text string, replacing all occurrences of the patterns with their respective replacement strings.

Output:


#Output
Hello Programmers! Welcome to http://www.sparkbyexamples.com where you will learn programming.

Handling overlapping patterns

One of the scenarios you will face as you are performing multiple pattern replacements is that of overlapping patterns. Before we dive deep, let us understand what this is. Overlapping patterns is just a scenario where multiple patterns in a text can match the same portion of the input string. One of the things to note when using the re.sub() method is that it processes the patterns from left to right and once a pattern matches, it moves to the next portion of the text thus leading to unexpected results when there are overlapping patterns. Here is an example that will help you understand this scenario:


import re

text = "I love Python programming and PyTorch library."

patterns = {
    r"Python": "Py",
    r"PyTorch": "PT"
}

# sorting patterns by length to handle overlapping matches
sorted_patterns = sorted(patterns.keys(), key=len, reverse=True)

# iterate through the sorted patterns
for pattern in sorted_patterns:
    # perform pattern substitution using re.sub()
    text = re.sub(pattern, patterns[pattern], text)

# print the modified text
print(text)

In this code snippet, we have two overlapping patterns "Python" and "PyTorch". By sorting the patterns in descending order of length, we ensure that the longer pattern "Python" is matched and replaced before the shorter pattern "PyTorch". This prevents incorrect replacements and ensures that abbreviations are handled correctly.

Output:


#Output
I love Py programming and PT library.

Conclusion

In conclusion, Python provides powerful tools for replacing multiple patterns using regular expressions (regex). The re.sub() function is particularly useful for performing pattern-based replacements in strings. By defining a pattern-replacement dictionary, you can specify multiple patterns and their corresponding replacements. The patterns can be simple strings or more complex regular expressions.