diff --git a/content/python/concepts/regex/terms/sub/sub.md b/content/python/concepts/regex/terms/sub/sub.md index 6d08e21c41d..5feec3c1641 100644 --- a/content/python/concepts/regex/terms/sub/sub.md +++ b/content/python/concepts/regex/terms/sub/sub.md @@ -21,17 +21,13 @@ The **`re.sub()`** function replaces matching substrings with a new string for a re.sub(, , string, , ) ``` -A `` is a [regular expression](https://www.codecademy.com/resources/docs/general/regular-expressions) 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 (``): `foo` -- An integer value for the number of replacements (``): `10` -- ``: `IGNORECASE`, `VERBOSE`, `DOTALL` +- ``: A [regular expression](https://www.codecademy.com/resources/docs/general/regular-expressions) pattern used to match substrings. + - A string: `Jane Smith` + - Character class codes: `/w`, `/s`, `/d` + - Regex symbols: `$`, `|`, `^` +- ``: The replacement argument. This can either be a string or a function. +- ``: An integer specifying the number of occurrences to replace. The default is to replace all matches. +- ``: Specifies additional options such as `IGNORECASE`, `VERBOSE`, `DOTALL`, etc. ## Example @@ -40,11 +36,14 @@ The following example replaces all occurrences of "BI" with "business intelligen ```py import re -blurb = '''The analytics firm uses a range of BI tools to visualize data. Their internal data science team suggests +def replace_business_intelligence(match): + return 'business intelligence' + +blurb = '''The analytics firm uses a range of BI tools to visualize data. Their internal data science team suggests bi 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 case +# Use the function `replace_bussines_intelligence` as the replacement argument in re.sub() +match = re.sub(r'bi', replace_business_intelligence, blurb, flags=re.IGNORECASE) print(match) ``` @@ -63,10 +62,12 @@ Replace all numerical values with "REDACTED": ```codebyte/python import re -confidential_str = '''The suspect's bank account (#333344444) and pin (#9999) were found in his cell''' +def redact_numbers(match): + return 'REDACTED' + +confidential_str = '''The suspect's bank account (#333344444) and pin (#9999) were found in his cell.''' -redacted = re.sub(r'\d+', 'REDACTED', confidential_str) -# \d matches any numerical character +redacted = re.sub(r'\d+', redact_numbers, confidential_str) print(redacted) ```