Tcl Regsub Pattern to Another Pattern

Tcl Regsub Pattern to Another Pattern

How can I regsub pattern to another pattern

set a "how1how ku2ku how2how" ; regsub "how*how" $a "oo_how\*how_oo"

I want all names "how(num)how" will be swap to the same name with prefix and suffix but the above regsub now work for me

1

1 Answer

A capture group followed by a backreference usually:

% set a "how1how ku2ku how2how"
how1how ku2ku how2how
% regsub -all {how([0-9]+)how} $a {oo_how\1how_oo}
oo_how1how_oo ku2ku oo_how2how_oo

Documentation for the syntax


Following the comments, I think that maybe what you are after is something like this?

set word "how"
set a "how1how ku2ku how2how"
regsub -all [format {%s([0-9]+)%s} $word $word] $a [format {oo_%s\1%s_oo} $word $word]

format allows some substitution where strings %s would be substituted with the provided parameters.

8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.