http://qs321.pair.com?node_id=1173856


in reply to Problem with capturing all matches with regex

I was hoping that the named-capture variables would collect all of your matches, but they also only keep the last one, so as an alternative, I can only suggest embedded code (the (?:...)++ ensures no backtracking over your push @lhs code):

#!/usr/bin/perl use 5.010; use Data::Dumper; my $equation = '979x + 87y - 8723z = 274320'; my @lhs; die "match failed" unless $equation =~ / ^ (?: (?<coeff>.*?) (?<var>[xyz]) (?{ push @lhs, $+{coeff}, $+{var} }) )++ \s* = \s* (?<rhs>.*) $ /ix; my $rhs = $+{rhs}; say Dumper([\@lhs, $rhs]);

This approach also allows you to construct something more interesting than a flat list, should that be desirable.

Good Day,
    Dean