Search code examples
tclregsub

How to set \n to a str in tcl


Hi I am new to tcl and want to assign "\n" to a variable and use that variable in regsub to replace that string. It should be like :

set a "\n\[\"this is my code\"\]"
puts $a

I was expecting this will give \n\[\"this is my code\"\], then I could use this $a in regsub {$a} $str "replace" sub_str. This regsub could search $a inside $str and replace the matching one with replace and strore it in sub_str. However, it gave me [this is my code] Is there a way I could get the 1st format as \n\[\"this is my code\"\] so I could use that to do the string regsub?

Thanks!


Solution

  • Use braces instead of quotes to prevent evaluation of the backslash escapes:

    set a {\n\[\"this is my code\"\]}
    puts $a
    

    prints

    \n\[\"this is my code\"\]