This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

replace every "*" in string

Help for those learning Tcl or writing their own scripts.
Post Reply
B
BCyo+8C4
Voice
Posts: 24
Joined: Sun Sep 03, 2006 11:11 am

replace every "*" in string

Post by BCyo+8C4 »

Hi,
I'm keeping the channels banlist in a text file but got a problem removing the entries from that file as I can't figure out how to escape the asterisks.

sed expects every "*" and "!" to be escaped like "\*" and "\!"

What's the easiest way to replace _every_ "*" with "\*" in a given string? (Replacing just one isn't a problem, but for 5 my code had around 50 lines and is fugly)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Regsub with a regular expression:

Code: Select all

set maskedString [regsub -all -- {\*|!} $oldString {\\\0}]
NML_375
B
BCyo+8C4
Voice
Posts: 24
Joined: Sun Sep 03, 2006 11:11 am

Post by BCyo+8C4 »

doesn't work :(
Tcl error [delban]: wrong # args: should be "regsub ?switches? exp string subSpec varName"
Entered the command exactly like you said, only changed the variable names:

Code: Select all

set maskedString [regsub -all -- {\*|!} $mask {\\\0}]
also tried with just "{\*}" as expression but that failed as well.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

That's odd... which version of tcl are you using?
Most versions of tcl have the varName argument optional. Nevertheless, try using something like this instead then:

Code: Select all

regsub -all -- {\*|!} $mask {\\\0} maskedString
NML_375
B
BCyo+8C4
Voice
Posts: 24
Joined: Sun Sep 03, 2006 11:11 am

Post by BCyo+8C4 »

nvm, just figured it out:

Code: Select all

regsub -all {\*|!} $mask {\\\0} mask
Post Reply