regsub(3tcl) | Tcl Built-In Commands | regsub(3tcl) |
regsub - Perform substitutions based on regular expression pattern matching
regsub ?switches? exp string subSpec ?varName?
This command matches the regular expression exp against string, and either copies string to the variable whose name is given by varName or returns string if varName is not present. (Regular expression matching is described in the re_syntax reference page.) If there is a match, then while copying string to varName (or to the result of this command if varName is not present) the portion of string that matched exp is replaced with subSpec. If subSpec contains a “&” or “\0”, then it is replaced in the substitution with the portion of string that matched exp. If subSpec contains a “\n”, where n is a digit between 1 and 9, then it is replaced in the substitution with the portion of string that matched the n'th parenthesized subexpression of exp. Additional backslashes may be used in subSpec to prevent special interpretation of “&”, “\0”, “\n” and backslashes. The use of backslashes in subSpec tends to interact badly with the Tcl parser's use of backslashes, so it is generally safest to enclose subSpec in braces if it includes backslashes.
If the initial arguments to regsub start with - then they are treated as switches. The following switches are currently supported:
If varName is supplied, the command returns a count of the number of matching ranges that were found and replaced, otherwise the string after replacement is returned. See the manual entry for regexp for details on the interpretation of regular expressions.
Replace (in the string in variable string) every instance of foo which is a word by itself with bar:
regsub -all {\mfoo\M} $string bar string
or (using the “basic regular expression” syntax):
regsub -all {(?b)\<foo\>} $string bar string
Insert double-quotes around the first instance of the word interesting, however it is capitalized.
regsub -nocase {\yinteresting\y} $string {"&"} string
Convert all non-ASCII and Tcl-significant characters into \u escape sequences by using regsub and subst in combination:
# This RE is just a character class for almost everything "bad" set RE {[][{};#\\\$ \r\t\u0080-\uffff]} # We will substitute with a fragment of Tcl script in brackets set substitution {[format \\\\u%04x [scan "\\&" %c]]} # Now we apply the substitution to get a subst-string that # will perform the computational parts of the conversion. Note # that newline is handled specially through string map since # backslash-newline is a special sequence. set quoted [subst [string map {\n {\\u000a}} \
[regsub -all $RE $string $substitution]]]
match, pattern, quoting, regular expression, substitution
8.3 | Tcl |