Products
GG网络技术分享 2025-11-13 23:46 6
python import re
text = "hello, today is --abc--" pattern = r"+@+.{2,}" match = re.search if match: print)

text = "The date is 2023-01-01" pattern = r"\d{4}-\d{2}-\d{2}" match = re.search if match: print)
text = "This is a test string, with some words." pattern = r",\s+" words = re.split print
text = "I love Python programming." pattern = r"Python" repl = "Java" new_text = re.sub print
text = "This is a test. This test is only a test." pattern = r"test" matches = re.findall print
代码说明白:
1. 先说说导入re模块。
2. 用re.search方法查找文本中的电子邮件地址和日期。
3. 用re.split方法按逗号和空格分割字符串。
4. 用re.sub方法替换文本中的"Python"为"Java"。
5. 用re.findall方法查找文本中全部出现的"test"。
Demand feedback