我使用以下正则表达式来搜索页面中的视频链接
(http(s?):/)(/[^/]+)\\S+.\\.(?:avi|flv|mp4)
不幸的是,如果链接后面有另一个匹配项,例如此视频链接,它不会在链接末尾停止
<a href="http://somevideo.flv">somevideoname.avi</a>
在正则表达式之后返回:
http://somevideo.flv">somevideoname.avi
如何调整正则表达式来避免这种情况?我想了解更多关于正则表达式的知识,它很有趣,但又很复杂!
最佳答案
以下是如何使用 JSoup 解析器执行类似的操作。
Scanner scanner = new Scanner(new File("input.txt"));
scanner.useDelimiter("\\Z");
String htmlString = scanner.next();
scanner.close();
Document doc = Jsoup.parse(htmlString);
// or to get connect of some page use
// Document doc = Jsoup.connect("http://example.com/").get();
Elements elements = doc.select("a[href]");//find all anchors with href attribute
for (Element el : elements) {
URL url = new URL(el.attr("href"));
if (url.getPath().matches(".*\\.(?:avi|flv|mp4)")) {
System.out.println("url: " + url);
//System.out.println("file: " + url.getPath());
System.out.println("file name: "
+ new File(url.getPath()).getName());
System.out.println("------");
}
}
关于java - 我如何调整这个正则表达式来过滤掉“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19742129/