Count overlapping regex matches in Perl OR Ruby

前端 未结 1 1907
庸人自扰
庸人自扰 2021-01-22 15:55

This is a follow-up to that question. I\'ve learned that finding overlapping regex matches in Python is not straight-forward, so decided to do an additional inquiry to see h

1条回答
  •  醉梦人生
    2021-01-22 16:21

    It looks like tchrist has done all the hard work. If storing the matches and counting them afterwards is eating too much resource, then you could just change the regex-embedded code to just count the matches:

    my $count = 0;
    
    "bbboobb" =~ /(b.*o.*b)(?{$count++})(*FAIL)/g;
    
    print "got $count matches\n";
    

    0 讨论(0)
提交回复
热议问题