In Perl, how to count the number of occurences of successful matches based on a condition on their absolute positions

前端 未结 2 1252
星月不相逢
星月不相逢 2021-01-28 17:51

Using just one Perl substitute or matching regular expression statement , how can we modify below code:

I need to modify the value of $pattern<

2条回答
  •  無奈伤痛
    2021-01-28 18:20

    You can use the perl special variable @LAST_MATCH_START to get the output:

    use strict;
    use warnings;
    
    my $pattern = "F1";  
    my $string  = "F1234F12F1F1234F12F13";     
    my $count;      
    while ( $string =~ /$pattern/g ) {
        $count++ if $-[0] % 5 == 0;
    } 
    print $count;
    

    Output:

    4
    

提交回复
热议问题