我检查了这个具体问题,找不到任何问题.我用Java编写一个程序,从网页内容进行分析,所以我需要一个正则表达式,可以剔除掉所有的链接和标签(href,img,等...),这样我就可以只显示纯内容的书面和可见在网页中.非常感谢.
嗨,我想让它更具体:
URLConnection connection = wordURL.openConnection("http://en.wikipedia.org/wiki/Bloom_filter");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String word = "bloom filter";
String regexp2 = word;
Pattern pattern2 = Pattern.compile(regexp2);
String HTML_REGEX = "(<.+?>)+"; // as per your answer(Martijn Courteaux)
while ((line = br.readLine()) != null)
{
String content;
if ( (content = line.replaceAll(HTML_REGEX, "\n") )!= null)
{
Matcher matcher2 = pattern2.matcher(line);
if(matcher2.find())
{
System.out.println(line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,它仍会打印出段落(<p>)标签以及<li带有一些垃圾的标签</li>.我想限制它只显示存在"布隆过滤器"的那些词.再次感谢.
HTML不是常规的,所以你不能用正则表达式做你想要的,但你可以使用JSoup.
jsoup是一个用于处理真实HTML的Java库.它提供了一个非常方便的API,用于提取和操作数据,使用最好的DOM,CSS和类似jquery的方法.
特别是您可能会喜欢其中一个示例中概述的以下内容......
String html = "<div><p>Lorem ipsum.</p>";
Document doc = Jsoup.parseBodyFragment(html);
Element body = doc.body();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
119 次 |
| 最近记录: |