从字符串中提取电子邮件而不是从字符串中提取整行

时间:2012-05-26 02:53:51

标签: python regex

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

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

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 个答案:

答案 0 :(得分:8)

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

email = re.search("Reason: 550 (.*?)... No such user", demo_text).group(1)

答案 1 :(得分:2)

改为检索组1。

....group(1)

答案 2 :(得分:-1)

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

r"[\w.]+@[\w.]+"

答案 3 :(得分:-1)

只需使用:

email_id = re.search(r'([\w.])+@([\w.])+', demo_text)
email_id.group(1) # the username part
email_id.group(2) # the host part