擅长:python、mysql、java
<p>这个正则表达式可以:</p>
<pre><code>\b(?!4\d{0,4}\b)\d+\b
</code></pre>
<p>这个想法是使用一个负的前瞻性来丢弃以<code>4</code>开头的数字,并且可以有更多的零到四个数字。你知道吗</p>
<h3><strong><a href="https://regex101.com/r/wC0QLX/1" rel="nofollow noreferrer">Regex Demo</a></strong></h3>
<h3>Python代码:</h3>
<pre><code>import re
s = '475555 4 41 412 4123 4123 412345'
print(re.findall(r'\b(?!4\d{0,4}\b)\d+\b', s))
</code></pre>
<h3>打印</h3>
<pre><code>['475555', '412345']
</code></pre>
<p>如果希望一次匹配一个数字作为整个字符串,可以使用以下正则表达式:</p>
<pre><code>^(?!4\d{0,4}$)\d+$
</code></pre>
<h3><strong><a href="https://regex101.com/r/wC0QLX/2" rel="nofollow noreferrer">Regex Demo as whole input</a></strong></h3>