Pattern正则

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

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

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