将not ^
运算符与反向引用结合使用时,为什么需要使用惰性匹配?好像not
应该打破了比赛。
例如:
<?php
preg_match('/(t)[^\1]*\1/', 'is this test ok', $matches);
echo $matches[0];
?>
尽管中间
this test
与this t
不匹配,但Will output t
而不是[^\1]
。我需要使用 /(t)[^\1]*?\1/
to match this t
。此外
preg_match('/t[^t]*t/', 'is this test ok', $matches);
does match only
this t
。怎么回事,我误解了什么?
最佳答案
它不起作用,因为此处的\1
并非字符类内部的反向引用。 \1
解释为ASCII值为1的字符。
您可以改用否定性环视来获得想要的效果:
'/(t)(?:(?!\1).)*\1/'
关于php - 为什么使用 “not”和backref的此正则表达式需要延迟匹配?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3571133/