简体   繁体   English

正则表达式匹配多个零

[英]regex match more than one zero

As the title suggests I need a way to match more than one zero at the end of a number $ . 如标题所示,我需要一种在数字$的末尾匹配多个零的方法。

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

I'm using php if that matters. 我正在使用php,如果那很重要。

Attempts: 0+(?!(0))$ 0[0-9]+$ 尝试: 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> 

If the number must start with a non-zero: 如果数字必须以非零开头:

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

If the number can start with a zero: 如果数字可以以零开头:

/^\d*00+$/

if you only want to check if 2 last digits are 0 you may use 如果您只想检查最后2位数字是否为0,则可以使用

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

regexp will be bit overkill here regexp在这里会有点矫kill过正

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

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