Advertisement
rfmonk

re_groups_match.py

Jan 2nd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 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. patterns = [
  12.     (r'^(\w+)', 'word at start of string'),
  13.     (r'(\w+)\S*$', 'word at end, with optional punctuation'),
  14.     (r'(\bt\w+)\W+(\w+)', 'word starting with t, another word'),
  15.     (r'(\w+t)\b', 'word ending with t'),
  16. ]
  17.  
  18. for pattern, desc in patterns:
  19.     regex = re.compile(pattern)
  20.     match = regex.search(text)
  21.     print 'Pattern %r (%s)\n' % (pattern, desc)
  22.     print ' ', match.groups()
  23.     print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement