import re
# 常用模式
# . 任意字符(除换行)
# \d 数字
# \w 单词字符
# \s 空白字符
# ^ 开头
# $ 结尾
# * 0次或多次
# + 1次或多次
# ? 0次或1次
# {n} n次
# {n,m} n到m次
# [] 字符类
# | 或
# () 分组
===== re模块函数 =====
import re
text = "The price is $42.50"
# search - 搜索第一个匹配
match = re.search(r'\$([\d.]+)', text)
if match:
print(match.group()) # $42.50
print(match.group(1)) # 42.50
# findall - 查找所有
prices = re.findall(r'\$([\d.]+)', text)
# match - 从开头匹配
result = re.match(r'The', text)
# sub - 替换
new_text = re.sub(r'\$([\d.]+)', r'\1 USD', text)
# split - 分割
parts = re.split(r'\s+', text)
# compile - 编译模式(重复使用)
pattern = re.compile(r'\$([\d.]+)')
matches = pattern.findall(text)
===== 高级用法 =====
# 命名分组
text = "John Doe, 30"
pattern = r'(?P\w+ \w+), (?P\d+)'
match = re.search(pattern, text)
print(match.group('name')) # John Doe
# 贪婪 vs 非贪婪
re.search(r'<.*>', 'content') # 贪婪,匹配全部
re.search(r'<.*?>', 'content') # 非贪婪,匹配
# 多行模式
text = "line1\nline2"
re.search(r'^line', text, re.MULTILINE)
# 忽略大小写
re.search(r'the', text, re.IGNORECASE)
===== 本章练习 =====
1. 验证邮箱格式
2. 提取所有URL
3. 解析日志文件
4. 替换敏感词
下一章:[[python_course:chapter27|第二十七章:网络编程]]