首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正向正向失效

正向正向失效
EN

Stack Overflow用户
提问于 2014-10-28 15:28:17
回答 2查看 29关注 0票数 1

在下面的regex构造中,

代码语言:javascript
运行
复制
<([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。但我觉得不应该。

代码语言:javascript
运行
复制
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);

但是,下面的结构验证了

代码语言:javascript
运行
复制
q(?=(regex))

如果能指出我的regex函数结构中的任何错误,我将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-28 15:33:54

你的背书指的是错误的组。它应指的是第2组而不是第1组。

代码语言:javascript
运行
复制
'#q(?=(<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\2>))#i'
      ^ ^              ^                  ^
      | 2--------------2                  |
      1-----------------------------------1
票数 3
EN

Stack Overflow用户

发布于 2014-10-28 15:34:31

代码语言:javascript
运行
复制
q(?=(<([A-Z][A-Z0-9]*)\b[^>]*>(?:.*?)<\/\2>))

你把grouping.Try搞砸了,\1和你期望的不一样。

见演示。

http://regex101.com/r/sU3fA2/31

如果您不捕获第一个组,您的正则表达式将运行fine.See

代码语言:javascript
运行
复制
q(?=(?:<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)<\/\1>))

http://regex101.com/r/sU3fA2/32

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26612187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档