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()){
Stringstr=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); }
原文链接:/regex/361264.html