gpt4 book ai didi

Python : Regex search on a file, 和下一行中的另一个正则表达式

转载 作者:行者123 更新时间:2023-12-01 04:55:00 24 4
gpt4 key购买 nike

我正在尝试在日志文件的每一行中搜索特定字符串,如果匹配,我需要能够从该特定错误中获取主机信息。

考虑如下日志条目:

05-05-2014 00:02:02,771 [HttpProxyServer-thread-1314] ERROR fd - Empty user name specified in NTLM authentication. Prompting for auth again.
Host=tools.google.com, Port=80, Client ip=/10.253.168.128, port=37271, User-Agent: Google Update/1.3.23.9;winhttp;cup-ecdsa
05-05-2014 00:02:02,771 [HttpProxyServer-thread-2156] ERROR fd - Empty user name specified in NTLM authentication. Prompting for auth again.
Host=tools.google.com, Port=80, Client ip=/10.253.168.148, port=37273, User-Agent: Google Update/1.3.23.9;winhttp;cup-ecdsa
05-05-2014 00:02:02,802 [HttpProxyServer-thread-604] ERROR fd - Empty user name specified in NTLM authentication. Prompting for auth again.
Host=tools.google.com, Port=80, Client ip=/10.253.168.92, port=37280, User-Agent: Google Update/1.3.23.9;winhttp;cup

这是我的代码:

for line in log_file:

if bool(re.search( r'Empty user name specified in NTLM authentication. Prompting for auth again.', line)):

host = re.search(r'Host=(\D+.\D+.\D+,)', line).group(1)

问题是主机信息与错误不在同一行。它在下一行。如何让 re.search(r'Host=(\D+.\D+.\D+,)', line).group(1) 在“line”当前所在的下一行中进行搜索?

最佳答案

只需插入一个

line = next(log_file)

当前在 for 循环中的两条语句之间。

关于Python : Regex search on a file, 和下一行中的另一个正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633006/

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