AppDividend
Latest Code Tutorials

Python re sub: How to Replace Substring in Python

0

A regular expression in Python is a special series of characters that helps you match or find other strings or substrings, using the particular syntax held in the pattern.

Python re sub

Python re.sub() is an inbuilt regex method that specifies a regular expression pattern in the first argument, a new string in the second argument, and the source string that needs to be processed in the third argument.

To replace one string with another in multiple places in Python, use the re.sub() method.

To use the re.sub() method, you need to import the re module.

# app.py

import re

Now, define a string in which you need to substitute a substring.

str = 'abel@xxx.com selena@yyy.com benny@zzz.com'

In this example, we need to replace all the substrings before @ character with the TONI substringTo do that, write the following code.

# app.py

import re

str = 'abel@xxx.com selena@yyy.com benny@zzz.com'

print(re.sub('[a-z]*@', 'TONI@', str))

Output

TONI@xxx.com TONI@yyy.com TONI@zzz.com

Replace multiple substrings with the same string

To replace multiple substrings with the same string in Python, use the re.sub() method.

Even if you are not accustomed to regular expressions, embed a string with [ ] to match any single character in it. It can be used to replace multiple several characters with the same string.

See the following code.

# app.py

import re

str = 'abel@xxx.com selena@yyy.com benny@zzz.com'

print(re.sub('[ben]', '1', str))

Output

a11l@xxx.com s1l11a@yyy.com 1111y@zzz.com

In this example, we are replacing the character of b, e, and n with 1. No matter the occurrence of those characters, if it finds, then it will replace it with 1.

Replace using the matched part

If part of the pattern is enclosed in () (rounded brackets), you can use the string that matches the part enclosed in () in the new string. See the following code.

# app.py

import re

str = 'abel@xxx.com selena@yyy.com benny@zzz.com'

print(re.sub('([a-z]*)@', '\\1-123@', str))

Output

abel-123@xxx.com selena-123@yyy.com benny-123@zzz.com

\1 corresponds to the part that matches (). If there are multiple (), use them like \2, \3. It is necessary to escape \ like \\1 if it is a normal string surrounded by ” or ” “, but if it is a raw string with r at the beginning like r”, you can write \1.

That is it for Python re sub() example.

See also

Python re search()

Python re replace()

Python regex

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.