此代码以字符串形式提取电子邮件地址。逐行阅读时使用
>>> import re>>> line = "should we use regex more often? let me know at 321dsasdsa@dasdsa.com.lol">>> match = re.search(r'[w.-]+@[w.-]+', line)>>> match.group(0)'321dsasdsa@dasdsa.com.lol'
如果您有多个电子邮件地址,请使用
findall:
>>> line = "should we use regex more often? let me know at 321dsasdsa@dasdsa.com.lol">>> match = re.findall(r'[w.-]+@[w.-]+', line)>>> match['321dsasdsa@dasdsa.com.lol', 'dadaads@dsdds.com']
上面的正则表达式可能会找到最常见的非虚假电子邮件地址。如果要完全符合RFC
5322,则应检查遵循该规范的电子邮件地址。请检查[此内容](https://stackoverflow.com/questions/201323/using-
**编辑:**@kostek的注释中建议:在字符串中,
Contactus atsupport@example.com.我的正则表达式返回support@example.com。(最后一个点)。为了避免这种情况,请使用
[w.,]+@[w.,]+.w+)
编辑II: 评论中提到了另一个出色的改进:
[w.-]+@[w.-]+.w+还将捕获example@do-main.com。



