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
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. Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal.
🌐
GeeksforGeeks
geeksforgeeks.org › python › re-sub-python-regex
re.sub() - Python RegEx - GeeksforGeeks
July 23, 2025 - re.sub() method in Python parts of a string that match a given regular expression pattern with a new substring.
🌐
Codidact
software.codidact.com › posts › 294905
Python re.sub() how to include and slice the match in the substitution? - Software Development
The call to re.sub is not magic; it works the same way as any other time that a function is called. The string that you pass for the replacement has special meaning, but that meaning comes from the regex implementation, not from Python.
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
Python Functions Python Arguments ... Python Recursion Python Generators Code Challenge Python Range ... Matplotlib Intro Matplotlib Get Started Matplotlib Pyplot Matplotlib Plotting Matplotlib Markers Matplotlib Line Matplotlib Labels Matplotlib Grid Matplotlib Subplot Matplotlib ...
🌐
Reddit
reddit.com › r/learnpython › regular expressions re.sub
r/learnpython on Reddit: Regular Expressions re.sub
February 28, 2024 -

Hi. I am currently building my very first python project. I need to get rid of all the Characts inside and including the <>. In the following example, the text "like the side of a house" should be left over.

<span class="idiom_proverb">like the side of a <strong class="tilde">house

</strong> </span>

I want to use re.sub with regular expressions on this one and my current code looks like this:

new_text = re.sub(r"(?:\<.*\>)*", "", old_text)

The problem is, when using re.sub this way, I am essentially deleting everything between the first < and the very last >, loosing all the information in between. Is there a way to make sure that my regular expressions refer to the very next > once a < is found?

This way it would only replace <spand class="idiom\_proverb">, <strong class="tilde">, </strong> and </span>?

I would really appreciate any help. Thank you!

Find elsewhere
🌐
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!
🌐
YouTube
youtube.com › watch
Python re.sub() Tutorial: Text Substitution with Regular Expressions - YouTube
🔥 Master text manipulation in Python with the `re.sub()` function! This comprehensive tutorial is designed for beginners and covers everything you need to k...
Published: September 6, 2025
🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex sub()
Python Regex sub() Function
December 10, 2021 - In this tutorial, you'll learn about the Python regex sub() function that returns a string after replacing the matched pattern in a string with a replacement.
🌐
Built In
builtin.com › articles › python-re-match
Python re.match() and re.sub() Explained | Built In
Python re.sub() is a function that’s used for substituting occurrences of a pattern in a string.
🌐
Medium
medium.com › apollo-data-solutions-blog › python-regex-search-and-replace-with-re-sub-remove-street-number-from-address-4a325556c25a
Python regex search and replace with re.sub — remove street number from address | by Daniel Chvatik | Apollo Data Solutions Blog | Medium
August 19, 2016 - only. For example ‘284–12 West Street’ should become ‘West Street’. Luckily, Python has excellent regular expression support via the re library. Here’s what we need to do to remove the street numbers from the address: import readdress = re.sub(r'^[\d-]+ ', '', address, 1)
🌐
Reddit
reddit.com › r/regex › python re.sub() regex to substitute a character with a string except in quotes
r/regex on Reddit: Python re.sub() regex to substitute a character with a string except in quotes
August 14, 2021 -

I'm trying to replace the @ character in a line with a string. However, this replacement must NOT occur within quotes, '' AND "".

For example, '@ foo@ "abc@google.com"' replaced with "bar" should become 'bar foobar "abc@google.com"'.

My attempts have got me to : re.sub(r"@(?=([^\"]*\"[^\"]*\")*[^\"]*$)"... but this only works for double quotes (""), not single quotes ('').

🌐
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.
🌐
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...
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
The re.sub(pat, replacement, str) function searches for all the instances of pattern in the given string, and replaces them.