As long as you can make do with str.replace(), you should use it. It avoids all the pitfalls of regular expressions (like escaping), and is generally faster.

Answer from Sven Marnach on Stack Overflow
🌐
DEV Community
dev.to › doridoro › python-differences-between-replace-and-resub-methods-1cfj
Python: differences between `.replace()` and `.re.sub()` methods - DEV Community
July 30, 2024 - Example: re.sub(r'\bworld\b', 'Python', 'hello world') results in 'hello Python'. ... Only supports simple string matching. Cannot use regular expressions or complex patterns. Replaces all occurrences of the substring if count is not specified.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Replace Strings in Python: replace(), translate(), and Regex | note.nkmk.me
May 4, 2025 - If you need to replace substrings based on regex patterns, use the sub() or subn() functions from the re module. re.sub() — Regular expression operations — Python 3.13.3 documentation
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
In string-type repl arguments, in addition to the character escapes and backreferences described above, \g<name> will use the substring matched by the group named name, as defined by the (?P<name>...) syntax. \g<number> uses the corresponding group number; \g<2> is therefore equivalent to \2, but isn’t ambiguous in a replacement such as \g<2>0. \20 would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character '0'. The backreference \g<0> substitutes in the entire substring matched by the RE.
🌐
Python.org
discuss.python.org › python help
Replacement argument in re.sub() - Python Help - Discussions on Python.org
November 10, 2022 - Hello, I have the following strings that I would like to match and replace. I am able to match the strings but struggling to carry out a replacement - the output either doesn’t change or it completely erases sections of the input string. i have included an example of one of many options I have tried. scenario 1: input string: (BXXADDB)(BXXXCAC1)(CXX2A)CANEVER desired output string: (BXXADDB)(BXXXCAC1)(CXX2A)*CANEVER pattern = r"^\([A-Za-z0-9]+\)\([A-Za-z0-9]+\)\([^)]*\)[a-zA-Z]+$" replaceme...
🌐
PYnative
pynative.com › home › python › regex › python regex replace pattern in a string using re.sub()
Python Regex Replace Pattern in a string using re.sub()
July 19, 2021 - Python's regex offers sub() the subn() methods to search and replace occurrences of a regex pattern in the string with a substitute string.
Find elsewhere
🌐
YouTube
youtube.com › watch
Regular Expressions 2018 | #8 re.sub() replace characters | Python | Tharun Shiv | Being A Pro - YouTube
Regular expressions are a sequence of special characters used to match, search , replace or do such operations on text data. They were first introduced into ...
Published   April 30, 2018
🌐
Sololearn
sololearn.com › en › Discuss › 1037746 › what-is-the-difference-between-replace-and-re-sub
What is the difference between .replace and re.sub? | Sololearn: Learn to code for FREE!
Hi David." pattern = r"David" newstr = re.sub(pattern, "Amy", str) print(newstr) ================================ newstr2 = str.replace("David", "Amy") print (newstr2) rereplacere.sub · 29th Jan 2018, 8:21 AM · Juca Navaz Reque · 1 AnswerAnswer · + 7 · https://stackoverflow.com/questions/5668947/use-pythons-string-replace-vs-re-sub#5668962 ·
🌐
LZone
lzone.de › examples › Python re.sub
Python re.sub Examples
def my_replace(m): if <some condition>: return <replacement variant 1> return <replacement variant 2> result = re.sub("\w+", my_replace, input)
🌐
Educative
educative.io › answers › what-is-the-resub-function-in-python
What is the re.sub() function in Python?
Python uses the re.sub() function to replace text that matches a regular expression pattern with a new value in a string.
🌐
Real Python
realpython.com › lessons › replace-string-python-resub-intro
Get to Know re.sub() (Video) – Real Python
00:15 re.sub() takes three positional ... So, what does re.sub() do? re.sub() returns the string obtained by replacing occurrences of pattern in string by the replacement that you pass in as repl....
Published   August 22, 2023
🌐
Built In
builtin.com › articles › python-re-match
Python re.match() and re.sub() Explained | Built In
More on PythonPython Set Difference: A Complete Guide · The re.sub() function is used for substituting occurrences of a pattern in a string. It takes three main arguments: The pattern you want to replace, a regular expression.
🌐
GeeksforGeeks
geeksforgeeks.org › python › re-sub-python-regex
re.sub() - Python RegEx - GeeksforGeeks
July 23, 2025 - This code uses re.sub() to find all matches of a name followed by an age (e.g., "John 25") in the string and swaps the order, placing the age first followed by the name. The replacement string \2 years old, \1 uses the second capture group (age) and the first capture group (name) to format the output.
🌐
Flexiple
flexiple.com › python › python-regex-replace
Python regex: How to search and replace strings | Flexiple - Flexiple
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.
🌐
SonarSource
rules.sonarsource.com › python › rspec-5361
`str.replace` should be preferred to `re.sub`
Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your PYTHON code