擅长:python、mysql、java
<p>您可以将<a href="https://docs.python.org/2/library/stdtypes.html?highlight=partition#str.partition" rel="nofollow noreferrer">str.partition</a>用于您的案例:</p>
<pre><code>def find_foo(x):
result = []
while x:
before, _, x = x.partition("foo")
result.append(before + "foo")
return result
>>> find_foo('foo test1 test1 foo test2 foo')
>>> ['foo', ' test1 test1 foo', ' test2 foo']
</code></pre>