首页 > 文章列表 > python正则表达式如何匹配内容

python正则表达式如何匹配内容

Python 正则表达式
266 2022-08-07

1、编写Python正则表达式字符串s。

2、使用re.compile()将正则表达式编译成正则对象Patternp。

3、正则对象p调用p.match()或p.fullmatch函数得到匹配对象match m。

4、判断匹配对象m内容是否成功。

实例

import re
 
s = "正则表达式"
p = re.compile(s)
m = p.match("检测的文本")
 
if m:
  print(m.group())

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)