Python text regex replacement

Replace <a></a> tags with regex

import re

text = "aaa<a href=\"http://www.baidu.com\">这是第一个链接</a>bbbb<a href=\"https://blog.csdn.net\">这是第二个链接</a>cccc"

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

print result:

aaabbbbcccc

Use regex to find matches

import re

text = "aaa<a href=\"http://www.baidu.com\">这是第一个链接</a>bbbb<a href=\"https://blog.csdn.net\">这是第二个链接</a>cccc"

# 查看下匹配到什么
text = re.compile(r"<a.*?>(.*?)</a>").findall(text)
print(text)

print result: 

['This is the first link', 'This is the second link'] 

Guess you like

Origin blog.csdn.net/watson2017/article/details/122722819