各位版友,请教一个关于java正则表达式的问题:
218.4.60.214 passport.99fund.com - [23/Aug/2011:15:10:34 +0800] "GET /servlet/buildimageservlet HTTP/1.1" 200 1342 "https://trade.99fund.com/index.jsp?utm_source=n100821020" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; znwb6300)" "JSESSIONID1=TTRHgwnrpvThlbtlGGW0Wv6kc6JrLGnGDGHnJhPnvZJLHDn34w44!-1839838299; utm_source=n110729001; sso_cookie=%2FaeNSbZlrc3ovm0HMv1NPQcsP43VuHZqd0Y6qDkNRsOpLcxna2btP%2BwMDUMXZl8o; ec_custid=3041221"针对上面这个字符串,我怎么用正则表达式把标红的字段显示出来啊,等号后面是参数值,也就是我要提取时间戳,jsessionid1后面的utm_source参数值和ec_custid参数值,url当中的utm_source值不需要提取。

解决方案 »

  1.   

    这是解析http协议的header,自己split去。
      

  2.   

    如果说 字符串格式比较统一 用indexof(String str)
      

  3.   

    是不是直接String变量查ut哪里有m_source和ec_custid 字串就可以了  至于时间 找到第一个有中括号的地方就可以了..  我觉得不要用正则
      

  4.   

    String str = "218.4.60.214 passport.99fund.com - [23/Aug/2011:15:10:34 +0800] \"GET /servlet/buildimageservlet HTTP/1.1\" 200 1342 \"https://trade.99fund.com/index.jsp?utm_source=n100821020\" \"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; znwb6300)\" \"JSESSIONID1=TTRHgwnrpvThlbtlGGW0Wv6kc6JrLGnGDGHnJhPnvZJLHDn34w44!-1839838299; utm_source=n110729001; sso_cookie=%2FaeNSbZlrc3ovm0HMv1NPQcsP43VuHZqd0Y6qDkNRsOpLcxna2btP%2BwMDUMXZl8o; ec_custid=3041221\"";
    Pattern pattern1 = Pattern.compile("\\[.*\\]");
    Pattern pattern2 = Pattern.compile("utm_source=\\w*");
    Pattern pattern3 = Pattern.compile("ec_custid=\\w*");
    Matcher matcher = pattern1.matcher(str);
    if (matcher.find()) {
    System.out.println(matcher.group());
    }
    matcher = pattern2.matcher(str);
    if (matcher.find()) {
    System.out.println(matcher.group());
    }
    matcher = pattern3.matcher(str);
    if (matcher.find()) {
    System.out.println(matcher.group());
    }
      

  5.   


    String test = "218.4.60.214 passport.99fund.com - [23/Aug/2011:15:10:34 +0800]"
            + " \"GET /servlet/buildimageservlet HTTP/1.1\""
            + " 200 1342 \"https://trade.99fund.com/index.jsp?utm_source=n100821020\" "
            + "\"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; znwb6300)\" "
            + "\"JSESSIONID1=TTRHgwnrpvThlbtlGGW0Wv6kc6JrLGnGDGHnJhPnvZJLHDn34w44!-1839838299; utm_source=n110729001; sso_cookie=%2FaeNSbZlrc3ovm0HMv1NPQcsP43VuHZqd0Y6qDkNRsOpLcxna2btP%2BwMDUMXZl8o; ec_custid=3041221\"";
    Matcher m = Pattern.compile(
            "\\[(.*)\\].*;\\s*utm_source=(.*?);\\s*sso_cookie=(.*?);")
            .matcher(test);
    if (m.find()) {
    System.out.println("timestamps = " + m.group(1));
    System.out.println("utm_source = " + m.group(2));
    System.out.println("sso_cookie = " + m.group(3));
    }/*
    结果
    timestamps = 23/Aug/2011:15:10:34 +0800
    utm_source = n110729001
    sso_cookie = %2FaeNSbZlrc3ovm0HMv1NPQcsP43VuHZqd0Y6qDkNRsOpLcxna2btP%2BwMDUMXZl8o
    */
      

  6.   

    哦  , 看错了 我以为要sso_cookie呢, 改一下成ec_custidString test = "218.4.60.214 passport.99fund.com - [23/Aug/2011:15:10:34 +0800]"
            + " \"GET /servlet/buildimageservlet HTTP/1.1\""
            + " 200 1342 \"https://trade.99fund.com/index.jsp?utm_source=n100821020\" "
            + "\"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; znwb6300)\" "
            + "\"JSESSIONID1=TTRHgwnrpvThlbtlGGW0Wv6kc6JrLGnGDGHnJhPnvZJLHDn34w44!-1839838299; utm_source=n110729001; sso_cookie=%2FaeNSbZlrc3ovm0HMv1NPQcsP43VuHZqd0Y6qDkNRsOpLcxna2btP%2BwMDUMXZl8o; ec_custid=3041221\"";
    Matcher m = Pattern.compile(
            "\\[(.*)\\].*;\\s*utm_source=(.*?);.*?;\\s*ec_custid=(\\d+)")
            .matcher(test);
    if (m.find()) {
    System.out.println("timestamps = " + m.group(1));
    System.out.println("utm_source = " + m.group(2));
    System.out.println("ec_custid = " + m.group(3));
    }
    /*
    结果
    timestamps = 23/Aug/2011:15:10:34 +0800
    utm_source = n110729001
    ec_custid = 3041221
    */
      

  7.   

    额,还真有直接上代码的。http header就是; 分割的', '这是规则。
      

  8.   

    嗯,主要是UTM_SOURCE和EC_CUSTID字段的参数值长度是不定的,不用正则的话比较麻烦吧?
      

  9.   


    public static void main(String[] args) {
    String test = "218.4.60.214 passport.99fund.com - [23/Aug/2011:15:10:34 +0800]"
    + " \"GET /servlet/buildimageservlet HTTP/1.1\""
    + " 200 1342 \"https://trade.99fund.com/index.jsp?utm_source=n100821020\" "
    + "\"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; znwb6300)\" "
    + "\"JSESSIONID1=TTRHgwnrpvThlbtlGGW0Wv6kc6JrLGnGDGHnJhPnvZJLHDn34w44!-1839838299; utm_source=n110729001; sso_cookie=%2FaeNSbZlrc3ovm0HMv1NPQcsP43VuHZqd0Y6qDkNRsOpLcxna2btP%2BwMDUMXZl8o; ec_custid=3041221\"";
    Matcher m = Pattern.compile("\\[(.*)\\].*\"JSESSIONID1=.*?utm_source=(.*?);.*?ec_custid=(.*?)[;\"]")
    .matcher(test);
    if (m.find()) {
    System.out.println("timestamps = " + m.group(1));
    System.out.println("utm_source = " + m.group(2));
    System.out.println("sso_cookie = " + m.group(3));
    }
    }