尝试使用Java正则表达式在一个HTML目录链接列表解析 [英] Trying to parse links in an HTML directory listing using Java regex

查看:152
本文介绍了尝试使用Java正则表达式在一个HTML目录链接列表解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道每个人都会告诉我,不要使用正则表达式解析HTML,但我在Android编程和没有一个HTML解析器随时访问(即我所知道的)。此外,这是服务器生成的HTML它的的比用户生成的HTML更加一致。

Ok I know everyone is going to tell me not to use RegEx for parsing HTML, but I'm programming on Android and don't have ready access to an HTML parser (that I'm aware of). Besides, this is server generated HTML which should be more consistent than user-generated HTML.

正则表达式如下:

Pattern patternMP3 = Pattern.compile(
        "<A HREF=\"[^\"]+.+\\.mp3</A>",
        Pattern.CASE_INSENSITIVE |
        Pattern.UNICODE_CASE);
Matcher matcherMP3 = patternMP3.matcher(HTML);
while (matcherMP3.find()) { ... }

输入HTML是全部在一行,这是造成问题。当HTML是在单独的行这种模式的工作原理。有什么建议?

The input HTML is all on one line, which is causing the problem. When the HTML is on separate lines this pattern works. Any suggestions?

推荐答案

正则表达式

"<A HREF=\"([^\"]+)\"[^>]*>([^<]+?)\\.mp3</A>"

应该符合你的链接,并在其组织的链接和文件名。
但请注意,这的href的参数不neccesarily需要包含在HTML中的引号。 (或者,如果它需要,既不浏览器也没有开发人员知道=)。 )

should match your links, and have the link and the filename in its groups. Note though, that the argument of href does not neccesarily need to be enclosed in quotes in html. (Or, if it needs to be, neither browsers nor developers know that =). )

这篇关于尝试使用Java正则表达式在一个HTML目录链接列表解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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