我试图使用正则表达式从一串文本中获取电子邮件地址。
如何让我的简单代码只提取电子邮件地址而不是整行?
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)
答案 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