【问题标题】:How do I remove all <…> inside an HTML file besides a specific tag? [closed]除了特定标签之外,如何删除 HTML 文件中的所有 <...> ? [关闭]
【发布时间】:2016-03-21 09:10:02
【问题描述】:

请参阅我有以下 HTML 文件。我想删除除

之外的所有标签

&lt;A href="MarineMammal.html"&gt;marine mammals.&lt;/A&gt;

我能够删除所有标签,但不知道如何保留特定标签。我希望能够得到上面标签周围的单词。这些词不应包含标签。谢谢!

<TITLE> Whale </TITLE>
<H2> Whale </H2>
(from Wikipedia)

<p>
Whale is the common name for a widely distributed and diverse group of 
fully aquatic placental 
<A href="MarineMammal.html">marine mammals.</A>. They are an informal grouping 
within the infraorder <A href="Cetacean.html">Cetacea,</A> usually excluding 
<A href="Dolphin.html">dolphins</A> and 
<A href="Porpoise.html">porpoises.</A> 
Whales, dolphins and porpoises belong to the order Cetartiodactyla with 
even-toed 
<A href="Ungulate.html">ungulates</A> and their 
closest living relatives are the 
<A href="Hippopotamus.html">hippopotamuses,</A> having 
diverged about 40 million years ago. 

【问题讨论】:

  • 您尝试过什么吗,请显示一些代码,到目前为止您尝试过什么?在 java 中解析 html 有很多很好的答案。这听起来像你想要的:stackoverflow.com/questions/240546/…
  • 对不起,我可以在几分钟前发表评论,因为没有为 SO 启用 javascript。
  • 如果你解释你想要实现什么,它会更容易帮助你

标签: java html string parsing


【解决方案1】:

this question 中已经讨论了很多方法来做到这一点


最简单的可能是this one:

replaceAll("\\<[^>]*>","")

【讨论】:

  • 那个正则表达式不好 - HTML 不需要在引用的属性值内转义 &gt;。这个更好:&lt;([^"'&gt;]|"[^"]*"|'[^']*')+&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
相关资源
最近更新 更多