Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude a string from re.findall?

This might be a silly question, but I'm just trying to learn!

I'm trying to build a simple email search tool to learn more about python. I'm modifying some open source code to parse the email address:

emails = re.findall(r'([A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*)', html)

Then I'm writing the results into a spreadsheet using the CSV module.

Since I'd like to keep the domain extension open to almost any, my results are outputting image files with an email type format:

example: [email protected]

How can I add to exclude "png" string from re.findall

Code:

  def scrape(self, page):
    try:
        request = urllib2.Request(page.url.encode("utf8"))
        html    = urllib2.urlopen(request).read()
    except Exception, e:
        return
       emails = re.findall(r'([A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*)', html)
       for email in emails:
        if email not in self.emails:  # if not a duplicate
            self.csvwriter.writerow([page.title.encode('utf8'), page.url.encode("utf8"), email])
            self.emails.append(email)
like image 750
Jtuck4491 Avatar asked Jul 12 '26 05:07

Jtuck4491


2 Answers

you already are only acting on an if ... just make part of the if check ... ...that will be much much much easier than trying to exclude it from the regex

if email not in self.emails and not email.endswith("png"):  # if not a duplicate
        self.csvwriter.writerow([page.title.encode('utf8'), page.url.encode("utf8"), email])
        self.emails.append(email)
like image 194
Joran Beasley Avatar answered Jul 13 '26 19:07

Joran Beasley


I know Joran already gave you a response, but here's another way to do it with Python regex that I found cool.

There is a (?!...) matching pattern that essentially says: "Wherever you place this matching pattern, if at that point in the string this pattern is checked and a match is found, then that match occurrence fails."

If that was a bad explanation, the Python document does a much better job: https://docs.python.org/2/howto/regex.html#lookahead-assertions

Also, here is a working example:

y = r'([A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.(?!png)[a-zA-z]*)'
s = '[email protected]'
re.findall(y, s) # Will return an empty list

s2 = '[email protected]'
re.findall(y, s2) # Will return a list with s2 string

s3 = s + ' ' + s2 # Concatenates the two e-mail-formatted strings
re.findall(y, s3) # Will only return s2 string in list
like image 40
Zhouster Avatar answered Jul 13 '26 19:07

Zhouster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!