[code.view]

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

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Searching a substring of the input.
     """
     #end_pymotw_header
     
     import re
     
     text = 'This is some text -- with punctuation.'
     pattern = re.compile(r'\b\w*is\w*\b')
     
     print 'Text:', text
     print
     
     pos = 0
     while True:
         match = pattern.search(text, pos)
         if not match:
             break
         s = match.start()
         e = match.end()
         print '  %2d : %2d = "%s"' % \
             (s, e-1, text[s:e])
         # Move forward in text for the next search
         pos = e
         
     

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

contact | logmethods.com