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

In reply to Re^2: How to capture backwards using regex? by jo37
in thread How to capture backwards using regex? by harangzsolt33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.