cat*_*cat 3 regex perl perl6 quantifiers
考虑以下Perl风味?(我想不是?)正则表达式来测试一个字符串是否是回文:
^((.)(?1)\2|.?)$
Run Code Online (Sandbox Code Playgroud)
试试吧.
下列
my regex palindrome {
^((.)(?1)\2|.?)$
}
say "$word is a palindrome"
if $word ~~ /<palindrome>/
&& $word.chars > 1;
Run Code Online (Sandbox Code Playgroud)
给出错误
===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?|.?)$
Run Code Online (Sandbox Code Playgroud)
我对(Python)正则表达式有一定的了解,当我说"量化器没有量化"时我得到了它的意思,但是当我要求递归而不是量化时,我不明白为什么它会这样说.
我没有足够的Perl知识来知道为什么它不喜欢反斜杠(另一个错误).
我确实试着搞乱语法和搜索,但就我和互联网而言,这个正则表达式工作,并且摆弄它会产生我不会得到的各种其他错误.
我究竟做错了什么?
一种方法是:
my regex palindrome { (.) <~~> $0 || .? }
say "aba" ~~ /^<palindrome>$/;
Run Code Online (Sandbox Code Playgroud)