You can use re.sub:

>>> import re
>>> text = "<a> text </a> <c> code </c>"
>>> new_text = re.sub(r'<c>.*?</c>', '', text)
>>> new_text
<a> text </a> 
Answer from Sash Sinha on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @wepypixel โ€บ complete-python-regex-replace-guide-using-re-sub-pypixel-9b30b2604d7a
Complete Python Regex Replace Guide using re.sub() | PyPixel | by Stilest | Medium
December 8, 2023 - In python you can get hold of any regex based properties with the help of re module. You simply need to import the module into your python file and use re.sub() method to perform the job of replacing in a string.
๐ŸŒ
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 # Original string student_names = "Emma-Kelly Jessa Joy Scott-Joe Jerry" # replace two pattern at the same time # use OR (|) to separate two pattern res = re.sub(r"(\s)|(-)", ",", student_names) print(res) # Output 'Emma,Kelly,Jessa,Joy,Scott,Joe,Jerry'Code language: Python (python) Run ยท To understand this take the example of the following string
Discussions

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
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
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
Python re.sub() regex to substitute a character with a string except in quotes
The third-party regex module is useful for such cases: >>> import regex >>> s1 = '@ foo@ "abc@google.com"' >>> s2 = "@ foo@ 'abc@google.com'" >>> regex.sub(r'''(?:'[^']+'|"[^"]+")(*SKIP)(*F)|@''', 'bar', s1) 'bar foobar "abc@google.com"' >>> regex.sub(r'''(?:'[^']+'|"[^"]+")(*SKIP)(*F)|@''', 'bar', s2) "bar foobar 'abc@google.com'" Otherwise, you can use a custom function in the replacement section: >>> import re >>> s1 = '@ foo@ "abc@google.com"' >>> s2 = "@ foo@ 'abc@google.com'" >>> re.sub(r'''@|'[^']+'|"[^"]+"''', lambda m: 'bar' if m[0] == '@' else m[0], s1) 'bar foobar "abc@google.com"' >>> re.sub(r'''@|'[^']+'|"[^"]+"''', lambda m: 'bar' if m[0] == '@' else m[0], s2) "bar foobar 'abc@google.com'" More on reddit.com
๐ŸŒ r/regex
12
5
August 14, 2021
๐ŸŒ
Codidact
software.codidact.com โ€บ posts โ€บ 294905
Python re.sub() how to include and slice the match in the substitution? - Software Development
November 1, 2025 - The capturing group will produce group match 1 in the result, so we can refer to it in the replacement string that way: r'<i>\g<1></i>'. Putting it all together gives Michael's example code. Aside from this kind of special string with references back to the group matches, Python allows you to pass a function as the "replacement" used by re.sub.
๐ŸŒ
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.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Replace Strings in Python: replace(), translate(), and Regex
May 4, 2025 - re.subn() โ€” Regular expression operations โ€” Python 3.13.3 documentation
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ regular expressions โ€บ re.sub()
Python | Regular Expressions | re.sub() | Codecademy
October 14, 2024 - Replace matching substrings with a new string for all occurrences, or a specified number.
Find elsewhere
๐ŸŒ
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!

๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ python-re-match
Python re.match() and re.sub() Explained | Built In
So, this example finds phone numbers in the 123-456-7890 format and converts them to (123) 456-7890. ... Contact me at (123) 456-7890 or (987) 654-3210. ... (): Used for capturing groups, allowing us to extract parts of the match and reference ...
๐ŸŒ
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 - 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 ...
๐ŸŒ
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 ('').

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ re-sub-python-regex
re.sub() - Python RegEx - GeeksforGeeks
July 23, 2025 - This code uses re.sub() to find all matches of a name followed by an age (e.g., "John 25") in the string and swaps the order, placing the age first followed by the name.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Use of re.sub for renaming files/strings not working - Python Help - Discussions on Python.org
October 26, 2022 - Hi, I have a scenario where I need to use an existing function of re.sub for renaming a particular file. we have a file at a particular location with a .zip extension as like abc.zip. and I have a string like bcd, I need to rename the abc name ...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python regex replace all
Python regex replace all - Spark By {Examples}
May 31, 2024 - In this Python article, we will explore how to use the re.sub() method with clear-cut examples to replace all instances of a pattern in a given text by using regex expressions.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations
5 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 of the same character for the same purpose in string literals; for example, to match ...
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python regular expressions
Python Regular Expressions | Python Education | Google for Developers
In Python a regular expression search is typically written as: ... The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise. Therefore, the search is usually immediately followed by an if-statement to test if the search succeeded, as shown in the following example which searches for the pattern 'word:' followed by a 3 letter word (details below):
๐ŸŒ
LearnByExample
learnbyexample.github.io โ€บ python-regex-cheatsheet
Python regular expression cheatsheet and examples
June 9, 2025 - >>> pet = re.compile(r'dog') >>> type(pet) <class 're.Pattern'> >>> bool(pet.search('They bought a dog')) True >>> bool(pet.search('A cat crossed their path')) False >>> pat = re.compile(r'\([^)]*\)') >>> pat.sub('', 'a+b(addition) - foo() + c%d(#modulo)') 'a+b - foo + c%d' >>> pat.sub('', 'Hi there(greeting). Nice day(a(b)') 'Hi there. Nice day' Visit my GitHub repo Understanding Python re(gex)? for details about the book I wrote on Python regular expressions. The book uses plenty of examples to explain the concepts from the basics and introduces more advanced concepts step-by-step.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ regular-expression-python-examples
Python RegEx - GeeksforGeeks
August 14, 2025 - Example: This code uses regular expression \d+ to find all sequences of one or more digits in the given string. ... import re string = """Hello my Number is 123456789 and my friend's number is 987654321""" regex = '\d+' match = re.findall(regex, ...