删除< a href>使用正则表达式标记 [英] Removing <a href > tag using regex
问题描述
我想从给定的HTML代码中提取纯文本。我尝试使用 regex
并得到了
String target = val.replaceAll(< a。*< / a>,);
。
我的主要要求是删除< a>
和< / a>
(包括链接名称)。使用上面的代码时,所有其他内容也被删除。
< a href =www.google.com> Google< / a>
这是一个Google链接
< a href =www.yahoo.com> Yahoo< / a> / code> 这是一个Yahoo链接
这里我想删除< a>之间的值;
和< / a>
。
最终输出应该是
这是Google链接这是一个Yahoo链接
使用非贪婪量词( *?
)。例如,要完全删除链接:
String target = val.replaceAll(< a。*?< / a,);
或者用链接标签的内容替换链接:
String target = val.replaceAll(< a [^>]>(。*?)< / a>,这是$ 1 Link);
然而,我仍然建议使用适当的DOM操作API。
I want to extract the plain text from given HTML code. I tried using regex
and got
String target = val.replaceAll("<a.*</a>", "");
.
My main requirement is I want remove everything between <a>
and </a>
(including the Link name). While using the above code all other contents also removed.
<a href="www.google.com">Google</a>
This is a Google Link
<a href="www.yahoo.com">Yahoo</a>
This is a Yahoo Link
Here I want to remove the values between <a>
and </a>
.
Final output should
This is a Google Link This is a Yahoo Link
Use a non-greedy quantifier (*?
). For example, to remove the link entirely:
String target = val.replaceAll("<a.*?</a>", "");
Or to replace the link with just the link tag's contents:
String target = val.replaceAll("<a[^>]*>(.*?)</a>", "This is a $1 Link");
However, I would still recommend using a proper DOM manipulation API.
这篇关于删除< a href>使用正则表达式标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!