re.sub()
StevenSwiniarski466 total contributions
Published Jul 30, 2021Updated Sep 5, 2023
Contribute to Docs
The re.sub()
function replaces matching substrings with a new string for all occurrences, or a specified number.
Syntax
re.sub(<pattern>, <replacement>, string, <count>, <flags>)
A <pattern>
is a regular expression that can include any of the following:
- A string:
Jane Smith
- A character class code:
/w
,/s
,/d
- A regex symbol:
$
,|
,^
The other arguments include:
- The replacement string (
<replacement>
):foo
- An integer value for the number of replacements (
<count>
):10
<flags>
:IGNORECASE
,VERBOSE
,DOTALL
Example
The following example replaces all occurrences of “BI” with “business intelligence”:
import reblurb = '''The analytics firm uses a range of BI tools to visualize data. Their internal data science team suggestsbi tools may be their most valuable resource.'''match = re.sub(r'bi','business intelligence',blurb,flags=re.IGNORECASE)# The IGNORECASE flag allows for matches regardless of the caseprint(match)
This will print the following where “bi” is replaced with “business intelligence”:
The analytics firm uses a range of business intelligence tools to visualize data. Their internal data science team suggestsbusiness intelligence tools may be their most valuable resource.
Codebyte Example
Replace all numerical values with “REDACTED”:
All contributors
- StevenSwiniarski466 total contributions
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- Anonymous contributorAnonymous contributor88 total contributions
- Anonymous contributorAnonymous contributor9 total contributions
- BrandonDusch580 total contributions
- StevenSwiniarski
- christian.dinh
- Anonymous contributor
- Anonymous contributor
- Anonymous contributor
- BrandonDusch
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.