Home > Blockchain >  Perl regular expression named groups array
Perl regular expression named groups array

Time:10-04

I'm trying to capture named groups in an array.

use strict;
use warnings;
use Data::Dumper;

my $text = 'My aunt is on vacation and eating some apples and banana';
$text =~ m/(?<Letter>A.)/img ;
print Dumper(%-) ;

output is

$VAR1 = 'Letter';
$VAR2 = [
          'au'
        ];

but I would expect or actually was hoping that any occurrence will appear in the array.

$VAR1 = 'Letter';
$VAR2 = [
          'au',
          'ac',
          'at',
          'an',
          'at',
          'an'
        ];

Any chance to get all groups in one array?

CodePudding user response:

You can either run the m//g in list context to get all the matching substrings (but without the capture names), or you need to match in a loop:

my @matches = $text =~ m/(?<Letter>A.)/img ;
print Dumper(\@matches) ;

or

while ($text =~ m/(?<Letter>A.)/img) {
       print Dumper(\% ) ;
}
  • Related