Showing results for python re sub
Search instead for python resub

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 28, 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
regex - How to use re.sub in Python? - Stack Overflow
Text = " text code " I want to remove code statement in python output = "<... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda 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 Scatter Matplotlib Bars Matplotlib Histograms Matplotlib Pie Charts
🌐
Reddit
reddit.com › r/learnpython › regular expressions re.sub
r/learnpython on Reddit: Regular Expressions re.sub
August 28, 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 - Use the Python regex sub() function to replace the occurrences of matches of a pattern with a replacement.
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 - The idea is to use the very normal form of the re.sub() method with only the first 3 arguments. Below is the implementation. ... # Python implementation of substituting a # specific text pattern in a string using regex # importing regex module import re # Function to perform # operations on the strings def substitutor(): # a string variable sentence1 = "It is raining outside."
🌐
Xah Lee
xahlee.info › python › python_re.sub.html
Python: Regex re.sub
# example of using re.sub(pattern, rep, str ) where rep is a function import re def ff(xx): if xx.group(0) == "ea": return "æ" elif xx.group(0) == "oo": return "u" else: return xx.group(0) print(re.sub(r"[aeiou]+", ff, "encyclopeadia")) # encyclopædia print(re.sub(r"[aeiou]+", ff, "book")) # buk print(re.sub(r"[aeiou]+", ff, "geek")) # geek
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python regex replace all
Python regex replace all - Spark By {Examples}
May 31, 2024 - In Python, you can use the regex re module to perform regular expression operations like replace all occurrences of strings. The re.sub() function is commonly used to replace all occurrences of a pattern with a specified replacement.