非常好 👍
下面是最系统、最实用的 Python 正则表达式(Regular Expression)和 re
模块速查与实战指南。
🧩 一、正则表达式简介
正则表达式(Regular Expression, 简称 Regex)是一种用于匹配、搜索、替换字符串的 规则语言。
Python 提供了功能强大的 re
模块来支持正则操作。
导入模块:
import re
⚙️ 二、常用函数(re 模块核心)
函数 | 说明 |
---|---|
re.match() | 从字符串开头匹配 |
re.search() | 扫描整个字符串,返回第一个匹配项 |
re.findall() | 返回所有匹配项列表 |
re.finditer() | 返回所有匹配项的迭代器(每个为 Match 对象) |
re.sub() | 替换匹配内容 |
re.split() | 按匹配规则分割字符串 |
re.compile() | 预编译正则表达式,提高性能 |
🔍 三、常用正则表达式符号
模式 | 说明 | 示例匹配 |
---|---|---|
. | 匹配任意字符(除换行符) | a.c → 匹配 "abc" , "axc" |
^ | 匹配字符串开头 | ^hello |
$ | 匹配字符串结尾 | world$ |
* | 前一个字符重复 0 次或多次 | ab* → "a" , "ab" , "abb" |
+ | 前一个字符重复 1 次或多次 | ab+ → "ab" , "abb" |
? | 前一个字符重复 0 次或 1 次 | ab? → "a" , "ab" |
{n} | 重复 n 次 | a{3} → "aaa" |
{n,} | 重复至少 n 次 | a{2,} → "aa" , "aaa" , "aaaa" |
{n,m} | 重复 n 到 m 次 | a{2,4} → "aa" , "aaa" , "aaaa" |
[] | 匹配字符集合 | [abc] → "a" , "b" , "c" |
[^] | 匹配不在集合中的字符 | [^0-9] → 非数字 |
` | ` | 或(OR) |
() | 分组 | (ab)+ |
\d | 数字 [0-9] | |
\D | 非数字 | |
\w | 单词字符 [a-zA-Z0-9_] | |
\W | 非单词字符 | |
\s | 空白字符(空格、制表符、换行) | |
\S | 非空白字符 |
🧮 四、基本用法示例
✅ 1. match()
import re
text = "Hello World"
m = re.match(r"Hello", text)
if m:
print(m.group()) # Hello
👉 match()
只匹配 字符串开头。
✅ 2. search()
text = "Python is powerful and fast"
m = re.search(r"power\w+", text)
print(m.group()) # powerful
👉 search()
匹配第一个符合规则的内容。
✅ 3. findall()
text = "My numbers are 123 and 456"
nums = re.findall(r"\d+", text)
print(nums) # ['123', '456']
✅ 4. sub()
(替换)
text = "I love cats and cats"
new_text = re.sub(r"cats", "dogs", text)
print(new_text) # I love dogs and dogs
✅ 5. split()
(分割)
text = "apple,banana;cherry orange"
result = re.split(r"[;,\s]+", text)
print(result) # ['apple', 'banana', 'cherry', 'orange']
✅ 6. finditer()
(获取详细匹配对象)
for m in re.finditer(r"\d+", "A1 B22 C333"):
print(m.group(), "位置:", m.start(), "-", m.end())
输出:
1 位置: 1 - 2
22 位置: 3 - 5
333 位置: 6 - 9
🧱 五、Match
对象属性
m = re.search(r"(\d+)", "Item 123 cost $45")
print(m.group()) # 123
print(m.start()) # 起始索引
print(m.end()) # 结束索引
print(m.span()) # (起始, 结束)
print(m.groups()) # 所有分组内容(tuple)
🧩 六、分组与命名捕获
✅ 普通分组
m = re.search(r"(\d+)-(\d+)-(\d+)", "2025-09-28")
print(m.groups()) # ('2025', '09', '28')
✅ 命名分组
m = re.search(r"(?P<year>\d+)-(?P<month>\d+)-(?P<day>\d+)", "2025-09-28")
print(m.group("year")) # 2025
print(m.groupdict()) # {'year': '2025', 'month': '09', 'day': '28'}
🚀 七、re.compile()
(预编译正则表达式)
当同一个模式需要多次使用时,推荐先编译再复用:
pattern = re.compile(r"\d+")
print(pattern.findall("123 abc 456"))
print(pattern.findall("789 000"))
🧰 八、常用模式修饰符(Flags)
修饰符 | 说明 |
---|---|
re.I | 忽略大小写 (IGNORECASE ) |
re.M | 多行模式:^ 匹配每行开头,$ 匹配每行结尾 |
re.S | 让 . 匹配包括换行符在内的所有字符 |
re.X | 忽略空白和注释,增强可读性 |
示例:
pattern = re.compile(r"""
^\d{4} # 年
-\d{2} # 月
-\d{2}$ # 日
""", re.X)
print(bool(pattern.match("2025-09-28"))) # True
🧮 九、常见正则表达式示例
用途 | 正则表达式 | 示例匹配 |
---|---|---|
邮箱 | ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ | test@mail.com |
手机号(中国) | ^1[3-9]\d{9}$ | 13888888888 |
IP 地址 | ^(\d{1,3}\.){3}\d{1,3}$ | 192.168.1.1 |
日期 | ^\d{4}-\d{2}-\d{2}$ | 2025-09-28 |
URL | https?://[^\s]+ | https://openai.com |
HTML 标签 | <[^>]+> | <div> , <a href=...> |
金额(含小数) | ^\d+(\.\d{1,2})?$ | 100, 100.50 |
🧩 十、实战例子
✅ 提取文本中的 Email 地址
text = "请联系我:user@example.com 或 support@openai.com"
emails = re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", text)
print(emails)
# ['user@example.com', 'support@openai.com']
✅ 屏蔽敏感词
text = "这个网站含有违规词汇ABC!"
text = re.sub(r"ABC", "***", text)
print(text) # 这个网站含有违规词汇***!
✅ 验证密码复杂度(至少8位,含大小写和数字)
pattern = re.compile(r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$")
print(bool(pattern.match("Aa123456"))) # True
✅ 总结速查表
功能 | 常用函数 | 场景 |
---|---|---|
匹配单个 | match() | 从字符串开头匹配 |
搜索任意位置 | search() | 查找第一个匹配 |
找全部 | findall() | 批量提取 |
替换 | sub() | 清洗文本 |
分割 | split() | 拆分字符串 |
提高性能 | compile() | 重复使用同一规则 |
调试与注释 | re.X | 多行正则可读性高 |
发表回复