Python provides a powerful library called
re to work with regex. This article will discuss how to use Python’s
re module to perform replaceall functionality using regex, along with various examples and use cases.
The re Module in Python
Python has a built-in library called
re that provides support for regular expressions. The
re module offers several functions to work with regex, such as
Replaceall Functionality with Regex
re.sub() function is used to replace all occurrences of a pattern in a given string. The syntax for
re.sub(pattern, repl, string, count=0, flags=0)
pattern is the regex pattern,
repl is the replacement string,
string is the input text,
count specifies the maximum number of replacements, and
flags allows you to modify the regex behavior.
re.subn() function is similar to
re.sub(), but it returns a tuple containing the modified string and the number of substitutions made. The syntax for
re.subn(pattern, repl, string, count=0, flags=0)
Simple String Replacement
Let’s start with a basic example of replacing all occurrences of a specific word in a given string:
import re text = "I love Python. Python is a great language." new_text = re.sub("Python", "Java", text) print(new_text)
I love Java. Java is a great language.
Case Insensitive Replacement
In some cases, you might want to perform a case-insensitive replacement. To do this, you can use the
import re text = "I love Python. PYTHON is a great language." new_text = re.sub("python", "Java", text, flags=re.IGNORECASE) print(new_text)
Replacing Multiple Patterns
To replace multiple patterns in a string, you can use the
| operator in the regex pattern:
import re text = "I love cats and dogs." new_text = re.sub("cats|dogs", "animals", text) print(new_text)
I love animals and animals.
Limiting the Number of Replacements
You can limit the number of replacements by specifying the
import re text = "Apple, Orange, Banana, Orange, Grape, Orange" new_text = re.sub("Orange", "Mango", text, count=2) print(new_text)
Apple, Mango, Banana, Mango, Grape, Orange
Custom Replacement Function
You can use a custom function for more complex replacements:
import re def custom_repl(match_obj): if match_obj.group(0) == "cat": return "dog" elif match_obj.group(0) == "dog": return "cat" else: return "animal" text = "I have a cat, a dog, and a parrot." new_text = re.sub("cat|dog|parrot", custom_repl, text) print(new_text)
I have a dog, a cat, and an animal.
In this article, we covered the replaceall functionality in Python using regex with various examples.
We also discussed some common use cases in enterprise applications. With the power of the
re module and regex, you can efficiently manipulate and process text data in your Python projects.