see code below, as an example, I am trying to find use regsub with backrefeence as means of selectively using string toupper. I am not getting what I have expected.
See simple example below (yes, I know that I can use string toupper $string 0 0, however, this is just for showing the principle, in a simple example).
> puts [ regsub {^(.)} "min" "s\\1" ]
smin
> puts [ regsub {^(.)} "min" [ string toupper "\\1" ] ]
min
As can be seen, string toupper applied on backreference does not work, but the backrefernce can be used in a double quote operation.
I am using TCL ver. 8.6
The
string toupperis working, but not doing what you want;string toupper "\\1"is just the string\1so theregsubisn't having much effect. The problem is thatregsubdoesn't have any way of doing “run this command at the substitution sites”; I've been wanting to fix that for years, but have never actually done so (too many projects for this one to make it to the fore).Instead, you need to
regsubin a command substitution into the string and thensubstthe result, but in order to do that, you need to first make the string otherwise safe tosubstwithstring map. Fortunately, that's actually pretty simple.I've split this apart to make it easier for you to examine exactly what each stage is doing: