2

我正在尝试使用正则表达式从一串文本中获取电子邮件地址。

如何让我的简单代码仅提取电子邮件地址而不是整行?

demo_text = """hsds hjdsjd ksdkj

Reason: 550 abc@gmail.com... No such user

sdhjsdjh
"""
# the following code extracts the whole line "Reason: 550 abc@gmail.com... No such user"
# how do I just extract "abc@gmail.com"?
email = re.search("Reason: 550 (.+)... No such user", demo_text).group(0)
4

4 回答 4

8

.group(0)返回整个字符串。你想要.group(1)

email = re.search("Reason: 550 (.*?)... No such user", demo_text).group(1)
于 2012-05-26T02:55:48.577 回答
2

取而代之的是检索组 1。

....group(1)
于 2012-05-26T02:55:46.017 回答
-1

更通用的正则表达式解决方案是:

r"[\w.]+@[\w.]+"
于 2012-05-26T02:56:53.843 回答
-1

只需使用:

email_id = re.search(r'([\w.])+@([\w.])+', demo_text)
email_id.group(1) # the username part
email_id.group(2) # the host part
于 2012-05-26T07:21:13.430 回答