in reply to How to capture backwards using regex?

Another possibility, passes all of LanX's tests and doesn't need 5.38 (still needs at least Perl 5 for look-ahead):
my ($after) = $str =~ /^.{$pos}(\d*)/; my ($before) = length $after ? reverse($str) =~ /(?=(\d*))(?=.{$pos}$)/ : ""; my $match = reverse($before) . $after;

Update: I wasn't able to use Grimy's trick to solve the problem. Anyone? I tried hundreds of variations of the following:

$str =~ /^.{$pos} ( (?<= (?= \D | ^ | (?1) (\d\d) ) ) ) (\d+) /x;

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: How to capture backwards using regex?
by jo37 (Curate) on Nov 11, 2024 at 15:46 UTC
    ... use Grimy's trick to solve the problem.

    Got some partial success. One of the problems is to capture the content of the lookbehind. As an assertion itself has zero length, capturing has to be done inside the assertion, but there is no part of the regex that contains the complete content. So I had to collect the content character-wise and had to suppress non-digits somehow. The final trouble is a failing "Nr at start" case. I have no idea how to solve this one. The rest of LanX's tests pass.

    our $pre; my $prefix; my $match = $str =~ m{^.{$pos} (?{local $pre = ''}) ( (?<= (?= \D | ^ | (?1)) (.) (?{my $m2 = $2; $pre .= $m2 if $m2 =~ m{\d}}) ) (?{$prefix = $pre}) ) (\d+)}x; my $suffix = $3; say $match ? $prefix . $suffix : '';

    Note: This is not meant as a solution to the OP. It's just an exercise.

    Update:

    Shifting the lookbehind by one position to the right solves the issue with "Nr at start". Now passes all of LanX's tests.

    my $match = $str =~ m{^.{$pos} (?{local $pre = ''}) \d ( (?<= (?= \D | ^ | (?1)) (?: \D | (\d)) (?{$pre .= $2 if defined $2}) ) (?{$prefix = $pre}) ) (\d*)}x;

    Update 2: added defined

    Update 3: polished up

    my ($start, $end); my $res = $str =~ m{ ^.{$pos} \d ( (?<= (?= (?: \D | ^ ) (?{$start = pos}) | (?1)) . ) ) (\d*)(?{$end = pos}) }x ? substr($str, $start, $end - $start) : undef ;

    Greetings,
    🐻

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re^2: How to capture backwards using regex?
by LanX (Saint) on Nov 11, 2024 at 19:40 UTC
    > passes all of LanX's tests and doesn't need 5.38

    Are you implying that 5.38 is needed for my code?

    I didn't bother to check version dependencies...

    Anyway I could think of other workarounds but the pos and \G solution is hard to beat.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      > Are you implying that 5.38 is needed for my code?

      No, sorry, I misread what you wrote:

      > Unfortunately with variable length this still seems experimental with 5.38 ...

      In fact, 5.30 is enough to run it, see perlre:

      Prior to Perl 5.30, it worked only for fixed-width lookbehind, but starting in that release, it can handle variable lengths from 1 to 255 characters as an experimental feature.

      The (*pla:pattern) syntax is from 5.28, in earlier versions, the only possible syntax was (?<=).

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        > (*pla:pattern) syntax is from 5.28

        Yes I was aware about this being "new", but honestly I really have problems remembering the old syntax.

        > Prior to Perl 5.30, it worked only for fixed-width lookbehind

        I think a more interesting task would be to show applications of *plb or *nlb which can't be emulated with \G

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery