Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. #
  4. # Copyright (c) 2010 Doug Hellmann. All rights reserved.
  5. #
  6. """Show the groups within the matches for a pattern.
  7. """
  8. #end_pymotw_header
  9.  
  10. import re
  11.  
  12. def test_patterns(text, patterns=[]):
  13. """Given source text and a list of patterns, look for
  14. matches for each pattern within the text and print
  15. them to stdout.
  16. """
  17. # Look for each pattern in the text and print the results
  18. for pattern, desc in patterns:
  19. print 'Pattern %r (%s)\n' % (pattern, desc)
  20. print ' %r' % text
  21. for match in re.finditer(pattern, text):
  22. s = match.start()
  23. e = match.end()
  24. prefix = ' ' * (s)
  25. print ' %s%r%s ' % (prefix, text[s:e], ' '*(len(text)-e)),
  26. print match.groups()
  27. if match.groupdict():
  28. print '%s%s' % (' ' * (len(text)-s), match.groupdict())
  29. print
  30. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement