Perl6 assign regex match groups to variables

  • A+
Category:Languages

In P5, I am able to do something like this

my ($var1, $var2, $var3) = $string =~ /(.+)/s(.+)/s(.+)/;

How do I do the same in Perl 6? If I do the same syntax, the $var1 will hold the entire $string value.

 


my $string = 'foo bar baz';  my $match = $string ~~ /(.+)/s(.+)/s(.+)/; say $match;     # 'foo bar baz' say $match[0];  # 'foo' say $match[1];  # 'bar' say $match[2];  # 'baz'  my ($foo, $bar, $baz) = @$match; say $foo;       # 'foo' say $bar;       # 'bar' say $baz;       # 'baz' 

therefore

my ($foo, $bar, $baz) = @($string ~~ /(.+)/s(.+)/s(.+)/); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: