OT: regex to find email

Steve lonetwin at gmail.com
Tue Sep 21 11:15:29 EDT 2004


Hi Josh,
> 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

I'm assuming you meant 'good regex to parse email ids'. I knew someone
would want this someday :). Here goes:
import re
r = re.compile (r"""(?P<name>["(].+[")]\s)?    # The name preceding
the id, if any
                    (?P<mailid>[<\w]+[-\w.<+]+@(\w+\.)+[\w>]+) # The actual id 
                    (?P<trailing>\s["(].+[")])?          # The
trailing name if any
                 """, re.VERBOSE)
re.search(r, '-sdf at sdf.cas').group('mailid')

HTH
Steve



More information about the Python-list mailing list