0

一段html代码:

<a class="context_link" href="/thuc-don/41-Thit-vit-ram-sa-gung.html">
        <img src="http://monngonmoingay.com/uploads/monan/201205170430310000000_thit" +
                "-vit-ram-sa-gung-48aq570.png" alt="Thịt vịt ram sả gừng " />

所以我使用正则表达式从代码中获取链接:

String pat = "<a\\s+class=\"context_link\"\\s+href=\"(.+)\"";       
   Pattern pattern = Pattern.compile(pat,Pattern.DOTALL | Pattern.UNIX_LINES);
   Matcher math = pattern.matcher(source);
   while(math.find()){Log.i("Value",math.group(1));}

当我检查是否匹配时,结果总是错误的。

谁能帮我修复错误呢?

4

1 回答 1

0

如果您尝试提取 HREF,那么您应该使用Jsouplibrary

现在工作示例:

import java.io.IOException;

import org.jsoup.Jsoup;

public class Test {

    public static void main(String args[]) throws IOException {

        String source = "<a class=\"context_link\" href=\"/thuc-don/41-Thit-vit-ram-sa-gung.html\">        <img src=\"http://monngonmoingay.com/uploads/monan/201205170430310000000_thit\" +                \"-vit-ram-sa-gung-48aq570.png\" alt=\"Th?t v?t ram s? g?ng \" />";
        String link = Jsoup.parse(source).select("a").first().attr("href");
        System.out.println("Your link  :" + link);

    }

}
于 2013-10-08T08:15:59.717 回答