ViewVC logotype

Contents of /code/tags/pcre-6.3/doc/pcreapi.3

Parent Directory Parent Directory | Revision Log Revision Log

Revision 84 - (show annotations)
Sat Feb 24 21:41:08 2007 UTC (14 years, 2 months ago) by nigel
File size: 68188 byte(s)
Tag code/trunk as code/tags/pcre-6.3.
3 PCRE - Perl-compatible regular expressions
5 .rs
6 .sp
7 .B #include <pcre.h>
8 .PP
9 .SM
10 .br
11 .B pcre *pcre_compile(const char *\fIpattern\fP, int \fIoptions\fP,
12 .ti +5n
13 .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
14 .ti +5n
15 .B const unsigned char *\fItableptr\fP);
16 .PP
17 .br
18 .B pcre *pcre_compile2(const char *\fIpattern\fP, int \fIoptions\fP,
19 .ti +5n
20 .B int *\fIerrorcodeptr\fP,
21 .ti +5n
22 .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
23 .ti +5n
24 .B const unsigned char *\fItableptr\fP);
25 .PP
26 .br
27 .B pcre_extra *pcre_study(const pcre *\fIcode\fP, int \fIoptions\fP,
28 .ti +5n
29 .B const char **\fIerrptr\fP);
30 .PP
31 .br
32 .B int pcre_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
33 .ti +5n
34 .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
35 .ti +5n
36 .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP);
37 .PP
38 .br
39 .B int pcre_dfa_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
40 .ti +5n
41 .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
42 .ti +5n
43 .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP,
44 .ti +5n
45 .B int *\fIworkspace\fP, int \fIwscount\fP);
46 .PP
47 .br
48 .B int pcre_copy_named_substring(const pcre *\fIcode\fP,
49 .ti +5n
50 .B const char *\fIsubject\fP, int *\fIovector\fP,
51 .ti +5n
52 .B int \fIstringcount\fP, const char *\fIstringname\fP,
53 .ti +5n
54 .B char *\fIbuffer\fP, int \fIbuffersize\fP);
55 .PP
56 .br
57 .B int pcre_copy_substring(const char *\fIsubject\fP, int *\fIovector\fP,
58 .ti +5n
59 .B int \fIstringcount\fP, int \fIstringnumber\fP, char *\fIbuffer\fP,
60 .ti +5n
61 .B int \fIbuffersize\fP);
62 .PP
63 .br
64 .B int pcre_get_named_substring(const pcre *\fIcode\fP,
65 .ti +5n
66 .B const char *\fIsubject\fP, int *\fIovector\fP,
67 .ti +5n
68 .B int \fIstringcount\fP, const char *\fIstringname\fP,
69 .ti +5n
70 .B const char **\fIstringptr\fP);
71 .PP
72 .br
73 .B int pcre_get_stringnumber(const pcre *\fIcode\fP,
74 .ti +5n
75 .B const char *\fIname\fP);
76 .PP
77 .br
78 .B int pcre_get_substring(const char *\fIsubject\fP, int *\fIovector\fP,
79 .ti +5n
80 .B int \fIstringcount\fP, int \fIstringnumber\fP,
81 .ti +5n
82 .B const char **\fIstringptr\fP);
83 .PP
84 .br
85 .B int pcre_get_substring_list(const char *\fIsubject\fP,
86 .ti +5n
87 .B int *\fIovector\fP, int \fIstringcount\fP, "const char ***\fIlistptr\fP);"
88 .PP
89 .br
90 .B void pcre_free_substring(const char *\fIstringptr\fP);
91 .PP
92 .br
93 .B void pcre_free_substring_list(const char **\fIstringptr\fP);
94 .PP
95 .br
96 .B const unsigned char *pcre_maketables(void);
97 .PP
98 .br
99 .B int pcre_fullinfo(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
100 .ti +5n
101 .B int \fIwhat\fP, void *\fIwhere\fP);
102 .PP
103 .br
104 .B int pcre_info(const pcre *\fIcode\fP, int *\fIoptptr\fP, int
105 .B *\fIfirstcharptr\fP);
106 .PP
107 .br
108 .B int pcre_refcount(pcre *\fIcode\fP, int \fIadjust\fP);
109 .PP
110 .br
111 .B int pcre_config(int \fIwhat\fP, void *\fIwhere\fP);
112 .PP
113 .br
114 .B char *pcre_version(void);
115 .PP
116 .br
117 .B void *(*pcre_malloc)(size_t);
118 .PP
119 .br
120 .B void (*pcre_free)(void *);
121 .PP
122 .br
123 .B void *(*pcre_stack_malloc)(size_t);
124 .PP
125 .br
126 .B void (*pcre_stack_free)(void *);
127 .PP
128 .br
129 .B int (*pcre_callout)(pcre_callout_block *);
130 .
131 .
133 .rs
134 .sp
135 PCRE has its own native API, which is described in this document. There is
136 also a set of wrapper functions that correspond to the POSIX regular expression
137 API. These are described in the
138 .\" HREF
139 \fBpcreposix\fP
140 .\"
141 documentation. Both of these APIs define a set of C function calls. A C++
142 wrapper is distributed with PCRE. It is documented in the
143 .\" HREF
144 \fBpcrecpp\fP
145 .\"
146 page.
147 .P
148 The native API C function prototypes are defined in the header file
149 \fBpcre.h\fP, and on Unix systems the library itself is called \fBlibpcre\fP.
150 It can normally be accessed by adding \fB-lpcre\fP to the command for linking
151 an application that uses PCRE. The header file defines the macros PCRE_MAJOR
152 and PCRE_MINOR to contain the major and minor release numbers for the library.
153 Applications can use these to include support for different releases of PCRE.
154 .P
155 The functions \fBpcre_compile()\fP, \fBpcre_compile2()\fP, \fBpcre_study()\fP,
156 and \fBpcre_exec()\fP are used for compiling and matching regular expressions
157 in a Perl-compatible manner. A sample program that demonstrates the simplest
158 way of using them is provided in the file called \fIpcredemo.c\fP in the source
159 distribution. The
160 .\" HREF
161 \fBpcresample\fP
162 .\"
163 documentation describes how to run it.
164 .P
165 A second matching function, \fBpcre_dfa_exec()\fP, which is not
166 Perl-compatible, is also provided. This uses a different algorithm for the
167 matching. This allows it to find all possible matches (at a given point in the
168 subject), not just one. However, this algorithm does not return captured
169 substrings. A description of the two matching algorithms and their advantages
170 and disadvantages is given in the
171 .\" HREF
172 \fBpcrematching\fP
173 .\"
174 documentation.
175 .P
176 In addition to the main compiling and matching functions, there are convenience
177 functions for extracting captured substrings from a subject string that is
178 matched by \fBpcre_exec()\fP. They are:
179 .sp
180 \fBpcre_copy_substring()\fP
181 \fBpcre_copy_named_substring()\fP
182 \fBpcre_get_substring()\fP
183 \fBpcre_get_named_substring()\fP
184 \fBpcre_get_substring_list()\fP
185 \fBpcre_get_stringnumber()\fP
186 .sp
187 \fBpcre_free_substring()\fP and \fBpcre_free_substring_list()\fP are also
188 provided, to free the memory used for extracted strings.
189 .P
190 The function \fBpcre_maketables()\fP is used to build a set of character tables
191 in the current locale for passing to \fBpcre_compile()\fP, \fBpcre_exec()\fP,
192 or \fBpcre_dfa_exec()\fP. This is an optional facility that is provided for
193 specialist use. Most commonly, no special tables are passed, in which case
194 internal tables that are generated when PCRE is built are used.
195 .P
196 The function \fBpcre_fullinfo()\fP is used to find out information about a
197 compiled pattern; \fBpcre_info()\fP is an obsolete version that returns only
198 some of the available information, but is retained for backwards compatibility.
199 The function \fBpcre_version()\fP returns a pointer to a string containing the
200 version of PCRE and its date of release.
201 .P
202 The function \fBpcre_refcount()\fP maintains a reference count in a data block
203 containing a compiled pattern. This is provided for the benefit of
204 object-oriented applications.
205 .P
206 The global variables \fBpcre_malloc\fP and \fBpcre_free\fP initially contain
207 the entry points of the standard \fBmalloc()\fP and \fBfree()\fP functions,
208 respectively. PCRE calls the memory management functions via these variables,
209 so a calling program can replace them if it wishes to intercept the calls. This
210 should be done before calling any PCRE functions.
211 .P
212 The global variables \fBpcre_stack_malloc\fP and \fBpcre_stack_free\fP are also
213 indirections to memory management functions. These special functions are used
214 only when PCRE is compiled to use the heap for remembering data, instead of
215 recursive function calls, when running the \fBpcre_exec()\fP function. This is
216 a non-standard way of building PCRE, for use in environments that have limited
217 stacks. Because of the greater use of memory management, it runs more slowly.
218 Separate functions are provided so that special-purpose external code can be
219 used for this case. When used, these functions are always called in a
220 stack-like manner (last obtained, first freed), and always for memory blocks of
221 the same size.
222 .P
223 The global variable \fBpcre_callout\fP initially contains NULL. It can be set
224 by the caller to a "callout" function, which PCRE will then call at specified
225 points during a matching operation. Details are given in the
226 .\" HREF
227 \fBpcrecallout\fP
228 .\"
229 documentation.
230 .
231 .
233 .rs
234 .sp
235 The PCRE functions can be used in multi-threading applications, with the
236 proviso that the memory management functions pointed to by \fBpcre_malloc\fP,
237 \fBpcre_free\fP, \fBpcre_stack_malloc\fP, and \fBpcre_stack_free\fP, and the
238 callout function pointed to by \fBpcre_callout\fP, are shared by all threads.
239 .P
240 The compiled form of a regular expression is not altered during matching, so
241 the same compiled pattern can safely be used by several threads at once.
242 .
243 .
245 .rs
246 .sp
247 The compiled form of a regular expression can be saved and re-used at a later
248 time, possibly by a different program, and even on a host other than the one on
249 which it was compiled. Details are given in the
250 .\" HREF
251 \fBpcreprecompile\fP
252 .\"
253 documentation.
254 .
255 .
257 .rs
258 .sp
259 .B int pcre_config(int \fIwhat\fP, void *\fIwhere\fP);
260 .PP
261 The function \fBpcre_config()\fP makes it possible for a PCRE client to
262 discover which optional features have been compiled into the PCRE library. The
263 .\" HREF
264 \fBpcrebuild\fP
265 .\"
266 documentation has more details about these optional features.
267 .P
268 The first argument for \fBpcre_config()\fP is an integer, specifying which
269 information is required; the second argument is a pointer to a variable into
270 which the information is placed. The following information is available:
271 .sp
273 .sp
274 The output is an integer that is set to one if UTF-8 support is available;
275 otherwise it is set to zero.
276 .sp
278 .sp
279 The output is an integer that is set to one if support for Unicode character
280 properties is available; otherwise it is set to zero.
281 .sp
283 .sp
284 The output is an integer that is set to the value of the code that is used for
285 the newline character. It is either linefeed (10) or carriage return (13), and
286 should normally be the standard character for your operating system.
287 .sp
289 .sp
290 The output is an integer that contains the number of bytes used for internal
291 linkage in compiled regular expressions. The value is 2, 3, or 4. Larger values
292 allow larger regular expressions to be compiled, at the expense of slower
293 matching. The default value of 2 is sufficient for all but the most massive
294 patterns, since it allows the compiled pattern to be up to 64K in size.
295 .sp
297 .sp
298 The output is an integer that contains the threshold above which the POSIX
299 interface uses \fBmalloc()\fP for output vectors. Further details are given in
300 the
301 .\" HREF
302 \fBpcreposix\fP
303 .\"
304 documentation.
305 .sp
307 .sp
308 The output is an integer that gives the default limit for the number of
309 internal matching function calls in a \fBpcre_exec()\fP execution. Further
310 details are given with \fBpcre_exec()\fP below.
311 .sp
313 .sp
314 The output is an integer that is set to one if internal recursion when running
315 \fBpcre_exec()\fP is implemented by recursive function calls that use the stack
316 to remember their state. This is the usual way that PCRE is compiled. The
317 output is zero if PCRE was compiled to use blocks of data on the heap instead
318 of recursive function calls. In this case, \fBpcre_stack_malloc\fP and
319 \fBpcre_stack_free\fP are called to manage memory blocks on the heap, thus
320 avoiding the use of the stack.
321 .
322 .
324 .rs
325 .sp
326 .B pcre *pcre_compile(const char *\fIpattern\fP, int \fIoptions\fP,
327 .ti +5n
328 .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
329 .ti +5n
330 .B const unsigned char *\fItableptr\fP);
331 .sp
332 .B pcre *pcre_compile2(const char *\fIpattern\fP, int \fIoptions\fP,
333 .ti +5n
334 .B int *\fIerrorcodeptr\fP,
335 .ti +5n
336 .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
337 .ti +5n
338 .B const unsigned char *\fItableptr\fP);
339 .P
340 Either of the functions \fBpcre_compile()\fP or \fBpcre_compile2()\fP can be
341 called to compile a pattern into an internal form. The only difference between
342 the two interfaces is that \fBpcre_compile2()\fP has an additional argument,
343 \fIerrorcodeptr\fP, via which a numerical error code can be returned.
344 .P
345 The pattern is a C string terminated by a binary zero, and is passed in the
346 \fIpattern\fP argument. A pointer to a single block of memory that is obtained
347 via \fBpcre_malloc\fP is returned. This contains the compiled code and related
348 data. The \fBpcre\fP type is defined for the returned block; this is a typedef
349 for a structure whose contents are not externally defined. It is up to the
350 caller to free the memory when it is no longer required.
351 .P
352 Although the compiled code of a PCRE regex is relocatable, that is, it does not
353 depend on memory location, the complete \fBpcre\fP data block is not
354 fully relocatable, because it may contain a copy of the \fItableptr\fP
355 argument, which is an address (see below).
356 .P
357 The \fIoptions\fP argument contains independent bits that affect the
358 compilation. It should be zero if no options are required. The available
359 options are described below. Some of them, in particular, those that are
360 compatible with Perl, can also be set and unset from within the pattern (see
361 the detailed description in the
362 .\" HREF
363 \fBpcrepattern\fP
364 .\"
365 documentation). For these options, the contents of the \fIoptions\fP argument
366 specifies their initial settings at the start of compilation and execution. The
367 PCRE_ANCHORED option can be set at the time of matching as well as at compile
368 time.
369 .P
370 If \fIerrptr\fP is NULL, \fBpcre_compile()\fP returns NULL immediately.
371 Otherwise, if compilation of a pattern fails, \fBpcre_compile()\fP returns
372 NULL, and sets the variable pointed to by \fIerrptr\fP to point to a textual
373 error message. The offset from the start of the pattern to the character where
374 the error was discovered is placed in the variable pointed to by
375 \fIerroffset\fP, which must not be NULL. If it is, an immediate error is given.
376 .P
377 If \fBpcre_compile2()\fP is used instead of \fBpcre_compile()\fP, and the
378 \fIerrorcodeptr\fP argument is not NULL, a non-zero error code number is
379 returned via this argument in the event of an error. This is in addition to the
380 textual error message. Error codes and messages are listed below.
381 .P
382 If the final argument, \fItableptr\fP, is NULL, PCRE uses a default set of
383 character tables that are built when PCRE is compiled, using the default C
384 locale. Otherwise, \fItableptr\fP must be an address that is the result of a
385 call to \fBpcre_maketables()\fP. This value is stored with the compiled
386 pattern, and used again by \fBpcre_exec()\fP, unless another table pointer is
387 passed to it. For more discussion, see the section on locale support below.
388 .P
389 This code fragment shows a typical straightforward call to \fBpcre_compile()\fP:
390 .sp
391 pcre *re;
392 const char *error;
393 int erroffset;
394 re = pcre_compile(
395 "^A.*Z", /* the pattern */
396 0, /* default options */
397 &error, /* for error message */
398 &erroffset, /* for error offset */
399 NULL); /* use default character tables */
400 .sp
401 The following names for option bits are defined in the \fBpcre.h\fP header
402 file:
403 .sp
405 .sp
406 If this bit is set, the pattern is forced to be "anchored", that is, it is
407 constrained to match only at the first matching point in the string that is
408 being searched (the "subject string"). This effect can also be achieved by
409 appropriate constructs in the pattern itself, which is the only way to do it in
410 Perl.
411 .sp
413 .sp
414 If this bit is set, \fBpcre_compile()\fP automatically inserts callout items,
415 all with number 255, before each pattern item. For discussion of the callout
416 facility, see the
417 .\" HREF
418 \fBpcrecallout\fP
419 .\"
420 documentation.
421 .sp
423 .sp
424 If this bit is set, letters in the pattern match both upper and lower case
425 letters. It is equivalent to Perl's /i option, and it can be changed within a
426 pattern by a (?i) option setting. In UTF-8 mode, PCRE always understands the
427 concept of case for characters whose values are less than 128, so caseless
428 matching is always possible. For characters with higher values, the concept of
429 case is supported if PCRE is compiled with Unicode property support, but not
430 otherwise. If you want to use caseless matching for characters 128 and above,
431 you must ensure that PCRE is compiled with Unicode property support as well as
432 with UTF-8 support.
433 .sp
435 .sp
436 If this bit is set, a dollar metacharacter in the pattern matches only at the
437 end of the subject string. Without this option, a dollar also matches
438 immediately before the final character if it is a newline (but not before any
439 other newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
440 set. There is no equivalent to this option in Perl, and no way to set it within
441 a pattern.
442 .sp
444 .sp
445 If this bit is set, a dot metacharater in the pattern matches all characters,
446 including newlines. Without it, newlines are excluded. This option is
447 equivalent to Perl's /s option, and it can be changed within a pattern by a
448 (?s) option setting. A negative class such as [^a] always matches a newline
449 character, independent of the setting of this option.
450 .sp
452 .sp
453 If this bit is set, whitespace data characters in the pattern are totally
454 ignored except when escaped or inside a character class. Whitespace does not
455 include the VT character (code 11). In addition, characters between an
456 unescaped # outside a character class and the next newline character,
457 inclusive, are also ignored. This is equivalent to Perl's /x option, and it can
458 be changed within a pattern by a (?x) option setting.
459 .P
460 This option makes it possible to include comments inside complicated patterns.
461 Note, however, that this applies only to data characters. Whitespace characters
462 may never appear within special character sequences in a pattern, for example
463 within the sequence (?( which introduces a conditional subpattern.
464 .sp
466 .sp
467 This option was invented in order to turn on additional functionality of PCRE
468 that is incompatible with Perl, but it is currently of very little use. When
469 set, any backslash in a pattern that is followed by a letter that has no
470 special meaning causes an error, thus reserving these combinations for future
471 expansion. By default, as in Perl, a backslash followed by a letter with no
472 special meaning is treated as a literal. There are at present no other features
473 controlled by this option. It can also be set by a (?X) option setting within a
474 pattern.
475 .sp
477 .sp
478 If this option is set, an unanchored pattern is required to match before or at
479 the first newline character in the subject string, though the matched text may
480 continue over the newline.
481 .sp
483 .sp
484 By default, PCRE treats the subject string as consisting of a single line of
485 characters (even if it actually contains newlines). The "start of line"
486 metacharacter (^) matches only at the start of the string, while the "end of
487 line" metacharacter ($) matches only at the end of the string, or before a
488 terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
489 Perl.
490 .P
491 When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
492 match immediately following or immediately before any newline in the subject
493 string, respectively, as well as at the very start and end. This is equivalent
494 to Perl's /m option, and it can be changed within a pattern by a (?m) option
495 setting. If there are no "\en" characters in a subject string, or no
496 occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no effect.
497 .sp
499 .sp
500 If this option is set, it disables the use of numbered capturing parentheses in
501 the pattern. Any opening parenthesis that is not followed by ? behaves as if it
502 were followed by ?: but named parentheses can still be used for capturing (and
503 they acquire numbers in the usual way). There is no equivalent of this option
504 in Perl.
505 .sp
507 .sp
508 This option inverts the "greediness" of the quantifiers so that they are not
509 greedy by default, but become greedy if followed by "?". It is not compatible
510 with Perl. It can also be set by a (?U) option setting within the pattern.
511 .sp
513 .sp
514 This option causes PCRE to regard both the pattern and the subject as strings
515 of UTF-8 characters instead of single-byte character strings. However, it is
516 available only when PCRE is built to include UTF-8 support. If not, the use
517 of this option provokes an error. Details of how this option changes the
518 behaviour of PCRE are given in the
519 .\" HTML <a href="pcre.html#utf8support">
520 .\" </a>
521 section on UTF-8 support
522 .\"
523 in the main
524 .\" HREF
525 \fBpcre\fP
526 .\"
527 page.
528 .sp
530 .sp
531 When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
532 automatically checked. If an invalid UTF-8 sequence of bytes is found,
533 \fBpcre_compile()\fP returns an error. If you already know that your pattern is
534 valid, and you want to skip this check for performance reasons, you can set the
535 PCRE_NO_UTF8_CHECK option. When it is set, the effect of passing an invalid
536 UTF-8 string as a pattern is undefined. It may cause your program to crash.
537 Note that this option can also be passed to \fBpcre_exec()\fP and
538 \fBpcre_dfa_exec()\fP, to suppress the UTF-8 validity checking of subject
539 strings.
540 .
541 .
543 .rs
544 .sp
545 The following table lists the error codes than may be returned by
546 \fBpcre_compile2()\fP, along with the error messages that may be returned by
547 both compiling functions.
548 .sp
549 0 no error
550 1 \e at end of pattern
551 2 \ec at end of pattern
552 3 unrecognized character follows \e
553 4 numbers out of order in {} quantifier
554 5 number too big in {} quantifier
555 6 missing terminating ] for character class
556 7 invalid escape sequence in character class
557 8 range out of order in character class
558 9 nothing to repeat
559 10 operand of unlimited repeat could match the empty string
560 11 internal error: unexpected repeat
561 12 unrecognized character after (?
562 13 POSIX named classes are supported only within a class
563 14 missing )
564 15 reference to non-existent subpattern
565 16 erroffset passed as NULL
566 17 unknown option bit(s) set
567 18 missing ) after comment
568 19 parentheses nested too deeply
569 20 regular expression too large
570 21 failed to get memory
571 22 unmatched parentheses
572 23 internal error: code overflow
573 24 unrecognized character after (?<
574 25 lookbehind assertion is not fixed length
575 26 malformed number after (?(
576 27 conditional group contains more than two branches
577 28 assertion expected after (?(
578 29 (?R or (?digits must be followed by )
579 30 unknown POSIX class name
580 31 POSIX collating elements are not supported
581 32 this version of PCRE is not compiled with PCRE_UTF8 support
582 33 spare error
583 34 character value in \ex{...} sequence is too large
584 35 invalid condition (?(0)
585 36 \eC not allowed in lookbehind assertion
586 37 PCRE does not support \eL, \el, \eN, \eU, or \eu
587 38 number after (?C is > 255
588 39 closing ) for (?C expected
589 40 recursive call could loop indefinitely
590 41 unrecognized character after (?P
591 42 syntax error after (?P
592 43 two named groups have the same name
593 44 invalid UTF-8 string
594 45 support for \eP, \ep, and \eX has not been compiled
595 46 malformed \eP or \ep sequence
596 47 unknown property name after \eP or \ep
597 .
598 .
600 .rs
601 .sp
602 .B pcre_extra *pcre_study(const pcre *\fIcode\fP, int \fIoptions\fP
603 .ti +5n
604 .B const char **\fIerrptr\fP);
605 .PP
606 If a compiled pattern is going to be used several times, it is worth spending
607 more time analyzing it in order to speed up the time taken for matching. The
608 function \fBpcre_study()\fP takes a pointer to a compiled pattern as its first
609 argument. If studying the pattern produces additional information that will
610 help speed up matching, \fBpcre_study()\fP returns a pointer to a
611 \fBpcre_extra\fP block, in which the \fIstudy_data\fP field points to the
612 results of the study.
613 .P
614 The returned value from \fBpcre_study()\fP can be passed directly to
615 \fBpcre_exec()\fP. However, a \fBpcre_extra\fP block also contains other
616 fields that can be set by the caller before the block is passed; these are
617 described
618 .\" HTML <a href="#extradata">
619 .\" </a>
620 below
621 .\"
622 in the section on matching a pattern.
623 .P
624 If studying the pattern does not produce any additional information
625 \fBpcre_study()\fP returns NULL. In that circumstance, if the calling program
626 wants to pass any of the other fields to \fBpcre_exec()\fP, it must set up its
627 own \fBpcre_extra\fP block.
628 .P
629 The second argument of \fBpcre_study()\fP contains option bits. At present, no
630 options are defined, and this argument should always be zero.
631 .P
632 The third argument for \fBpcre_study()\fP is a pointer for an error message. If
633 studying succeeds (even if no data is returned), the variable it points to is
634 set to NULL. Otherwise it points to a textual error message. You should
635 therefore test the error pointer for NULL after calling \fBpcre_study()\fP, to
636 be sure that it has run successfully.
637 .P
638 This is a typical call to \fBpcre_study\fP():
639 .sp
640 pcre_extra *pe;
641 pe = pcre_study(
642 re, /* result of pcre_compile() */
643 0, /* no options exist */
644 &error); /* set to NULL or points to a message */
645 .sp
646 At present, studying a pattern is useful only for non-anchored patterns that do
647 not have a single fixed starting character. A bitmap of possible starting
648 bytes is created.
649 .
650 .
651 .\" HTML <a name="localesupport"></a>
653 .rs
654 .sp
655 PCRE handles caseless matching, and determines whether characters are letters
656 digits, or whatever, by reference to a set of tables, indexed by character
657 value. When running in UTF-8 mode, this applies only to characters with codes
658 less than 128. Higher-valued codes never match escapes such as \ew or \ed, but
659 can be tested with \ep if PCRE is built with Unicode character property
660 support.
661 .P
662 An internal set of tables is created in the default C locale when PCRE is
663 built. This is used when the final argument of \fBpcre_compile()\fP is NULL,
664 and is sufficient for many applications. An alternative set of tables can,
665 however, be supplied. These may be created in a different locale from the
666 default. As more and more applications change to using Unicode, the need for
667 this locale support is expected to die away.
668 .P
669 External tables are built by calling the \fBpcre_maketables()\fP function,
670 which has no arguments, in the relevant locale. The result can then be passed
671 to \fBpcre_compile()\fP or \fBpcre_exec()\fP as often as necessary. For
672 example, to build and use tables that are appropriate for the French locale
673 (where accented characters with values greater than 128 are treated as letters),
674 the following code could be used:
675 .sp
676 setlocale(LC_CTYPE, "fr_FR");
677 tables = pcre_maketables();
678 re = pcre_compile(..., tables);
679 .sp
680 When \fBpcre_maketables()\fP runs, the tables are built in memory that is
681 obtained via \fBpcre_malloc\fP. It is the caller's responsibility to ensure
682 that the memory containing the tables remains available for as long as it is
683 needed.
684 .P
685 The pointer that is passed to \fBpcre_compile()\fP is saved with the compiled
686 pattern, and the same tables are used via this pointer by \fBpcre_study()\fP
687 and normally also by \fBpcre_exec()\fP. Thus, by default, for any single
688 pattern, compilation, studying and matching all happen in the same locale, but
689 different patterns can be compiled in different locales.
690 .P
691 It is possible to pass a table pointer or NULL (indicating the use of the
692 internal tables) to \fBpcre_exec()\fP. Although not intended for this purpose,
693 this facility could be used to match a pattern in a different locale from the
694 one in which it was compiled. Passing table pointers at run time is discussed
695 below in the section on matching a pattern.
696 .
697 .
699 .rs
700 .sp
701 .B int pcre_fullinfo(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
702 .ti +5n
703 .B int \fIwhat\fP, void *\fIwhere\fP);
704 .PP
705 The \fBpcre_fullinfo()\fP function returns information about a compiled
706 pattern. It replaces the obsolete \fBpcre_info()\fP function, which is
707 nevertheless retained for backwards compability (and is documented below).
708 .P
709 The first argument for \fBpcre_fullinfo()\fP is a pointer to the compiled
710 pattern. The second argument is the result of \fBpcre_study()\fP, or NULL if
711 the pattern was not studied. The third argument specifies which piece of
712 information is required, and the fourth argument is a pointer to a variable
713 to receive the data. The yield of the function is zero for success, or one of
714 the following negative numbers:
715 .sp
716 PCRE_ERROR_NULL the argument \fIcode\fP was NULL
717 the argument \fIwhere\fP was NULL
718 PCRE_ERROR_BADMAGIC the "magic number" was not found
719 PCRE_ERROR_BADOPTION the value of \fIwhat\fP was invalid
720 .sp
721 The "magic number" is placed at the start of each compiled pattern as an simple
722 check against passing an arbitrary memory pointer. Here is a typical call of
723 \fBpcre_fullinfo()\fP, to obtain the length of the compiled pattern:
724 .sp
725 int rc;
726 unsigned long int length;
727 rc = pcre_fullinfo(
728 re, /* result of pcre_compile() */
729 pe, /* result of pcre_study(), or NULL */
730 PCRE_INFO_SIZE, /* what is required */
731 &length); /* where to put the data */
732 .sp
733 The possible values for the third argument are defined in \fBpcre.h\fP, and are
734 as follows:
735 .sp
737 .sp
738 Return the number of the highest back reference in the pattern. The fourth
739 argument should point to an \fBint\fP variable. Zero is returned if there are
740 no back references.
741 .sp
743 .sp
744 Return the number of capturing subpatterns in the pattern. The fourth argument
745 should point to an \fBint\fP variable.
746 .sp
748 .sp
749 Return a pointer to the internal default character tables within PCRE. The
750 fourth argument should point to an \fBunsigned char *\fP variable. This
751 information call is provided for internal use by the \fBpcre_study()\fP
752 function. External callers can cause PCRE to use its internal tables by passing
753 a NULL table pointer.
754 .sp
756 .sp
757 Return information about the first byte of any matched string, for a
758 non-anchored pattern. (This option used to be called PCRE_INFO_FIRSTCHAR; the
759 old name is still recognized for backwards compatibility.)
760 .P
761 If there is a fixed first byte, for example, from a pattern such as
762 (cat|cow|coyote), it is returned in the integer pointed to by \fIwhere\fP.
763 Otherwise, if either
764 .sp
765 (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
766 starts with "^", or
767 .sp
768 (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
769 (if it were set, the pattern would be anchored),
770 .sp
771 -1 is returned, indicating that the pattern matches only at the start of a
772 subject string or after any newline within the string. Otherwise -2 is
773 returned. For anchored patterns, -2 is returned.
774 .sp
776 .sp
777 If the pattern was studied, and this resulted in the construction of a 256-bit
778 table indicating a fixed set of bytes for the first byte in any matching
779 string, a pointer to the table is returned. Otherwise NULL is returned. The
780 fourth argument should point to an \fBunsigned char *\fP variable.
781 .sp
783 .sp
784 Return the value of the rightmost literal byte that must exist in any matched
785 string, other than at its start, if such a byte has been recorded. The fourth
786 argument should point to an \fBint\fP variable. If there is no such byte, -1 is
787 returned. For anchored patterns, a last literal byte is recorded only if it
788 follows something of variable length. For example, for the pattern
789 /^a\ed+z\ed+/ the returned value is "z", but for /^a\edz\ed/ the returned value
790 is -1.
791 .sp
795 .sp
796 PCRE supports the use of named as well as numbered capturing parentheses. The
797 names are just an additional way of identifying the parentheses, which still
798 acquire numbers. A convenience function called \fBpcre_get_named_substring()\fP
799 is provided for extracting an individual captured substring by name. It is also
800 possible to extract the data directly, by first converting the name to a number
801 in order to access the correct pointers in the output vector (described with
802 \fBpcre_exec()\fP below). To do the conversion, you need to use the
803 name-to-number map, which is described by these three values.
804 .P
805 The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT gives
806 the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size of each
807 entry; both of these return an \fBint\fP value. The entry size depends on the
808 length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
809 entry of the table (a pointer to \fBchar\fP). The first two bytes of each entry
810 are the number of the capturing parenthesis, most significant byte first. The
811 rest of the entry is the corresponding name, zero terminated. The names are in
812 alphabetical order. For example, consider the following pattern (assume
813 PCRE_EXTENDED is set, so white space - including newlines - is ignored):
814 .sp
815 .\" JOIN
816 (?P<date> (?P<year>(\ed\ed)?\ed\ed) -
817 (?P<month>\ed\ed) - (?P<day>\ed\ed) )
818 .sp
819 There are four named subpatterns, so the table has four entries, and each entry
820 in the table is eight bytes long. The table is as follows, with non-printing
821 bytes shows in hexadecimal, and undefined bytes shown as ??:
822 .sp
823 00 01 d a t e 00 ??
824 00 05 d a y 00 ?? ??
825 00 04 m o n t h 00
826 00 02 y e a r 00 ??
827 .sp
828 When writing code to extract data from named subpatterns using the
829 name-to-number map, remember that the length of each entry is likely to be
830 different for each compiled pattern.
831 .sp
833 .sp
834 Return a copy of the options with which the pattern was compiled. The fourth
835 argument should point to an \fBunsigned long int\fP variable. These option bits
836 are those specified in the call to \fBpcre_compile()\fP, modified by any
837 top-level option settings within the pattern itself.
838 .P
839 A pattern is automatically anchored by PCRE if all of its top-level
840 alternatives begin with one of the following:
841 .sp
842 ^ unless PCRE_MULTILINE is set
843 \eA always
844 \eG always
845 .\" JOIN
846 .* if PCRE_DOTALL is set and there are no back
847 references to the subpattern in which .* appears
848 .sp
849 For such patterns, the PCRE_ANCHORED bit is set in the options returned by
850 \fBpcre_fullinfo()\fP.
851 .sp
853 .sp
854 Return the size of the compiled pattern, that is, the value that was passed as
855 the argument to \fBpcre_malloc()\fP when PCRE was getting memory in which to
856 place the compiled data. The fourth argument should point to a \fBsize_t\fP
857 variable.
858 .sp
860 .sp
861 Return the size of the data block pointed to by the \fIstudy_data\fP field in
862 a \fBpcre_extra\fP block. That is, it is the value that was passed to
863 \fBpcre_malloc()\fP when PCRE was getting memory into which to place the data
864 created by \fBpcre_study()\fP. The fourth argument should point to a
865 \fBsize_t\fP variable.
866 .
867 .
869 .rs
870 .sp
871 .B int pcre_info(const pcre *\fIcode\fP, int *\fIoptptr\fP, int
872 .B *\fIfirstcharptr\fP);
873 .PP
874 The \fBpcre_info()\fP function is now obsolete because its interface is too
875 restrictive to return all the available data about a compiled pattern. New
876 programs should use \fBpcre_fullinfo()\fP instead. The yield of
877 \fBpcre_info()\fP is the number of capturing subpatterns, or one of the
878 following negative numbers:
879 .sp
880 PCRE_ERROR_NULL the argument \fIcode\fP was NULL
881 PCRE_ERROR_BADMAGIC the "magic number" was not found
882 .sp
883 If the \fIoptptr\fP argument is not NULL, a copy of the options with which the
884 pattern was compiled is placed in the integer it points to (see
886 .P
887 If the pattern is not anchored and the \fIfirstcharptr\fP argument is not NULL,
888 it is used to pass back information about the first character of any matched
889 string (see PCRE_INFO_FIRSTBYTE above).
890 .
891 .
893 .rs
894 .sp
895 .B int pcre_refcount(pcre *\fIcode\fP, int \fIadjust\fP);
896 .PP
897 The \fBpcre_refcount()\fP function is used to maintain a reference count in the
898 data block that contains a compiled pattern. It is provided for the benefit of
899 applications that operate in an object-oriented manner, where different parts
900 of the application may be using the same compiled pattern, but you want to free
901 the block when they are all done.
902 .P
903 When a pattern is compiled, the reference count field is initialized to zero.
904 It is changed only by calling this function, whose action is to add the
905 \fIadjust\fP value (which may be positive or negative) to it. The yield of the
906 function is the new value. However, the value of the count is constrained to
907 lie between 0 and 65535, inclusive. If the new value is outside these limits,
908 it is forced to the appropriate limit value.
909 .P
910 Except when it is zero, the reference count is not correctly preserved if a
911 pattern is compiled on one host and then transferred to a host whose byte-order
912 is different. (This seems a highly unlikely scenario.)
913 .
914 .
916 .rs
917 .sp
918 .B int pcre_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
919 .ti +5n
920 .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
921 .ti +5n
922 .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP);
923 .P
924 The function \fBpcre_exec()\fP is called to match a subject string against a
925 compiled pattern, which is passed in the \fIcode\fP argument. If the
926 pattern has been studied, the result of the study should be passed in the
927 \fIextra\fP argument. This function is the main matching facility of the
928 library, and it operates in a Perl-like manner. For specialist use there is
929 also an alternative matching function, which is described
930 .\" HTML <a href="#dfamatch">
931 .\" </a>
932 below
933 .\"
934 in the section about the \fBpcre_dfa_exec()\fP function.
935 .P
936 In most applications, the pattern will have been compiled (and optionally
937 studied) in the same process that calls \fBpcre_exec()\fP. However, it is
938 possible to save compiled patterns and study data, and then use them later
939 in different processes, possibly even on different hosts. For a discussion
940 about this, see the
941 .\" HREF
942 \fBpcreprecompile\fP
943 .\"
944 documentation.
945 .P
946 Here is an example of a simple call to \fBpcre_exec()\fP:
947 .sp
948 int rc;
949 int ovector[30];
950 rc = pcre_exec(
951 re, /* result of pcre_compile() */
952 NULL, /* we didn't study the pattern */
953 "some string", /* the subject string */
954 11, /* the length of the subject string */
955 0, /* start at offset 0 in the subject */
956 0, /* default options */
957 ovector, /* vector of integers for substring information */
958 30); /* number of elements (NOT size in bytes) */
959 .
960 .\" HTML <a name="extradata"></a>
961 .SS "Extra data for \fBpcre_exec()\fR"
962 .rs
963 .sp
964 If the \fIextra\fP argument is not NULL, it must point to a \fBpcre_extra\fP
965 data block. The \fBpcre_study()\fP function returns such a block (when it
966 doesn't return NULL), but you can also create one for yourself, and pass
967 additional information in it. The fields in a \fBpcre_extra\fP block are as
968 follows:
969 .sp
970 unsigned long int \fIflags\fP;
971 void *\fIstudy_data\fP;
972 unsigned long int \fImatch_limit\fP;
973 void *\fIcallout_data\fP;
974 const unsigned char *\fItables\fP;
975 .sp
976 The \fIflags\fP field is a bitmap that specifies which of the other fields
977 are set. The flag bits are:
978 .sp
983 .sp
984 Other flag bits should be set to zero. The \fIstudy_data\fP field is set in the
985 \fBpcre_extra\fP block that is returned by \fBpcre_study()\fP, together with
986 the appropriate flag bit. You should not set this yourself, but you may add to
987 the block by setting the other fields and their corresponding flag bits.
988 .P
989 The \fImatch_limit\fP field provides a means of preventing PCRE from using up a
990 vast amount of resources when running patterns that are not going to match,
991 but which have a very large number of possibilities in their search trees. The
992 classic example is the use of nested unlimited repeats.
993 .P
994 Internally, PCRE uses a function called \fBmatch()\fP which it calls repeatedly
995 (sometimes recursively). The limit is imposed on the number of times this
996 function is called during a match, which has the effect of limiting the amount
997 of recursion and backtracking that can take place. For patterns that are not
998 anchored, the count starts from zero for each position in the subject string.
999 .P
1000 The default limit for the library can be set when PCRE is built; the default
1001 default is 10 million, which handles all but the most extreme cases. You can
1002 reduce the default by suppling \fBpcre_exec()\fP with a \fBpcre_extra\fP block
1003 in which \fImatch_limit\fP is set to a smaller value, and
1004 PCRE_EXTRA_MATCH_LIMIT is set in the \fIflags\fP field. If the limit is
1005 exceeded, \fBpcre_exec()\fP returns PCRE_ERROR_MATCHLIMIT.
1006 .P
1007 The \fIpcre_callout\fP field is used in conjunction with the "callout" feature,
1008 which is described in the
1009 .\" HREF
1010 \fBpcrecallout\fP
1011 .\"
1012 documentation.
1013 .P
1014 The \fItables\fP field is used to pass a character tables pointer to
1015 \fBpcre_exec()\fP; this overrides the value that is stored with the compiled
1016 pattern. A non-NULL value is stored with the compiled pattern only if custom
1017 tables were supplied to \fBpcre_compile()\fP via its \fItableptr\fP argument.
1018 If NULL is passed to \fBpcre_exec()\fP using this mechanism, it forces PCRE's
1019 internal tables to be used. This facility is helpful when re-using patterns
1020 that have been saved after compiling with an external set of tables, because
1021 the external tables might be at a different address when \fBpcre_exec()\fP is
1022 called. See the
1023 .\" HREF
1024 \fBpcreprecompile\fP
1025 .\"
1026 documentation for a discussion of saving compiled patterns for later use.
1027 .
1028 .SS "Option bits for \fBpcre_exec()\fP"
1029 .rs
1030 .sp
1031 The unused bits of the \fIoptions\fP argument for \fBpcre_exec()\fP must be
1032 zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL,
1034 .sp
1036 .sp
1037 The PCRE_ANCHORED option limits \fBpcre_exec()\fP to matching at the first
1038 matching position. If a pattern was compiled with PCRE_ANCHORED, or turned out
1039 to be anchored by virtue of its contents, it cannot be made unachored at
1040 matching time.
1041 .sp
1043 .sp
1044 This option specifies that first character of the subject string is not the
1045 beginning of a line, so the circumflex metacharacter should not match before
1046 it. Setting this without PCRE_MULTILINE (at compile time) causes circumflex
1047 never to match. This option affects only the behaviour of the circumflex
1048 metacharacter. It does not affect \eA.
1049 .sp
1051 .sp
1052 This option specifies that the end of the subject string is not the end of a
1053 line, so the dollar metacharacter should not match it nor (except in multiline
1054 mode) a newline immediately before it. Setting this without PCRE_MULTILINE (at
1055 compile time) causes dollar never to match. This option affects only the
1056 behaviour of the dollar metacharacter. It does not affect \eZ or \ez.
1057 .sp
1059 .sp
1060 An empty string is not considered to be a valid match if this option is set. If
1061 there are alternatives in the pattern, they are tried. If all the alternatives
1062 match the empty string, the entire match fails. For example, if the pattern
1063 .sp
1064 a?b?
1065 .sp
1066 is applied to a string not beginning with "a" or "b", it matches the empty
1067 string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
1068 valid, so PCRE searches further into the string for occurrences of "a" or "b".
1069 .P
1070 Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
1071 of a pattern match of the empty string within its \fBsplit()\fP function, and
1072 when using the /g modifier. It is possible to emulate Perl's behaviour after
1073 matching a null string by first trying the match again at the same offset with
1074 PCRE_NOTEMPTY and PCRE_ANCHORED, and then if that fails by advancing the
1075 starting offset (see below) and trying an ordinary match again. There is some
1076 code that demonstrates how to do this in the \fIpcredemo.c\fP sample program.
1077 .sp
1079 .sp
1080 When PCRE_UTF8 is set at compile time, the validity of the subject as a UTF-8
1081 string is automatically checked when \fBpcre_exec()\fP is subsequently called.
1082 The value of \fIstartoffset\fP is also checked to ensure that it points to the
1083 start of a UTF-8 character. If an invalid UTF-8 sequence of bytes is found,
1084 \fBpcre_exec()\fP returns the error PCRE_ERROR_BADUTF8. If \fIstartoffset\fP
1085 contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.
1086 .P
1087 If you already know that your subject is valid, and you want to skip these
1088 checks for performance reasons, you can set the PCRE_NO_UTF8_CHECK option when
1089 calling \fBpcre_exec()\fP. You might want to do this for the second and
1090 subsequent calls to \fBpcre_exec()\fP if you are making repeated calls to find
1091 all the matches in a single subject string. However, you should be sure that
1092 the value of \fIstartoffset\fP points to the start of a UTF-8 character. When
1093 PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid UTF-8 string as a
1094 subject, or a value of \fIstartoffset\fP that does not point to the start of a
1095 UTF-8 character, is undefined. Your program may crash.
1096 .sp
1098 .sp
1099 This option turns on the partial matching feature. If the subject string fails
1100 to match the pattern, but at some point during the matching process the end of
1101 the subject was reached (that is, the subject partially matches the pattern and
1102 the failure to match occurred only because there were not enough subject
1103 characters), \fBpcre_exec()\fP returns PCRE_ERROR_PARTIAL instead of
1104 PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is used, there are restrictions on what
1105 may appear in the pattern. These are discussed in the
1106 .\" HREF
1107 \fBpcrepartial\fP
1108 .\"
1109 documentation.
1110 .
1111 .SS "The string to be matched by \fBpcre_exec()\fP"
1112 .rs
1113 .sp
1114 The subject string is passed to \fBpcre_exec()\fP as a pointer in
1115 \fIsubject\fP, a length in \fIlength\fP, and a starting byte offset in
1116 \fIstartoffset\fP. In UTF-8 mode, the byte offset must point to the start of a
1117 UTF-8 character. Unlike the pattern string, the subject may contain binary zero
1118 bytes. When the starting offset is zero, the search for a match starts at the
1119 beginning of the subject, and this is by far the most common case.
1120 .P
1121 A non-zero starting offset is useful when searching for another match in the
1122 same subject by calling \fBpcre_exec()\fP again after a previous success.
1123 Setting \fIstartoffset\fP differs from just passing over a shortened string and
1124 setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
1125 lookbehind. For example, consider the pattern
1126 .sp
1127 \eBiss\eB
1128 .sp
1129 which finds occurrences of "iss" in the middle of words. (\eB matches only if
1130 the current position in the subject is not a word boundary.) When applied to
1131 the string "Mississipi" the first call to \fBpcre_exec()\fP finds the first
1132 occurrence. If \fBpcre_exec()\fP is called again with just the remainder of the
1133 subject, namely "issipi", it does not match, because \eB is always false at the
1134 start of the subject, which is deemed to be a word boundary. However, if
1135 \fBpcre_exec()\fP is passed the entire string again, but with \fIstartoffset\fP
1136 set to 4, it finds the second occurrence of "iss" because it is able to look
1137 behind the starting point to discover that it is preceded by a letter.
1138 .P
1139 If a non-zero starting offset is passed when the pattern is anchored, one
1140 attempt to match at the given offset is made. This can only succeed if the
1141 pattern does not require the match to be at the start of the subject.
1142 .
1143 .SS "How \fBpcre_exec()\fP returns captured substrings"
1144 .rs
1145 .sp
1146 In general, a pattern matches a certain portion of the subject, and in
1147 addition, further substrings from the subject may be picked out by parts of the
1148 pattern. Following the usage in Jeffrey Friedl's book, this is called
1149 "capturing" in what follows, and the phrase "capturing subpattern" is used for
1150 a fragment of a pattern that picks out a substring. PCRE supports several other
1151 kinds of parenthesized subpattern that do not cause substrings to be captured.
1152 .P
1153 Captured substrings are returned to the caller via a vector of integer offsets
1154 whose address is passed in \fIovector\fP. The number of elements in the vector
1155 is passed in \fIovecsize\fP, which must be a non-negative number. \fBNote\fP:
1156 this argument is NOT the size of \fIovector\fP in bytes.
1157 .P
1158 The first two-thirds of the vector is used to pass back captured substrings,
1159 each substring using a pair of integers. The remaining third of the vector is
1160 used as workspace by \fBpcre_exec()\fP while matching capturing subpatterns,
1161 and is not available for passing back information. The length passed in
1162 \fIovecsize\fP should always be a multiple of three. If it is not, it is
1163 rounded down.
1164 .P
1165 When a match is successful, information about captured substrings is returned
1166 in pairs of integers, starting at the beginning of \fIovector\fP, and
1167 continuing up to two-thirds of its length at the most. The first element of a
1168 pair is set to the offset of the first character in a substring, and the second
1169 is set to the offset of the first character after the end of a substring. The
1170 first pair, \fIovector[0]\fP and \fIovector[1]\fP, identify the portion of the
1171 subject string matched by the entire pattern. The next pair is used for the
1172 first capturing subpattern, and so on. The value returned by \fBpcre_exec()\fP
1173 is the number of pairs that have been set. If there are no capturing
1174 subpatterns, the return value from a successful match is 1, indicating that
1175 just the first pair of offsets has been set.
1176 .P
1177 Some convenience functions are provided for extracting the captured substrings
1178 as separate strings. These are described in the following section.
1179 .P
1180 It is possible for an capturing subpattern number \fIn+1\fP to match some
1181 part of the subject when subpattern \fIn\fP has not been used at all. For
1182 example, if the string "abc" is matched against the pattern (a|(z))(bc)
1183 subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
1184 values corresponding to the unused subpattern are set to -1.
1185 .P
1186 If a capturing subpattern is matched repeatedly, it is the last portion of the
1187 string that it matched that is returned.
1188 .P
1189 If the vector is too small to hold all the captured substring offsets, it is
1190 used as far as possible (up to two-thirds of its length), and the function
1191 returns a value of zero. In particular, if the substring offsets are not of
1192 interest, \fBpcre_exec()\fP may be called with \fIovector\fP passed as NULL and
1193 \fIovecsize\fP as zero. However, if the pattern contains back references and
1194 the \fIovector\fP is not big enough to remember the related substrings, PCRE
1195 has to get additional memory for use during matching. Thus it is usually
1196 advisable to supply an \fIovector\fP.
1197 .P
1198 Note that \fBpcre_info()\fP can be used to find out how many capturing
1199 subpatterns there are in a compiled pattern. The smallest size for
1200 \fIovector\fP that will allow for \fIn\fP captured substrings, in addition to
1201 the offsets of the substring matched by the whole pattern, is (\fIn\fP+1)*3.
1202 .
1203 .\" HTML <a name="errorlist"></a>
1204 .SS "Return values from \fBpcre_exec()\fP"
1205 .rs
1206 .sp
1207 If \fBpcre_exec()\fP fails, it returns a negative number. The following are
1208 defined in the header file:
1209 .sp
1211 .sp
1212 The subject string did not match the pattern.
1213 .sp
1215 .sp
1216 Either \fIcode\fP or \fIsubject\fP was passed as NULL, or \fIovector\fP was
1217 NULL and \fIovecsize\fP was not zero.
1218 .sp
1220 .sp
1221 An unrecognized bit was set in the \fIoptions\fP argument.
1222 .sp
1224 .sp
1225 PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
1226 the case when it is passed a junk pointer and to detect when a pattern that was
1227 compiled in an environment of one endianness is run in an environment with the
1228 other endianness. This is the error that PCRE gives when the magic number is
1229 not present.
1230 .sp
1232 .sp
1233 While running the pattern match, an unknown item was encountered in the
1234 compiled pattern. This error could be caused by a bug in PCRE or by overwriting
1235 of the compiled pattern.
1236 .sp
1238 .sp
1239 If a pattern contains back references, but the \fIovector\fP that is passed to
1240 \fBpcre_exec()\fP is not big enough to remember the referenced substrings, PCRE
1241 gets a block of memory at the start of matching to use for this purpose. If the
1242 call via \fBpcre_malloc()\fP fails, this error is given. The memory is
1243 automatically freed at the end of matching.
1244 .sp
1246 .sp
1247 This error is used by the \fBpcre_copy_substring()\fP,
1248 \fBpcre_get_substring()\fP, and \fBpcre_get_substring_list()\fP functions (see
1249 below). It is never returned by \fBpcre_exec()\fP.
1250 .sp
1252 .sp
1253 The recursion and backtracking limit, as specified by the \fImatch_limit\fP
1254 field in a \fBpcre_extra\fP structure (or defaulted) was reached. See the
1255 description above.
1256 .sp
1258 .sp
1259 This error is never generated by \fBpcre_exec()\fP itself. It is provided for
1260 use by callout functions that want to yield a distinctive error code. See the
1261 .\" HREF
1262 \fBpcrecallout\fP
1263 .\"
1264 documentation for details.
1265 .sp
1267 .sp
1268 A string that contains an invalid UTF-8 byte sequence was passed as a subject.
1269 .sp
1271 .sp
1272 The UTF-8 byte sequence that was passed as a subject was valid, but the value
1273 of \fIstartoffset\fP did not point to the beginning of a UTF-8 character.
1274 .sp
1276 .sp
1277 The subject string did not match, but it did match partially. See the
1278 .\" HREF
1279 \fBpcrepartial\fP
1280 .\"
1281 documentation for details of partial matching.
1282 .sp
1284 .sp
1285 The PCRE_PARTIAL option was used with a compiled pattern containing items that
1286 are not supported for partial matching. See the
1287 .\" HREF
1288 \fBpcrepartial\fP
1289 .\"
1290 documentation for details of partial matching.
1291 .sp
1293 .sp
1294 An unexpected internal error has occurred. This error could be caused by a bug
1295 in PCRE or by overwriting of the compiled pattern.
1296 .sp
1298 .sp
1299 This error is given if the value of the \fIovecsize\fP argument is negative.
1300 .
1301 .
1303 .rs
1304 .sp
1305 .B int pcre_copy_substring(const char *\fIsubject\fP, int *\fIovector\fP,
1306 .ti +5n
1307 .B int \fIstringcount\fP, int \fIstringnumber\fP, char *\fIbuffer\fP,
1308 .ti +5n
1309 .B int \fIbuffersize\fP);
1310 .PP
1311 .br
1312 .B int pcre_get_substring(const char *\fIsubject\fP, int *\fIovector\fP,
1313 .ti +5n
1314 .B int \fIstringcount\fP, int \fIstringnumber\fP,
1315 .ti +5n
1316 .B const char **\fIstringptr\fP);
1317 .PP
1318 .br
1319 .B int pcre_get_substring_list(const char *\fIsubject\fP,
1320 .ti +5n
1321 .B int *\fIovector\fP, int \fIstringcount\fP, "const char ***\fIlistptr\fP);"
1322 .PP
1323 Captured substrings can be accessed directly by using the offsets returned by
1324 \fBpcre_exec()\fP in \fIovector\fP. For convenience, the functions
1325 \fBpcre_copy_substring()\fP, \fBpcre_get_substring()\fP, and
1326 \fBpcre_get_substring_list()\fP are provided for extracting captured substrings
1327 as new, separate, zero-terminated strings. These functions identify substrings
1328 by number. The next section describes functions for extracting named
1329 substrings. A substring that contains a binary zero is correctly extracted and
1330 has a further zero added on the end, but the result is not, of course,
1331 a C string.
1332 .P
1333 The first three arguments are the same for all three of these functions:
1334 \fIsubject\fP is the subject string that has just been successfully matched,
1335 \fIovector\fP is a pointer to the vector of integer offsets that was passed to
1336 \fBpcre_exec()\fP, and \fIstringcount\fP is the number of substrings that were
1337 captured by the match, including the substring that matched the entire regular
1338 expression. This is the value returned by \fBpcre_exec()\fP if it is greater
1339 than zero. If \fBpcre_exec()\fP returned zero, indicating that it ran out of
1340 space in \fIovector\fP, the value passed as \fIstringcount\fP should be the
1341 number of elements in the vector divided by three.
1342 .P
1343 The functions \fBpcre_copy_substring()\fP and \fBpcre_get_substring()\fP
1344 extract a single substring, whose number is given as \fIstringnumber\fP. A
1345 value of zero extracts the substring that matched the entire pattern, whereas
1346 higher values extract the captured substrings. For \fBpcre_copy_substring()\fP,
1347 the string is placed in \fIbuffer\fP, whose length is given by
1348 \fIbuffersize\fP, while for \fBpcre_get_substring()\fP a new block of memory is
1349 obtained via \fBpcre_malloc\fP, and its address is returned via
1350 \fIstringptr\fP. The yield of the function is the length of the string, not
1351 including the terminating zero, or one of
1352 .sp
1354 .sp
1355 The buffer was too small for \fBpcre_copy_substring()\fP, or the attempt to get
1356 memory failed for \fBpcre_get_substring()\fP.
1357 .sp
1359 .sp
1360 There is no substring whose number is \fIstringnumber\fP.
1361 .P
1362 The \fBpcre_get_substring_list()\fP function extracts all available substrings
1363 and builds a list of pointers to them. All this is done in a single block of
1364 memory that is obtained via \fBpcre_malloc\fP. The address of the memory block
1365 is returned via \fIlistptr\fP, which is also the start of the list of string
1366 pointers. The end of the list is marked by a NULL pointer. The yield of the
1367 function is zero if all went well, or
1368 .sp
1370 .sp
1371 if the attempt to get the memory block failed.
1372 .P
1373 When any of these functions encounter a substring that is unset, which can
1374 happen when capturing subpattern number \fIn+1\fP matches some part of the
1375 subject, but subpattern \fIn\fP has not been used at all, they return an empty
1376 string. This can be distinguished from a genuine zero-length substring by
1377 inspecting the appropriate offset in \fIovector\fP, which is negative for unset
1378 substrings.
1379 .P
1380 The two convenience functions \fBpcre_free_substring()\fP and
1381 \fBpcre_free_substring_list()\fP can be used to free the memory returned by
1382 a previous call of \fBpcre_get_substring()\fP or
1383 \fBpcre_get_substring_list()\fP, respectively. They do nothing more than call
1384 the function pointed to by \fBpcre_free\fP, which of course could be called
1385 directly from a C program. However, PCRE is used in some situations where it is
1386 linked via a special interface to another programming language which cannot use
1387 \fBpcre_free\fP directly; it is for these cases that the functions are
1388 provided.
1389 .
1390 .
1392 .rs
1393 .sp
1394 .B int pcre_get_stringnumber(const pcre *\fIcode\fP,
1395 .ti +5n
1396 .B const char *\fIname\fP);
1397 .PP
1398 .br
1399 .B int pcre_copy_named_substring(const pcre *\fIcode\fP,
1400 .ti +5n
1401 .B const char *\fIsubject\fP, int *\fIovector\fP,
1402 .ti +5n
1403 .B int \fIstringcount\fP, const char *\fIstringname\fP,
1404 .ti +5n
1405 .B char *\fIbuffer\fP, int \fIbuffersize\fP);
1406 .PP
1407 .br
1408 .B int pcre_get_named_substring(const pcre *\fIcode\fP,
1409 .ti +5n
1410 .B const char *\fIsubject\fP, int *\fIovector\fP,
1411 .ti +5n
1412 .B int \fIstringcount\fP, const char *\fIstringname\fP,
1413 .ti +5n
1414 .B const char **\fIstringptr\fP);
1415 .PP
1416 To extract a substring by name, you first have to find associated number.
1417 For example, for this pattern
1418 .sp
1419 (a+)b(?P<xxx>\ed+)...
1420 .sp
1421 the number of the subpattern called "xxx" is 2. You can find the number from
1422 the name by calling \fBpcre_get_stringnumber()\fP. The first argument is the
1423 compiled pattern, and the second is the name. The yield of the function is the
1424 subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no subpattern of
1425 that name.
1426 .P
1427 Given the number, you can extract the substring directly, or use one of the
1428 functions described in the previous section. For convenience, there are also
1429 two functions that do the whole job.
1430 .P
1431 Most of the arguments of \fIpcre_copy_named_substring()\fP and
1432 \fIpcre_get_named_substring()\fP are the same as those for the similarly named
1433 functions that extract by number. As these are described in the previous
1434 section, they are not re-described here. There are just two differences:
1435 .P
1436 First, instead of a substring number, a substring name is given. Second, there
1437 is an extra argument, given at the start, which is a pointer to the compiled
1438 pattern. This is needed in order to gain access to the name-to-number
1439 translation table.
1440 .P
1441 These functions call \fBpcre_get_stringnumber()\fP, and if it succeeds, they
1442 then call \fIpcre_copy_substring()\fP or \fIpcre_get_substring()\fP, as
1443 appropriate.
1444 .
1445 .
1447 .rs
1448 .sp
1449 The traditional matching function uses a similar algorithm to Perl, which stops
1450 when it finds the first match, starting at a given point in the subject. If you
1451 want to find all possible matches, or the longest possible match, consider
1452 using the alternative matching function (see below) instead. If you cannot use
1453 the alternative function, but still need to find all possible matches, you
1454 can kludge it up by making use of the callout facility, which is described in
1455 the
1456 .\" HREF
1457 \fBpcrecallout\fP
1458 .\"
1459 documentation.
1460 .P
1461 What you have to do is to insert a callout right at the end of the pattern.
1462 When your callout function is called, extract and save the current matched
1463 substring. Then return 1, which forces \fBpcre_exec()\fP to backtrack and try
1464 other alternatives. Ultimately, when it runs out of matches, \fBpcre_exec()\fP
1465 will yield PCRE_ERROR_NOMATCH.
1466 .
1467 .
1468 .\" HTML <a name="dfamatch"></a>
1470 .rs
1471 .sp
1472 .B int pcre_dfa_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
1473 .ti +5n
1474 .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
1475 .ti +5n
1476 .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP,
1477 .ti +5n
1478 .B int *\fIworkspace\fP, int \fIwscount\fP);
1479 .P
1480 The function \fBpcre_dfa_exec()\fP is called to match a subject string against
1481 a compiled pattern, using a "DFA" matching algorithm. This has different
1482 characteristics to the normal algorithm, and is not compatible with Perl. Some
1483 of the features of PCRE patterns are not supported. Nevertheless, there are
1484 times when this kind of matching can be useful. For a discussion of the two
1485 matching algorithms, see the
1486 .\" HREF
1487 \fBpcrematching\fP
1488 .\"
1489 documentation.
1490 .P
1491 The arguments for the \fBpcre_dfa_exec()\fP function are the same as for
1492 \fBpcre_exec()\fP, plus two extras. The \fIovector\fP argument is used in a
1493 different way, and this is described below. The other common arguments are used
1494 in the same way as for \fBpcre_exec()\fP, so their description is not repeated
1495 here.
1496 .P
1497 The two additional arguments provide workspace for the function. The workspace
1498 vector should contain at least 20 elements. It is used for keeping track of
1499 multiple paths through the pattern tree. More workspace will be needed for
1500 patterns and subjects where there are a lot of possible matches.
1501 .P
1502 Here is an example of a simple call to \fBpcre_exec()\fP:
1503 .sp
1504 int rc;
1505 int ovector[10];
1506 int wspace[20];
1507 rc = pcre_exec(
1508 re, /* result of pcre_compile() */
1509 NULL, /* we didn't study the pattern */
1510 "some string", /* the subject string */
1511 11, /* the length of the subject string */
1512 0, /* start at offset 0 in the subject */
1513 0, /* default options */
1514 ovector, /* vector of integers for substring information */
1515 10, /* number of elements (NOT size in bytes) */
1516 wspace, /* working space vector */
1517 20); /* number of elements (NOT size in bytes) */
1518 .
1519 .SS "Option bits for \fBpcre_dfa_exec()\fP"
1520 .rs
1521 .sp
1522 The unused bits of the \fIoptions\fP argument for \fBpcre_dfa_exec()\fP must be
1523 zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL,
1525 PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last three of these are
1526 the same as for \fBpcre_exec()\fP, so their description is not repeated here.
1527 .sp
1529 .sp
1530 This has the same general effect as it does for \fBpcre_exec()\fP, but the
1531 details are slightly different. When PCRE_PARTIAL is set for
1532 \fBpcre_dfa_exec()\fP, the return code PCRE_ERROR_NOMATCH is converted into
1533 PCRE_ERROR_PARTIAL if the end of the subject is reached, there have been no
1534 complete matches, but there is still at least one matching possibility. The
1535 portion of the string that provided the partial match is set as the first
1536 matching string.
1537 .sp
1539 .sp
1540 Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to stop as
1541 soon as it has found one match. Because of the way the DFA algorithm works,
1542 this is necessarily the shortest possible match at the first possible matching
1543 point in the subject string.
1544 .sp
1546 .sp
1547 When \fBpcre_dfa_exec()\fP is called with the PCRE_PARTIAL option, and returns
1548 a partial match, it is possible to call it again, with additional subject
1549 characters, and have it continue with the same match. The PCRE_DFA_RESTART
1550 option requests this action; when it is set, the \fIworkspace\fP and
1551 \fIwscount\fP options must reference the same vector as before because data
1552 about the match so far is left in them after a partial match. There is more
1553 discussion of this facility in the
1554 .\" HREF
1555 \fBpcrepartial\fP
1556 .\"
1557 documentation.
1558 .
1559 .SS "Successful returns from \fBpcre_dfa_exec()\fP"
1560 .rs
1561 .sp
1562 When \fBpcre_dfa_exec()\fP succeeds, it may have matched more than one
1563 substring in the subject. Note, however, that all the matches from one run of
1564 the function start at the same point in the subject. The shorter matches are
1565 all initial substrings of the longer matches. For example, if the pattern
1566 .sp
1567 <.*>
1568 .sp
1569 is matched against the string
1570 .sp
1571 This is <something> <something else> <something further> no more
1572 .sp
1573 the three matched strings are
1574 .sp
1575 <something>
1576 <something> <something else>
1577 <something> <something else> <something further>
1578 .sp
1579 On success, the yield of the function is a number greater than zero, which is
1580 the number of matched substrings. The substrings themselves are returned in
1581 \fIovector\fP. Each string uses two elements; the first is the offset to the
1582 start, and the second is the offset to the end. All the strings have the same
1583 start offset. (Space could have been saved by giving this only once, but it was
1584 decided to retain some compatibility with the way \fBpcre_exec()\fP returns
1585 data, even though the meaning of the strings is different.)
1586 .P
1587 The strings are returned in reverse order of length; that is, the longest
1588 matching string is given first. If there were too many matches to fit into
1589 \fIovector\fP, the yield of the function is zero, and the vector is filled with
1590 the longest matches.
1591 .
1592 .SS "Error returns from \fBpcre_dfa_exec()\fP"
1593 .rs
1594 .sp
1595 The \fBpcre_dfa_exec()\fP function returns a negative number when it fails.
1596 Many of the errors are the same as for \fBpcre_exec()\fP, and these are
1597 described
1598 .\" HTML <a href="#errorlist">
1599 .\" </a>
1600 above.
1601 .\"
1602 There are in addition the following errors that are specific to
1603 \fBpcre_dfa_exec()\fP:
1604 .sp
1606 .sp
1607 This return is given if \fBpcre_dfa_exec()\fP encounters an item in the pattern
1608 that it does not support, for instance, the use of \eC or a back reference.
1609 .sp
1611 .sp
1612 This return is given if \fBpcre_dfa_exec()\fP encounters a condition item in a
1613 pattern that uses a back reference for the condition. This is not supported.
1614 .sp
1616 .sp
1617 This return is given if \fBpcre_dfa_exec()\fP is called with an \fIextra\fP
1618 block that contains a setting of the \fImatch_limit\fP field. This is not
1619 supported (it is meaningless).
1620 .sp
1622 .sp
1623 This return is given if \fBpcre_dfa_exec()\fP runs out of space in the
1624 \fIworkspace\fP vector.
1625 .sp
1627 .sp
1628 When a recursive subpattern is processed, the matching function calls itself
1629 recursively, using private vectors for \fIovector\fP and \fIworkspace\fP. This
1630 error is given if the output vector is not large enough. This should be
1631 extremely rare, as a vector of size 1000 is used.
1632 .P
1633 .in 0
1634 Last updated: 16 May 2005
1635 .br
1636 Copyright (c) 1997-2005 University of Cambridge.

  ViewVC Help
Powered by ViewVC 1.1.5