2011-08-21 186 views
1

正如標題所示,我需要在數字末尾匹配多個零的方式$正則表達式匹配多個零

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

我正在使用php,如果這件事。

嘗試: 0+(?!(0))$ 0[0-9]+$

+0

你想要的整數賦予了它有多於一個零或者僅僅是零的數字時比一個孤單更匹配嗎? – sg3s

回答

5
$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> 
+0

謝謝!你真棒! – ThomasReggi

1

如果數必須以非從零開始:

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

如果數字可以零開始的:

/^\d*00+$/ 
1

如果你只想檢查2 la ST數字爲0,你可以使用

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

正則表達式會有點矯枉過正這裏