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 3 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
36 changes: 19 additions & 17 deletions content/python/concepts/regex/terms/sub/sub.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ The **`re.sub()`** function replaces matching substrings with a new string for a
```pseudo
re.sub(<pattern>, <replacement>, string, <count>, <flags>)
```
- **`<pattern>`**: A [regular expression](https://www.codecademy.com/resources/docs/general/regular-expressions) pattern used to match substrings.

Examples:
- A string: `Jane Smith`
- Character class codes: `/w`, `/s`, `/d`
- Regex symbols: `$`, `|`, `^`

A `<pattern>` is a [regular expression](https://www.codecademy.com/resources/docs/general/regular-expressions) that can include any of the following:
- **`<replacement>`**: The replacement argument. This can either be a string or a function.

- A string: `Jane Smith`
- A character class code: `/w`, `/s`, `/d`
- A regex symbol: `$`, `|`, `^`
- **`<count>`**: An integer specifying the number of occurrences to replace. The default is to replace all matches.

The other arguments include:

- The replacement string (`<replacement>`): `foo`
- An integer value for the number of replacements (`<count>`): `10`
- `<flags>`: `IGNORECASE`, `VERBOSE`, `DOTALL`
- **`<flags>`**: Specifies additional options such as `IGNORECASE`, `VERBOSE`, `DOTALL`, etc.

## Example

Expand All @@ -40,12 +40,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
bi tools may be their most valuable resource.'''
def replace_bussines_intelligence(match):
return 'business intelligence'

match = re.sub(r'bi','business intelligence',blurb,flags=re.IGNORECASE)
# The IGNORECASE flag allows for matches regardless of the case
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.'''

# Use the function `replace_bussines_intelligence` as the replacement argument in re.sub()
match = re.sub(r'bi', replace_func, blurb, flags=re.IGNORECASE)
rrayhka marked this conversation as resolved.
Show resolved Hide resolved
print(match)
```

Expand All @@ -63,10 +65,10 @@ 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'''

redacted = re.sub(r'\d+', 'REDACTED', confidential_str)
# \d matches any numerical character
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+', redact_numbers, confidential_str)
print(redacted)
```
Loading