2012-07-22 97 views
0

,比如我有以下字符串正则表达式接受字符串PHP的一部分

$s = "asd$5asd"; 

我怎么可以只包含在PHP之后,“d” 来了“$ 5”的一部分,我们有以下

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)

+1

使用['preg_match_all('#d \ $([0-9])#',$ s,$ matches,PREG_PATTERN_ORDER]'] (http://us.php.net/preg_match)获取所有匹配。 '$ matches [1]'将包含你想要的字符串的所有实例。 – DCoder 2012-07-22 08:39:54

回答

2

取决于你在寻找什么来匹配,但下面将返回所有比赛 -

preg_match_all('/d(\$[0-9]+)/i',$s,$aMatches); 

$aMatches[1]包含了所有比赛的数组。示例匹配 - $ 5 $ 10 $ 20(仅匹配$和任意长度的数字)

+0

值得注意的是,这是对@DCoders答案的阐述。我注意到他的正则表达式不支持多个数字,例如,它不匹配$ 10 – Chris 2012-07-22 08:50:28

+0

好吧,如果我的正则表达式是'/((优惠券|输入)\ x20code \ x20)(“([^”] +)“)/ '我怎么能grep的第四个支架($ 4)...谢谢 – Hilmi 2012-07-22 08:59:03

+0

@ user1225246你有一个你会发送的例子字符串,你会期望返回什么? – Chris 2012-07-22 09:04:27