Thanks to visit codestin.com
Credit goes to flexiple.com

Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Python regex: How to search and replace strings

Python regex: How to search and replace strings

Author image

Rajat Gupta

Software Developer

Published on Tue Mar 22 2022

In this tutorial, you will learn about how to use regex (or regular expression) to do search and replace operations on strings in Python. Regex can be used to perform various tasks in Python. It is used to do search and replace operations, replace patterns in text, and check if a string contains a specific pattern. Today we will learn about performing search and replace operations using regex.

Python regex replace

Python has a built-in module called re, which helps in searching and replacing. In order to use re, we need to import the module at the beginning of our code.

import re

Now we'll learn about the search and replace operation using regex.

Regex to search and replace

The regex method searches a string and then replaces it with some other value. Python re.sub() function in the re module is used to do so.

Syntax:

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

First of all, let us understand what all these parameters mean:
pattern: The regular expression you want to search and find inside the given string in Python.
replacement: The string to replace the matched pattern.
string: The variable that contains the given string on which you want to perform the operation.
count: If the pattern occurs multiple times in the string, the number of times you want it to be replaced. The default value is 0. It is optional.
flags: The regex flags are optional.

Input:

import re  
str = "[email protected]"
print(re.sub("[a-z]*@", "abc@", str))

Output:

[email protected]

Replacing multiple patterns using regex

We can use regex to replace multiple patterns at one time using regex. This can be easily done using the following syntax.

Syntax:

re.sub(pattern_1 | pattern_2, replacement, string, count=0, flags=0)

Input:

import re  

str = "Joe-Kim Ema Max Aby Liza"
print(re.sub("(\s)|(-)", ", ", str))

Output:

Joe, Kim, Ema, Max, Aby, Liza

Replacing multiple patterns with multiple replacements using regex

If you want to replace multiple patterns with different replacements, regex can be used. This can be done with a minor modification, as shown in the following example.

Input:

import re  

def convert_case(match_obj):
  if match_obj.group(1) is not None:
    return match_obj.group(1).lower()
  if match_obj.group(2) is not None:
    return match_obj.group(2).upper()

str = "jOE kIM mAx ABY lIzA"
print(re.sub(r"([A-Z]+)|([a-z]+)", convert_case, str))

In this example, the string contains uppercase and lowercase letters that we need to replace. We need to replace the uppercase with the lowercase and vice versa. In order to do that, we will make two groups and then add a function for the replacement.

Output:

Joe Kim MaX aby LiZa

Closing thoughts

To replace a string in Python, the regex sub() method is used. It is a built-in Python method in the re module that returns a replaced string. Don't forget to import the re module. This method searches the pattern in the string and then replaces it with a new given expression. One can learn about more Python concepts here.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.