OT: regex to find email

Josh Close narshe at gmail.com
Tue Sep 21 11:29:41 EDT 2004


On Tue, 21 Sep 2004 11:16:45 -0400, Peter Hansen <peter at engcorp.com> wrote:
> 
> 
> Josh Close wrote:
> > I've been trying to find a good regex to parse emails, but haven't
> > found any to my liking. I basically need to have
> >
> > ( r'[a-z0-9\.\-\_]@[a-z0-9\.\-\_]', re.IGNORECASE )
> >
> > but the first part can't start with .-_ and the last part has to have
> > a . in it (first/last being before/after the @).
> 
> Use this instead:
> 
> from email.Utils import parseaddr

The docs say this.

parseaddr(address)
    Parse address - which should be the value of some
address-containing field such as To: or Cc: - into its constituent
realname and email address parts. Returns a tuple of that information,
unless the parse fails, in which case a 2-tuple of ('', '') is
returned.

I need to find all address in a message, not just in the headers. It
looks like for all these methods you have to specify To: From: CC: etc
(i guess that wouldn't necessarily be headers then).

If I missed something there, let me know, 'cause this would be a lot easier.

-Josh



More information about the Python-list mailing list