[ Team LiB ] Previous Section Next Section

Searching Text

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 36-4 summarizes the options to the search operation:

Table 36-4. Options to the search operation

-forward

Searches forward from index. This is the default.

-backward

Searches backward from index.

-exact

Matches pattern exactly. This is the default.

-regexp

Uses regular expression pattern matching.

-nocase

Lowercase letters in pattern can match upper case letters.

-count varName

Returns 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"
    [ Team LiB ] Previous Section Next Section