关于python:python正则表达式怎么查找和替换内容

8次阅读

共计 338 个字符,预计需要花费 1 分钟才能阅读完成。

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

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

3、正则对象 p 调用 p.search 或 p.findall 或 p.finditer 查找内容。

4、正则对象 p 调用 p.sub 或 p.subn 替换内容。

实例

import re
 
s = "正则表达式"
p = re.compile(s)
 
# 查找
mf1 = p.search("检测内容")
mf2 = p.findall("检测内容")
mf3 = p.finditer("检测内容")
 
# 替换
ms = p.sub("检测内容")
ms2 = p.subn("检测内容")
 
# 宰割
mp = p.split("检测内容")

以上就是本次分享的全部内容,当初想要学习编程的小伙伴指路微信公众号 -Python 技术大本营,欢送各位的到来哦~

正文完
 0