use strict; my $toFind="String"; while(){ my $line=$_; chomp($line); if($line=~m/\b$toFind(?=[^\w\.]+|$)/i){ print "$line\n"; } } #### __DATA__ blah blah string blah important stringstuff we wish to keep Unimportant string more stuff we wish to String.o Some more unimportant String.c