使用Jsoup很容易计算特定标签在文本中出现的次数。例如,我正试图查看给定文本中存在多少次锚标记。使用Jsoup存在HTML标签
String content = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>. <p>An <a href='http://example.com/'><b>example</b></a> link.</p>. <p>An <a href='http://example.com/'><b>example</b></a> link.</p>. <p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(content);
Elements links = doc.select("a[href]"); // a with href
System.out.println(links.size());
这给我的4计数如果我有一句话,我想知道,如果句子中包含任何HTML标记或没有,这可能与Jsoup?谢谢。
谢谢。 doc.select(“*”)为我工作,因为我的htmls不包含您提到的标签。但是,是的,我意识到正则表达式会更好地解决这个问题。 – 2013-02-18 18:26:47