考虑以下Perl风味的产品? (我想不是吗?)regex to test if a string is a palindrome:

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

try it here

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

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

给出一个错误
===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⏏|.?)$

我对(Python)正则表达式有一定的了解,当它说“量化符什么都不量化”时,我明白了它的意思,但是当我要求递归而不是量化时,我不明白为什么它这么说。

我对Perl的了解不足,无法知道为什么它不喜欢反斜杠(另一个错误)。

我确实尝试过弄乱语法并四处搜索,但是就我和互联网而言,此正则表达式有效,而摆弄它会产生我无法理解的各种其他错误。

我究竟做错了什么?

最佳答案

一种方法是这样的:

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

09-08 04:25