Reputation: 43
In file.txt
obj = "hi/this/is[1]/script"
convert the string to
obj = "hi/this/is\[1]/script"
Is there a way to do this in tcl?
Upvotes: 0
Views: 2263
Reputation: 137567
If you want to replace all occurrences of a character, string map
is pretty suitable:
# Careful with the quoting here
set obj [string map [list {[} {\[}] $obj]
If you want to just replace the first occurrence, regsub
is a better tool
set obj [regsub {\[} $obj {\\&}]
(The &
becomes the matched string, and we need to be careful with backslashes in both the RE and the substitution text.)
Upvotes: 2
Reputation: 52344
One way is using string map
:
% set obj {obj = "hi/this/is[1]/script"}
obj = "hi/this/is[1]/script"
% string map {\[ \\\[} $obj
obj = "hi/this/is\[1]/script"
Upvotes: 0