【发布时间】:2016-03-07 22:12:22
【问题描述】:
我正在尝试弄清楚如何改进正则表达式以仅获取emails 不以".jpg" 结尾,并从电子邮件的左右部分删除--(如果找到)。示例参数为source,它是一个字符串。
<html>
<body>
<p>aaa@example.jpg</p>
<p>--bbb@example.com--</p>
<p>ccc@example.com--</p>
<p>--ddd@example.com</p>
</body>
</html>
结果应包含:bbb@example.com、ccc@example.com、ddd@example.com
所以基本上,我希望看到无论如何改进这个功能,这样正则表达式就可以在没有的情况下生成电子邮件——如果可能的话,改进if not email[0].endswith('.png'),以防我想添加更多,这看起来很紧迫。
def extract_emails(source):
regex = re.compile(r'([\w\-\.]{1,100}@(\w[\w\-]+\.)+[\w\-]+)')
emails = list(set(regex.findall(source.decode("utf8"))))
all_emails = []
for email in emails:
if not email[0].endswith('.png') and not email[0].endswith('.jpg') \
and not email[0].endswith('.gif') and not email[0].endswith('.rar')\
and not email[0].endswith('.zip') and not email[0].endswith('.swf'):
all_emails.append(email[0].lower())
return list(set(all_emails))
【问题讨论】:
-
@Epodax 错误地选择了所有建议的标签。
-
不要使用正则表达式,使用 html 解析器