正则表达式仅用锚文本替换具有特定 class 的链接
Regex to replace links that have a certain class only with anchor text
我有一个 HTML 数量有限的 CSV 文件,只有纯文本(标题和段落)和一些链接,所以请不要建议解析而不是正则表达式。 :)
下面是出现在里面的两种类型的链接:
<a href="http://www.example.com/1" class="linkclass" title="linktitle">anchor text 1</a>
<a href="http://www.example.com/2">anchor text 2</a>
与 class 的链接在此示例中始终具有相同的 class - "linkclass"。它们也总是具有相同的结构:
<a href="X" class="Y" title="Z">anchor text 1</a>
我只需要 select 具有 class "linkclass" 的链接,删除它们并只留下锚文本。其他链接应保持不变。最终结果应该是这样的:
anchor text 1
<a href="http://www.example.com/2">anchor text 2</a>
这是我用于 select 这些链接的代码,但它也 select 锚文本:
<a href="(.*?)" class="linkclass" title="(.*?)">(.*?)</a>
如何调整这个正则表达式而不是 select 锚文本?
给你:
<a\b[^<]*class=['"]?linkclass["']?\b[^<]*?>([^<]*)<\/a>
替换为
。
见demo
对于记事本++:
点击 CTRL + H
选择替换
然后正则表达式在最下面
查找:
<a.*?linkclass.*?>(.*?)</a>
替换:
结果:
anchor text 1
<a href="http://www.example.com/2">anchor text 2</a>
我有一个 HTML 数量有限的 CSV 文件,只有纯文本(标题和段落)和一些链接,所以请不要建议解析而不是正则表达式。 :)
下面是出现在里面的两种类型的链接:
<a href="http://www.example.com/1" class="linkclass" title="linktitle">anchor text 1</a>
<a href="http://www.example.com/2">anchor text 2</a>
与 class 的链接在此示例中始终具有相同的 class - "linkclass"。它们也总是具有相同的结构:
<a href="X" class="Y" title="Z">anchor text 1</a>
我只需要 select 具有 class "linkclass" 的链接,删除它们并只留下锚文本。其他链接应保持不变。最终结果应该是这样的:
anchor text 1
<a href="http://www.example.com/2">anchor text 2</a>
这是我用于 select 这些链接的代码,但它也 select 锚文本:
<a href="(.*?)" class="linkclass" title="(.*?)">(.*?)</a>
如何调整这个正则表达式而不是 select 锚文本?
给你:
<a\b[^<]*class=['"]?linkclass["']?\b[^<]*?>([^<]*)<\/a>
替换为。
见demo
对于记事本++:
点击 CTRL + H
选择替换
然后正则表达式在最下面
查找:
<a.*?linkclass.*?>(.*?)</a>
替换:
结果:
anchor text 1
<a href="http://www.example.com/2">anchor text 2</a>