小册名称:Python合辑4-130个字符串操作示例
在Python中查找字符串中所有出现的单词的所有索引
import resentence = 'this is a sentence this this'word = 'this'for match in re.finditer(word, sentence): print(match.start(), match.end())
import re
sentence = 'this is a sentence this this'
word = 'this'
for match in re.finditer(word, sentence):
print(match.start(), match.end())
Output:
0 419 2324 28
0 4
19 23
24 28