编程题:将一个网址的中间部分截取出来,得到alipay.com

解决方案 »

  1.   

    String url = "http://topic.csdn.net/u/20091019/10/f8173f20-5eda-438b-a8ae-17e3ff9ec94e.html?40383";
            String pattern = "(http://)?([^/]*)(/?.*)";
            
            System.out.println(url.replaceAll(pattern, "$2"));
      

  2.   

    private static void p(String s) {
        Pattern p=Pattern.compile("alipay[.]com");
        Matcher m=p.matcher(s);
    while(m.find()){
    System.out.println(m.group());
    System.out.println(m.start());
    }
      

  3.   


    $2 代表第二个子匹配([^/]*),相应的$3代表第三个,子匹配是以左括号“(”开始从左向右匹配的。understand?