Advertisement
rfmonk

re_search_substring.py

Jan 2nd, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.39 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import re
  5.  
  6. text = 'This is some text -- with punctuation.'
  7. pattern = re.compile(r'\b\w*is\w*\b')
  8.  
  9. print 'Text:', text
  10. print
  11.  
  12. pos = 0
  13. while True:
  14.     match = pattern.search(text, pos)
  15.     if not match:
  16.         break
  17.     s = match.start()
  18.     e = match.end()
  19.     print ' %2d : %2d = "%s"' % \
  20.         (s, e - 1, text[s:e])
  21.     # move forward in text for the next search
  22.     pos = e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement