How can I find the location of a regex match in Perl?
The built-in variables @-
and @+
hold the start and end positions, respectively, of the last successful match. $-[0]
and $+[0]
correspond to entire pattern, while $-[N]
and $+[N]
correspond to the $N
($1
, $2
, etc.) submatches.
Forget my previous post, I've got a better idea.
sub match_positions {
my ($regex, $string) = @_;
return if not $string =~ /$regex/;
return ($-[0], $+[0]);
}
sub match_all_positions {
my ($regex, $string) = @_;
my @ret;
while ($string =~ /$regex/g) {
push @ret, [ $-[0], $+[0] ];
}
return @ret
}
This technique doesn't change the regex in any way.
Edited to add: to quote from perlvar on $1..$9. "These variables are all read-only and dynamically scoped to the current BLOCK." In other words, if you want to use $1..$9, you cannot use a subroutine to do the matching.