可以使用正则表达式;匹配对象带有位置信息附加.使用 Python 2 的示例:
You can use regular expressions; the match objects come with position information attached. Example using Python 2:
>>> import re
>>> example = 'abcdefabcdefabcdefg'
>>> for match in re.finditer('abc', example):
print match.start(), match.end()
0 3
6 9
12 15