Pattern正则

来源:互联网 发布:js css压缩工具推荐 编辑:程序博客网 时间:2024/06/06 04:18

nginx的日志经常是用引号把字段抱起来,如下:

['UfaNode" "10.4.3.175" "10.4.1.51:31002" "[18/Sep/2014:13:00:01 +0800]" "0.110" "0.110" "-" "200" "Ok" "163" "102400" "xiaomi" "thumbnail" "-" "POST /ufa_new/downloadBlock HTTP/1.1" "Python-urllib/2.6']

我们在解析的时候,就需要使用正则去解析:

Pattern pattern = Pattern.compile("\"([^\"]+)\" \"([^\"]+)\" \"([^\"]+)\" \"([^\"]+)\" \"([^\"]+)\" \"([^\"]+)\" \"([^\"]+)\" \"([^\"]+)\" \"([^\"]+)\" \"([^\"]+)\"" +
" \"([^\"]+)\"");

Matcher matcher = pattern.matcher(text);
if(matcher.find()){

String str=matcher.group(x);   

}

----------------------------------------------------------------------------------

public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p = Pattern.compile("\"(.*?)\"");
String s = new String("\"aaa\" \"bbb\" \"ccc d, ee\" ").trim();
System.out.println(s);
Matcher m = p.matcher(s);

while(m.find()){
System.out.println(m.group());
}

System.out.println(m);

}

0 0
原创粉丝点击