Pattern正则

前端之家收集整理的这篇文章主要介绍了Pattern正则前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

['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']
@H_403_1@

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

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

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

Stringstr=matcher.group(x);@H_403_1@

}@H_403_1@

----------------------------------------------------------------------------------@H_403_1@

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); } @H_403_1@

猜你在找的正则表达式相关文章