Access to match parts in Perl 6

When I use a named regular expression, I can print its contents:

my regex rgx { \w\w };
my $string = 'abcd';

$string ~~ / <rgx> /;
say $<rgx>; # 「ab」

But if I want a match with an adverb :gor :ex, therefore, there is more than one match, it does not work. Following

my regex rgx { \w\w };
my $string = 'abcd';

$string ~~ m:g/ <rgx> /;
say $<rgx>; # incorrect

gives an error:

Type List does not support associative indexing.
  in block <unit> at test1.p6 line 5

How do I change the code?

UPD: Based on the description of @piojo, I changed the last line as follows and solved my problem:

say $/[$_]<rgx> for ^$/.elems;

The following would be simpler, but for some reason it does not work:

say $_<verb> for $/; # incorrect
+4
source share
1 answer

, :g :overlap : , / <rgx>* /, $<rgx>[0], $<rgx>[1] .. . , , $/. , $<foo> $/<foo>.

, , $/ . , $/[0]<rgx> $/[1]<rgx>.

+5

Source: https://habr.com/ru/post/1688440/


All Articles