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.

Regsubbing

Old posts that have not been replied to for several years.
Locked
B
Baerchen

Post by Baerchen »

Hi,

Is it possible to put 2 different substitutions into one regsub? Say I want to regsub "1" by "one and "2" by "two".

regsub -all 1 $a one a
regsub -all 2 $a two a

does the job.

Is it possible to put that into _one_ regsub ?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

You could do it with one regsub and a subst. It's probably better to just do 2 regsubs though :)

set str "this is 1 test of 2 things"
set keys(1) one
set keys(2) two
regsub -all 1|2 $str {${keys(&)}} str
set str [subst $str]

Just make sure your source string doesn't have special characters in it :)
B
Baerchen

Post by Baerchen »

Hmm, thanks a lot.
My intention was to reduce the amount of regsubs by putting them all into one, but as it seems, it doesn't work. Anyway your solution is very interesting from a genral point of view. Thanks !

Baerchen
P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

personally i just like to write a proc for such things

eg

Code: Select all

proc myregsub {a} {
regsub -all 1 $a one a 
regsub -all 2 $a two a
return a
}
can then just regsub a string by doing [myregsub $blah] etc
Locked