从字符串中提取电子邮件
Posted
技术标签:
【中文标题】从字符串中提取电子邮件【英文标题】:Extract email from string 【发布时间】:2022-01-13 04:07:45 【问题描述】:我有一个字符串:
string="""Recipient:
nolo.lamb@golab.com \--- mail_boundary --- ATTENTION: This email came from an
external source.
Sender: nlrt@vloh.net Subject: [External] *LEGALZOOM OPENS AT $30, IPO
AT $28 Message-Id: <60DC94E60001AE8432F70080_0_2129298@mscv03>
Recipient: Nichole.wen@golab.com \--- mail_boundary --- """
我只想提取与关键字 收件人: 对应的电子邮件 ID,即 email_id=['nolo.lamb@golab.com','Nichole.wen@golab.com']
我尝试过的:
email_id=re.findall(r'Recipient: (.+)',string)
【问题讨论】:
参考这里:***.com/questions/17681670/… @gretal 已经检查过了。它以字符串形式提供所有电子邮件 ID。我只想要与关键字Recipient:
对应的那些
【参考方案1】:
你可以使用
re.findall(r'\bRecipient:\s*(\S+@\S+)', text)
请参阅regex demo。 详情:
\b
- 字边界
Recipient:
- 固定字符串
\s*
- 零个或多个空格
(\S+@\S+)
- 第 1 组:一个或多个非空白字符,@
和一个或多个非空白字符
见Python demo:
import re
text = """Recipient:
nolo.lamb@golab.com \--- mail_boundary --- ATTENTION: This email came from an
external source.
Sender: nlrt@vloh.net Subject: [External] *LEGALZOOM OPENS AT $30, IPO
AT $28 Message-Id: <60DC94E60001AE8432F70080_0_2129298@mscv03>
Recipient: Nichole.wen@golab.com \--- mail_boundary --- """
print( re.findall(r'\bRecipient:\s*(\S+@\S+)', text) )
# => ['nolo.lamb@golab.com', 'Nichole.wen@golab.com']
【讨论】:
以上是关于从字符串中提取电子邮件的主要内容,如果未能解决你的问题,请参考以下文章