正则表达式是PHP字符串的一部分
例如,我有以下字符串正则表达式是PHP字符串的一部分,php,regex,Php,Regex,例如,我有以下字符串 $s = "asd$5asd"; 我怎么能只取d后面有“$5”的部分 在php中,我们有以下内容 preg_mach //returns int number of matches preg_replace //returns the string with a replaced content //--------------------- preg_grep //it greps but how to grep the "$5" //
$s = "asd$5asd";
我怎么能只取d后面有“$5”的部分
在php中,我们有以下内容
preg_mach //returns int number of matches
preg_replace //returns the string with a replaced content
//---------------------
preg_grep //it greps but how to grep the "$5"
//that comes after "d" without grepping the "d" it's self ???
我的正则表达式是
/((coupon|enter)\x20code\x20)("([^"]+)")/
我想grep第四个括号的值($4)完全取决于您要匹配的内容,但下面将返回所有匹配项-
preg_match_all('/d(\$[0-9]+)/i',$s,$aMatches);
$aMatches[1]
包含所有匹配项的数组。示例匹配-$5$10$20(仅匹配$和任意长度的数字)用于获取所有匹配项<代码>$matches[1]将包含所需字符串的所有实例。值得注意的是,这是对@DCoders answer的详细说明。我注意到他的正则表达式不支持多个数字,因此,例如,如果我的正则表达式是“/((优惠券|输入)\x20code\x20)(“([^”]+)”)/”,它将与$10ok不匹配,那么我如何才能grep第四个括号($4)...thanks@user1225246您是否有一个示例字符串,您将发送,您希望返回什么?我的字符串是\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu0152513@user1225246您是否正在尝试获取优惠券代码的值?那么实际代码本身是否来自字符串?