in reply to Re: Problem with capturing all matches with regex
in thread Problem with capturing all matches with regex

So, this means, I have to do it this way (according to my updated version):
my $equation = '979x + 87y - 8723z = 274320'; my @parts = ($equation =~ /(.*?)([xyz])/ig); push(@parts, ($equation =~ /\G(.*)=(.*)/));
Too bad, I thought, I could do it in one swipe.

Replies are listed 'Best First'.
Re^3: Problem with capturing all matches with regex
by AnomalousMonk (Archbishop) on Oct 12, 2016 at 16:25 UTC
    ... do it in one swipe.

    See tybalt89's post for a one-swiper. If you don't like the  grep defined, ... before the array assignment and if you have Perl version 5.10+ which supports the  (?| ... | ... ) "branch reset" grouping, there's this:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use 5.010; ;; my $equation = '979x + 87y - 8723z = 274320'; my @parts = $equation =~ m{ \G (?| (.*?)([xyz]) | (.*) = (.*) \z) }xm +sig; dd \@parts; " [979, "x", " + 87", "y", " - 8723", "z", " ", " 274320"]

    Update: Please see  "(?|pattern)" in Extended Patterns in perlre.


    Give a man a fish:  <%-{-{-{-<

      Awesome, I am glad, I've posted this question, I've learned so many things about regex, that didn't grasp from several times of rereading various perlre* tuts/docs in perl's documentation.