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))
原文地址:https://www.cnblogs.com/mydesky2012/p/9079476.html
时间: 2024-10-30 21:01:46