I am trying to figure out how to improve the regex to only get emails
not ending with ".jpg"
and to remove --
from both left and right part of the emails if any is found. Example parameter as source
which is a string.
<html>
<body>
<p>aaa@example.jpg</p>
<p>--bbb@example.com--</p>
<p>ccc@example.com--</p>
<p>--ddd@example.com</p>
</body>
</html>
The result should contain: bbb@example.com, ccc@example.com, ddd@example.com
So basically, I want to see anyway to improve this function so the regex would could produce emails without -- and if possible improve the if not email[0].endswith('.png')
in case i want to add more, this could look urgly.
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))