use strict; use warnings; my $line = 'This line has a hit here and a hit there.'; my $word = 'hit'; my $count = $line =~ s/\b($word)\b/[$1]/gi; print "$line\n"; print "$word was found $count times\n"; __END__ This line has a [hit] here and a [hit] there. hit was found 2 times