Search code examples
regexperlrakuquantifiers

"Quantifier quantifies nothing" but I never asked for a quantifier


Consider the following Perl-flavoured?? (i suppose not?) regex to test if a string is a palindrome:

^((.)(?1)\2|.?)$

try it here.

the following

my regex palindrome {
    ^((.)(?1)\2|.?)$
}

say "$word is a palindrome"
    if $word ~~ /<palindrome>/
    && $word.chars > 1;

gives an error

===SORRY!===
Quantifier quantifies nothing
at /home/cat/projects/perl6/code/misc/words.pl6:6
------>   ^((.)(?⏏1)\2|.?)$
Unrecognized backslash sequence (did you mean $1?)
at /home/cat/projects/perl6/code/misc/words.pl6:6
------>   ^((.)(?1)\2⏏|.?)$

I have a working knowledge of (Python) regexes, and I get what it means when it says "Quantifier quantifies nothing", but I don't get why it says this when I was asking for recursion, not a quantification.

I don't have enough knowledge of Perl to know why it doesn't like the backslash (the other error).

I did try messing around with the syntax and searching around, but as far as I and the internet are concerned, this regex works, and fiddling with it generates various other errors I don't get.

What am I doing wrong?


Solution

  • One way to do it is this:

    my regex palindrome { (.) <~~> $0 || .? }
    say "aba" ~~ /^<palindrome>$/;