gpt4 book ai didi

regex - 用于检索日志文件中前一行的正则表达式

转载 作者:行者123 更新时间:2023-12-03 23:38:08 26 4
gpt4 key购买 nike

我的日志文件包含以下内容:

2009-03-12T12:44:27+0000 something was logged
2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times


我可以编写一个正则表达式来检索带有“重复最后一条消息...”语句的行,但是,如果不检索它之前的行,该行就毫无意义。

话虽如此,有没有人知道一个正则表达式,它可以让我在检测到“最后一条消息重复...”语句时检索两行?

最佳答案

编辑为 2 组匹配正则表达式。你可以试试:RegexLib

不那么优化,但这个:

([\r\n].*?)(?:=?\r|\n)(.*?(?:last message repeated).*)

应该努力从这样的事情中得到结果:
2009-03-12T12:44:27+0000 something1 was logged
2009-03-12T12:44:27+0000 something2 was logged
2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times
2009-03-12T12:44:27+0000 something3 was logged
2009-03-12T12:44:27+0000 something4 was logged
2009-03-12T12:44:27+0000 something5 was logged
2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times

导致:
Matches
First Match, First Group: 2009-03-12T12:44:27+0000 something2 was logged
First Match, Second Group: 2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times
Second Match, First Group: 2009-03-12T12:44:27+0000 something5 was logged
Second Match, Second Group: 2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times

关于regex - 用于检索日志文件中前一行的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/639814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com