[code.view]

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

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Looking for a specific group in a match
     """
     #end_pymotw_header
     
     import re
     
     text = 'This is some text -- with punctuation.'
     
     print 'Input text            :', text
     
     # word starting with 't' then another word
     regex = re.compile(r'(\bt\w+)\W+(\w+)')
     print 'Pattern               :', regex.pattern
     
     match = regex.search(text)
     print 'Entire match          :', match.group(0)
     print 'Word starting with "t":', match.group(1)
     print 'Word after "t" word   :', match.group(2)
     

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

contact | logmethods.com