使用正则表达式解析日志

使用正则表达式解析日志,第1张

使用正则表达式解析日志

我想出了一种根据可能的/期望的值从与各个字段匹配的块中构建正则表达式的方法。

    String rexa = "(\d+(?:\.\d+){3})";  // an IP address    String rexs = "(\S+)";     // a single token (no spaces)    String rexdt = "\[([^\]]+)\]";      // something between [ and ]    String rexstr = ""([^"]*?)"";       // a quoted string    String rexi = "(\d+)";     // unsigned integer    String rex = String.join( " ", rexa, rexs, rexs, rexdt, rexstr,        rexi, rexi, rexstr, rexstr );    Pattern pat = Pattern.compile( rex );    Matcher mat = pat.matcher( h );    if( mat.matches() ){        for( int ig = 1; ig <= mat.groupCount(); ig++ ){ System.out.println( mat.group( ig ) );        }    }

当然,可以用rex代替rexa或rexi。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5430538.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-11
下一篇2022-12-11

发表评论

登录后才能评论

评论列表(0条)

    保存