in reply to Why is variable length lookahead implemented while lookbehind is not?
When you write a backtracking regex engine like the one in Perl 5, a full (variable length) look-ahead is pretty easy to implement. You just have to a normal match, and if the match succeeds, you reset the match position to where the look-ahead started off. And one can mark the look-ahead as not needing to backtrack.
On the other hand a look-behind requires either a backwards search, or some sufficiently advanced magic that resets the match position to the start of the string, and then matches something like (?s:.*)$look_behind\K, and anchors the end of that match to the position where the look-behind started, and then resets all the match variables as if that match has never happened, execept the captures from within $look_behind.
And even when you do that, the performance is dependent on the length of the string up to the current position, instead of just depending on the string in the vicinity of the current match position; to do it properly, you'd really need to search backwards.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why is variable length lookahead implemented while lookbehind is not?
by ikegami (Patriarch) on Sep 07, 2011 at 20:04 UTC | |
by moritz (Cardinal) on Sep 07, 2011 at 20:56 UTC | |
by GrandFather (Saint) on Sep 07, 2011 at 20:42 UTC | |
by CountZero (Bishop) on Sep 07, 2011 at 21:11 UTC | |
by ikegami (Patriarch) on Sep 07, 2011 at 20:51 UTC |