简体   繁体   English

如何在Perl中找到正则表达式匹配的_all_位置?

[英]How can I find _all_ locations of a regex match in Perl?

I can see from this answer that if I do 我可以从这个答案看到,如果我这样做

sub match_all_positions {
    my ($regex, $string) = @_;
    my @ret;
    while ($string =~ /$regex/g) { push @ret, $-[0] }
    return @ret
}

print join ',', match_all_positions('0{3}', '001100010000');

I get 我明白了

4,8

What do I need to do to get the indexes of all matches, even when the overlap, such as positions 8 and 9 in the example above? 我需要做什么来获得所有匹配的索引,即使重叠,例如上面示例中的位置8和9?

I can do 我可以

sub match_all_positions_b  {
    my ($substr, $string) = @_;
    return unless index($string, $substr) > 0;
    my @res;
    my $i = 0;
    while ($i <= (length($string) - $length)) {
        $i = index($string, $substr, $i);
        last if $i < 0;
        push @res, $i++;
    }
    return @res;
}

print join ',', match_all_positions_b('000', '001100010000');

which just lets me match a substring, or 它只是让我匹配一个子字符串,或

sub match_all_positions_c {
    my ($substr, $string) = @_;
    my $re = '^' . $substr;
    my @res;
    for (0..(length($string) - $length)) {
         push @res, $_ if substr($string, $_) =~ /$re/;
    }
    return @res;
}

print join ',', match_all_positions_c('0{3}', '001100010000');

Which is twice as slow. 这是慢两倍。

is there a way to get all matches, even when they overlap? 是否有办法获得所有比赛,即使它们重叠? Or should I just take the speed loss because it's inherent to using regex matches? 或者我应该采取速度损失,因为它是使用正则表达式匹配固有的?

You need to update your regex for zero-width look-ahead matching. 您需要更新正则表达式以进行零宽度前瞻匹配。

Try calling your function like this: 尝试像这样调用你的函数:

print join ',', match_all_positions('(?=0{3})', '001100010000');

If you want to find the positions at which it matches: 如果要查找匹配的位置:

my @matches;
push @matches, "$-[1]:$+[1]" while "aabbcc" =~ /(?=(a.*c))/sg;

Output: 输出:

0:6
1:6

If you want all possible matches, 如果你想要所有可能的比赛,

local our @matches;
"aabbcc" =~ /(a.*?c)(?{ push @matches, "$-[1]:$+[1]" })(?!)/s;

Output: 输出:

0:5
0:6
1:5
1:6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM