//几种url
href="http://diy.sina.com.cn/cardshow.php" target="_blank">
href=http://video.sina.com.cn/p/sports/k/v/2011-10-17/092261530255.html target="_blank">
href="http://auto.sina.com.cn/?c=spr_web_sina_sina_www_T001">我想得到href后面的有效url,正则怎么弄?

解决方案 »

  1.   

    public static String getPaperName(String url){
    String res = "";
    Pattern p = Pattern.compile("href=\"?(.*?)[\"|\\s)]");
    Matcher m = p.matcher(url);
    while(m.find()){
    res = m.group(1);
    }
    return res;

    public static void main(String[] args) {
    System.out.println(getPaperName("href=\"http://diy.sina.com.cn/cardshow.php\" target=\"_blank\">"));
    System.out.println(getPaperName("href=http://video.sina.com.cn/p/sports/k/v/2011-10-17/092261530255.html target=\"_blank\">"));
    System.out.println(getPaperName("href=\"http://auto.sina.com.cn/?c=spr_web_sina_sina_www_T001\">"));
    }
      

  2.   

    http(s)?://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?
      

  3.   

    [code=Java]<SCRIPT LANGUAGE="javascript">
    <!--function prints(str) {var matchPIC = new RegExp("((?:http|https|ftp|mms|rtsp)://(&(?=amp;)|[A-Za-z0-9\./=\?%_~@#:;\+\-])+(gif|jpg|png))", "ig");
    var matchURL = new RegExp("((?:http|https|ftp|mms|rtsp)://(&(?=amp;)|[A-Za-z0-9\./=\?%_~@&#:;\+\-])+)","ig");
    var str2 ="";
    str2 =str.replace("&nbsp;"," ")
        if(matchPIC.test(str)){                    
             document.write(str2.replace(matchPIC, "<img src=\"$1\" hint=\"$1\"></img>"));     
        }else{        
               document.write(str2.replace(matchURL, "<a target=\"_blank\" href=\"$1\">$1</a>"));
        }
    }
    //-->
    </SCRIPT>code]