Python re sub: How to Replace Substring in Python
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 = 'firstname.lastname@example.org email@example.com firstname.lastname@example.org'
In this example, we need to replace all the substrings before @ character with the TONI substring. To do that, write the following code.
# app.py import re str = 'email@example.com firstname.lastname@example.org email@example.com' print(re.sub('[a-z]*@', 'TONI@', str))
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 = 'firstname.lastname@example.org email@example.com firstname.lastname@example.org' print(re.sub('[ben]', '1', str))
email@example.com firstname.lastname@example.org email@example.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 = 'firstname.lastname@example.org email@example.com firstname.lastname@example.org' print(re.sub('([a-z]*)@', '\\1-123@', str))
email@example.com firstname.lastname@example.org email@example.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.