Well, you can make a combined regex like '\b'.join("|",@words).'\b', but unfortunately that regex will be compiled every time (I tried with a qr//, but at least the Tk I use can't handle that). It's also annoying that you have to rediscover the wordlength to be able to determine the proper range. So you can make it work O(textlength*numberOfWords) which might be fast enough, but it indeed gets awkward.

So doing it at text insertion time is probably simpler:

#!/usr/bin/perl -w use strict; use Tk; my $top = MainWindow->new; my $t = $top->Text(); $t->tagConfigure("boring", -foreground => "blue"); $t->tagConfigure("special", -foreground => "red"); # Change the match rule according to taste my @words = qw(your be is silly); my $regex = join("|", @words); $regex = qr/(.*?)\b($regex)\b/s; my $text = "If you want to be immune from silly letters, don't carry y +our monomark in your hat.\n"; while ($text =~ /$regex/gc) { $t->insert("end", $1, "boring"); $t->insert("end", $2, "special"); } $t->insert("end", $1, "boring") if $text =~ /(.+)/gs; $t->pack; MainLoop;
This should avoid too many regex recompiles, and give perl a chance to build a good matcher. Maybe it's good enough for what you want ?

If that's not good enough, one way might be to build a statemachine for the match by hand and operate on the characters as they come in at the same time as you are putting them in the widget. Though that's guaranteed linear time, it's quite a bit of work per character, so I don't expect it to win until you have a lot of words. It might be useable to handle userinput while he's typing though. Surprisingly I don't directly see a perl module on CPAN (using a search on "DFA") to generate such a statemachine from a set of words.


In reply to Re^3: Coloring text using TK? by thospel
in thread Coloring text using TK? by Elijah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.