The PCRE regex /..(?<=(.)\1)/
fails to compile: "Subpattern references are not allowed within a lookbehind assertion." Interestingly it seems to be acceptable in lookaheads, like /(?=(.)\1)../
, just not in lookbehinds.
Is there a technical reason why backreferences are not allowed in lookbehinds specifically?
With Python's re module, group references are not supported in lookbehind, even if they match strings of some fixed length.
Lookbehinds doesn't fully support PCRE rules. Concretely, when the regex engine reaches a lookbehind it'll try to determine it size, and then jump back to check the match.
This size determination brings you to a choice:
As the first solution would be the best for us (users), it's obviously the slowest, and the hardest to develop. And so for PCRE regex, they resolved to use the second solution. The Java regex engine, for another example, allows semi-variable lookbehinds: you only need to determine the maximum size.
I came to PCRE and Python's re module.
I've not found anything else in PCRE documentation than this error code:
COMPILATION ERROR CODES
25: lookbehind assertion is not fixed length
But in this case, the lookbehind assertion is fixed length.
Now, here is what we can find in re documentation:
The contained pattern must only match strings of some fixed length, meaning that abc or a|b are allowed, but a* and a{3,4} are not. Group references are not supported even if they match strings of some fixed length.
We've got our guilty... If you want, you can try the Python's regex module , which seems to support variable length lookbehind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With