in reply to Regex and loop question

Forgive me my question, I know about nothing of Tk, but if you already use a RegEx, why don't you let it do all the work for you? Something like this
#!perl use strict; use warnings; my $string = "I don't know what to do ** when I do this"; if ($string =~ /(.*?\*\*.*?)(\b[a-zA-Z']+\b)/) { print "Match <$2> at offset " . length($1) . $/; } else { print "No match" . $/; } my @chars = (split //, $2); @chars = @chars[0..9] if (@chars > 10); print join(' - ', @chars) . $/; __END__ OUTPUT: Match <when> at offset 27 w - h - e - n
should in my eyes work if $string contained the contents of the text field.
But again, I hardly know any Tk, and if there should be any problem with the code above, I'd be happy if you pointed me to it.

Replies are listed 'Best First'.
Re: Re: Regex and loop question
by perl_seeker (Scribe) on Aug 22, 2003 at 10:18 UTC
    Hi,
    Your code would also work to some extent, I guess. We could put all the text in the Tk window in a scalar
    variable, and do a regex match with the string stored in that variable.

    Your offset value of '27' would give me the character number of the starting point of the word.But I would
    also need the line number if I am to do something further with the word, for example change the color of the word using code like this:
    $t->tagConfigure("wrong",-foreground=>"red"); $t->tagAdd("wrong","$start","$end_pt");
    Tk text widget indices are of the form: line no.char no, chars begin at 0, lines begin at 1

    i.e. I would need => line no.27

    Thanx for the regex bit of code anyway.
    :)