Debian Codegraph @

doc/RegExp.3

raw
1 '\"
2 '\" Copyright (c) 1994 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 '\" Copyright (c) 1998-1999 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 Tcl_RegExpMatch 3 8.1 Tcl "Tcl Library Procedures"
10 .so man.macros
11 .BS
12 .SH NAME
13 Tcl_RegExpMatch, Tcl_RegExpCompile, Tcl_RegExpExec, Tcl_RegExpRange, Tcl_GetRegExpFromObj, Tcl_RegExpMatchObj, Tcl_RegExpExecObj, Tcl_RegExpGetInfo \- Pattern matching with regular expressions
14 .SH SYNOPSIS
15 .nf
16 \fB#include <tcl.h>\fR
17 .sp
18 int
19 \fBTcl_RegExpMatchObj\fR(\fIinterp\fR, \fItextObj\fR, \fIpatObj\fR)
20 .sp
21 int
22 \fBTcl_RegExpMatch\fR(\fIinterp\fR, \fItext\fR, \fIpattern\fR)
23 .sp
24 Tcl_RegExp
25 \fBTcl_RegExpCompile\fR(\fIinterp\fR, \fIpattern\fR)
26 .sp
27 int
28 \fBTcl_RegExpExec\fR(\fIinterp\fR, \fIregexp\fR, \fItext\fR, \fIstart\fR)
29 .sp
30 void
31 \fBTcl_RegExpRange\fR(\fIregexp\fR, \fIindex\fR, \fIstartPtr\fR, \fIendPtr\fR)
32 .sp
33 Tcl_RegExp
34 \fBTcl_GetRegExpFromObj\fR(\fIinterp\fR, \fIpatObj\fR, \fIcflags\fR)
35 .sp
36 int
37 \fBTcl_RegExpExecObj\fR(\fIinterp\fR, \fIregexp\fR, \fItextObj\fR, \fIoffset\fR, \fInmatches\fR, \fIeflags\fR)
38 .sp
39 void
40 \fBTcl_RegExpGetInfo\fR(\fIregexp\fR, \fIinfoPtr\fR)
41 .fi
42 .SH ARGUMENTS
43 .AS Tcl_RegExpInfo *interp in/out
44 .AP Tcl_Interp *interp in
45 Tcl interpreter to use for error reporting. The interpreter may be
46 NULL if no error reporting is desired.
47 .AP Tcl_Obj *textObj in/out
48 Refers to the value from which to get the text to search. The
49 internal representation of the value may be converted to a form that
50 can be efficiently searched.
51 .AP Tcl_Obj *patObj in/out
52 Refers to the value from which to get a regular expression. The
53 compiled regular expression is cached in the value.
54 .AP "const char" *text in
55 Text to search for a match with a regular expression.
56 .AP "const char" *pattern in
57 String in the form of a regular expression pattern.
58 .AP Tcl_RegExp regexp in
59 Compiled regular expression. Must have been returned previously
60 by \fBTcl_GetRegExpFromObj\fR or \fBTcl_RegExpCompile\fR.
61 .AP "const char" *start in
62 If \fItext\fR is just a portion of some other string, this argument
63 identifies the beginning of the larger string.
64 If it is not the same as \fItext\fR, then no
65 .QW \fB^\fR
66 matches will be allowed.
67 .AP int index in
68 Specifies which range is desired: 0 means the range of the entire
69 match, 1 or greater means the range that matched a parenthesized
70 sub-expression.
71 .AP "const char" **startPtr out
72 The address of the first character in the range is stored here, or
73 NULL if there is no such range.
74 .AP "const char" **endPtr out
75 The address of the character just after the last one in the range
76 is stored here, or NULL if there is no such range.
77 .AP int cflags in
78 OR-ed combination of the compilation flags \fBTCL_REG_ADVANCED\fR,
79 \fBTCL_REG_EXTENDED\fR, \fBTCL_REG_BASIC\fR, \fBTCL_REG_EXPANDED\fR,
80 \fBTCL_REG_QUOTE\fR, \fBTCL_REG_NOCASE\fR, \fBTCL_REG_NEWLINE\fR,
81 \fBTCL_REG_NLSTOP\fR, \fBTCL_REG_NLANCH\fR, \fBTCL_REG_NOSUB\fR, and
82 \fBTCL_REG_CANMATCH\fR. See below for more information.
83 .AP int offset in
84 The character offset into the text where matching should begin.
85 The value of the offset has no impact on \fB^\fR matches. This
86 behavior is controlled by \fIeflags\fR.
87 .AP int nmatches in
88 The number of matching subexpressions that should be remembered for
89 later use. If this value is 0, then no subexpression match
90 information will be computed. If the value is \-1, then
91 all of the matching subexpressions will be remembered. Any other
92 value will be taken as the maximum number of subexpressions to
93 remember.
94 .AP int eflags in
95 OR-ed combination of the execution flags \fBTCL_REG_NOTBOL\fR and
96 \fBTCL_REG_NOTEOL\fR. See below for more information.
97 .AP Tcl_RegExpInfo *infoPtr out
98 The address of the location where information about a previous match
99 should be stored by \fBTcl_RegExpGetInfo\fR.
100 .BE
101 .SH DESCRIPTION
102 .PP
103 \fBTcl_RegExpMatch\fR determines whether its \fIpattern\fR argument
104 matches \fIregexp\fR, where \fIregexp\fR is interpreted
105 as a regular expression using the rules in the \fBre_syntax\fR
106 reference page.
107 If there is a match then \fBTcl_RegExpMatch\fR returns 1.
108 If there is no match then \fBTcl_RegExpMatch\fR returns 0.
109 If an error occurs in the matching process (e.g. \fIpattern\fR
110 is not a valid regular expression) then \fBTcl_RegExpMatch\fR
111 returns \-1 and leaves an error message in the interpreter result.
112 \fBTcl_RegExpMatchObj\fR is similar to \fBTcl_RegExpMatch\fR except it
113 operates on the Tcl values \fItextObj\fR and \fIpatObj\fR instead of
114 UTF strings.
115 \fBTcl_RegExpMatchObj\fR is generally more efficient than
116 \fBTcl_RegExpMatch\fR, so it is the preferred interface.
117 .PP
118 \fBTcl_RegExpCompile\fR, \fBTcl_RegExpExec\fR, and \fBTcl_RegExpRange\fR
119 provide lower-level access to the regular expression pattern matcher.
120 \fBTcl_RegExpCompile\fR compiles a regular expression string into
121 the internal form used for efficient pattern matching.
122 The return value is a token for this compiled form, which can be
123 used in subsequent calls to \fBTcl_RegExpExec\fR or \fBTcl_RegExpRange\fR.
124 If an error occurs while compiling the regular expression then
125 \fBTcl_RegExpCompile\fR returns NULL and leaves an error message
126 in the interpreter result.
127 Note: the return value from \fBTcl_RegExpCompile\fR is only valid
128 up to the next call to \fBTcl_RegExpCompile\fR; it is not safe to
129 retain these values for long periods of time.
130 .PP
131 \fBTcl_RegExpExec\fR executes the regular expression pattern matcher.
132 It returns 1 if \fItext\fR contains a range of characters that
133 match \fIregexp\fR, 0 if no match is found, and
134 \-1 if an error occurs.
135 In the case of an error, \fBTcl_RegExpExec\fR leaves an error
136 message in the interpreter result.
137 When searching a string for multiple matches of a pattern,
138 it is important to distinguish between the start of the original
139 string and the start of the current search.
140 For example, when searching for the second occurrence of a
141 match, the \fItext\fR argument might point to the character
142 just after the first match; however, it is important for the
143 pattern matcher to know that this is not the start of the entire string,
144 so that it does not allow
145 .QW \fB^\fR
146 atoms in the pattern to match.
147 The \fIstart\fR argument provides this information by pointing
148 to the start of the overall string containing \fItext\fR.
149 \fIStart\fR will be less than or equal to \fItext\fR; if it
150 is less than \fItext\fR then no \fB^\fR matches will be allowed.
151 .PP
152 \fBTcl_RegExpRange\fR may be invoked after \fBTcl_RegExpExec\fR
153 returns; it provides detailed information about what ranges of
154 the string matched what parts of the pattern.
155 \fBTcl_RegExpRange\fR returns a pair of pointers in \fI*startPtr\fR
156 and \fI*endPtr\fR that identify a range of characters in
157 the source string for the most recent call to \fBTcl_RegExpExec\fR.
158 \fIIndex\fR indicates which of several ranges is desired:
159 if \fIindex\fR is 0, information is returned about the overall range
160 of characters that matched the entire pattern; otherwise,
161 information is returned about the range of characters that matched the
162 \fIindex\fR'th parenthesized subexpression within the pattern.
163 If there is no range corresponding to \fIindex\fR then NULL
164 is stored in \fI*startPtr\fR and \fI*endPtr\fR.
165 .PP
166 \fBTcl_GetRegExpFromObj\fR, \fBTcl_RegExpExecObj\fR, and
167 \fBTcl_RegExpGetInfo\fR are value interfaces that provide the most
168 direct control of Henry Spencer's regular expression library. For
169 users that need to modify compilation and execution options directly,
170 it is recommended that you use these interfaces instead of calling the
171 internal regexp functions. These interfaces handle the details of UTF
172 to Unicode translations as well as providing improved performance
173 through caching in the pattern and string values.
174 .PP
175 \fBTcl_GetRegExpFromObj\fR attempts to return a compiled regular
176 expression from the \fIpatObj\fR. If the value does not already
177 contain a compiled regular expression it will attempt to create one
178 from the string in the value and assign it to the internal
179 representation of the \fIpatObj\fR. The return value of this function
180 is of type \fBTcl_RegExp\fR. The return value is a token for this
181 compiled form, which can be used in subsequent calls to
182 \fBTcl_RegExpExecObj\fR or \fBTcl_RegExpGetInfo\fR. If an error
183 occurs while compiling the regular expression then
184 \fBTcl_GetRegExpFromObj\fR returns NULL and leaves an error message in
185 the interpreter result. The regular expression token can be used as
186 long as the internal representation of \fIpatObj\fR refers to the
187 compiled form. The \fIcflags\fR argument is a bit-wise OR of
188 zero or more of the following flags that control the compilation of
189 \fIpatObj\fR:
190 .RS 2
191 .TP
192 \fBTCL_REG_ADVANCED\fR
193 Compile advanced regular expressions
194 .PQ ARE s .
195 This mode corresponds to
196 the normal regular expression syntax accepted by the Tcl \fBregexp\fR and
197 \fBregsub\fR commands.
198 .TP
199 \fBTCL_REG_EXTENDED\fR
200 Compile extended regular expressions
201 .PQ ERE s .
202 This mode corresponds
203 to the regular expression syntax recognized by Tcl 8.0 and earlier
204 versions.
205 .TP
206 \fBTCL_REG_BASIC\fR
207 Compile basic regular expressions
208 .PQ BRE s .
209 This mode corresponds
210 to the regular expression syntax recognized by common Unix utilities
211 like \fBsed\fR and \fBgrep\fR. This is the default if no flags are
212 specified.
213 .TP
214 \fBTCL_REG_EXPANDED\fR
215 Compile the regular expression (basic, extended, or advanced) using an
216 expanded syntax that allows comments and whitespace. This mode causes
217 non-backslashed non-bracket-expression white
218 space and #-to-end-of-line comments to be ignored.
219 .TP
220 \fBTCL_REG_QUOTE\fR
221 Compile a literal string, with all characters treated as ordinary characters.
222 .TP
223 \fBTCL_REG_NOCASE\fR
224 Compile for matching that ignores upper/lower case distinctions.
225 .TP
226 \fBTCL_REG_NEWLINE\fR
227 Compile for newline-sensitive matching. By default, newline is a
228 completely ordinary character with no special meaning in either
229 regular expressions or strings. With this flag,
230 .QW [^
231 bracket expressions and
232 .QW .
233 never match newline,
234 .QW ^
235 matches an empty string
236 after any newline in addition to its normal function, and
237 .QW $
238 matches
239 an empty string before any newline in addition to its normal function.
240 \fBREG_NEWLINE\fR is the bit-wise OR of \fBREG_NLSTOP\fR and
241 \fBREG_NLANCH\fR.
242 .TP
243 \fBTCL_REG_NLSTOP\fR
244 Compile for partial newline-sensitive matching,
245 with the behavior of
246 .QW [^
247 bracket expressions and
248 .QW .
249 affected, but not the behavior of
250 .QW ^
251 and
252 .QW $ .
253 In this mode,
254 .QW [^
255 bracket expressions and
256 .QW .
257 never match newline.
258 .TP
259 \fBTCL_REG_NLANCH\fR
260 Compile for inverse partial newline-sensitive matching,
261 with the behavior of
262 .QW ^
263 and
264 .QW $
265 (the
266 .QW anchors )
267 affected, but not the behavior of
268 .QW [^
269 bracket expressions and
270 .QW . .
271 In this mode
272 .QW ^
273 matches an empty string
274 after any newline in addition to its normal function, and
275 .QW $
276 matches
277 an empty string before any newline in addition to its normal function.
278 .TP
279 \fBTCL_REG_NOSUB\fR
280 Compile for matching that reports only success or failure,
281 not what was matched. This reduces compile overhead and may improve
282 performance. Subsequent calls to \fBTcl_RegExpGetInfo\fR or
283 \fBTcl_RegExpRange\fR will not report any match information.
284 .TP
285 \fBTCL_REG_CANMATCH\fR
286 Compile for matching that reports the potential to complete a partial
287 match given more text (see below).
288 .RE
289 .PP
290 Only one of
291 \fBTCL_REG_EXTENDED\fR,
292 \fBTCL_REG_ADVANCED\fR,
293 \fBTCL_REG_BASIC\fR, and
294 \fBTCL_REG_QUOTE\fR may be specified.
295 .PP
296 \fBTcl_RegExpExecObj\fR executes the regular expression pattern
297 matcher. It returns 1 if \fIobjPtr\fR contains a range of characters
298 that match \fIregexp\fR, 0 if no match is found, and \-1 if an error
299 occurs. In the case of an error, \fBTcl_RegExpExecObj\fR leaves an
300 error message in the interpreter result. The \fInmatches\fR value
301 indicates to the matcher how many subexpressions are of interest. If
302 \fInmatches\fR is 0, then no subexpression match information is
303 recorded, which may allow the matcher to make various optimizations.
304 If the value is \-1, then all of the subexpressions in the pattern are
305 remembered. If the value is a positive integer, then only that number
306 of subexpressions will be remembered. Matching begins at the
307 specified Unicode character index given by \fIoffset\fR. Unlike
308 \fBTcl_RegExpExec\fR, the behavior of anchors is not affected by the
309 offset value. Instead the behavior of the anchors is explicitly
310 controlled by the \fIeflags\fR argument, which is a bit-wise OR of
311 zero or more of the following flags:
312 .RS 2
313 .TP
314 \fBTCL_REG_NOTBOL\fR
315 The starting character will not be treated as the beginning of a
316 line or the beginning of the string, so
317 .QW ^
318 will not match there.
319 Note that this flag has no effect on how
320 .QW \fB\eA\fR
321 matches.
322 .TP
323 \fBTCL_REG_NOTEOL\fR
324 The last character in the string will not be treated as the end of a
325 line or the end of the string, so
326 .QW $
327 will not match there.
328 Note that this flag has no effect on how
329 .QW \fB\eZ\fR
330 matches.
331 .RE
332 .PP
333 \fBTcl_RegExpGetInfo\fR retrieves information about the last match
334 performed with a given regular expression \fIregexp\fR. The
335 \fIinfoPtr\fR argument contains a pointer to a structure that is
336 defined as follows:
337 .PP
338 .CS
339 typedef struct Tcl_RegExpInfo {
340 int \fInsubs\fR;
341 Tcl_RegExpIndices *\fImatches\fR;
342 long \fIextendStart\fR;
343 } \fBTcl_RegExpInfo\fR;
344 .CE
345 .PP
346 The \fInsubs\fR field contains a count of the number of parenthesized
347 subexpressions within the regular expression. If the \fBTCL_REG_NOSUB\fR
348 was used, then this value will be zero. The \fImatches\fR field
349 points to an array of \fInsubs\fR+1 values that indicate the bounds of each
350 subexpression matched. The first element in the array refers to the
351 range matched by the entire regular expression, and subsequent elements
352 refer to the parenthesized subexpressions in the order that they
353 appear in the pattern. Each element is a structure that is defined as
354 follows:
355 .PP
356 .CS
357 typedef struct Tcl_RegExpIndices {
358 long \fIstart\fR;
359 long \fIend\fR;
360 } \fBTcl_RegExpIndices\fR;
361 .CE
362 .PP
363 The \fIstart\fR and \fIend\fR values are Unicode character indices
364 relative to the offset location within \fIobjPtr\fR where matching began.
365 The \fIstart\fR index identifies the first character of the matched
366 subexpression. The \fIend\fR index identifies the first character
367 after the matched subexpression. If the subexpression matched the
368 empty string, then \fIstart\fR and \fIend\fR will be equal. If the
369 subexpression did not participate in the match, then \fIstart\fR and
370 \fIend\fR will be set to \-1.
371 .PP
372 The \fIextendStart\fR field in \fBTcl_RegExpInfo\fR is only set if the
373 \fBTCL_REG_CANMATCH\fR flag was used. It indicates the first
374 character in the string where a match could occur. If a match was
375 found, this will be the same as the beginning of the current match.
376 If no match was found, then it indicates the earliest point at which a
377 match might occur if additional text is appended to the string. If it
378 is no match is possible even with further text, this field will be set
379 to \-1.
380 .SH "SEE ALSO"
381 re_syntax(n)
382 .SH KEYWORDS
383 match, pattern, regular expression, string, subexpression, Tcl_RegExpIndices, Tcl_RegExpInfo