Python Text Processing 简明教程

Python - Extract Emails from Text

若要从文本中提取电子邮件,我们可以使用正则表达式。在下面的示例中,我们使用正则表达式包的帮助来定义电子邮件 ID 的模式,然后使用 findall() 函数来检索与该模式匹配的那些文本。

To extract emails form text, we can take of regular expression. In the below example we take help of the regular expression package to define the pattern of an email ID and then use the findall() function to retrieve those text which match this pattern.

import re
text = "Please contact us at contact@tutorialspoint.com for further information."+\
        " You can also give feedbacl at feedback@tp.com"


emails = re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", text)
print emails

当我们运行以上程序时,我们得到以下输出:

When we run the above program, we get the following output −

['contact@tutorialspoint.com', 'feedback@tp.com']