擅长:python、mysql、java
<p>包're'似乎不支持重叠匹配(这就是为什么<code>findall</code>或<code>finditer</code>找不到您想要的两个匹配)。你知道吗</p>
<p>然而,有一个名为regex的新Python包似乎解决了这个问题:<a href="https://pypi.python.org/pypi/regex" rel="nofollow">https://pypi.python.org/pypi/regex</a>。您可以使用以下两个命令之一进行安装:</p>
<pre><code>pip install regex
easy_install regex
</code></pre>
<p>导入包regex而不是re,以下脚本执行您所需的操作:</p>
<pre><code>#!/usr/bin/python
import regex as re
s = "SHVANSGYMGMTPRLGLESLLE*A*MIRVAS"
matches = re.findall(r'M.*?\*', s, overlapped=True)
for match in matches: print match
</code></pre>