使用python从绑定日志字符串中提取ip地址和其他内容

时间:2013-09-12 12:18:28

标签: python regex

拥有像这样的绑定日志字符串

'09-Sep-2013 10:22:42.540 queries: info: client 10.12.12.66#39177: query: google.com IN AXFR -T (10.10.10.11)\n',

使用正则表达式提取日期,IP地址和查询

re.compile("(.*?) queries.*client (.*?): query: (.*?) IN")

并获得以下输出

[('09-Sep-2013 10:22:42.540','10.12.12.66#39177','google.com')]

几乎很棒,但只是无法摆脱哈希端口关闭ip地址。喜欢这个#39177。也许有人可以帮助我使用正确的模式,它返回没有哈希和端口东西的IP地址。

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试这个(仅在IP地址保存组后添加#\d+):

"(.*?) queries.*client (.*?)#\d+: query: (.*?) IN"

样本:

>>> s = '09-Sep-2013 10:22:42.540 queries: info: client 10.12.12.66#39177: query: google.com IN AXFR -T (10.10.10.11)\n'
>>> re.search("(.*?) queries.*client (.*?)#\d+: query: (.*?) IN", s).groups()
('09-Sep-2013 10:22:42.540', '10.12.12.66', 'google.com')