Searching Text

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 33.  The Text Widget


The search operation scans the text widget for a string that matches a pattern. The index of the text that matches the pattern is returned. The search starts at an index and covers all the text widget unless a stop index is supplied. You can use end as the stop index to prevent the search from wrapping back to the beginning of the document. The general form of the search operation is this:

 $t search ?options? pattern index ?stopIndex? 

Table 33-4 summarizes the options to the search operation:

Table 33-4. Options to the search operation.
-forwardSearches forward from index. This is the default.
-backwardSearches backward from index.
-exactMatches pattern exactly. This is the default.
-regexpUses regular expression pattern matching.
-nocaseLowercase letters in pattern can match upper case letters.
-count varNameReturns in varName the number of characters that matched pattern.
--Ends the options. Necessary if pattern begins with -.

If you use a regular expression to match a pattern, you may be interested in how much text matched so you can highlight the match. The -count option specifies a variable that gets the number of matching characters:

 set start [$t search -count cnt -regexp -- $pattern 1.0 end] $t tag add sel $start "$start +$cnt chars" 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net