Python string replace regular expression

Python regex replace: How to Search and Replace Strings

To replace a string that matches the regex in Python, you can use the “re.sub()” method. The “re.sub()” method accepts the maximum five arguments and returns replaced strings.

Syntax

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

Parameters

  1. pattern: The regular expression pattern to find inside the target string.
  2. repl: The replacement that we are going to insert for each occurrence of a pattern. The replacement can be a string or function.
  3. string: The variable pointing to the target string (In which we want to perform the replacement).
  4. count: Maximum number of pattern occurrences to be replaced. The count must always be a positive integer if specified. By default, the count is set to zero, which means the re.sub() method will replace all pattern occurrences in the target string.
  5. flags: Finally, the last argument is optional and refers to regex flags. By default, no flags are applied.

Return value

It returns the string obtained by replacing the pattern occurrences in the string with the replacement string.

Example 1: How to search and replace string in Python

The re.sub() function in the “re” module can replace substrings.

To use the sub() method, first, we have to import the “re” module, and then we can use its sub() method.

import re str = 'aaa@gmail.com' print(re.sub('[a-z]*@', 'ApD@', str)) 

In the above example, we try to replace small letter cases from a to z before @ character.

Читайте также:  Для чего нужна html верстка

From the output, we can see that we have successfully updated the email addresses.

If we use the string replace() method to replace the string, a new string will be replaced to match the old string entirely.

Example 2: Specifying the count

You can pass a count parameter in the regex sub() method. It suggests to the compiler that please don’t replace more than the count’s value.

import re str = 'aaa@gmail.com' print(re.sub('[a-z]*@', 'ApD@', str, 2)) 
ApD@gmail.com ApD@hotmail.com ccc@apple.com

The output shows that it replaced only two email addresses; the last address is as it is.

Replacing multiple patterns using regex

Use the regex to replace multiple patterns simultaneously using re.sub(pattern_1 | pattern_2, replacement, string, count= 0 , flags= 0 ) syntax.

import re str = "biden-Putin Volodymir Modi Abe Jacinda" print(re.sub("(\s) | (-)", ", ", str))

Replacing multiple substrings with the exact string

You can replace multiple substrings with the exact string by enclosing the string with [ ] to match any character.

str = 'aaa@gmail.com bbb@hotmail.com ccc@apple.com' print(re.sub('[a-z]*@', 'info@', str))
info@gmail.com info@hotmail.com info@apple.com

You can see that it replaces the same string with multiple substrings.

If | delimits patterns, it matches any pattern. And also, it is possible to use special characters of regular expression for each pattern, but it is OK even if the usual string is specified.

It can be used to replace multiple different strings with the same string.

import re str = 'aaa@gmail.com bbb@hotmail.com ccc@apple.com' print(re.sub('gmail|hotmail|apple', 'appdividend', str)) 
aaa@appdividend.com bbb@appdividend.com ccc@appdividend.com

You can see that all substring is replaced with appdividend.

Let’s see a scenario in which only one pattern matches.

import re str = 'aaa@gmail.com bbb@amazon.com ccc@walmart.com' print(re.sub('gmail|hotmail|apple', 'appdividend', str)) 
aaa@appdividend.com bbb@amazon.com ccc@walmart.com

You can see that bbb@amazon.com and ccc@walmart.com are unchanged.

Only aaa@gmail.com is replaced with aaa@appdividend.com.

Replacing a string using the matched part

To replace a substring using the matched part, use the string that matches the part enclosed in () in the new string.

import re str = 'aaa@gmail.com bbb@amazon.com ccc@walmart.com' print(re.sub('([a-z]*)@', '\1 19-@', str)) 
19-@gmail.com 19-@amazon.com 19-@walmart.com

The \1 corresponds to the part that matches (). If there are multiple ( ), use them like \2, \3 …

Читайте также:  Изменение стиля при наведении мыши css

It is important to escape \ like \\1 if it is a regular string surrounded by the ” or ” “, but if it is the raw string with r at the beginning like r”, you can write \1.

Replacing a string by position using a slice

There is no method for specifying and replacing the position; dividing the slice and concatenating them with the arbitrary string can create a new string in which a specified position is replaced.

import re str = 'albertodelrio' print(str[:4] + 'HANAYA' + str[7:]) 

In the above code example, you can see that we have added a substring between string indexes 4 to 7.

Источник

Python regex: How to search and replace strings

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

Table of contents

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.

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 replace 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.
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 to you want it to be replaced. The default value is 0. It is optional.
flags: The regex flags are optional.

Читайте также:  Форматирование даты kotlin android

Input:

Output:

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:

Replacing multiple patterns with multiple replacements using regex

Now, if ever, you want to replace multiple patterns but with different replacements then also regex can be used. It can be done with a minor modification which you can see 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 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:

Closing thoughts

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

Источник

Оцените статью