我想在php中执行一个正则表达式。从$ str变量我想选择common_name = Bangladesh。我穿着我的正则表达式。正则表达式在这里没有找到任何匹配。我该如何解决这个问题?
$str = "| conventional_long_name = People's Republic of Bangladesh | common_name = Bangladesh | image_flag = Flag of Bangladesh.svg";
$pat = "/(?=\common_name\s=).*?(?=\s\|)/";
if(preg_match($pat, $str, $matches)){
echo "Matches found";
echo $matches[0];
} else {
echo "No match found";
}
结果显示
No match found
答案 0 :(得分:3)
您在第一行错过了分号;
答案 1 :(得分:2)
您需要将;
放在$str = "...."
答案 2 :(得分:0)
你在第一行末尾错过了一个分号:
将其更改为:
$str = "| conventional_long_name = People's Republic of Bangladesh | common_name = Bangladesh | image_flag = Flag of Bangladesh.svg";
此外,获得一个体面的IDE,每次都可以省去在SO上提出这个问题的麻烦! :)
答案 3 :(得分:0)
您可能在代码的第一行缺少分号
$str = "| conventional_long_name = People's Republic of Bangladesh | common_name = Bangladesh | image_flag = Flag of Bangladesh.svg";
$pat = "/(?=\common_name\s=).*?(?=\s\|)/";
if(preg_match($pat, $str, $matches)){
echo "Matches found";
echo $matches[0];
} else {
echo "No match found";
}