首页 > 编程语言 > 详细

python 字符串替换、正则查找替换

时间:2018-05-23 21:37:31      阅读:258      评论:0      收藏:0      [点我收藏+]
import re

if __name__ == "__main__":
    url = "  \n   deded<a href = "">这是第一个链接</a><a href = "">这是第二个链接</a> \n      "
    # 去除\n
    one = url.replace("\n", "")
    # 去掉两端空格
    two = one.strip()
    # 正则匹配 re.match从字符串起始处匹配。
    three = re.match(r"(<a ([^>]*?)>)(.*?)(</a>)", two)
    if three is not None:
        arr = three.groups()
        print(arr[0] + "hahaha" + arr[3])

    # 正则查找是否含有</a>,re.search查找整个字符串,只要匹配即可。re.match从字符串开始查找,若开头没有匹配上则认为没有
    result = re.search("</a>", two).group()
    print(result)

    # 正则查找并替换
    print(re.sub(re.compile(r"<a.*?</a>", re.S), "", two))

 

python 字符串替换、正则查找替换

原文:https://www.cnblogs.com/mydesky2012/p/9079476.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!