Debian Codegraph @

doc/regsub.n

raw
1 '\"
2 '\" Copyright (c) 1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 '\" Copyright (c) 2000 Scriptics Corporation.
5 '\"
6 '\" See the file "license.terms" for information on usage and redistribution
7 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8 '\"
9 .TH regsub n 8.3 Tcl "Tcl Built-In Commands"
10 .so man.macros
11 .BS
12 '\" Note: do not modify the .SH NAME line immediately below!
13 .SH NAME
14 regsub \- Perform substitutions based on regular expression pattern matching
15 .SH SYNOPSIS
16 \fBregsub \fR?\fIswitches\fR? \fIexp string subSpec \fR?\fIvarName\fR?
17 .BE
18 .SH DESCRIPTION
19 .PP
20 This command matches the regular expression \fIexp\fR against
21 \fIstring\fR,
22 and either copies \fIstring\fR to the variable whose name is
23 given by \fIvarName\fR or returns \fIstring\fR if \fIvarName\fR is not
24 present.
25 (Regular expression matching is described in the \fBre_syntax\fR
26 reference page.)
27 If there is a match, then while copying \fIstring\fR to \fIvarName\fR
28 (or to the result of this command if \fIvarName\fR is not present)
29 the portion of \fIstring\fR that
30 matched \fIexp\fR is replaced with \fIsubSpec\fR.
31 If \fIsubSpec\fR contains a
32 .QW &
33 or
34 .QW \e0 ,
35 then it is replaced in the substitution with the portion of
36 \fIstring\fR that matched \fIexp\fR.
37 If \fIsubSpec\fR contains a
38 .QW \e\fIn\fR ,
39 where \fIn\fR is a digit
40 between 1 and 9, then it is replaced in the substitution with
41 the portion of \fIstring\fR that matched the \fIn\fR'th
42 parenthesized subexpression of \fIexp\fR.
43 Additional backslashes may be used in \fIsubSpec\fR to prevent special
44 interpretation of
45 .QW & ,
46 .QW \e0 ,
47 .QW \e\fIn\fR
48 and backslashes.
49 The use of backslashes in \fIsubSpec\fR tends to interact badly
50 with the Tcl parser's use of backslashes, so it is generally
51 safest to enclose \fIsubSpec\fR in braces if it includes
52 backslashes.
53 .LP
54 If the initial arguments to \fBregsub\fR start with \fB\-\fR then
55 they are treated as switches. The following switches are
56 currently supported:
57 .TP
58 \fB\-all\fR
59 .
60 All ranges in \fIstring\fR that match \fIexp\fR are found and
61 substitution is performed for each of these ranges.
62 Without this switch only the first
63 matching range is found and substituted.
64 If \fB\-all\fR is specified, then
65 .QW &
66 and
67 .QW \e\fIn\fR
68 sequences are handled for each substitution using the information
69 from the corresponding match.
70 .TP
71 \fB\-expanded\fR
72 .
73 Enables use of the expanded regular expression syntax where
74 whitespace and comments are ignored. This is the same as specifying
75 the \fB(?x)\fR embedded option (see the \fBre_syntax\fR manual page).
76 .TP
77 \fB\-line\fR
78 .
79 Enables newline-sensitive matching. By default, newline is a
80 completely ordinary character with no special meaning. With this flag,
81 .QW [^
82 bracket expressions and
83 .QW .
84 never match newline,
85 .QW ^
86 matches an empty string after any newline in addition to its normal
87 function, and
88 .QW $
89 matches an empty string before any newline in
90 addition to its normal function. This flag is equivalent to
91 specifying both \fB\-linestop\fR and \fB\-lineanchor\fR, or the
92 \fB(?n)\fR embedded option (see the \fBre_syntax\fR manual page).
93 .TP
94 \fB\-linestop\fR
95 .
96 Changes the behavior of
97 .QW [^
98 bracket expressions and
99 .QW .
100 so that they
101 stop at newlines. This is the same as specifying the \fB(?p)\fR
102 embedded option (see the \fBre_syntax\fR manual page).
103 .TP
104 \fB\-lineanchor\fR
105 .
106 Changes the behavior of
107 .QW ^
108 and
109 .QW $
110 (the
111 .QW anchors )
112 so they match the
113 beginning and end of a line respectively. This is the same as
114 specifying the \fB(?w)\fR embedded option (see the \fBre_syntax\fR
115 manual page).
116 .TP
117 \fB\-nocase\fR
118 .
119 Upper-case characters in \fIstring\fR will be converted to lower-case
120 before matching against \fIexp\fR; however, substitutions specified
121 by \fIsubSpec\fR use the original unconverted form of \fIstring\fR.
122 .TP
123 \fB\-start\fR \fIindex\fR
124 .
125 Specifies a character index offset into the string to start
126 matching the regular expression at.
127 The \fIindex\fR value is interpreted in the same manner
128 as the \fIindex\fR argument to \fBstring index\fR.
129 When using this switch,
130 .QW ^
131 will not match the beginning of the line, and \eA will still
132 match the start of the string at \fIindex\fR.
133 \fIindex\fR will be constrained to the bounds of the input string.
134 .TP
135 \fB\-\|\-\fR
136 .
137 Marks the end of switches. The argument following this one will
138 be treated as \fIexp\fR even if it starts with a \fB\-\fR.
139 .PP
140 If \fIvarName\fR is supplied, the command returns a count of the
141 number of matching ranges that were found and replaced, otherwise the
142 string after replacement is returned.
143 See the manual entry for \fBregexp\fR for details on the interpretation
144 of regular expressions.
145 .SH EXAMPLES
146 .PP
147 Replace (in the string in variable \fIstring\fR) every instance of
148 \fBfoo\fR which is a word by itself with \fBbar\fR:
149 .PP
150 .CS
151 \fBregsub\fR -all {\emfoo\eM} $string bar string
152 .CE
153 .PP
154 or (using the
155 .QW "basic regular expression"
156 syntax):
157 .PP
158 .CS
159 \fBregsub\fR -all {(?b)\e<foo\e>} $string bar string
160 .CE
161 .PP
162 Insert double-quotes around the first instance of the word
163 \fBinteresting\fR, however it is capitalized.
164 .PP
165 .CS
166 \fBregsub\fR -nocase {\eyinteresting\ey} $string {"&"} string
167 .CE
168 .PP
169 Convert all non-ASCII and Tcl-significant characters into \eu escape
170 sequences by using \fBregsub\fR and \fBsubst\fR in combination:
171 .PP
172 .CS
173 # This RE is just a character class for almost everything "bad"
174 set RE {[][{};#\e\e\e$ \er\et\eu0080-\euffff]}
175
176 # We will substitute with a fragment of Tcl script in brackets
177 set substitution {[format \e\e\e\eu%04x [scan "\e\e&" %c]]}
178
179 # Now we apply the substitution to get a subst-string that
180 # will perform the computational parts of the conversion. Note
181 # that newline is handled specially through \fBstring map\fR since
182 # backslash-newline is a special sequence.
183 set quoted [subst [string map {\en {\e\eu000a}} \e
184 [\fBregsub\fR -all $RE $string $substitution]]]
185 .CE
186 .SH "SEE ALSO"
187 regexp(n), re_syntax(n), subst(n), string(n)
188 .SH KEYWORDS
189 match, pattern, quoting, regular expression, substitution
190 '\" Local Variables:
191 '\" mode: nroff
192 '\" End: