tags:

views:

1250

answers:

4

I need to write a function that receives a string and a regex. I need to check if there is a match and return the start and end location of a match. (the regex was already compiled by qr//)

The function might also receive a "global" flag and then I need to return the (start,end) pairs of all the matches.

ps. I cannot change the regex not even adding () around it as the user might use () and \1. OK maybe I can us (?:)

ps2. given "ababab" and the regex qr/ab/, in the global case I need to get back 3 pairs of (start, end)

+4  A: 

The pos function gives you the position of the match. If you put your regex in parentheses you can get the length (and thus the end) using length $1. Like this

sub match_positions {
    my ($regex, $string) = @_;
    return if not $string =~ /($regex)/;
    return (pos, pos + length $1);
}
sub all_match_positions {
    my ($regex, $string) = @_;
    my @ret;
    while ($string =~ /($regex)/g) {
        push @ret, [pos, pos + length $1];
    }
    return @ret
}
Leon Timmermans
A: 

You can also use the deprecated $` variable, if you're willing to have all the REs in your program execute slower. From perlvar:

   $‘      The string preceding whatever was matched by the last successful pattern match (not
           counting any matches hidden within a BLOCK or eval enclosed by the current BLOCK).
           (Mnemonic: "`" often precedes a quoted string.)  This variable is read-only.

           The use of this variable anywhere in a program imposes a considerable performance penalty
           on all regular expression matches.  See "BUGS".
zigdon
+8  A: 

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 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.

Leon Timmermans
I think this still won't work if there are () in the regex
szabgab
Yeah, see my ETA.
Leon Timmermans
You can use a subroutine to do the match, but you want the captures you'll have to use substr(), @-, and @+ to extract the matches and return them to the user.
Michael Carman
Correct, but that's a particular PITA.
Leon Timmermans
+18  A: 

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.

Michael Carman