简体   繁体   中英

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.

Attempts: 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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