擅长:python、mysql、java
<pre><code>In [9]: import re
In [13]: def find_ind(word, s):
return [(w.start(), w.end() - 1) for w in re.finditer(word, s) if s.count(word) > 1]
In [14]: find_ind("apple",s)
[(0, 4), (13, 17)]
In [15]: find_ind("orange",s)
[]
</code></pre>
<p>使用python和<a href="https://docs.python.org/2/library/re.html#re.finditer" rel="nofollow">re.finditer</a></p>
<p><em>返回一个迭代器,在字符串中的RE模式的所有非重叠匹配上生成MatchObject实例</em></p>