url1: http://www.xxx.com/aaa/bbb/1.html
url2: http://www.xxx.com/aaa/bbb
url3: http://www.xxx.com/ccc.jpg
url4: http://www.xxx.com/ddd.css
用正则匹配以上四种url,只获取url1和url2,不要url3和url4
这样的正则表达式怎么写?

解决方案 »

  1.   

    为什么一定正则表达式呢?
    url::http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? 
      

  2.   

    因为我用urlrewrite必须写正则的。
      

  3.   

    上面的正则报如下错误
    [21:06:08.125] org.tuckey.web.filters.urlrewrite.Conf DEBUG: about to parse conf
    [21:06:08.126] org.tuckey.web.filters.urlrewrite.ConfHandler DEBUG: Resolving to DTD /org/tuckey/web/filters/urlrewrite/dtds/urlrewrite3.2.dtd
    [21:06:08.135] org.tuckey.web.filters.urlrewrite.ConfHandler DEBUG: error: expected name at `='
    [21:06:08.143] org.tuckey.web.filters.urlrewrite.Conf ERROR: Exception loading conf  file:/E:/Project/GIP4/V4.5.0/WebRoot/WEB-INF/urlrewrite.xml:6: expected name at `='
    [21:06:08.143] com.caucho.xml.XmlParseException: file:/E:/Project/WebRoot/WEB-INF/urlrewrite.xml:6: expected name at `='
    [21:06:08.143]  at com.caucho.xml.XmlParser.error(XmlParser.java:2958)
      

  4.   

    可是LZ这样的URL有什么规律呢
      

  5.   

    有规律的。
    只要结尾时html的,或者什么都没有的,不要其他的。
      

  6.   

    没必要非要用正则,可以截最后三位字符串,是tml或者bbb就符合~
      

  7.   

    如果要是用程序的我也知道很简单,但是他的这个框架是不支持写程序的,只能写正则表达式,所以我也没有办法,如果还不行的,我只能自己做urlrewrite了
      

  8.   


    //package com.ricky.www;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;/*
    url1: http://www.xxx.com/aaa/bbb/1.html
    url2: http://www.xxx.com/aaa/bbb
    */public class Test{
    public static void main(String[] args){
    String url1 = "http://www.xxx.com/aaa/bbb/1.html";
    String url2 = "http://www.xxx.com/aaa/bbb";
    test(url1);
    test(url2);
    } public static void test(String url){
    //^https?://[\\w_]+(\\.[\\w_]+)*(\\.html)?$
    String regex = "^https?://[\\w_]+(\\.[\\w_]+)*(/[\\w_]+)*(\\.html)?$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(url);
    if(matcher.matches()){
    System.out.println("true");
    }else{
    System.out.println("false");
    }
    }
    }