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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Problem with capturing all matches with regex
by igoryonya (Pilgrim) on Oct 12, 2016 at 15:23 UTC | |
by AnomalousMonk (Archbishop) on Oct 12, 2016 at 16:50 UTC | |
by Anonymous Monk on Oct 12, 2016 at 17:52 UTC | |
by igoryonya (Pilgrim) on Oct 13, 2016 at 01:37 UTC | |
by duelafn (Parson) on Oct 13, 2016 at 09:07 UTC | |
by igoryonya (Pilgrim) on Oct 15, 2016 at 07:20 UTC | |
|
In Section
Seekers of Perl Wisdom