in reply to grep match variable position

You can just refine the regex within the grep. In this case using the block form of grep is I think better... If the stop word is either at the beginning or at the end of the "$word", then it is a match.
#!/usr/bin/perl use warnings; use strict; my @words = ("of the blue","the blue of","out of blue"); my @stopwords= ("a","the","of"); foreach my $word (@words) { if (grep{$word =~ /^$_/i or $word =~ /$_$/i}@stopwords) { print "Stopword $word discarded\n"; } else { print "$word is not a stop word\n"; } } __END__ Stopword of the blue discarded Stopword the blue of discarded out of blue is not a stop word