Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/regsub.n @ 42

Last change on this file since 42 was 25, checked in by landauf, 17 years ago

added tcl to libs

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