in reply to Re: How to capture backwards using regex?
in thread How to capture backwards using regex?

... 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$