Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Regex Substitution and Splitting | Intermediate Regular Expressions
Python Regular Expressions

bookRegex Substitution and Splitting

Introduction to re.sub and re.split

When working with text data in Python, you often need to modify or segment strings based on complex patterns. The re module offers two powerful functions for these tasks: re.sub for substitution and re.split for splitting. Both functions use regular expressions to match parts of a string, allowing you to perform replacements or divide the string wherever a pattern occurs. This approach goes beyond simple string methods by letting you specify flexible, rule-based criteria for your operations.

12345
import re text = "My phone number is 123-456-7890 and my ID is 987654." masked = re.sub(r"\d", "#", text) print(masked)
copy

In this example, every digit in the string is replaced with #. The re.sub function searches for all matches of the pattern \d (which matches any digit) and substitutes each occurrence with the replacement character. The result is a string where all digits are masked, while the rest of the text remains unchanged. The pattern you provide determines exactly which parts of the string are replaced—using a different pattern would target different substrings.

question mark

Which function is used to split a string by a regex pattern?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how re.split works with an example?

What other patterns can I use with re.sub to replace different parts of a string?

How does re.sub differ from the standard str.replace method?

Awesome!

Completion rate improved to 6.67

bookRegex Substitution and Splitting

Sveip for å vise menyen

Introduction to re.sub and re.split

When working with text data in Python, you often need to modify or segment strings based on complex patterns. The re module offers two powerful functions for these tasks: re.sub for substitution and re.split for splitting. Both functions use regular expressions to match parts of a string, allowing you to perform replacements or divide the string wherever a pattern occurs. This approach goes beyond simple string methods by letting you specify flexible, rule-based criteria for your operations.

12345
import re text = "My phone number is 123-456-7890 and my ID is 987654." masked = re.sub(r"\d", "#", text) print(masked)
copy

In this example, every digit in the string is replaced with #. The re.sub function searches for all matches of the pattern \d (which matches any digit) and substitutes each occurrence with the replacement character. The result is a string where all digits are masked, while the rest of the text remains unchanged. The pattern you provide determines exactly which parts of the string are replaced—using a different pattern would target different substrings.

question mark

Which function is used to split a string by a regex pattern?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt