擅长:python、mysql、java
<p>我想我找到了一个更好的解决方案(至少满足我的需要)。我用消极的眼光看未来:</p>
<pre><code>^(?!1{1,2}3)1{1,2}.*
</code></pre>
<p>说明:</p>
^{pr2}$
<p>最棒的是,它的工作原理与佩里贡的答案相同,但它更短,更容易阅读,更容易适应。在</p>
<p><strong>编辑:</strong></p>
<p>cdlane给了我一个很好的解决方案。我为<code>re.fullmatch</code>改编了它,它比我的原始版本更容易适应,也更短了1个字节:</p>
<pre><code>^(?![^1]|1{1,2}3).*
</code></pre>
<p>说明:</p>
<pre><code>^ # Beginning of string
(?! # Do not match if
[^1] # The string starts with anything other than 1
|1{1,2}3) # Or there is one or two 1s followed by a 3
.* # Anything else is accepted
</code></pre>
<p>对于感兴趣的人,您还可以通过插入以下内容轻松限制开头出现的1的数量:</p>
<pre><code>|1{limit,}
</code></pre>
<p>像这样:</p>
<pre><code>^(?![^1]|1{1,2}3|1{3,}).*
</code></pre>
<p>如果您想更改<em>必须</em>出现的1的数量,而不是:</p>
<pre><code>[^1]
</code></pre>
<p>使用:</p>
<pre><code>(?!1{minimum,}
</code></pre>
<p>组合起来:</p>
<pre><code>^(?!(?!1{4,})|1{4,5}3|1{6,}).*
</code></pre>