string substitution regular expression not working in tcl
- by Puneet Mittal
i am trying to replace all the special characters including white space, hyphen, etc, to underscore, from a string variable in tcl. I wrote the code below but it doesn't seem to be working.
set varname $origVar
puts "Variable Name :>> $varname"
if {$varname != ""} {
regsub -all {[\s-\]\[$^?+*()|\\%&#]} $varname "_" $newVar
}
puts "New Variable :>> $newVar"
one issue is that, instead of replacing the string in $varname, it is replacing the data inside $origVar. No idea why, and also i read the example code (for proper syntax) in my tcl book and according to that it should be something like this
regsub -all {[\s-][$^?+*()|\\%&#]} $varname "_" newVar
so i used the same syntax but it didn't work and gave the same result as modifying the $origVar instead of required $varname value.