负面展望未按预期工作 [英] Negative look ahead not working as expected

查看:37
本文介绍了负面展望未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的情况,积极前瞻按预期工作,但消极前瞻却没有.请看下面的代码:

我想匹配任何没有跟在 ...$ 后面的主题标签并将它大写(实际上它将用 href 解析出来,但为了简单起见,只是大写暂时吧).

这些是带有相应输出的正则表达式:

'/#\w+(?=...$)/' 匹配任何以 ...$ 结尾的主题标签并大写,按预期工作:

RT @Startup_Collab:@RiseOfRest 将前往 OMA &LNK #showcase 我们新兴的#startup 生态系统.学到更多!https://example.net #RISEOF...

'/#\w+(?!...$)/' 匹配任何不以 ...$ 结尾的主题标签,并将其大写,不起作用,所有主题标签大写:

RT @Startup_Collab:@RiseOfRest 将前往 OMA &LNK 到#SHOWCASE 我们新兴的#STARTUP 生态系统.学到更多!https://example.net #RISEOf...

非常感谢您的任何帮助、建议、想法和耐心.

-- 天使

解决方案

这是因为与主题标签的部分匹配的回溯.使用所有格量词避免回溯到 \w+ 子模式:

/#\w++(?!...$)/^^

查看正则表达式演示

现在,匹配了 1 个或多个单词字符,并且 (?!...$) 负前瞻仅在这些单词字符匹配后执行一次.如果有 false 结果,则不会发生回溯,整个匹配失败.

在此处查看有关 占有量词的更多信息.>

I have a bizarre situation where positive lookahead works as expected but negative lookahead doesn't. Please take a look at the following code:

<?php

$tweet = "RT @Startup_Collab: @RiseOfRest is headed to OMA & LNK to #showcase our emerging #startup ecosystem. Learn more! https://example.net #Riseof…";

$patterns=array(
    '/#\w+(?=…$)/',
);

$tweet = preg_replace_callback($patterns,function($m)
{
    switch($m[0][0])
    {
        case "#":
            return strtoupper($m[0]);
        break;
    }
},$tweet);


echo $tweet;

I want to match any hashtag not followed by …$ and upper case it (in reality it will be parsed out with an href but for simplicity's sake just upper case it for now ).

These are regexes with their corresponding outputs:

'/#\w+(?=…$)/' Match any hashtag ending with …$ and upper-case it, works as expected:

RT @Startup_Collab: @RiseOfRest is headed to OMA & LNK to #showcase our emerging #startup ecosystem. Learn more! https://example.net #RISEOF…

'/#\w+(?!…$)/' Match any hashtag NOT ending with …$ and upper-case it, does not work, all hashtags are uppercased:

RT @Startup_Collab: @RiseOfRest is headed to OMA & LNK to #SHOWCASE our emerging #STARTUP ecosystem. Learn more! https://example.net #RISEOf…

Thank you ver much for any help, suggestions, ideas and patience.

-- Angel

解决方案

That is because of backtracking that matches the part of a hashtag. Use a possessive quantifier to avoid backtracking into the \w+ subpattern:

/#\w++(?!…$)/
    ^^

See the regex demo

Now, 1 or more word chars are matched, and the (?!…$) negative lookahead is only executed once after these word chars matched. If there is a false result, no backtracking occurs, and the whole match is failed.

See more on possessive quantifiers here.

这篇关于负面展望未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆