When using the not ^ operator in combination with a back reference, why do I need to use a lazy match? It seems like the not should break the match.
For example:
<?php
preg_match('/(t)[^\1]*\1/', 'is this test ok', $matches);
echo $matches[0];
?>
Will output this test, instead of this t, in spite of the fact that the middle t does not match [^\1]. I need to use /(t)[^\1]*?\1/ to match this t.
Furthermore
preg_match('/t[^t]*t/', 'is this test ok', $matches);
What is going on, and what am I misunderstanding?
It doesn’t work because the
\1here is not a backreference inside a character class. The\1is interpreted as the character with ASCII value 1.You could use a negative lookaround instead to get the effect you want: