Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Problem with capturing all matches with regex

by toolic (Bishop)
on Oct 12, 2016 at 14:26 UTC ( [id://1173841]=note: print w/replies, xml ) Need Help??


in reply to Problem with capturing all matches with regex

I changed 3 things:

  • Got rid of ^ anchor
  • Got rid of + at end
  • Added //g for global matching
use warnings; use strict; use Data::Dumper; my $equation = '979x + 87y - 8723z = 274320'; my @parts = ($equation =~ /(?:(.*?)([xyz]))/ig); print Dumper(\@parts); __END__ $VAR1 = [ '979', 'x', ' + 87', 'y', ' - 8723', 'z' ];

Tip #9 from the Basic debugging checklist ... YAPE::Regex::Explain

Replies are listed 'Best First'.
Re^2: Problem with capturing all matches with regex
by igoryonya (Pilgrim) on Oct 12, 2016 at 14:44 UTC
    OK, I've abbreviated my problem @ first, but your answer suggested, that I should tell the whole story, as why I didn't use /g, for example.
    I've updated my original post.
      #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1173839 use strict; use warnings; my $equation = '979x + 87y - 8723z = 274320'; #my @parts = ($equation =~ /^(?:(.*?)([xyz]))+/i); my @parts = grep defined, $equation =~ /(.*?)([xyz])|(.*?)=(.*)/gi; use Data::Dumper; print Dumper \@parts;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1173841]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-19 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found