[Wellington-pm] capturing quantified matches
Grant McLean
grant at mclean.net.nz
Tue Oct 4 12:53:21 PDT 2011
On Tue, 2011-10-04 at 21:45 +1300, Richard Hector wrote:
> Hi all,
>
> I'm trying to use a regex like this:
>
> ($foo, $bar, @baz) = /^(xx)\s(yy)\s(zz)*$/
>
> but it seems I can't get all the arbitrary number of values from the 3rd
> capture. Any suggestions for that?
I don't think a single bracketed section in a regex can return multiple
values unless you're using /g
One alternative approach is to use two matches with the second one
picking up at the end of the first:
my $str = 'one two three four five';
my @match1 = $str =~ /^(\w+) (\w+)/gc;
print Dumper(\@match1);
my @match2 = $str =~ /\G (\w+)/g;
print Dumper(\@match2);
Which outputs
$VAR1 = [
'one',
'two'
];
$VAR1 = [
'three',
'four',
'five'
];
That's harder to understand than your code though.
Cheers
Grant
More information about the Wellington-pm
mailing list