[code.view]

[top] / python / PyMOTW / re / re_id.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Positive look ahead assertion
     """
     #end_pymotw_header
     
     import re
     
     address = re.compile(
         '''
         ^
     
         # A name is made up of letters, and may include "." for title
         # abbreviations and middle initials.
         (?P<name>
            ([\w.]+\s+)*[\w.]+
          )?
         \s*
     
         # Email addresses are wrapped in angle brackets, but we only want
         # the brackets if we found a name.
         (?(name)
           # remainder wrapped in angle brackets because we have a name
           (?P<brackets>(?=(<.*>$)))
           |
           # remainder does not include angle brackets without name
           (?=([^<].*[^>]$))
          )
     
         # Only look for a bracket if our look ahead assertion found both
         # of them.
         (?(brackets)<|\s*)
     
         # The address itself: username@domain.tld
         (?P<email>
           [\w\d.+-]+       # username
           @
           ([\w\d.]+\.)+    # domain name prefix
           (com|org|edu)    # limit the allowed top-level domains
          )
     
         # Only look for a bracket if our look ahead assertion found both
         # of them.
         (?(brackets)>|\s*)
     
         $
         ''',
         re.UNICODE | re.VERBOSE)
     
     candidates = [
         u'First Last <first.last@example.com>',
         u'No Brackets first.last@example.com',
         u'Open Bracket <first.last@example.com',
         u'Close Bracket first.last@example.com>',
         u'no.brackets@example.com',
         ]
     
     for candidate in candidates:
         print
         print 'Candidate:', candidate
         match = address.search(candidate)
         if match:
             print '  Match name :', match.groupdict()['name']
             print '  Match email:', match.groupdict()['email']
         else:
             print '  No match'
     

[top] / python / PyMOTW / re / re_id.py

contact | logmethods.com