0

I have a string, let's say an email From field:

str1 = "Name <[email protected]>"

(or perhaps with another format, the thing is that inside of str an email address is found...)

And I have a list of addresses:

lst = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]

What is the most pythonic way to search if the part of str with the email address is one of the members on lst ?

In the example, the email part of str1 is part of lst, but for:

str2 = "Another email [email protected]"

it is not...

Also,

str3 = "Example [email protected]"

would match because [email protected] is in the list, no matter there's no '<' '>' surrounding the email addres...

Javier Novoa C.
  • 11,257
  • 13
  • 57
  • 75

3 Answers3

2

from http://love-python.blogspot.com/2008/04/python-code-to-scrape-email-address.html

>>> email_pattern = re.compile("[-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+.[a-zA-Z0-9_.]+")
>>> str = "Name <[email protected]>"
>>> str2 = "Another email [email protected]"
>>> lst = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
>>> import re
>>> set(re.findall(email_pattern, str)).intersection(lst)
set(['[email protected]'])
>>> set(re.findall(email_pattern, str2)).intersection(lst)
set([])
Marco Mariani
  • 13,556
  • 6
  • 39
  • 55
2

Usually regex are not considered pythonic, but this seems a task made exactly for them.

So I would use them, extract the email adress and check if it's in the list:

>>> re.search(r'<(.*)>', "Name <[email protected]>").group(1) in lst
True

"pythonic" isn't a word to throw there that will solve any problem, one should consider all the available options and choose the best one.

Edit: If the format of your field isn't standard, no problem: you just need a better regex that will match the email. (I'm sure there are a ton of examples out there, I'm not going to google it for you).

But that doesn't mean that you shouldn't use regex for this kind of task.

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
  • @JavierNovoaC.: It doesn't matter if the email adress is not surrounded by `<>`, I just show you an simple example with a basic regex. You can use a different regex to extract the email adress. I really don't see the problem. – Rik Poggi Mar 15 '12 at 00:01
  • thanks, I'll search for it. I was looking for a pythonic solution, thinking on the context of my problem, but that thing you mention is a good advice... – Javier Novoa C. Mar 15 '12 at 00:02
1

I don't know if this is pythonic:

return str1.split('<')[1].split('>')[0] in lst
lllluuukke
  • 1,304
  • 2
  • 13
  • 17