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
4 days ago - However, Unicode strings and 8-bit ... 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 ...
🌐
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.
Discussions

Regular Expressions re.sub
There is, by making the pattern lazy rather than greedy: new_text = re.sub(r"(?:\<.*?\>)*", "", old_text) However, you should not use regex to parse HTML, as Tony the Pony will tell you . More on reddit.com
🌐 r/learnpython
10
9
August 29, 2023
Replacement argument in re.sub()
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 ... More on discuss.python.org
🌐 discuss.python.org
0
November 10, 2022
Regex re.sub("^\s*(.-)\s*$", "%1", s).replace("\\n", "\n") explained
re.sub replaces matches with something else. ^\s*(.-)\s*$ ^ means start of string $ means end of string \s* means match 0 or more empty space characters () means this is a group we want, this will be group 1 for replace .- means match any 1 character and a dash after it. \s* again means 0 or more empty spaces basically it takes a " a- " and replaces it with "a-". >>> re.findall("^\s*(.-)\s*$"," s- ") ['s-'] >>> re.findall("^\s*(.-)\s*$"," xx- ") [] More on reddit.com
🌐 r/learnpython
6
2
June 4, 2020
Module re: add possibility to return matches during substitution
Problem I would like to have access to the matches from a substitution. This would allow to reconstruct the original string (or parts of it) from the replaced string. An example use case is to (efficiently) parse a long … More on discuss.python.org
🌐 discuss.python.org
0
April 12, 2024
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
Reddit
reddit.com › r/learnpython › regular expressions re.sub
r/learnpython on Reddit: Regular Expressions re.sub
August 29, 2023 -

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!

🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex sub()
Python Regex sub() Function
December 10, 2021 - re.sub(pattern, repl, string, count=0, flags=0)Code language: Python (python)
Find elsewhere
🌐
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.
🌐
ZetCode
zetcode.com › python › regex-sub-function
Python re.sub - Mastering Regular Expression Substitution
April 20, 2025 - The basic syntax of re.sub has three required parameters: ... Optional count limits replacements. flags modify matching behavior. The function returns the modified string. Let's start with a simple example replacing colors in a sentence. ... #!/usr/bin/python import re text = "The sky is blue and the grass is green" result = re.sub(r'blue', 'gray', text) print(result)
🌐
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.
🌐
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!
🌐
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.
🌐
Codidact
software.codidact.com › posts › 294905
Python re.sub() how to include and slice the match in the substitution? - Software Development
November 1, 2025 - I recently started learning regular expressions and I'm trying to use Python's re module, specifically the re.sub() function, to convert a subset of Markdown syntax to HTML whenever it appears 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...
🌐
YouTube
youtube.com › pymoondra
Python Regular Expressions -part #12 - Re.sub - YouTube
In this video series, we will be tackling Python Regular Expressions from beginners to advanced.This video goes over:1) re.sub -- vanilla subs, as well as us...
Published   August 25, 2017
Views   18K
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-substituting-patterns-in-text-using-regex
Python - Substituting patterns in text using regex - GeeksforGeeks
July 12, 2025 - To perform this type of substitution the re.sub() method has an argument count. By providing a numeric value to this argument, the number of characters on which substitution will occur can be controlled. Below is the implementation. ... # Python implementation to perform substitution # up to a certain number of characters # importing regex module import re # Function to perform # operations on the strings def substitutor(): # a string variable sentence = "Follow your Passion."
🌐
Python.org
discuss.python.org › python help
Module re: add possibility to return matches during substitution - Python Help - Discussions on Python.org
April 12, 2024 - Problem I would like to have access to the matches from a substitution. This would allow to reconstruct the original string (or parts of it) from the replaced string. An example use case is to (efficiently) parse a long string and then parse it again, e.g., s1 = re.sub(' and ', '', 'a and b and c and d') # 3 matches, s1 = 'abac' re.finditer('ab|cd', s1) # two matches span 'ab', 'ac' Now, I would like to reconstruct the original string partially to obtain ab and ac.
🌐
Xah Lee
xahlee.info › python › python_re.sub.html
Python: Regex re.sub
# example of using re.sub( ) import re # add alt to image tag t1 = '<img src="cat.jpg">' t2 = re.sub(r'src="([a-z]+)\.jpg">', r'src="\1.jpg" alt="\1">', t1) print(t1) # <img src="cat.jpg"> print(t2) # <img src="cat.jpg" alt="cat">