my ($cnt_rex, $cnt_match) = (0, 0); while () { ++$cnt_rex; next unless /$SEARCH/i; ++$cnt_match; #.... do your processing here .... }