Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 6.67

bookRegex Substitution and Splitting

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
some-alt