|
|
| No such thing as a small change | |
| PerlMonks |
Re: Possessive sub-pattern with non-greedy content + recursion: WHY does this work??by ikegami (Patriarch) |
| on Aug 04, 2025 at 21:09 UTC ( [id://11165956]=note: print w/replies, xml ) | Need Help?? |
|
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.
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
Same, but using named subpatterns:
(*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 Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||