Updated 2014-08-23 02:17:59 by AMG

Synopsis  edit

string match ?-nocase? pattern string

Description  edit

Determine whether pattern matches string, returning return 1 if it does, 0 if it doesn't. If -nocase is specified, then the pattern attempts to match against the string in a case insensitive manner.

string equal compares strings literally, but string match matches interprets a pattern expression and matches a string against that.

For the two strings to match, their contents must be identical except that the following special sequences may appear in pattern:
*
Matches any sequence of characters in string, including a null string.
?
Matches any single character in string.
[chars]
Matches any character in the set given by chars. If a sequence of the form x-y appears in chars, then any character between x and y, inclusive, will match. When used with -nocase, the end points of the range are converted to lower case first. Whereas {[A-z]} matches '_' when matching case-sensitively ('_' falls between the 'Z' and 'a'), with -nocase this is considered like {[A-Za-z]} (and probably what was meant in the first place).
\x
Matches the single character x. This provides a way of avoiding the special interpretation of the characters *?[]\ in pattern.

Beware that the parsing of strings inside grouping [] is not particularly robust -- neither the manual, the tests nor the code takes pains to specify how to interpret combinations of \[]*?- inside brackets. If you need a character class which includes any of these special characters, you are probably better off with a regexp. (see also [1]).

string match does not use the same code as glob

JJM - With the primary notable difference being that glob supports the notion of (optionally nested?) curly braces allowing for a logical OR-style operation in the pattern.

Example  edit

string match *is* "this test is" ;# -> true

Layers of Quoting  edit

to match a single left bracket, the match pattern should be a backslash followed by a left bracket so that string match sees the left bracket as a literal character. One possibility is to place the backslash and left bracket in curly quotes so that Tcl leaves them alone:
string match {\[} {[}

Alternatively, the backslash could be preceded by a backslash and the left bracket could be preceded by a backslash:
string match \\\[ \[

Pattern Ending in Backslash  edit

A pattern ending in a backslash doesn't match a string ending in a backslash. Bug?
string match a\\ a\\
# -> 0

See Also  edit