繁体   English   中英

正则表达式匹配多个零

[英]regex match more than one zero

如标题所示,我需要一种在数字$的末尾匹配多个零的方法。

100 //match
10 // no match
4000 // match
340 // no match
74003 //no match

我正在使用php,如果那很重要。

尝试: 0+(?!(0))$ 0[0-9]+$

$regex = "/0{2,}$/";
var_dump(preg_match($regex, "4000"));

php> $regex = "/0{2,}$/"

php> preg_match($regex, "100")

php> echo preg_match($regex, "100")
1
php> echo preg_match($regex, "10")
0
php> echo preg_match($regex, "4000")
1
php> echo preg_match($regex, "340")
0
php> echo preg_match($regex, "74003")
0
php> 

如果数字必须以非零开头:

/^[1-9]+00+$/

如果数字可以以零开头:

/^\d*00+$/

如果您只想检查最后2位数字是否为0,则可以使用

if($number % 100 == 0){
    //0 will be here too, if you need not it try to add (and $number!=0)
}

regexp在这里会有点矫kill过正

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM