Given these two Regex:
(?!^)\G
and
\G(?!^)
What's the difference between negative lookahead after and before \G
anchor?
Logically, they come out to be the same thing. They are functionally equivalent. The (?!^)
and the \G
check two different conditions at the same location in the string, so it makes no logical difference what order the conditions are checked in.
The conditions are:
(?!^)
= “we are not at the beginning of the input string”\G
= “we are in the location where the previous match ended”However, in terms of performance, I suspect (though I haven’t tested) that the latter is faster. I would expect the regular expression engine to have an optimisation so that a regex that starts with \G
would only be executed from the previous match’s end onwards, while the other one would go through the whole string “looking for” the location of the previous match.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments