gpt4 book ai didi

使用通配符选项的 Python 搜索日志

转载 作者:行者123 更新时间:2023-12-02 19:25:23 25 4
gpt4 key购买 nike

我有一个非常大的 netflow 数据集,如下所示:

192.168.1.3  www.123.com
192.168.1.6 api.123.com
192.168.1.3 blah.123.com
192.168.1.3 www.google.com
192.168.1.6 www.xyz.com
192.168.1.6 test.xyz.com
192.168.1.3 3.xyz.co.uk
192.168.1.3 www.blahxyzblah.com
....

我还有一个小得多的通配符域数据集,如下所示:

*.xyz.com
api.123.com
...

我希望能够使用 python 搜索我的数据集并找到所有匹配项。因此,在上面的示例中,我将匹配:

192.168.1.6  www.xyz.com
192.168.1.6 test.xyz.com
192.168.1.6 api.123.com

我尝试使用 re 模块,但无法使其匹配任何内容。

for f in offendingsites:
for l in logs:
if re.search(f,l):
print(l)

最佳答案

您的违规网站不是正则表达式,而是 shell 通配符。但是,您可以使用 fnmatch.translate将它们转换为正则表达式:

for f in offendingsites:
r = fnmatch.translate(f)
for l in logs:
if re.search(r, l):
print(l)

关于使用通配符选项的 Python 搜索日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62485695/

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