#python 3. 6 #蔡军生 #http://blog.csdn.net/caimouse/article/details/51749579 # import re text = 'http://blogcsdn.net/caimouse abbaaabbbbaaaaa' pattern = 'ab' for match in re.finditer(pattern, text): s = match.start() e = match.end() print('Found {!r} at {:d}:{:d}'.format( text[s:e], s, e))
结果输出如下:
Found 'ab' at 29:31
Found 'ab' at 34:36