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 OverflowPython 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)
Videos
20:24
Python Regex Sub - How to Replace a Pattern in a String? - YouTube
03:48
Python re.sub() Tutorial: Text Substitution with Regular Expressions ...
17:24
Python Regular Expressions -part #12 - Re.sub - YouTube
01:56
Python Regex: How To Replace All Substrings In a String - YouTube
04:12
Python Regex: How Find a Substring in a String - YouTube
05:50
Python standard library: Replacing text with regexps using re.sub ...
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:
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!
Top answer 1 of 2
5
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, '')
2 of 2
4
Try to use re.sub to replace the match:
import re
text = """\
Questions and Answers
Operator [1]
Shannon Siemsen Cross, Cross Research LLC - Co-Founder, Principal & Analyst [2]
I hope everyone is well. Tim, you talked about seeing some improvement in the second half of April. So I was wondering if you could just talk maybe a bit more on the segment and geographic basis what you're seeing in the various regions that you're selling in and what you're hearing from your customers. And then I have a follow-up.
Timothy D. Cook, Apple Inc. - CEO & Director [3]"""
text = re.sub(r".*\d]", "", text)
print(text)
Prints:
Questions and Answers
I hope everyone is well. Tim, you talked about seeing some improvement in the second half of April. So I was wondering if you could just talk maybe a bit more on the segment and geographic basis what you're seeing in the various regions that you're selling in and what you're hearing from your customers. And then I have a follow-up.
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
Top answer 1 of 3
6
You mean this?
>>> import re
>>> m = re.sub(r'10', r'20', "hello number 10, Agosto 19")
>>> m
'hello number 20, Agosto 19'
OR
Using lookbehind,
>>> number = "20"
>>> number
'20'
>>> m = re.sub(r'(?<=number )\d+', number, "hello number 10, Agosto 19")
>>> m
'hello number 20, Agosto 19'
2 of 3
2
Do not use regex for such simple cases. Normal string manipulation is enough (and fast):
s = "hello number 10, Agosto 19"
s = s.replace('10', '20')
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
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
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.