PHP - preg_match - 如何匹配以零开头的数字
I am trying to use preg_match to match numbers that start with a zero to indicate an invalid input. Numbers such as 01
, 0023
, 00.4
are invalid; however numbers such as 0.12
are valid. The input would be a calculator entry, such as 09 * 108 + 0.19 + 4009
. In this case, the expression would be invalid because of the 09
.
So far my expression looks like: preg_match("/[^1-9]+0[0-9]+/", $subject)
What I tried to do was match when a zero precedes any string of digits, but is also not preceded by another string of digits. So 109
is valid, but 1+09 is invalid. However it isn't working for cases like 1 + 4009
.
我正在尝试使用preg_match匹配以零开头的数字来表示无效输入。 到目前为止,我的表达式如下: 我尝试做的是匹配,当零在任何数字字符串之前,但也不在另一个数字字符串之前。 所以 01 code>,
0023 code>,
00.4 code>等数字无效; 但是
0.12 code>之类的数字是有效的。 输入将是计算器条目,例如
09 * 108 + 0.19 + 4009 code>。 在这种情况下,由于
09 code>,表达式将无效。 p>
preg_match(“/ [^ 1-9] +0 [0-9] + /”,$ subject) code>
109 code>有效,但1 + 09无效。 但是它不适用于
1 + 4009 code>等情况。 p>
div>
preg_match('/\b0[0-9]+(?:\.[0-9]+)?\b/', $subject)
\b
for word boundary.