在匹配模式时遇到问题

时间:2013-07-16 06:03:15

标签: android pattern-matching

我正在开发Music Downloader应用程序,因为我必须阅读网站的整个网页,然后需要显示它的结果。所以我读了整个网络的源代码。现在歌曲名称和URL可以在html页面中如下:

HTML代码:

<div id="right_song">
    <div style="font-size:15px;"><b>Name of song</b></div>
    <div style="clear:both;"></div>
    <div style="float:left;">
    <div style="float:left; height:27px; font-size:13px; padding-top:2px;">
    <div style="float:left;"><a href="link of song" rel="nofollow" target="_blank" style="color:green;">Download</a>

所以我想到使用Pattern of this然后匹配它来获取名称和URL。

我制作了以下模式:

<div id=\"right_song\">
<div style=\"font-size:15px;\"><b>([^<]*)</b></div>
<div style=\"clear:both;\"></div>
<div style=\"float:left;\">
<div style=\"float:left; height:27px; font-size:13px; padding-top:2px;\">
<div style=\"float:left;\"><a href=\"([^\"]*)\" rel=\"nofollow\" target=\"_blank\" style=\"color:green;\">Download</a>

但是它总是在没有图案的情况下返回,所以我出错了。

请引导我完善它。

1 个答案:

答案 0 :(得分:0)

纯粹基于这个例子。看起来像终止线问题+ SO的4个空格引用..

/<div id=\"right_song\">[\r\n\t\ ]+<div style=\"font-size:15px;\"><b>([^<]*)</b></div>[\r\n\t\ ]+<div style=\"clear:both;\"></div>[\r\n\t\ ]+<div style=\"float:left;\">[\r\n\t\ ]+<div style=\"float:left; height:27px; font-size:13px; padding-top:2px;\">[\r\n\t\ ]+<div style=\"float:left;\"><a href=\"([^\"]*)\" rel=\"nofollow\" target=\"_blank\" style=\"color:green;\">Download</a>/

然而,有更短/更安全的grep'ing模式,允许第三方改变微妙的CSS细节

/<div[^>]+>[\r\n\t\ ]+<div[^>]+><b>([^<]*)</b></div>[\r\n\t\ ]+<div[^>]+></div>[\r\n\t\ ]+<div[^>]+>[\r\n\t\ ]+<div[^>]+>[\r\n\t\ ]+<div[^>]+><a href=\"([^\"]*)\"[^>]+>Download</a>/