Your question appears to be "why is the anchor needed?"
Honestly, instead of spending time analysing your code, I'd rather show how to write this cleanly. Especially since your code doesn't think 1221 is a palindrome.
First, let's define palindrome.
An empty string? No. Too weird.
A single character? No. Again, too weird.
So a palindrome looks like this:
Since the minimum length of a palindrome (as we defined it) is two, we see it's always going to be a pair of matching chars, with the following in between:
So we get
echo abABCDCBAdeXYZYXfg1221 | perl -Mv5.14 -nle' / ( (.) (?: (?-2) | .? ) \g-1 ) (?{ say $1 }) (*FAIL) /xs '
Same, but using named subpatterns:
echo abABCDCBAdeXYZYXfg1221 | perl -Mv5.14 -nle' / ( (?&PALINDROME) ) (?{ say $1 }) (*FAIL) (?(DEFINE) (?<PALINDROME> (.) (?: (?&PALINDROME) | .? ) \g-1 ) ) /xs '
(*FAIL) is reached after finding and printing a palindrome. It forces a backtrack, which is to say it causes the engine to look for another palindrome.
In reply to Re: Possessive sub-pattern with non-greedy content + recursion: WHY does this work??
by ikegami
in thread Possessive sub-pattern with non-greedy content + recursion: WHY does this work??
by Anonymous Monk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |