Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么这个正则表达式使用;不是"&;backref,需要惰性匹配吗?_Php_Regex_Regex Negation - Fatal编程技术网

Php 为什么这个正则表达式使用;不是"&;backref,需要惰性匹配吗?

Php 为什么这个正则表达式使用;不是"&;backref,需要惰性匹配吗?,php,regex,regex-negation,Php,Regex,Regex Negation,当将not^操作符与back引用结合使用时,为什么需要使用惰性匹配?似乎而不是应该打破这场比赛 例如: <?php preg_match('/(t)[^\1]*\1/', 'is this test ok', $matches); echo $matches[0]; ?> 发生了什么,我误解了什么?不能在角色类中使用反向引用[^\1]表示“除1以外的任何字符” 相反,请使用/(t)(?:(?!\1)。*\1/ (?:…)是非捕获组 (?!…)是一个“消极前瞻”,断言子表达式不匹配

当将not
^
操作符与back引用结合使用时,为什么需要使用惰性匹配?似乎
而不是
应该打破这场比赛

例如:

<?php
preg_match('/(t)[^\1]*\1/', 'is this test ok', $matches);
echo $matches[0];
?>


发生了什么,我误解了什么?

不能在角色类中使用反向引用<代码>[^\1]
表示“除
1
以外的任何字符”

相反,请使用
/(t)(?:(?!\1)。*\1/

(?:…)
是非捕获组

(?!…)
是一个“消极前瞻”,断言子表达式不匹配


(?!\1)。
\1
是单个字符时,表示“任何与
\1
不匹配的字符都无效,因为这里的
\1
不是字符类中的反向引用。
\1
被解释为ASCII值为1的字符

您可以使用负面环视来获得想要的效果:

'/(t)(?:(?!\1).)*\1/'

我刚刚在我自己的答案中添加了一个确切的例子。我想伟大的人都有相同的想法。-)谢谢,每次都有正则表达式!
'/(t)(?:(?!\1).)*\1/'