my $line = 'This line has a hit here and a hit there.'; my $word = 'hit'; my $count = 0; my $hilit = ''; while ($line =~ /(.*?)\b($word)\b/sgci) { $hilit .= "$1[$2]"; ++$count; } $hilit .= substr($line, pos($line)); print "$hilit\n"; print "$count occurrences of $word\n";