Is it possible to have do different substitutions in an expression using regsub?
example:
set a ".a/b.c..d/e/f//g"
Now, in this expression, is it possible to substitute
“.” as “yes”
“..” as “no”
“/” as “true”
“//” as “false” in a single regsub command?
With a
regsub, no. There’s a long-standing feature request for this sort of thing (which requires substitution with the result of evaluating a command on the match information) but it’s not been acted on to date.But you can use
string mapto do what you want in this case:Note that when building the map, if any from-value is a prefix of another, the longer from-value should be put first. (This is because the
string mapimplementation checks which change to make in the order you list them in…)It’s possible to use
regsubandsubstto do multiple-target replacements in a two-step process, but I don’t advise it for anything other than very complex cases! A nicestring mapis far easier to work with.