in reply to Help to improve script that searches a file for key words in a second file.

I believe that \b seems to already do what you want with dashes, and you should be able to reduce your regex down to just: $annotation =~ /\b$term\b/i

See below.
#!/usr/bin/perl use strict; use warnings; my @words=qw(one); my $text=<<END; This is one (will match) This is onething (won't match) This is one-thing. (will match) This is a thing-one (will match) This is a -one (will match) This is a _one (won't match) END foreach my $line (split(/\n/,$text)) { foreach my $word (@words) { if ($line =~ /\b$word\b/) { print "matched [$line]\n"; } else { print "not matched [$line]\n"; } } }
  • Comment on Re: Help to improve script that searches a file for key words in a second file.
  • Download Code

Replies are listed 'Best First'.
Re^2: Help to improve script that searches a file for key words in a second file.
by mastarr (Novice) on Apr 01, 2013 at 18:06 UTC

    Oh great! Thank you very much! That was a very clear and informative little script!