【发布时间】:2021-08-05 00:00:51
【问题描述】:
我正在解析一个日志文件并试图找到一个以给定模式(比如Start log )开头并以两种模式之一(比如exit code \d. 或took \d* seconds.)结束的子字符串,以较晚者为准。
我尝试了以下方法但没有成功:
block_regex1 = re.compile('Start log .*?(exit code \d.|took \d* seconds.)', re.DOTALL)
block_regex2 = re.compile('Start log .*? exit code \d.|Start log .*? took \d* seconds.)', re.DOTALL)
block_regex.findall(log)
示例日志文件:
Start log 1
doing stuff
Finished with exit code 1.
Start log 2
doing stuff
Finished with exit code 0.
log 2 took 12 seconds.
Start log 3
doing stuff
Finished with exit code 0.
log 3 took 10 seconds.
Start log 4
doing stuff
Finished with exit code 1.
使用上面的代码,它应该输出一个列表:
Start log 1 doing stuff Finished with exit code 1.Start log 2 doing stuff Finished with exit code 0. log 2 took 12 seconds.- ...
最终,我想获取日志 ID、退出代码以及以秒为单位的时间(如果存在)。我想我可以使用组来实现这一点,但仍在研究它。
【问题讨论】: