使用正则表达式提取链接

时间:2015-06-21 10:54:09

标签: java html regex url

您好我几个星期以来尝试以编程方式获取Java中的Web源代码中的链接但是我在使用正则表达式时遇到问题这是我到目前为止所尝试的内容:

private static StringBuilder a;
public static void main(String[] args) throws IOException
{

    String a = "http://www.mediafire.com/download/w8kvregpyx3zncc/Candy+by+hack94.apk";

    URL yahoo = new URL(a);
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                               yc.getInputStream(), "UTF-8"));
    String inputLine;
    String regExp = "kNO = \"\"(.+?)\"\";";
    Pattern p = Pattern.compile( regExp, Pattern.CASE_INSENSITIVE );

    while ((inputLine = in.readLine()) != null) {
        // Using Oscar's RegEx.
        Matcher m = p.matcher( inputLine );  
        if( m.matches() ) {
            System.out.println( m.group(1) );
        }
    }
}

private static String getUrlSource(String url) throws IOException {
    URL yahoo = new URL(url);
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                               yc.getInputStream(), "UTF-8"));
    String inputLine;
     a = new StringBuilder();
    while ((inputLine = in.readLine()) != null)
        a.append(inputLine);
    in.close();

    return a.toString();
}

包含随机链接的行我必须获取

kNO = "RANDOM_URL";

有谁知道我做错了什么?

0 个答案:

没有答案