re
是 Python 中用于处理正则表达式的内置模块,提供了字符串匹配、搜索、替换等功能。以下是 re
模块的核心用法和常见函数:
1. 常用函数
-
re.match(pattern, string)
从字符串开头匹配模式,若匹配成功返回匹配对象,否则返回None
。import re result = re.match(r'Hello', 'Hello World') print(result.group()) # 输出: Hello(获取匹配内容)
-
re.search(pattern, string)
在整个字符串中搜索第一个匹配项,不限制开头。result = re.search(r'World', 'Hello World') print(result.group()) # 输出: World
-
re.findall(pattern, string)
查找字符串中所有匹配项,返回列表。result = re.findall(r'\d+', 'Age: 25, Score: 90') print(result) # 输出: ['25', '90'](匹配所有数字)
-
re.sub(pattern, repl, string)
替换字符串中所有匹配项,repl
可以是字符串或函数。result = re.sub(r'cat', 'dog', 'I have a cat, a black cat') print(result) # 输出: I have a dog, a black dog
-
re.split(pattern, string)
按匹配模式分割字符串,返回列表。result = re.split(r'\s+', 'Hello world python') # 按任意空格分割 print(result) # 输出: ['Hello', 'world', 'python']
2. 匹配对象(Match
)的常用方法
当 match
或 search
匹配成功时,返回一个 Match
对象,包含以下常用方法:
group()
:返回匹配的字符串(可指定分组索引,如group(1)
获取第一个分组)。start()
:返回匹配的起始位置。end()
:返回匹配的结束位置。span()
:返回(start, end)
元组。
3. 常用正则表达式符号
.
:匹配任意字符(除换行符)。*
:匹配前一个字符 0 次或多次。+
:匹配前一个字符 1 次或多次。?
:匹配前一个字符 0 次或 1 次(非贪婪模式)。^
:匹配字符串开头。$
:匹配字符串结尾。[]
:匹配括号内的任意字符(如[0-9]
匹配数字)。()
:分组,可通过group(n)
提取。\d
:匹配数字(等价于[0-9]
)。\w
:匹配字母、数字、下划线(等价于[a-zA-Z0-9_]
)。\s
:匹配空白字符(空格、制表符等)。
示例:提取邮箱地址
import re
text = "Contact: a@test.com, b@example.org"
emails = re.findall(r'\w+@\w+\.\w+', text)
print(emails) # 输出: ['a@test.com', 'b@example.org']
通过 re
模块,可高效处理复杂的字符串模式匹配和处理任务。