Description
Performs substitutions based on regular expression pattern matching.
Synopsis
regsub [-all] exp string subspec varname
Options
Specify to substitute all portions that matched a regular expression. If omitted, only the first matching portion is substituted.
Specify the regular expression that identifies the portion to substitute.
Specify the string to be replaced.
Specify the string to replace. The string to replace must be enclosed in braces ({}). To include "\" or "&" in the string to replace, they must be written "\\" or "\&", respectively.
Specify the name of the variable to store the string that was replaced.
Point
A regular expression is a shorthand for describing the structure of a string using symbols. This command searches the string specified by string for the portion that matches the structure represented in the regular expression exp. If there is a match, it replaces it with the string specified by subspec and stores in the variable whose name is given by varname.
For how to write a regular expression, see "15.1.4 Regular Expression".
Return Values
0: | Not matched. |
1: | Matched. |
Example
The following example replaces all spaces between strings with commas.
set text "a b c d" regsub -all " " $text {,} result puts $result
Execution Results/Output Format
a,b,c,d