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

发布时间:2022-12-05 09:32:41 人气:106 作者:多测师

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

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

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

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

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

  实例

  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培训之正则表达式查找和替换内容,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线