[code.view]

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

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Naming pattern groups
     """
     #end_pymotw_header
     
     import re
     
     text = 'This is some text -- with punctuation.'
     
     print text
     print
     
     for pattern in [ r'^(?P<first_word>\w+)',
                      r'(?P<last_word>\w+)\S*$',
                      r'(?P<t_word>\bt\w+)\W+(?P<other_word>\w+)',
                      r'(?P<ends_with_t>\w+t)\b',
                      ]:
         regex = re.compile(pattern)
         match = regex.search(text)
         print 'Matching "%s"' % pattern
         print '  ', match.groups()
         print '  ', match.groupdict()
         print
     
     

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

contact | logmethods.com