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
🌐
Codecademy
codecademy.com › docs › python › regular expressions › re.sub()
Python | Regular Expressions | re.sub() | Codecademy
October 14, 2024 - <pattern>: A regular expression pattern used to match substrings. A string: Jane Smith · Character class codes: /w, /s, /d · Regex symbols: $, |, ^ <replacement>: The replacement argument. This can either be a string or a function. <count>: An integer specifying the number of occurrences to replace. The default is to replace all matches. <flags>: Specifies additional options such as IGNORECASE, VERBOSE, DOTALL, etc. The following example replaces all occurrences of “BI” with “business intelligence”: import re ·
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
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 ...
🌐
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.
🌐
Educative
educative.io › answers › what-is-the-resub-function-in-python
What is the re.sub() function in Python?
In re.sub(), these groups are referenced in the replacement string with back references like \1 for the first match and \2 for the second, making substitutions more dynamic. Let us now understand how to use capturing groups in pattern parameter with the help of code example:
🌐
LZone
lzone.de › examples › Python re.sub
Python re.sub Examples
def my_replace(m): if <some condition>: return <replacement variant 1> return <replacement variant 2> result = re.sub("\w+", my_replace, input)
🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex sub()
Python Regex sub() Function
December 10, 2021 - The backreference \1 refers to ... language: Python (python) And you want to square the number in each list element. For example, A1 becomes A1, A2 becomes A4, and A3 becomes A9. To do this, you can use the sub() function....
🌐
Python Examples
pythonexamples.org › python-re-sub
Python re.sub() - Replace using Regular Expression
In this example, we will take a string and replace patterns that contains a continuous occurrence of numbers with the string NN. We will do the replacement using re.sub() function.
Find elsewhere
🌐
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 - In this example, we will use the \s regex special sequence that matches any whitespace character, short for [ \t\n\x0b\r\f] Let’s assume you have the following string and you wanted to replace all the whitespace with an underscore. target_string = "Jessa knows testing and machine learning" ... 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
🌐
Note.nkmk.me
note.nkmk.me › home › python
Replace Strings in Python: replace(), translate(), and Regex | note.nkmk.me
May 4, 2025 - To replace characters individually, use the translate() method, discussed later in this article. Swapping two substrings using sequential replace() calls may not work as expected.
🌐
Built In
builtin.com › articles › python-re-match
Python re.match() and re.sub() Explained | Built In
Here, \d+ is the regex pattern that matches one or more digits. The re.sub() function replaces all occurrences of this pattern with the string 'NUM'.
🌐
W3Schools
w3schools.com › python › python_regex.asp
W3Schools.com
HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference · HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples
🌐
Finxter
blog.finxter.com › home › learn python blog › python regex re.sub()
Python Regex re.sub() - Be on the Right Side of Change
June 13, 2023 - The easiest use is with only three arguments: the pattern 'sing‘, the replacement string 'program', and the string you want to modify (text in our example). >>> import re >>> text = 'Learn to sing because singing is fun.' >>> re.sub('sing', 'program', text) 'Learn to program because programing is fun.'
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-substituting-patterns-in-text-using-regex
Python - Substituting patterns in text using regex - GeeksforGeeks
July 12, 2025 - This example demonstrates the use of mentioned shorthand character classes for the substitution and preprocessing of text to get clean and error-free strings. Below is the implementation. ... # Python implementation of Substitution using # shorthand character class and preprocessing of text # importing regex module import re # Function to perform # operations on the strings def substitutor(): # list of strings S = ["2020 Olympic games have @# been cancelled", "Dr Vikram Sarabhai was +%--the ISRO’s first chairman", "Dr Abdul Kalam, the father of India's missile programme"] # loop to iterate e
🌐
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
🌐
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › 15_module_re › sub.html
Function re.sub - Python for network engineers
In [9]: mac_table = ''' ...: 100 aabb.cc10.7000 DYNAMIC Gi0/1 ...: 200 aabb.cc20.7000 DYNAMIC Gi0/2 ...: 300 aabb.cc30.7000 DYNAMIC Gi0/3 ...: 100 aabb.cc40.7000 DYNAMIC Gi0/4 ...: 500 aabb.cc50.7000 DYNAMIC Gi0/5 ...: 200 aabb.cc60.7000 DYNAMIC Gi0/6 ...: 300 aabb.cc70.7000 DYNAMIC Gi0/7 ...: ''' In [4]: print(re.sub(r' *(\d+) +' ...: r'([a-f0-9]+)\.' ...: r'([a-f0-9]+)\.' ...: r'([a-f0-9]+) +\w+ +' ...: r'(\S+)', ...: r'\1 \2:\3:\4 \5', ...: mac_table)) ...: 100 aabb:cc10:7000 Gi0/1 200 aabb:cc20:7000 Gi0/2 300 aabb:cc30:7000 Gi0/3 100 aabb:cc40:7000 Gi0/4 500 aabb:cc50:7000 Gi0/5 200 aabb:cc60:7000 Gi0/6 300 aabb:cc70:7000 Gi0/7 ... In a second regex these groups are used.
Top answer
1 of 5
71

Firstly, why doesn't your solution work. You mix up a lot of concepts. Mostly character class with other ones. In the first character class you use | which stems from alternation. In character classes you don't need the pipe. Just list all characters (and character ranges) you want:

[Uu]

Or simply write u if you use the case-insensitive modifier. If you write a pipe there, the character class will actually match pipes in your subject string.

Now in the second character class you use the comma to separate your characters for some odd reason. That does also nothing but include commas into the matchable characters. s and W are probably supposed to be the built-in character classes. Then escape them! Otherwise they will just match literal s and literal W. But then \W already includes everything else you listed there, so a \W alone (without square brackets) would have been enough. And the last part (^a-zA-Z) also doesn't work, because it will simply include ^, (, ) and all letters into the character class. The negation syntax only works for entire character classes like [^a-zA-Z].

What you actually want is to assert that there is no letter in front or after your u. You can use lookarounds for that. The advantage is that they won't be included in the match and thus won't be removed:

r'(?<![a-zA-Z])uU'

Note that I used a raw string. Is generally good practice for regular expressions, to avoid problems with escape sequences.

These are negative lookarounds that make sure that there is no letter character before or after your u. This is an important difference to asserting that there is a non-letter character around (which is similar to what you did), because the latter approach won't work at the beginning or end of the string.

Of course, you can remove the spaces around you from the replacement string.

If you don't want to replace u that are next to digits, you can easily include the digits into the character classes:

r'(?<![a-zA-Z0-9])uU'

And if for some reason an adjacent underscore would also disqualify your u for replacement, you could include that as well. But then the character class coincides with the built-in \w:

r'(?<!\w)uU'

Which is, in this case, equivalent to EarlGray's r'\b[uU]\b'.

As mentioned above you can shorten all of these, by using the case-insensitive modifier. Taking the first expression as an example:

re.sub(r'(?<![a-z])u(?![a-z])', 'you', text, flags=re.I)

or

re.sub(r'(?<![a-z])u(?![a-z])', 'you', text, flags=re.IGNORECASE)

depending on your preference.

I suggest that you do some reading through the tutorial I linked several times in this answer. The explanations are very comprehensive and should give you a good headstart on regular expressions, which you will probably encounter again sooner or later.

2 of 5
15

Use a special character \b, which matches empty string at the beginning or at the end of a word:

print re.sub(r'\b[uU]\b', 'you', text)

spaces are not a reliable solution because there are also plenty of other punctuation marks, so an abstract character \b was invented to indicate a word's beginning or end.

🌐
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 - import re phone = "412-555-1234" formatted = re.sub("\D", "", phone) print(formatted) # Output: "4125551234" Here, “\D” matches any non-digit character. The empty quotes replace matches with nothing effectively removing non-numeric symbols.
🌐
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!