开发者

customizing preg_match

开发者 https://www.devze.com 2023-04-11 20:11 出处:网络
How do i preg开发者_StackOverflow社区_match an string to match the following format in php: $m=\"123/456789/01\";

How do i preg开发者_StackOverflow社区_match an string to match the following format in php:

 $m="123/456789/01";

if(pregmatch(????, $m){
   // match
}else{
   // doesn't match
}

i.e. 3 digits + "/" + 6 digits + "/" + 2 digits.


This is my try :)

if(preg_match('/[0-9]{3}\/[0-9]{6}\/[0-9]{2}/', $m)
{
    // match
}
else
{
    // Doesn't match
}


if (preg_match("#\d{3}/\d{6}/\d{2}#", $string)) {
  // yeah
} else {
  // nope
}

have a look at Pattern Syntax specifically Escape sequences.


Depending on what you would like to parse, regular expressions are not always needed:

$m="123/456789/01";

if(3 == count(sscanf($m, '%d/%d/%d'))) {
   // match
}else{
   // doesn't match
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号