Table of Contents
regexp ?switches? exp string ?matchVar? ?subMatch1 ...
subMatchN?Searches
string for the regular
expression
exp. If a parameter
matchVar is given, then the substring that
matches the regular expression is copied to
matchVar. If
subMatchN
variables exist, then the parenthetical parts of the matching
string are copied to the
subMatch
variables, working from left to right.
regsub ?switches? exp string subSpec varNameSearches
string for substrings that
match the regular expression
exp and
replaces them with
subSpec. The resulting
string is copied into
varName.
The regular expression (exp) in the two
regular expression parsing commands is evaluated by the Tcl parser
during the Tcl substitution phase. This can provide a great deal of
power, and also requires a great deal of care.
These examples show some of the trickier aspects of regular expression evaluation. The fields in each example are discussed in painful detail in the most verbose level.
The points to remember as you read the examples are:
A left square bracket ([) has meaning to the substitution phase, and to the regular expression parser.
A set of parentheses, a plus sign, and a star have meaning to the regular expression parser, but not the Tcl substitution phase.
A backslash sequence (\n, \t, etc) has meaning to the Tcl substitution phase, but not to the regular expression parser.
A backslash escaped character (\[) has no special meaning to either the Tcl substitution phase or the regular expression parser.
The phase at which a character has meaning affects how many escapes are necessary to match the character you wish to match. An escape can be either enclosing the phrase in braces, or placing a backslash before the escaped character.
To pass a left bracket to the regular expression parser to evaluate as a range of characters takes 1 escape. To have the regular expression parser match a literal left bracket takes 2 escapes (one to escape the bracket in the Tcl substitution phase, and one to escape the bracket in the regular expression parsing.). If you have the string placed within quotes, then a backslash that you wish passed to the regular expression parser must also be escaped with a backslash.
Note: You can copy the code and run it in tclsh or wish to see the effects.