我需要在python中创建一个正则表达式,它可以采用下面的示例并拆分每个日志条目。我使用日期作为一种方法来标识每个日志条目的开始,但是它只能从日期开始到第一行末尾的一行。它完全忽略了堆栈跟踪的所有内容。我想要所有的日志条目,因为有很多重复的日志记录,我希望能够过滤掉重复的内容,并将其减少到少数几个唯一的日志条目。我还希望能够在识别了日志条目之后删除字符串(如日期时间戳)的任何唯一性,以便比较函数可以将其标记为重复项。我尝试过使用正lookaheads和多行标志,但是没有用。有人知道我想做什么吗?在
我试过的一些正则表达式
^\d{4}-\d{2}-\d{2}.*\(.*\)$ // it matches single line date to parenthesis
^(\d{4}-\d{2}-\d{2}|\s|).*\)$ // matches single line with tabs - not much better
^\d{4}-\d{2}-\d{2}.*(?=\d{4}-\d{2}-\d{2}) // positive lookahead but barely works
示例字符串:
^{pr2}$期望输出:
匹配1:
INFO:Starting. (com.X.s.f.o.o)
匹配2:
SEVERE: Error attempting to s: StatusRuntimeException (com.Y.W.Z_H.ZHGC.sHToVe)
io.G.StatusRuntimeException: EXCEEDED
at io.G.stub.CCalls.toStatusRuntimeException(CCalls.java:227)
at io.G.stub.CCalls.getUnchecked(CCalls.java:208)
at io.G.stub.CCalls.blockingUnaryCall(CCalls.java:141)
匹配3:
SEVERE: Error attempt: StatusRuntimeException (com.Y.W.Z_H.ZHGC.sHToVe)
io.G.StatusRuntimeException: EXCEEDED
at io.G.stub.CCalls.toStatusRuntimeException(CCalls.java:227)
at io.G.stub.CCalls.getUnchecked(CCalls.java:208)
at io.G.stub.CCalls.blockingUnaryCall(CCalls.java:141)
匹配4:
SEVERE: getConfigInteger(): eGSWindowsPortNumber (com.Y.W.Y_Z_config_s.YZConfigs.getInteger)
匹配5:
SEVERE: Failed to get (com.Y.W.Z_H.ZHGC.create)
无需尝试将整个字符串与regex匹配,您只需匹配日期并使用它将字符串分隔到所需的日志中:
yield_matches
将把每一行添加到名为log
的列表中,直到找到另一个日期。当它找到日期时,^{输出如下:
^{pr2}$我在阅读了以下几条信息后,才明白了这一点:
python: multiline regular expression
https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch02s08.html
如果以下正则表达式以日期},直到第一次找到另一个日期条目
^\d{4}-\d{2}-\d{2}
开头,并继续向前看{.+?
,并将其作为匹配项返回。这与多行字符串匹配!:天下面的正则表达式将执行与@Sean Breckenridge的解决方案相同的操作,但这次要删除我要删除的字符串的唯一部分。非常有用!在
^{pr2}$相关问题 更多 >
编程相关推荐