<p>正则表达式总是匹配的,因为它允许空字符串匹配(因为整个正则表达式都包含在可选组中)。在</p>
<p>如果您测试这个<a href="https://regex101.com/r/750sOO/1" rel="nofollow noreferrer">live on regex101.com</a>,您可以立即看到它,并且它与整个字符串不匹配,只匹配它的一部分。在</p>
<p>我已经纠正了您的<a href="http://www.regular-expressions.info/charclass.html" rel="nofollow noreferrer">character classes</a>中关于使用不必要/有害的交替运算符(<code>|</code>)和不正确的破折号位置,使其成为范围运算符(<code>-</code>)的两个错误,但仍然不正确。在</p>
<p>我想你想要更像这样的东西:</p>
<pre><code>^ # Make sure the match begins at the start of the string
(?: # Start a non-capturing group that matches...
-? # an optional minus sign,
\d+ # one or more digits
(?:\.\d+)? # an optional group that contains a dot and one or more digits.
(?: # Start of a non-capturing group that either matches...
[+*/-] # an operator
| # or
$ # the end of the string.
) # End of inner non-capturing group
)+ # End of outer non-capturing group, required to match at least once.
(?<![+*/-]) # Make sure that the final character isn't an operator.
$ # Make sure that the match ends at the end of the string.
</code></pre>
<p>测试一下<a href="https://regex101.com/r/750sOO/4" rel="nofollow noreferrer">live on regex101.com</a>。在</p>