• 技术文章 >Python技术 >Python基础教程

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

    小妮浅浅小妮浅浅2021-11-09 20:44:41原创21766

    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正则表达式查找和替换内容,希望对大家有所帮助。更多Python学习指路:python基础教程

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

    专题推荐:python 正则表达式
    品易云
    上一篇:python正则表达式如何匹配内容 下一篇:EOFError:EOF when reading a line

    相关文章推荐

    • 什么是网络协议• python中的去除重复项的操作• python中少见的函数map()和partial()• python的sort()排序方法• Python中的文件读写-理论知识

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网