Python re 模块常用函数速查

re.compile(pattern, flags=0)

将正则表达式字符串编译成 Pattern 对象,后续匹配更高效。常用标志位有 re.I(不区分大小写)re.M(多行匹配)等。

import re

# 编译一个不区分大小写的模式
pat = re.compile(r'\bpython\b', re.I)

# 使用编译对象进行匹配
print(pat.search('I love Python!'))   # <re.Match object...>
    

re.match(pattern, string, flags=0)

仅在字符串开头尝试匹配,成功返回 Match 对象,否则 None

import re

m = re.match(r'\d{3}-\d{2}-\d{4}', '123-45-6789 is a SSN')
print(m.group())   # 123-45-6789
    

re.search(pattern, string, flags=0)

在整个字符串中搜索第一次出现的匹配,返回 Match 对象。

import re

s = 'Contact: alice@example.com'
m = re.search(r'[\w\.-]+@[\w\.-]+', s)
print(m.group())   # alice@example.com
    

re.findall(pattern, string, flags=0)

返回所有非重叠匹配的列表,适合快速获取匹配结果。

import re

text = 'Python 3.9, Python 3.10, Python 2.7'
versions = re.findall(r'Python\s\d\.\d+', text)
print(versions)   # ['Python 3.9', 'Python 3.10', 'Python 2.7']
    

re.finditer(pattern, string, flags=0)

返回一个迭代器,每个元素都是 Match 对象,适合需要获取匹配位置等信息的场景。

import re

text = 'a1 b22 c333'
for m in re.finditer(r'\d+', text):
    print(m.group(), m.start(), m.end())
# 输出:
# 1 1 2
# 22 4 6
# 333 7 10
    

re.split(pattern, string, maxsplit=0, flags=0)

使用正则模式作为分隔符将字符串切割成列表。

import re

s = 'apple, banana; orange|grape'
parts = re.split(r'[,;|]\s*', s)
print(parts)   # ['apple', 'banana', 'orange', 'grape']
    

re.sub(pattern, repl, string, count=0, flags=0)

将匹配到的子串替换为 replcount 控制替换次数。

import re

s = '2023-04-01, 2023-05-12'
new_s = re.sub(r'(\d{4})-(\d{2})-(\d{2})', r'\3/\2/\1', s)
print(new_s)   # 01/04/2023, 12/05/2023
    

re.subn(pattern, repl, string, count=0, flags=0)

功能同 sub,但返回 (new_string, number_of_substitutions)

import re

s = 'cat dog cat dog'
new_s, n = re.subn(r'cat', 'mouse', s)
print(new_s)   # mouse dog mouse dog
print(n)      # 2
    

re.escape(string)

把字符串中的正则特殊字符全部转义,常用于把用户输入安全地嵌入正则表达式。

import re

user_input = 'file[1].txt'
pattern = re.escape(user_input) + r'\d+'
print(pattern)   # file\[1\]\.txt\d+
    

常用标志位(flags)

帮助快速掌握 re 模块的核心功能。若想进一步了解正则表达式的语法(如字符集、量词、分组等),建议参考官方文档或上述教程。