No. Regular expressions in Python are handled by the re module.

article = re.sub(r'(?is)</html>.+', '</html>', article)

In general:

str_output = re.sub(regex_search_term, regex_replacement, str_input)
Answer from Ignacio Vazquez-Abrams on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations โ€” Python 3.14.7 ...
Source code: Lib/re/ 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-...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-regex-replace-captured-groups
Python Regex: Replace Captured Groups - GeeksforGeeks
July 23, 2025 - Regex is supported in almost all major programming languages, and in Python, the `re` module provides an extensive set of functionalities for regex operations. This article dives into one of the crucial aspects of regex in Python: Regex Groups, and demonstrates how to use `re.sub()` for group replacement with practical examples.
๐ŸŒ
PythonTest
pythontest.com โ€บ python โ€บ regex-search-replace
Python regex Search and Replace Examples | PythonTest
I think the most basic form of a search/replace script in python is something like this: import fileinput import re for line in fileinput.input(): line = re.sub('foo','bar', line.rstrip()) print(line) The fileinput module takes care of the stream verses filename input handling. The re (regex, ...
๐ŸŒ
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 target_str = "Jessa knows testing and machine learning" # replace only first occurrence res_str = re.sub(r"\s", "-", target_str, count=1) # String after replacement print(res_str) # Output 'Jessa-knows testing and machine learning' # replace three occurrence res_str = re.sub(r"\s", "-", target_str, count=3) print(res_str) # Output 'Jessa-knows-testing-and machine learning'Code language: Python (python) Run ยท We saw how to find and replace the regex pattern with a fixed string in the earlier example.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Replace Strings in Python: replace(), translate(), and Regex | note.nkmk.me
May 4, 2025 - Use the | operator to match multiple patterns. Each pattern may include special regex characters or literal substrings. This allows you to replace different substrings with the same string.
Find elsewhere
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ python string tutorial โ€บ python regex replace
Python regex replace | Learn the Parameters of Python regex replace
May 13, 2024 - In Python, we use replace() function ... the entire string; hence to do this, we use regular expression which provides โ€œreโ€ module and to replace part of the string we use sub() function as we saw how simple it is to use this ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ regex-advanced-string-replacement-python
Python Regex Replace: How to Replace Strings Using re Module
The re.sub() function is the primary tool for performing string replacements with regex in Python. It allows you to specify a regex pattern to search for, a replacement string, and the target string where the replacement will occur.
๐ŸŒ
Dive into Python
diveintopython.org โ€บ home โ€บ learn python programming โ€บ regex in python
RegEx in Python: Match and Replace Basics with Examples
May 3, 2024 - The re module in Python provides a method called sub() which stands for substitute. It's the cornerstone for performing python replaceall regex operations. The syntax is straightforward: re.sub(pattern, replacement, string).
๐ŸŒ
Real Python
realpython.com โ€บ replace-string-python
How to Replace a String in Python โ€“ Real Python
October 22, 2025 - In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-substituting-patterns-in-text-using-regex
Python - Substituting patterns in text using regex - GeeksforGeeks
July 12, 2025 - # 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." # replacing text 'raining' in the string # variable sentence1 with 'sunny' thus # passing first parameter as raining # second as sunny, third as the # variable name in which string is stored # and printing the modified string print(re.sub(r"raining", "sunny", sentence1)) # a string variable sentence2 = "Thank you very very much."
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ python-python โ€บ python-replace-regex
python .replace() regex | JanBask Training Community
May 7, 2025 - `str.replace()` performs a direct substring match. It does not interpret patterns, special characters, or regex tokens. ... Nothing changes because `d+` is treated as a literal string, not a pattern.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.str.replace.html
pandas.Series.str.replace โ€” pandas 3.0.5 documentation
Method to replace occurrences of a substring with another substring. ... Extract substrings using a regular expression. ... Find all occurrences of a pattern or regex in each string.
๐ŸŒ
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 - The re.sub() method in Python provides powerful search and replace functionality using regular expressions. The regex replace works by specifying the string to modify, defining a regex pattern to match against, and providing a replacement substring.
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ python
Python replace regex for searching and replacing strings
July 1, 2023 - In this example, the re.sub() function is used to replace all occurrences of 'codedamn' with 'CODEDAMN'. The output would be: Hello, CODEDAMN coders! Welcome to CODEDAMN community! The real power of regex comes with its ability to use special characters to construct search patterns, making it a vital tool for string manipulations in Python.
๐ŸŒ
Plain English
python.plainenglish.io โ€บ the-incredible-power-of-pythons-replace-regex-6cc217643f37
The incredible power of Pythonโ€™s replace regex | by Josh Weinstein | Python in Plain English
November 1, 2020 - The second argument, the replace ... groups. In Pythonโ€™s regex, the notation \<x> , where x is an integer between and including 1 to 99, signifies the nth matched group in the regex....
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ python .replace regex
python .replace regex | Codemia
September 23, 2025 - In Python, str.replace does not understand regular expressions. It only replaces exact literal text. If you need pattern matching, capture groups, or conditional replacement logic, the correct tool is re.sub. Most confusion on this topic comes from trying to use replace with a regex pattern ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to search for regex and replace with a modified version?
r/learnpython on Reddit: How to search for regex and replace with a modified version?
March 10, 2021 -

Hi,

The title may not be perfect but I could not think of a better one.

How am I able to do a regex search within a string and replace all occurrences with a modified version of the matching part?

Background:

The task I'm going to do is that I'm looking for regex for ISO datestamps and shift them according to a user-defined delta in days. Therefore, I'm looking for r'(?P<year>\d{4,4})-(?P<month>[01]\d)-(?P<day>[0123]\d)'. When I've got matches, I want to retrieve each match, add or subtract a delta in days to it and replace its original date-stamp accordingly.

I got the impression that re.subn() is not able to do something like this for me:

re.subn(r'(?P<year>\d{4,4})-(?P<month>[01]\d)-(?P<day>[0123]\d)', calculate_shifted_ISO_string_from_ymd(\1, \2, \3), line_to_parse)

Is my only option to separate the identification of matches, manually split the string for each occurrence, generate a datetime from the extracted string, add a delta-datetime in days and re-concatenate the result for all occurrences?

SOLUTION:

I chose the provided tip to use a function as described in the comments below.

The resulting tool which shifts ISO date-stamps within text-files is hosted on https://github.com/novoid/isodateshifter