in reply to Why is variable length lookahead implemented while lookbehind is not?

In Important Notes About Lookbehind, Jan Goyvaerts explains it this way:

The bad news is that most regex flavors do not allow you to use just any regex inside a lookbehind, because they cannot apply a regular expression backwards. Therefore, the regular expression engine needs to be able to figure out how many steps to step back before checking the lookbehind.

In modern versions of Perl, variable-length look-behind is effectively supported using the \K pattern ("K" for "keep"). See perlre.

#!perl use strict; use warnings; use feature qw( say ); my $string = 'aaaabcccc'; $string =~ s/a+\Kb(?=c+)/B/; say $string; __END__ aaaaBcccc

Replies are listed 'Best First'.
Re^2: Why is variable length lookahead implemented while lookbehind is not?
by AnomalousMonk (Archbishop) on Sep 07, 2011 at 20:25 UTC

    Correct me if I'm wrong, but doesn't the  \K special escape emulate only the positive look-behind assertion? This is certainly suggested by perlre, in which the use of  \K for variable-length look-behind is discussed only in the context of the  (?<=pattern) assertion.

    Update: However, the  (*SKIP)(*FAIL) pair of Special Backtracking Control Verbs (5.10+) can be pressed into service for negative look-behind:

    >perl -wMstrict -le "my $s = 'aaxbbbxccccxddxex'; ;; printf qq{'$_' } for $s =~ m{ [a-e]+ x }xmsg; print ''; ;; printf qq{'$_' } for $s =~ m{ [bd]+ (*SKIP)(*FAIL) | [a-e]+ x }xmsg; print ''; ;; printf qq{'$_' } for $s =~ m{ (?: [bd]+ (*SKIP)(*FAIL))? [a-e]+ x }xmsg; print ''; " 'aax' 'bbbx' 'ccccx' 'ddx' 'ex' 'aax' 'ccccx' 'ex' 'aax' 'ccccx' 'ex'