my @words = ("while", "foreach", "loop"); foreach my $word (@words) { my $word_len = length $word; my $next = "1.0"; while (my $from = $t->search(-regexp, "\\b$word\\b", $next, "end")) { $next = "$from + $word_len chars"; $t->tagAdd("green", $from, $next); $t->tagAdd("bold", $from, $next); } }