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