在下面的regex构造中,
<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1> will match the opening and closing pair of any HTML tag, while
q(?=(<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>)) this positive lookahead construct should match every q followed by a pair of any HTML tag.下面的函数返回null。但我觉得不应该。
function regex($detail)
{
if(preg_match('#q(?=(<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>))#i', $detail))
{
return true;
}
return false;
}
$detail = 'q<p>is this my first post is this my first post is this my first post is this my first post is this</p>';
echo regex($detail);但是,下面的结构验证了
q(?=(regex))如果能指出我的regex函数结构中的任何错误,我将不胜感激。
发布于 2014-10-28 15:33:54
你的背书指的是错误的组。它应指的是第2组而不是第1组。
'#q(?=(<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\2>))#i'
^ ^ ^ ^
| 2--------------2 |
1-----------------------------------1发布于 2014-10-28 15:34:31
q(?=(<([A-Z][A-Z0-9]*)\b[^>]*>(?:.*?)<\/\2>))你把grouping.Try搞砸了,\1和你期望的不一样。
见演示。
http://regex101.com/r/sU3fA2/31
如果您不捕获第一个组,您的正则表达式将运行fine.See
q(?=(?:<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)<\/\1>))http://regex101.com/r/sU3fA2/32
https://stackoverflow.com/questions/26612187
复制相似问题