我们为什么要使用 Python 正则表达式中的 re.compile() 方法?


re.compile() 方法

re.compile(pattern, repl, string)

 我们可以将 正则表达式 模式合并到模式对象中,该模式对象可用于模式匹配。它还有助于再次搜索模式,而无需重写它。

示例

import re
pattern=re.compile('TP')
result=pattern.findall('TP Tutorialspoint TP')
print result
result2=pattern.findall('TP is most popular tutorials site of India')
print result2

输出

['TP', 'TP']
['TP']

更新于:02-Nov-2023

12K+ 查看

开启你的 职业

通过完成课程获得认证

开始
广告
© . All rights reserved.