my $line = 'This line has a hit here and a hit there.'; my $word = 'hit'; my $count = (my $hilit = $line) =~ s/\b($word)\b/[$1]/gi; print "$hilit\n"; print "$count occurrences of $word\n";