将正则表达式字符串编译成 Pattern 对象,后续匹配更高效。常用标志位有 re.I(不区分大小写)re.M(多行匹配)等。
import re
# 编译一个不区分大小写的模式
pat = re.compile(r'\bpython\b', re.I)
# 使用编译对象进行匹配
print(pat.search('I love Python!')) # <re.Match object...>
仅在字符串开头尝试匹配,成功返回 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
在整个字符串中搜索第一次出现的匹配,返回 Match 对象。
import re
s = 'Contact: alice@example.com'
m = re.search(r'[\w\.-]+@[\w\.-]+', s)
print(m.group()) # alice@example.com
返回所有非重叠匹配的列表,适合快速获取匹配结果。
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']
返回一个迭代器,每个元素都是 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
使用正则模式作为分隔符将字符串切割成列表。
import re
s = 'apple, banana; orange|grape'
parts = re.split(r'[,;|]\s*', s)
print(parts) # ['apple', 'banana', 'orange', 'grape']
将匹配到的子串替换为 repl,count 控制替换次数。
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
功能同 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
把字符串中的正则特殊字符全部转义,常用于把用户输入安全地嵌入正则表达式。
import re
user_input = 'file[1].txt'
pattern = re.escape(user_input) + r'\d+'
print(pattern) # file\[1\]\.txt\d+
re.I(或 re.IGNORECASE)——不区分大小写。re.M(或 re.MULTILINE)——^、$ 匹配每行的开头/结尾。re.S(或 re.DOTALL)——. 匹配换行符。re.X(或 re.VERBOSE)——允许在正则中加入空格和注释,提高可读性。帮助快速掌握 re 模块的核心功能。若想进一步了解正则表达式的语法(如字符集、量词、分组等),建议参考官方文档或上述教程。