我的情况是我有以下这样的内容
This is a <a href="https://www.test.com">test1</a>. This is <a href="/node/1">test2</a>. This is <a href="https://nct.com">test3</a>. This is a <a href="www.test.com">test4</a>. This is a <a href="http://test.com">test5</a>.
nct.com是我的网站。我不想删除包含在标记内的链接和文本。所以是/ node / 1。
我期待的输出是
This is a test1. This is <a href="/node/1">test2</a>. This is <a href="https://nct.com">test3</a>. This is a test4. This is a test5.
对于像test.com这样的外部网站,我想要一个标签内容而不删除包含在标签内的文本。
我正在使用的正则表达式是
#<a [^>]*\bhref=(['"])http.?://((?<!mywebsite)[^'"])+\1 *.*?</a>#i
这将删除标签内容以及标签内的文本。
答案 0 :(得分:0)
我创建了一个正如我认为你需要的正则表达式:
/<a [^>]*\bhref=(['"])((https?:\/\/|www.)((?!nct\.com).)(.*?))['"]*\b<\/a>/
答案 1 :(得分:0)
你可以试试这个:
import re
s = 'This is a <a href="https://www.test.com">test1</a>. This is <a href="/node/1">test2</a>. This is <a href="https://nct.com">test3</a>. This is a <a href="www.test.com">test4</a>. This is a <a href="http://test.com">test5</a>.'
final_list = [re.findall("^[a-zA-Z\s]+", i)[0]+re.findall('com">(.*?)</a>', i)[0] if "nct.com" not in i and "node" not in i else i for i in re.split("\.\s(?=This)", s)]
输出:
['This is a test1', 'This is <a href="/node/1">test2</a>', 'This is <a href="https://nct.com">test3</a>', 'This is a test4', 'This is a test5']