The first argument to re.sub is treated as a regular expression, so the square brackets get a special meaning and don't match literally.

You don't need a regular expression for this replacement at all though (and you also don't need the loop counter i):

for name_line in name_lines:
    text = text.replace(name_line, '')
Answer from Thomas on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations โ€” Python 3.14.3 ...
This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings (str) as well as 8-bit strings (bytes). However, Unicode strings and 8-bit strings cannot be mixed: that is, you cannot match a Unicode string with a bytes pattern or vice-versa; similarly, when asking for a substitution, the replacement string must be of the same type as both the pattern and the search string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ re-sub-python-regex
re.sub() - Python RegEx - GeeksforGeeks
July 23, 2025 - If regular expression has capture groups(defined by parentheses()), we can use the groups in the replacement string using \1,\3,etc., or by using replacement function. ... import re a = "John 25, Jane 30, Jack 22" # Match name and age pattern = r"(\w+) (\d+)" # Use age first, then name repl = r"\2 years old, \1" # Swap names and ages result = re.sub(pattern, repl, a) print(result)
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python regex โ€บ python regex sub()
Python Regex sub() Function
December 10, 2021 - import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'<b>\1<\\b>' html = re.sub(pattern, replacement, s) print(html)Code language: Python (python) ... In this example, the pattern r'\*(.*?)\*' find the text that begins and ends with the asterisk (*). It has a capturing group that captures the text between asterisks (*). The replacement is a regular expression with a backreference. The backreference \1 refers to the first group in the pattern, which is the text between the asterisks (*). Suppose you have a list of strings where each element contain both alphabet and number:
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Replace Strings in Python: replace(), translate(), and Regex | note.nkmk.me
May 4, 2025 - Although Python does not have a built-in method to replace substrings at specific positions, you can achieve this by splitting the string with slicing and concatenating the parts with the replacement string.
๐ŸŒ
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.
๐ŸŒ
LZone
lzone.de โ€บ examples โ€บ Python re.sub
Python re.sub Examples
result = re.sub('abc', '', input) # Delete pattern abc result = re.sub('abc', 'def', input) # Replace pattern abc -> def result = re.sub(r'\s+', ' ', input) # Eliminate duplicate whitespaces using wildcards result = re.sub('abc(def)ghi', r'\1', input) # Replace a string with a part of itself
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ regular expressions โ€บ re.sub()
Python | Regular Expressions | re.sub() | Codecademy
October 14, 2024 - The re.sub() function replaces matching substrings with a new string for all occurrences, or a specified number. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Find elsewhere
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-re-sub
Python re.sub() - Replace using Regular Expression
Original string Account Number - 12345, Amount - 586.32 After replacement Account Number - NN, Amount - NN.NN ยท We can limit the maximum number of replacements by re.sub() function by specifying count optional argument.
๐ŸŒ
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 - import re target_str = "Jessa knows testing and machine learning" res_str = re.sub(r"\s", "_", target_str) # String after replacement print(res_str) # Output 'Jessa_knows_testing_and_machine_learning'Code language: Python (python) Run
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ python-regex-replace
Python regex: How to search and replace strings | Flexiple - Flexiple
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.
๐ŸŒ
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...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_regex.asp
Python RegEx
Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans ... Python Operators Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Identity Operators Membership Operators Bitwise Operators Operator Precedence Code Challenge Python Lists ยท Python Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join Lists List Methods List Exercises Code Challenge Python Tuples
๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ python-re-match
Python re.match() and re.sub() Explained | Built In
Since the string starts with "Price123", it successfully matches and prints it. More on PythonPython Set Difference: A Complete Guide ยท The re.sub() function is used for substituting occurrences of a pattern in a string.
๐ŸŒ
Python
docs.python.org โ€บ 3.1 โ€บ library โ€บ re.html
7.2. re โ€” Regular expression operations โ€” Python v3.1.5 documentation
For example, if the pattern is (?P<id>[a-zA-Z_]\w*), the group can be referenced by its name in arguments to methods of match objects, such as m.group('id') or m.end('id'), and also by name in the regular expression itself (using (?P=id)) and replacement text given to .sub() (using \g<id>). ... Matches whatever text was matched by the earlier group named name. ... A comment; the contents of the parentheses are simply ignored. ... Matches if ... matches next, but doesnโ€™t consume any of the string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-substituting-patterns-in-text-using-regex
Python - Substituting patterns in text using regex - GeeksforGeeks
July 12, 2025 - It is used for substituting a specific pattern in the string. There are in total 5 arguments of this function. Syntax: re.sub(pattern, repl, string, count=0, flags=0) Parameters: pattern - the pattern which is to be searched and substituted ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ regex.html
Regular Expression HOWTO โ€” Python 3.14.3 documentation
Backreferences like this arenโ€™t often useful for just searching through a string โ€” there are few text formats which repeat data in this way โ€” but youโ€™ll soon find out that theyโ€™re very useful when performing string substitutions.