user1134991
user1134991

Reputation: 3103

Can one Match the charachter * and ? using string match in Tcl?

I am trying to get matching of the character * using Tcl string match, and fail, with a preceding backslash and w/o it
I can do it with regexp, but wanted to know if there is a way w/o it.
See Tcl log below, for fail:

% puts [ string match {\*} {hop} ]
0
% puts [ string match {\*} {hop*} ]
0
% puts [ string match {*} {hop} ]
1

As can be seen using {*} just matches everything, and using {\*} does not match hop*

Upvotes: 1

Views: 78

Answers (1)

glenn jackman
glenn jackman

Reputation: 246827

You are testing that the string equals a single *.

% string match {\*} {hop*} 
0

To test that the string contains a *, you need some more globbing:

% string match {*\**} {hop*} 
1

or

% expr {[string first {*} {hop*}] > -1}
1

Upvotes: 3

Related Questions