use warnings; use strict; my %stops = map {$_=>1} qw/ I am the of and /; my %terms = ( 'manager of sales' => 1 ); my $str = 'I am the senior manager of sales and of marketing'; my ($re_stops) = map {qr/\b(?:$_)\b/i} join '|', map {quotemeta} sort { length $b <=> length $a or $a cmp $b } keys %stops; my ($re_terms) = map {qr/\b(?:$_)\b/i} join '|', map {quotemeta} sort { length $b <=> length $a or $a cmp $b } keys %terms; my @s = split /($re_terms)/, $str; for my $i (0..$#s) { if ($i%2) { print "*", $s[$i], "*\n"; } else { $s[$i] =~ s/$re_stops//g; $s[$i] =~ s/^\s+|\s+$//g; print $s[$i], "\n"; } } __END__ senior *manager of sales* marketing