|
From: newtophp2000 on 16 Jul 2005 13:05 I receive input from a text widget. I need to extract all variations of a given word. If for example, the keyword is "cat", I need to list all words that start with cat: cat, cats, catfish, etc. If I could also find out the starting location of each word, that would be great, but not necessary. I was wondering if this would be a good candidate for regular expressions or if I need to search through the string one char at a time. Any suggestions?
From: Sektor van Skijlen on 16 Jul 2005 17:18 Dnia 16 Jul 2005 10:05:40 -0700, newtophp2000(a)yahoo.com skrobie: > I receive input from a text widget. I need to extract all variations > of a given word. If for example, the keyword is "cat", I need to list > all words that start with cat: cat, cats, catfish, etc. If I could > also find out the starting location of each word, that would be great, > but not necessary. > I was wondering if this would be a good candidate for regular > expressions or if I need to search through the string one char at a > time. Any suggestions? Use lsearch -glob. With -all option it will return a list of indices for each found word. With -inline option it will return the found words rather than indices. I state, of course, that the words you search for are collected in a list. -- // _ ___ Michal "Sektor" Malecki <sektor(whirl)kis.p.lodz.pl> \\ L_ |/ `| /^\ ,() <ethourhs(O)wp.pl> // \_ |\ \/ \_/ /\ C++ bez cholesterolu: http://www.intercon.pl/~sektor/cbx "Java does not have pointers!"
From: Michael A. Cleverly on 16 Jul 2005 17:17 On Sat, 16 Jul 2005 newtophp2000(a)yahoo.com wrote: > I receive input from a text widget. I need to extract all variations > of a given word. If for example, the keyword is "cat", I need to list > all words that start with cat: cat, cats, catfish, etc. If I could > also find out the starting location of each word, that would be great, > but not necessary. Use a pattern of {cat[a-z]*} (I wouldn't use {cat\w*} because \w will also match an underscore). Use -nocase if you want to match mixed case or all upper-case instances as well. % regexp -indices -all -nocase -inline {cat[a-z]*} "The cat caught a catnap before catching a catfish" {4 6} {17 22} {31 38} {42 48} Michael
From: Schelte Bron on 17 Jul 2005 06:50 newtophp2000(a)yahoo.com wrote: > I receive input from a text widget. I need to extract all > variations > of a given word. If for example, the keyword is "cat", I need to > list > all words that start with cat: cat, cats, catfish, etc. If I > could also find out the starting location of each word, that would > be great, but not necessary. > To be able to find the starting location of each word in the text widget you would have to use the search command of the text widget. Something like this: set pattern {\mcat[a-z]*} $text tag configure highlight -foreground yellow -background red $text mark set search 1.0 while {1} { # Find one occurance of the pattern set index [$text search -count len -regexp $pattern search end] if {$index eq ""} { # Not found, exit the loop break } # Set the search mark to the end of the found pattern $text mark set search $index+${len}c # Get the matching word from the text set word [$text get $index search] # Highlight the found word in the text $text tag add highlight $index search puts "Word $word found at $index" } Schelte -- set Reply-To [string map {nospam schelte} $header(From)]
From: Wojciech Kocjan on 17 Jul 2005 10:51 Schelte Bron napisaB(a): > set pattern {\mcat[a-z]*} > $text tag configure highlight -foreground yellow -background red > $text mark set search 1.0 > while {1} { > # Find one occurance of the pattern > set index [$text search -count len -regexp $pattern search end] > if {$index eq ""} { > # Not found, exit the loop > break > } > # Set the search mark to the end of the found pattern > $text mark set search $index+${len}c > # Get the matching word from the text > set word [$text get $index search] > # Highlight the found word in the text > $text tag add highlight $index search > puts "Word $word found at $index" > } Wow. This is something I didn't know about (text search). Great example. -- WK
|
Pages: 1 Prev: remote control a tcl/tk from com1: Next: Help with freewrap |