【发布时间】:2017-10-13 08:48:42
【问题描述】:
当我使用捕获组创建regex 变量时,整个匹配都可以,但捕获组是Nil。
my $str = 'nn12abc34efg';
my $atom = / \d ** 2 /;
my $rgx = / ($atom) \w+ ($atom) /;
$str ~~ / $rgx / ;
say ~$/; # 12abc34
say $0; # Nil
say $1; # Nil
如果我修改程序以避免$rgx,一切都会按预期进行:
my $str = 'nn12abc34efg';
my $atom = / \d ** 2 /;
my $rgx = / ($atom) \w+ ($atom) /;
$str ~~ / ($atom) \w+ ($atom) /;
say ~$/; # 12abc34
say $0; # 「12」
say $1; # 「34」
【问题讨论】:
-
有趣的问题。我不确定为什么会发生这种情况,但您可以使用例如
$rgx命名正则表达式my regex rgx { ($atom) \w+ ($atom) }。然后在$str ~~ / <rgx>/之后,$<rgx>[0]代表第一个捕获组(例如)。 -
谢谢!!不知道命名的正则表达式。
标签: raku