php preg_match匹配字符串末尾的字符

use*_*857 4 php regex preg-match

我有一个if if语句,它永远不会返回True.怎么了?

我是php和regex的新手.

$String = '123456'; 
$Pattern = "/\d{2}$/"; 

//i intend to match '56', which is the last two digit of the string.

if(preg_match($Pattern $String ,$matches))
{
 echo 'Matched';
}
Run Code Online (Sandbox Code Playgroud)

如果$Pattern"/^\d{2}/",则返回true并匹配数字'12';

编辑:我的错误.上面的代码运行良好.在实际的代码中,$ String是从变量中分配的,而alwasys最终得到了一个我不知道的点.匹配上面最后两位数的要求仅用于问题解释.在实际代码中需要正则表达式.

juc*_*uco 9

你是对的.

$String = '123456';
$Pattern = "/\d{2}$/";
$Pattern2 = "/^\d{2}/";

if(preg_match($Pattern, $String ,$matches))
{
    print_r($matches); // 56
}

if(preg_match($Pattern2, $String ,$matches))
{
    print_r($matches); // 12
}
Run Code Online (Sandbox Code Playgroud)