使用正则表达式从邮件头查找IP地址

2024-09-25 00:31:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,我需要抓取IP地址从以下部分的电子邮件标题。在

Received: from smtprelay.b.mail.com (smtprelay0225.b.mail.com. [11.11.11.11])
    by mx.google.com with ESMTP id g7si12282480pat.225.2014.07.26.06.53.24
    for <a@gmail.com>;

我只需要在python中使用正则表达式输出11.11.11.11。在

我们会很感激你的帮助。在

谢谢。在


Tags: fromcomid标题forby电子邮件with
3条回答
(?<=\[)\d+(?:\.\d+){3}(?=\])

试试看这个。用re.findall。在

^{pr2}$

参见演示。在

http://regex101.com/r/gT6kI4/10

似乎您正在尝试获取[]括号内的数据。在

>>> import re
>>> s = """Received: from smtprelay.b.mail.com (smtprelay0225.b.mail.com. [11.11.11.11])
...     by mx.google.com with ESMTP id g7si12282480pat.225.2014.07.26.06.53.24
...     for <a@gmail.com>;"""
>>> re.search(r'(?<=\[)[^\[\]]*(?=\])', s).group()
'11.11.11.11'

或者

^{pr2}$

使用正则表达式

(?<=\[)\d{1,3}(?:\.\d{1,3}){3}(?=\])

提取ip

查看regex的工作原理:http://regex101.com/r/lI0rU3/1

^{pr2}$

相关问题 更多 >