[code.view]

[top] / python / PyMOTW / re / re_look_ahead.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+
         ) # name is no longer optional
     
         # LOOKAHEAD
         # Email addresses are wrapped in angle brackets, but we only want
         # the brackets if they are both there, or neither are.
         (?= (<.*>$)       # remainder wrapped in angle brackets
             |
             ([^<].*[^>]$) # remainder *not* wrapped in angle brackets
           )
     
         <? # optional opening angle bracket
     
         # 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
         )
     
         >? # optional closing angle bracket
         ''',
         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>',
         ]
     
     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_look_ahead.py

contact | logmethods.com