in reply to Possessive sub-pattern with non-greedy content + recursion: WHY does this work??

Gonna start with this:

(?>^.*?)
simplifies to
^

.*? will always starting by attempting to match 0 characters. And the (?>...) prevents it from matching anything else.

(All I have time for now.)

Replies are listed 'Best First'.
Re^2: Possessive sub-pattern with non-greedy content + recursion: WHY does this work??
by Anonymous Monk on Aug 04, 2025 at 19:39 UTC
    simplifies to ^

    Oh... I didn't understand the whole construct, then. Thanks! I guess "Possessive sub-pattern with non-greedy..., etc." can be stricken out from title. To be replaced with, start-of-string anchor and failure? Because,

    Match fails -- it's irrelevant

    But looks like it is. And, in parallel, comparing with look-ahead, which at 1st glance can be a simpler alternative (?):

    say $-[1], ': ', $1 while 'olololo' =~ / (?= ( (.)(?-2)\g-1 | (.).\g-1 ) ) /gx; say '*'; say 'match' if 'olololo' =~ / ^ ( ( (.)(?-3)\g-1 | (.).\g-1 ) (?{ say $-[2], ': ', $2 }) ) (?#*F) /x;

    Output:

    0: olololo 1: lolol 2: ololo 3: lol 4: olo * 4: olo 3: lol 2: ololo 2: olo 1: lolol 0: olololo match

    Look-ahead misses some PD's for obvious reasons. But match success in case of recursion leads to omission of 2 PD's, too. Why? Converting "comment" to "fail" i.e. (F*) gives all nine PD's, visited exactly once each (with some disruption to "visit in reverse" order):

    4: olo 3: lol 2: ololo 2: olo 1: lolol 0: olololo 1: lol 0: ololo 0: olo

      omission of 2 omission of 3