擅长:python、mysql、java
<p>如果你决定用正则表达式来做这件事,那么对于“simplify”的某些定义,这可能是合适的</p>
<pre class="lang-regex prettyprint-override"><code>(?=^0*((10*){2})*?$)(?=^1*(01*)((01*){2})*?$)^.*$
(?= ) assert that
^ $ between the start and end of the string
0* (consume leading zeros)
( )*? there appears as many times as necessary
{2} two instances of
(10*) a 1 followed by any number of 0s
(?=^1* ((01*){2})*?$) perform the same check as before
(01*) but require an extra 0 at the start
</code></pre>
<p>这依赖于<code>{2}</code>量词要求所讨论的数字为2的倍数,而不是一次全部验证字符串,而是对字符串执行2次检查:第一次检查1的偶数,第二次检查0的偶数,再加上额外的0</p>
<p><a href="https://regex101.com/r/qzKAFP/1/" rel="nofollow noreferrer">Demo</a></p>