canislupus
canislupus

Reputation: 1

remove square brackets from string using tcl

I'm new to using Tcl and am trying to remove the square brackets from a string using Tcl.

set f "abc [def]"
set bracket1 {[}
set bracket2 {]}
    regsub -all "($bracket1) ($bracket2)" $f "" g
    puts $g

Upvotes: 0

Views: 3205

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137577

To remove square brackets from a string, it's easier (and quicker) to use string map:

set g [string map {{[} "" {]} ""} $f]

You can use regsub, but because [ and ] are RE metacharacters and RE character-set metacharacters, it can be rather awkward:

regsub -all {[][]} $f "" g
# or
set g [regsub -all {[][]} $f ""]

Upvotes: 2

Related Questions