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.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 6.67
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.
Дякуємо за ваш відгук!