Just to clarify, when used in an alternation in list context, a capture group always returns something if it matches or not. If it does not match, undef is returned. One way to filter out these undefs is with a grep.
>perl -wMstrict -MData::Dump -le "my $s = 'AAA BBB CCC DDD AAA BBB'; ;; my @captures = $s =~ m{ (AAA) | (BBB) | (XXX) | (YYY) }xmsg; dd \@captures ;; my @matches = grep defined, $s =~ m{ (AAA) | (BBB) | (XXX) | (YYY) }x +msg; dd \@matches; " [ "AAA", undef, undef, undef, undef, "BBB", undef, undef, "AAA", undef, undef, undef, undef, "BBB", undef, undef, ] ["AAA", "BBB", "AAA", "BBB"]
Update: Another way to capture only matches is with the "branch reset" extended pattern (see "(?|pattern)" in Extended Patterns in perlre) available with Perl version 5.10+.
>perl -wMstrict -MData::Dump -le "use 5.010; ;; my $s = 'AAA BBB CCC DDD AAA BBB'; ;; my @matches = $s =~ m{ (?| (AAA) | (BBB) | (XXX) | (YYY) ) }xmsg; dd \@matches " ["AAA", "BBB", "AAA", "BBB"]
In reply to Re^2: Problems counting regex matches
by AnomalousMonk
in thread Problems counting regex matches
by elcilorien
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |