compile 函数用于编译正则表达式,返回一个正则表达式对象,供 match()、search() 、findall()等函数使用。
格式:re.compile(正则表达式【, 标志位】)
import re
a ='a1b2c3d4e5f6'
pattern = re.compile('\d') # 匹配数字
s = pattern.match(a) # 查找头部,没有匹配
print(s)
s = pattern.match(a,1,10) # 查找从索引 1 开始10结束
print(s) # 返回一个 Match 对象
import re
a ='a1b2c3d4e5f6'
pattern = re.compile('\d') # 匹配数字
s = pattern.findall(a) # 查找头部,没有匹配
print(s) # 返回一个列表
s = pattern.findall(a,5,10) # 查找从索引 5 开始10结束
print(s)