python正则表达式查找和替换内容

Python (228) 2023-06-09 21:06:55

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

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

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

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

实例

纯文本
复制到剪贴板
在新窗口中打开代码
EnlighterJS 3 Syntax Highlighter
importre
s="正则表达式"
p=re.compile(s)
#查找
mf1=p.search("检测内容")
mf2=p.findall("检测内容")
mf3=p.finditer("检测内容")
#替换
ms=p.sub("检测内容")
ms2=p.subn("检测内容")
#分割
mp=p.split("检测内容")
importre s="正则表达式" p=re.compile(s) #查找 mf1=p.search("检测内容") mf2=p.findall("检测内容") mf3=p.finditer("检测内容") #替换 ms=p.sub("检测内容") ms2=p.subn("检测内容") #分割 mp=p.split("检测内容")
importre

s="正则表达式"
p=re.compile(s)

#查找
mf1=p.search("检测内容")
mf2=p.findall("检测内容")
mf3=p.finditer("检测内容")

#替换
ms=p.sub("检测内容")
ms2=p.subn("检测内容")

#分割
mp=p.split("检测内容")

以上就是python正则表达式查找和替换内容,希望对大家有所帮助。

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

THE END

发表回复