1regsub(n) Tcl Built-In Commands regsub(n)
2
3
4
5______________________________________________________________________________
6
8 regsub - Perform substitutions based on regular expression pattern
9 matching
10
12 regsub ?switches? exp string subSpec ?varName?
13_________________________________________________________________
14
16 This command matches the regular expression exp against string, and
17 either copies string to the variable whose name is given by varName or
18 returns string if varName is not present. (Regular expression matching
19 is described in the re_syntax reference page.) If there is a match,
20 then while copying string to varName (or to the result of this command
21 if varName is not present) the portion of string that matched exp is
22 replaced with subSpec. If subSpec contains a “&” or “\0”, then it is
23 replaced in the substitution with the portion of string that matched
24 exp. If subSpec contains a “\n”, where n is a digit between 1 and 9,
25 then it is replaced in the substitution with the portion of string that
26 matched the n'th parenthesized subexpression of exp. Additional back‐
27 slashes may be used in subSpec to prevent special interpretation of
28 “&”, “\0”, “\n” and backslashes. The use of backslashes in subSpec
29 tends to interact badly with the Tcl parser's use of backslashes, so it
30 is generally safest to enclose subSpec in braces if it includes back‐
31 slashes.
32
33 If the initial arguments to regsub start with - then they are treated
34 as switches. The following switches are currently supported:
35
36 -all All ranges in string that match exp are found and substitu‐
37 tion is performed for each of these ranges. Without this
38 switch only the first matching range is found and substi‐
39 tuted. If -all is specified, then “&” and “\n” sequences are
40 handled for each substitution using the information from the
41 corresponding match.
42
43 -expanded Enables use of the expanded regular expression syntax
44 where whitespace and comments are ignored. This is the
45 same as specifying the (?x) embedded option (see the
46 re_syntax manual page).
47
48 -line Enables newline-sensitive matching. By default, newline
49 is a completely ordinary character with no special mean‐
50 ing. With this flag, “[^” bracket expressions and “.”
51 never match newline, “^” matches an empty string after
52 any newline in addition to its normal function, and “$”
53 matches an empty string before any newline in addition
54 to its normal function. This flag is equivalent to
55 specifying both -linestop and -lineanchor, or the (?n)
56 embedded option (see the re_syntax manual page).
57
58 -linestop Changes the behavior of “[^” bracket expressions and “.”
59 so that they stop at newlines. This is the same as
60 specifying the (?p) embedded option (see the re_syntax
61 manual page).
62
63 -lineanchor Changes the behavior of “^” and “$” (the “anchors”) so
64 they match the beginning and end of a line respectively.
65 This is the same as specifying the (?w) embedded option
66 (see the re_syntax manual page).
67
68 -nocase Upper-case characters in string will be converted to lower-
69 case before matching against exp; however, substitutions
70 specified by subSpec use the original unconverted form of
71 string.
72
73 -start index
74 Specifies a character index offset into the string to start
75 matching the regular expression at. The index value is │
76 interpreted in the same manner as the index argument to │
77 string index. When using this switch, “^” will not match the
78 beginning of the line, and \A will still match the start of
79 the string at index. index will be constrained to the bounds
80 of the input string.
81
82 -- Marks the end of switches. The argument following this one
83 will be treated as exp even if it starts with a -.
84
85 If varName is supplied, the command returns a count of the number of
86 matching ranges that were found and replaced, otherwise the string
87 after replacement is returned. See the manual entry for regexp for
88 details on the interpretation of regular expressions.
89
91 Replace (in the string in variable string) every instance of foo which
92 is a word by itself with bar:
93 regsub -all {\mfoo\M} $string bar string
94 or (using the “basic regular expression” syntax):
95 regsub -all {(?b)\<foo\>} $string bar string
96
97 Insert double-quotes around the first instance of the word interesting,
98 however it is capitalized.
99 regsub -nocase {\yinteresting\y} $string {"&"} string
100
101 Convert all non-ASCII and Tcl-significant characters into \u escape
102 sequences by using regsub and subst in combination:
103 # This RE is just a character class for everything "bad"
104 set RE {[][{};#\\\$\s\u0080-\uffff]}
105
106 # We will substitute with a fragment of Tcl script in brackets
107 set substitution {[format \\\\u%04x [scan "\\&" %c]]}
108
109 # Now we apply the substitution to get a subst-string that
110 # will perform the computational parts of the conversion.
111 set quoted [subst [regsub -all $RE $string $substitution]]
112
114 regexp(n), re_syntax(n), subst(n), string(n) │
115
117 match, pattern, quoting, regular expression, substitute
118
119
120
121Tcl 8.3 regsub(n)