#! perl -slw use strict; my $word = $ARGV[0] or die "No search term"; ( my $text = do{ local $/; } ) =~ tr[\n][]d; $text =~ s[ ( (?: \S+ \s+ ){1,3} ) ( $word ) [[:punct:]]* (?= ( (?: \s+ \S+ ){1,3} ) ) ][ print "$1<$2>$3" ]gex; __END__ this finds and matches and highlights matches.