Perl: Array of all regexp captures
Perl v5.25.7 added support for the @{^CAPTURE}
variable which wrapped all the regexp parenthesis captures up into an array. If you need this functionality in a version of Perl older than that you can use this function:
my $str = "Hello Jason Doolis";
$str =~ /Hello (\w+) (\w+)/;
my @captures = get_captures(); # ("Jason", "Doolis")
sub get_captures {
my $str = $&;
my @captures = ();
# Build array using the offsets from @- and @+
for (my $i = 1; $i < @-; $i++) {
my $start = $-[$i];
my $end = $+[$i];
my $len = $end - $start;
my $str = substr($str, $start, $len);
push(@captures, $str);
}
return @captures;
}