Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sub.md #5404

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions content/python/concepts/regex/terms/sub/sub.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ The **`re.sub()`** function replaces matching substrings with a new string for a
re.sub(<pattern>, <replacement>, string, <count>, <flags>)
```

A `<pattern>` 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 (`<replacement>`): `foo`
- An integer value for the number of replacements (`<count>`): `10`
- `<flags>`: `IGNORECASE`, `VERBOSE`, `DOTALL`
- `<pattern>`: 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: `$`, `|`, `^`
- `<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.

## Example

Expand All @@ -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)
```
Expand All @@ -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)
```
Loading