使用php变量的preg_匹配问题
我想在preg_匹配中结合一些正则表达式使用一个变量:使用php变量的preg_匹配问题,php,regex,Php,Regex,我想在preg_匹配中结合一些正则表达式使用一个变量: $string = "cheese-123-asdf"; $find = "cheese"; if(preg_match("/$find-/d.*/", $string)) { echo "matched"; } 在我的模式中,我尝试使用cheese进行匹配,后面跟一个-和一个数字,后面跟其他任何数字。您为\输入了错误的/: if(preg_match("/$find-\d.*/", $string)) { *也不是必需的,因为
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-/d.*/", $string)) {
echo "matched";
}
在我的模式中,我尝试使用cheese进行匹配,后面跟一个-和一个数字,后面跟其他任何数字。您为
\
输入了错误的/
:
if(preg_match("/$find-\d.*/", $string)) {
*
也不是必需的,因为模式将匹配任何一种方式。对于数字,它是\d
if(preg_match("/$find-\d.*/", $string)) {
/d
更改为\d
*
/
或*
或..
),则可能会导致您的匹配出现问题
<?php
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-\d/", $string))
{
echo "matched";
}
?>