I am using Tcl_StringCaseMatch
function in C++ code for string pattern matching. Everything works fine until input pattern or string has []
bracket
The problem is []
are special characters in the pattern matching. You need to escape them using a backslash to have them treated like plain characters
pattern= "pq\\[*\\]"
I don't think this should affect the string as well. The reason for double slashing is you want to pass the backslash itself to the TCL engine.
For the casual reader:
[]
have a special meaning in TCL in general, beyond the pattern matching role they take here - "run command" (like `` or $()
in shells), but [number]
will have no effect, and the brackets are treated normally - thus the string str1
does not need escaping here.
For extra confusion:
TCL will interpret ]
with no preceding [
as a normal character by default. I feel that's getting too confusing, and would rather that TCL complains on unbalanced brackets. As OP mentions though, this allows you to forgo the final two backslashes and use "pq\\[*]"
. I dislike this, and rather make it obvious both are treated normally and not the usual TCL way, but to each her/is own.