Skip to content
Tcl Source Code View Ticket
Login

View Ticket

string match failure with meta-characters

Closed / Invalid787339ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-08-12 11:21:42
string match fails if pattern contains the meta-chars *, 
\ , [, ] or ?, even using backslash in pattern (as man 
page states).
* and ? meta  chars are always interpreted as meta, 
even if backslashed.
The remaining ones are never matched.

Reproducible bug:

string match "\*" "sjdjshddsdsj"
gives 1

set a "jashjdhasjd\\"
string match "\\" $a
gives 0

Expected result:
for the first example:
false, because trailing backslash should force the * to 
be interpreted as literal.

for the second example:
true, because $a contains a backslash

How to circumvent:
use regexp with ascii encoding (braces are mandatory):

regexp {\x02a} "hsajdhds*sadd"
gives 1
while
regexp {\x02a} "hsajdhdssadd"
gives 0, and so on.

\ = 05c 
* = 02a
? = 03f
[ = 50b
] =  05d

[email protected]
dkfplain2003-08-12 20:31:10
Logged In: YES 
user_id=79902

You're getting caught out by the standard Tcl quoting rules
which always apply.  Try:
  string match {\*} $yourString
instead of:
  string match "\*" $yourString
though the following should work too:
  string match "\\*" $yourString
Log in (anonymous is fine) to add a comment.