<p>我想你想要的是:</p>
<pre><code>[len(s) for s in re.findall(r'{}+'.format(CHAR), input)]
</code></pre>
<p>当然,如果<code>CHAR</code>是一个特殊的值,比如<code>\</code>,这就不起作用了。如果这是一个问题:</p>
<pre><code>[len(s) for s in re.findall(r'{}+'.format(re.escape(CHAR)), input)]
</code></pre>
<hr/>
<p>如果要匹配两个或多个而不是一个或多个,则语法为<code>{2,}</code>。正如<a href="http://docs.python.org/2/library/re.html#regular-expression-syntax" rel="nofollow">the docs</a>所说:</p>
<blockquote>
<p><code>{m,n}</code> Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, <code>a{3,5}</code> will match from 3 to 5 <code>'a'</code> characters. Omitting <em>m</em> specifies a lower bound of zero, and omitting <em>n</em> specifies an infinite upper bound. As an example, <code>a{4,}b</code> will match <code>aaaab</code> or a thousand <code>'a'</code> characters followed by a <code>b</code>, but not <code>aaab</code>…</p>
</blockquote>
<p>当我们使用<code>{}</code>进行字符串格式化时,这有点难看,所以让我们切换到<code>%</code>格式:</p>
<pre><code>[len(s) for s in re.findall(r'%s{2,}' % (re.escape(CHAR),), input)]
</code></pre>
<p>…或者只是简单的串联:</p>
<pre><code>[len(s) for s in re.findall(re.escape(CHAR) + r'{2,}', input)]
</code></pre>