Advertisement
rfmonk

re_groups_named.py

Jan 2nd, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import re
  5.  
  6. text = 'This is some text -- with punctuation.'
  7.  
  8. print text
  9. print
  10.  
  11. for pattern in [r'^(?P<first_word>\w+)',
  12.                 r'(?P<last_word>\w+)\S*$',
  13.                 r'(?P<t_word>\bt\w+)\W(?P<other_word>\w+)',
  14.                 r'(?P<ends_with_t>\w+t)\b',
  15.                 ]:
  16.     regex = re.compile(pattern)
  17.     match = regex.search(text)
  18.     print 'Matching "%s"' % pattern
  19.     print '  ', match.groups()
  20.     print '  ', match.groupdict()
  21.     print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement