vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I want to create an array from all matches in parenthesis. Something like:
my @arr = $string_containing_needed_matches = /XXX(re)YYY/g;
It works for for the entire expression, but I need to collect only from () or $1s.
Of course I know how to do this in 2 stages, but I want it to be in one expression, which I know is possible.
  • Comment on collecting array from pattern parenthesis

Replies are listed 'Best First'.
Re: collecting array from pattern parenthesis
by kyle (Abbot) on Sep 26, 2008 at 15:11 UTC

    You're close enough already that I wonder if I'm misunderstanding the question.

    use strict; use warnings; use Data::Dumper; my $string_containing_needed_matches = 'XXXreYYY' x 3; my @arr = $string_containing_needed_matches =~ /XXX(re)YYY/g; print Dumper \@arr; __END__ $VAR1 = [ 're', 're', 're' ];
      Yes, I am. I do not know why it did not work before. Now it works and thaks a lot.
        It didn't work because you wrote = instead of =~.