string substitution regular expression not working in tcl

Posted by Puneet Mittal on Stack Overflow See other posts from Stack Overflow or by Puneet Mittal
Published on 2012-10-01T21:35:18Z Indexed on 2012/10/01 21:37 UTC
Read the original article Hit count: 184

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about regex

Related posts about tcl