Escape square bracket in Tcl_StringCaseMatch

后端 未结 1 783
孤城傲影
孤城傲影 2021-01-16 05:22

I am using Tcl_StringCaseMatch function in C++ code for string pattern matching. Everything works fine until input pattern or string has [] bracket

相关标签:
1条回答
  • 2021-01-16 06:12

    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.

    0 讨论(0)
提交回复
热议问题