在python中使用正则表达式提取电子邮件

在python中使用正则表达式提取电子邮件,python,regex,Python,Regex,我编写了下面的程序,用它来提取文件“on_campus.txt”中以“@aun.edu.ng”结尾的电子邮件,使用regex字符asterix。但当我运行它时,它似乎也将asterix作为字符串 import re def aun_mails(): mail_file = open('on_campus.txt') for m in mail_file: m = m.strip() if re.search('*@aun.edu.ng', m):

我编写了下面的程序,用它来提取文件“on_campus.txt”中以“@aun.edu.ng”结尾的电子邮件,使用regex字符asterix。但当我运行它时,它似乎也将asterix作为字符串

import re

def aun_mails():
    mail_file = open('on_campus.txt')
    for m in mail_file:
        m = m.strip()
        if re.search('*@aun.edu.ng', m):

            print m

aun_mails()

我认为,如果没有
re
模块,您将能够完成您想要的任务。 请看以下示例:

myString = "Blah blah email blah sent by: some.name@aun.edu.ng"
isEmail = myString.endswith('aun.edu.ng') # isEmail will be True in this case

if isEmail == True:
    print myString # or print(myString for python 3.x)

我不是100%确定你想要什么,但你可能想要
\S*@aun\.edu\.ng
作为你的正则表达式。另外,
匹配任何字符,而不是点,您应该使用
\.
来代替。您拥有的电子邮件以及正确匹配/未正确匹配的内容的示例会有所帮助。请注意,
*
是一个量词。它必须用于量化字符、字符类或任何其他正则表达式子结构。如果每行有一封电子邮件,您只需检查
如果m:
中的'@aun.edu.ng',您所说的“它似乎也将星号作为字符串”是什么意思?什么星号?代码中没有星号(*)。如何将其视为字符串?