ViewVC logotype

Contents of /code/trunk/doc/pcreapi.3

Parent Directory Parent Directory | Revision Log Revision Log

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

  ViewVC Help
Powered by ViewVC 1.1.5