Regex 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.
12345import re text = "My phone number is 123-456-7890 and my ID is 987654." masked = re.sub(r"\d", "#", text) print(masked)
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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
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
Regex Substitution and Splitting
Swipe to show menu
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.
12345import re text = "My phone number is 123-456-7890 and my ID is 987654." masked = re.sub(r"\d", "#", text) print(masked)
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.
Thanks for your feedback!