删除< a href>使用正则表达式标记 [英] Removing <a href > tag using regex

查看:166
本文介绍了删除< a href>使用正则表达式标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从给定的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.

这篇关于删除&lt; a href&gt;使用正则表达式标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆