全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-19 8 分钟 ✍️ juanwangdev

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() 从开头匹配,不匹配返回 None
  • search() 搜索任意位置首次匹配
  • findall() 返回所有匹配列表
  • finditer() 返回 Match 迭代器,节省内存
  • fullmatch() 要求整个字符串匹配
  • Match 对象提供 group、start、end 等属性
  • 可通过 flags 参数控制匹配行为
  • re模块是文本匹配和处理的核心工具

📝 发现内容有误?点击此处直接编辑

← 上一篇 Python 异步网络编程概览
下一篇 → Python 正则表达式分组与捕获
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库