Python re模块基础
re模块是 Python 正则表达式的核心模块,提供多种匹配函数。
match 从开头匹配
Python
import re
# match 从字符串开头匹配
text = "hello world"
match = re.match(r"hello", text)
print(match.group()) # hello
# 开头不匹配则返回 None
match = re.match(r"world", text)
print(match) # None
search 搜索首次匹配
Python
import re
# search 在字符串任意位置搜索
text = "hello world"
match = re.search(r"world", text)
print(match.group()) # world
# 返回第一个匹配
text = "abc 123 def 456"
match = re.search(r"\d+", text)
print(match.group()) # 123
findall 查找所有匹配
Python
import re
# findall 返回所有匹配的列表
text = "abc 123 def 456 ghi 789"
matches = re.findall(r"\d+", text)
print(matches) # ['123', '456', '789']
# 有分组时返回分组内容
text = "name=Alice, name=Bob"
matches = re.findall(r"name=(\w+)", text)
print(matches) # ['Alice', 'Bob']
finditer 返回匹配迭代器
Python
import re
text = "abc 123 def 456"
for match in re.finditer(r"\d+", text):
print(f"匹配: {match.group()}, 位置: {match.start()}-{match.end()}")
# 输出:
# 匹配: 123, 位置: 4-7
# 匹配: 456, 位置: 12-15
Match 对象属性
Python
import re
text = "hello world"
match = re.search(r"(\w+)", text)
print(match.group()) # hello(完整匹配)
print(match.group(1)) # hello(第一个分组)
print(match.start()) # 0(开始位置)
print(match.end()) # 5(结束位置)
print(match.span()) # (0, 5)
print(match.string) # hello world(原字符串)
fullmatch 完整匹配
Python
import re
# fullmatch 要求整个字符串匹配
text = "12345"
match = re.fullmatch(r"\d+", text)
print(match.group()) # 12345
text = "123abc"
match = re.fullmatch(r"\d+", text)
print(match) # None
split 分割字符串
Python
import re
text = "a,b;c d"
parts = re.split(r"[,; ]", text)
print(parts) # ['a', 'b', 'c', 'd']
# 限制分割次数
parts = re.split(r"[,; ]", text, maxsplit=2)
print(parts) # ['a', 'b', 'c d']
sub 替换字符串
Python
import re
text = "hello 123 world 456"
result = re.sub(r"\d+", "NUM", text)
print(result) # hello NUM world NUM
# 限制替换次数
result = re.sub(r"\d+", "NUM", text, count=1)
print(result) # hello NUM world 456
函数对比
| 函数 | 功能 | 返回值 |
|---|---|---|
| match | 从开头匹配 | Match 或 None |
| search | 搜索首次匹配 | Match 或 None |
| findall | 查找所有匹配 | 列表 |
| finditer | 返回匹配迭代器 | 迭代器 |
| fullmatch | 完整匹配 | Match 或 None |
| split | 分割字符串 | 列表 |
| sub | 替换字符串 | 新字符串 |
使用标志
Python
import re
text = "Hello World"
# 忽略大小写
match = re.search(r"hello", text, flags=re.IGNORECASE)
print(match.group()) # Hello
# 多行模式
text = "line1\nline2"
matches = re.findall(r"^line", text, flags=re.MULTILINE)
print(matches) # ['line', 'line']
要点总结
match()从开头匹配,不匹配返回 Nonesearch()搜索任意位置首次匹配findall()返回所有匹配列表finditer()返回 Match 迭代器,节省内存fullmatch()要求整个字符串匹配- Match 对象提供 group、start、end 等属性
- 可通过 flags 参数控制匹配行为
- re模块是文本匹配和处理的核心工具
📝 发现内容有误?点击此处直接编辑