/[pcre]/code/trunk/doc/pcreapi.3
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (hide annotations) (download)
Sat Feb 24 21:41:21 2007 UTC (7 years, 6 months ago) by nigel
File size: 70011 byte(s)
Load pcre-6.5 into code/trunk.

1 nigel 79 .TH PCREAPI 3
2 nigel 63 .SH NAME
3     PCRE - Perl-compatible regular expressions
4 nigel 75 .SH "PCRE NATIVE API"
5 nigel 63 .rs
6     .sp
7     .B #include <pcre.h>
8     .PP
9     .SM
10     .br
11 nigel 75 .B pcre *pcre_compile(const char *\fIpattern\fP, int \fIoptions\fP,
12 nigel 63 .ti +5n
13 nigel 75 .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
14 nigel 63 .ti +5n
15 nigel 75 .B const unsigned char *\fItableptr\fP);
16 nigel 63 .PP
17     .br
18 nigel 77 .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 nigel 75 .B pcre_extra *pcre_study(const pcre *\fIcode\fP, int \fIoptions\fP,
28 nigel 63 .ti +5n
29 nigel 75 .B const char **\fIerrptr\fP);
30 nigel 63 .PP
31     .br
32 nigel 75 .B int pcre_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
33 nigel 63 .ti +5n
34 nigel 75 .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
35 nigel 63 .ti +5n
36 nigel 75 .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP);
37 nigel 63 .PP
38     .br
39 nigel 77 .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 nigel 75 .B int pcre_copy_named_substring(const pcre *\fIcode\fP,
49 nigel 63 .ti +5n
50 nigel 75 .B const char *\fIsubject\fP, int *\fIovector\fP,
51 nigel 63 .ti +5n
52 nigel 75 .B int \fIstringcount\fP, const char *\fIstringname\fP,
53 nigel 63 .ti +5n
54 nigel 75 .B char *\fIbuffer\fP, int \fIbuffersize\fP);
55 nigel 63 .PP
56     .br
57 nigel 75 .B int pcre_copy_substring(const char *\fIsubject\fP, int *\fIovector\fP,
58 nigel 63 .ti +5n
59 nigel 75 .B int \fIstringcount\fP, int \fIstringnumber\fP, char *\fIbuffer\fP,
60 nigel 63 .ti +5n
61 nigel 75 .B int \fIbuffersize\fP);
62 nigel 63 .PP
63     .br
64 nigel 75 .B int pcre_get_named_substring(const pcre *\fIcode\fP,
65 nigel 63 .ti +5n
66 nigel 75 .B const char *\fIsubject\fP, int *\fIovector\fP,
67 nigel 63 .ti +5n
68 nigel 75 .B int \fIstringcount\fP, const char *\fIstringname\fP,
69 nigel 63 .ti +5n
70 nigel 75 .B const char **\fIstringptr\fP);
71 nigel 63 .PP
72     .br
73 nigel 75 .B int pcre_get_stringnumber(const pcre *\fIcode\fP,
74 nigel 63 .ti +5n
75 nigel 75 .B const char *\fIname\fP);
76 nigel 63 .PP
77     .br
78 nigel 75 .B int pcre_get_substring(const char *\fIsubject\fP, int *\fIovector\fP,
79 nigel 63 .ti +5n
80 nigel 75 .B int \fIstringcount\fP, int \fIstringnumber\fP,
81 nigel 63 .ti +5n
82 nigel 75 .B const char **\fIstringptr\fP);
83 nigel 63 .PP
84     .br
85 nigel 75 .B int pcre_get_substring_list(const char *\fIsubject\fP,
86 nigel 63 .ti +5n
87 nigel 75 .B int *\fIovector\fP, int \fIstringcount\fP, "const char ***\fIlistptr\fP);"
88 nigel 63 .PP
89     .br
90 nigel 75 .B void pcre_free_substring(const char *\fIstringptr\fP);
91 nigel 63 .PP
92     .br
93 nigel 75 .B void pcre_free_substring_list(const char **\fIstringptr\fP);
94 nigel 63 .PP
95     .br
96     .B const unsigned char *pcre_maketables(void);
97     .PP
98     .br
99 nigel 75 .B int pcre_fullinfo(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
100 nigel 63 .ti +5n
101 nigel 75 .B int \fIwhat\fP, void *\fIwhere\fP);
102 nigel 63 .PP
103     .br
104 nigel 75 .B int pcre_info(const pcre *\fIcode\fP, int *\fIoptptr\fP, int
105     .B *\fIfirstcharptr\fP);
106 nigel 63 .PP
107     .br
108 nigel 77 .B int pcre_refcount(pcre *\fIcode\fP, int \fIadjust\fP);
109     .PP
110     .br
111 nigel 75 .B int pcre_config(int \fIwhat\fP, void *\fIwhere\fP);
112 nigel 63 .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 nigel 73 .B void *(*pcre_stack_malloc)(size_t);
124     .PP
125     .br
126     .B void (*pcre_stack_free)(void *);
127     .PP
128     .br
129 nigel 63 .B int (*pcre_callout)(pcre_callout_block *);
130 nigel 75 .
131     .
132     .SH "PCRE API OVERVIEW"
133 nigel 63 .rs
134     .sp
135 nigel 77 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 nigel 75 .\" HREF
139     \fBpcreposix\fP
140     .\"
141 nigel 77 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 nigel 75 .P
148 nigel 77 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 nigel 75 Applications can use these to include support for different releases of PCRE.
154     .P
155 nigel 77 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 nigel 75 .\" HREF
161     \fBpcresample\fP
162     .\"
163     documentation describes how to run it.
164     .P
165 nigel 77 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 nigel 75 In addition to the main compiling and matching functions, there are convenience
177 nigel 77 functions for extracting captured substrings from a subject string that is
178     matched by \fBpcre_exec()\fP. They are:
179 nigel 75 .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 nigel 63 provided, to free the memory used for extracted strings.
189 nigel 75 .P
190     The function \fBpcre_maketables()\fP is used to build a set of character tables
191 nigel 77 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 nigel 75 .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 nigel 63 some of the available information, but is retained for backwards compatibility.
199 nigel 75 The function \fBpcre_version()\fP returns a pointer to a string containing the
200 nigel 63 version of PCRE and its date of release.
201 nigel 75 .P
202 nigel 77 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 nigel 75 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 nigel 63 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 nigel 75 .P
212     The global variables \fBpcre_stack_malloc\fP and \fBpcre_stack_free\fP are also
213 nigel 73 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 nigel 77 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 nigel 75 .P
223     The global variable \fBpcre_callout\fP initially contains NULL. It can be set
224 nigel 63 by the caller to a "callout" function, which PCRE will then call at specified
225 nigel 75 points during a matching operation. Details are given in the
226     .\" HREF
227     \fBpcrecallout\fP
228     .\"
229 nigel 63 documentation.
230 nigel 75 .
231     .
232 nigel 63 .SH MULTITHREADING
233     .rs
234     .sp
235     The PCRE functions can be used in multi-threading applications, with the
236 nigel 75 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 nigel 63 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 nigel 75 .
243     .
244     .SH "SAVING PRECOMPILED PATTERNS FOR LATER USE"
245 nigel 63 .rs
246     .sp
247 nigel 75 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     .
256     .SH "CHECKING BUILD-TIME OPTIONS"
257     .rs
258     .sp
259     .B int pcre_config(int \fIwhat\fP, void *\fIwhere\fP);
260 nigel 63 .PP
261 nigel 75 The function \fBpcre_config()\fP makes it possible for a PCRE client to
262 nigel 63 discover which optional features have been compiled into the PCRE library. The
263     .\" HREF
264 nigel 75 \fBpcrebuild\fP
265 nigel 63 .\"
266     documentation has more details about these optional features.
267 nigel 75 .P
268     The first argument for \fBpcre_config()\fP is an integer, specifying which
269 nigel 63 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 nigel 75 .sp
272 nigel 63 PCRE_CONFIG_UTF8
273 nigel 75 .sp
274 nigel 63 The output is an integer that is set to one if UTF-8 support is available;
275     otherwise it is set to zero.
276 nigel 75 .sp
277     PCRE_CONFIG_UNICODE_PROPERTIES
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
282 nigel 63 PCRE_CONFIG_NEWLINE
283 nigel 75 .sp
284 nigel 63 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 nigel 75 .sp
288 nigel 63 PCRE_CONFIG_LINK_SIZE
289 nigel 75 .sp
290 nigel 63 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 nigel 75 .sp
296 nigel 63 PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
297 nigel 75 .sp
298 nigel 63 The output is an integer that contains the threshold above which the POSIX
299 nigel 75 interface uses \fBmalloc()\fP for output vectors. Further details are given in
300     the
301     .\" HREF
302     \fBpcreposix\fP
303     .\"
304     documentation.
305     .sp
306 nigel 63 PCRE_CONFIG_MATCH_LIMIT
307 nigel 75 .sp
308 nigel 63 The output is an integer that gives the default limit for the number of
309 nigel 75 internal matching function calls in a \fBpcre_exec()\fP execution. Further
310     details are given with \fBpcre_exec()\fP below.
311     .sp
312 nigel 87 PCRE_CONFIG_MATCH_LIMIT_RECURSION
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
318 nigel 73 PCRE_CONFIG_STACKRECURSE
319 nigel 75 .sp
320 nigel 77 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 nigel 75 .
328     .
329     .SH "COMPILING A PATTERN"
330 nigel 63 .rs
331     .sp
332 nigel 75 .B pcre *pcre_compile(const char *\fIpattern\fP, int \fIoptions\fP,
333 nigel 63 .ti +5n
334 nigel 75 .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
335 nigel 63 .ti +5n
336 nigel 75 .B const unsigned char *\fItableptr\fP);
337 nigel 77 .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 nigel 75 .P
346 nigel 77 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 nigel 75 .P
351 nigel 77 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 nigel 63 Although the compiled code of a PCRE regex is relocatable, that is, it does not
359 nigel 75 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 nigel 87 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 nigel 75 \fIerroffset\fP, which must not be NULL. If it is, an immediate error is given.
383     .P
384 nigel 77 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 nigel 75 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 nigel 63 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 nigel 75 .sp
408     The following names for option bits are defined in the \fBpcre.h\fP header
409     file:
410     .sp
411 nigel 63 PCRE_ANCHORED
412 nigel 75 .sp
413 nigel 63 If this bit is set, the pattern is forced to be "anchored", that is, it is
414 nigel 75 constrained to match only at the first matching point in the string that is
415 nigel 63 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 nigel 75 .sp
419     PCRE_AUTO_CALLOUT
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
429 nigel 63 PCRE_CASELESS
430 nigel 75 .sp
431 nigel 63 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 nigel 77 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 nigel 75 .sp
441 nigel 63 PCRE_DOLLAR_ENDONLY
442 nigel 75 .sp
443 nigel 63 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 nigel 75 .sp
450 nigel 63 PCRE_DOTALL
451 nigel 75 .sp
452 nigel 63 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 nigel 75 .sp
458 nigel 63 PCRE_EXTENDED
459 nigel 75 .sp
460 nigel 63 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 nigel 75 .P
467 nigel 63 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 nigel 75 .sp
472 nigel 63 PCRE_EXTRA
473 nigel 75 .sp
474 nigel 63 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 nigel 75 .sp
483 nigel 77 PCRE_FIRSTLINE
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
489 nigel 63 PCRE_MULTILINE
490 nigel 75 .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 nigel 63 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 nigel 75 .P
498 nigel 63 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 nigel 75 setting. If there are no "\en" characters in a subject string, or no
503 nigel 63 occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no effect.
504 nigel 75 .sp
505 nigel 63 PCRE_NO_AUTO_CAPTURE
506 nigel 75 .sp
507 nigel 63 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 nigel 75 .sp
513 nigel 63 PCRE_UNGREEDY
514 nigel 75 .sp
515 nigel 63 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 nigel 75 .sp
519 nigel 63 PCRE_UTF8
520 nigel 75 .sp
521 nigel 63 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 nigel 75 available only when PCRE is built to include UTF-8 support. If not, the use
524 nigel 63 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 nigel 75 \fBpcre\fP
533 nigel 63 .\"
534     page.
535 nigel 75 .sp
536 nigel 71 PCRE_NO_UTF8_CHECK
537 nigel 75 .sp
538 nigel 71 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 nigel 75 \fBpcre_compile()\fP returns an error. If you already know that your pattern is
541 nigel 71 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 nigel 77 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 nigel 75 .
548     .
549 nigel 77 .SH "COMPILATION ERROR CODES"
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     .
606 nigel 75 .SH "STUDYING A PATTERN"
607 nigel 63 .rs
608     .sp
609 nigel 77 .B pcre_extra *pcre_study(const pcre *\fIcode\fP, int \fIoptions\fP
610 nigel 63 .ti +5n
611 nigel 75 .B const char **\fIerrptr\fP);
612 nigel 63 .PP
613 nigel 75 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 nigel 63 fields that can be set by the caller before the block is passed; these are
624 nigel 75 described
625     .\" HTML <a href="#extradata">
626     .\" </a>
627     below
628     .\"
629     in the section on matching a pattern.
630     .P
631 nigel 77 If studying the pattern does not produce any additional information
632 nigel 75 \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 nigel 63 studying succeeds (even if no data is returned), the variable it points to is
641 nigel 87 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 nigel 75 .P
646     This is a typical call to \fBpcre_study\fP():
647     .sp
648 nigel 63 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 nigel 75 .sp
654 nigel 63 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 nigel 75 bytes is created.
657     .
658     .
659 nigel 63 .\" HTML <a name="localesupport"></a>
660 nigel 75 .SH "LOCALE SUPPORT"
661 nigel 63 .rs
662     .sp
663 nigel 77 PCRE handles caseless matching, and determines whether characters are letters
664 nigel 75 digits, or whatever, by reference to a set of tables, indexed by character
665 nigel 77 value. When running in UTF-8 mode, this applies only to characters with codes
666 nigel 75 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 nigel 87 support. The use of locales with Unicode is discouraged.
669 nigel 75 .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 nigel 63 tables = pcre_maketables();
686     re = pcre_compile(..., tables);
687 nigel 75 .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     .
706     .SH "INFORMATION ABOUT A PATTERN"
707 nigel 63 .rs
708     .sp
709 nigel 75 .B int pcre_fullinfo(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
710 nigel 63 .ti +5n
711 nigel 75 .B int \fIwhat\fP, void *\fIwhere\fP);
712 nigel 63 .PP
713 nigel 75 The \fBpcre_fullinfo()\fP function returns information about a compiled
714     pattern. It replaces the obsolete \fBpcre_info()\fP function, which is
715 nigel 63 nevertheless retained for backwards compability (and is documented below).
716 nigel 75 .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 nigel 63 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 nigel 75 .sp
724     PCRE_ERROR_NULL the argument \fIcode\fP was NULL
725     the argument \fIwhere\fP was NULL
726 nigel 63 PCRE_ERROR_BADMAGIC the "magic number" was not found
727 nigel 75 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 nigel 63 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 nigel 75 .sp
741     The possible values for the third argument are defined in \fBpcre.h\fP, and are
742 nigel 63 as follows:
743 nigel 75 .sp
744 nigel 63 PCRE_INFO_BACKREFMAX
745 nigel 75 .sp
746 nigel 63 Return the number of the highest back reference in the pattern. The fourth
747 nigel 75 argument should point to an \fBint\fP variable. Zero is returned if there are
748 nigel 63 no back references.
749 nigel 75 .sp
750 nigel 63 PCRE_INFO_CAPTURECOUNT
751 nigel 75 .sp
752 nigel 63 Return the number of capturing subpatterns in the pattern. The fourth argument
753 nigel 75 should point to an \fBint\fP variable.
754     .sp
755 nigel 77 PCRE_INFO_DEFAULT_TABLES
756 nigel 75 .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
763 nigel 63 PCRE_INFO_FIRSTBYTE
764 nigel 75 .sp
765 nigel 63 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 nigel 75 .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 nigel 63 (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
774     starts with "^", or
775 nigel 75 .sp
776 nigel 63 (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 nigel 75 .sp
779 nigel 63 -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 nigel 75 .sp
783 nigel 63 PCRE_INFO_FIRSTTABLE
784 nigel 75 .sp
785 nigel 63 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 nigel 75 fourth argument should point to an \fBunsigned char *\fP variable.
789     .sp
790 nigel 63 PCRE_INFO_LASTLITERAL
791 nigel 75 .sp
792 nigel 65 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 nigel 75 argument should point to an \fBint\fP variable. If there is no such byte, -1 is
795 nigel 65 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 nigel 75 /^a\ed+z\ed+/ the returned value is "z", but for /^a\edz\ed/ the returned value
798 nigel 65 is -1.
799 nigel 75 .sp
800 nigel 63 PCRE_INFO_NAMECOUNT
801     PCRE_INFO_NAMEENTRYSIZE
802     PCRE_INFO_NAMETABLE
803 nigel 75 .sp
804 nigel 63 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 nigel 75 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 nigel 63 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 nigel 75 entry; both of these return an \fBint\fP value. The entry size depends on the
816 nigel 63 length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
817 nigel 75 entry of the table (a pointer to \fBchar\fP). The first two bytes of each entry
818 nigel 63 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 nigel 75 .sp
823     .\" JOIN
824     (?P<date> (?P<year>(\ed\ed)?\ed\ed) -
825     (?P<month>\ed\ed) - (?P<day>\ed\ed) )
826     .sp
827 nigel 63 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 nigel 75 bytes shows in hexadecimal, and undefined bytes shown as ??:
830     .sp
831 nigel 63 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 nigel 75 .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
840 nigel 63 PCRE_INFO_OPTIONS
841 nigel 75 .sp
842 nigel 63 Return a copy of the options with which the pattern was compiled. The fourth
843 nigel 75 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 nigel 63 top-level option settings within the pattern itself.
846 nigel 75 .P
847 nigel 63 A pattern is automatically anchored by PCRE if all of its top-level
848     alternatives begin with one of the following:
849 nigel 75 .sp
850 nigel 63 ^ unless PCRE_MULTILINE is set
851 nigel 75 \eA always
852     \eG always
853     .\" JOIN
854 nigel 63 .* if PCRE_DOTALL is set and there are no back
855     references to the subpattern in which .* appears
856 nigel 75 .sp
857 nigel 63 For such patterns, the PCRE_ANCHORED bit is set in the options returned by
858 nigel 75 \fBpcre_fullinfo()\fP.
859     .sp
860 nigel 63 PCRE_INFO_SIZE
861 nigel 75 .sp
862 nigel 63 Return the size of the compiled pattern, that is, the value that was passed as
863 nigel 75 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 nigel 63 variable.
866 nigel 75 .sp
867 nigel 63 PCRE_INFO_STUDYSIZE
868 nigel 75 .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     .
876     .SH "OBSOLETE INFO FUNCTION"
877 nigel 63 .rs
878     .sp
879 nigel 75 .B int pcre_info(const pcre *\fIcode\fP, int *\fIoptptr\fP, int
880     .B *\fIfirstcharptr\fP);
881 nigel 63 .PP
882 nigel 75 The \fBpcre_info()\fP function is now obsolete because its interface is too
883 nigel 63 restrictive to return all the available data about a compiled pattern. New
884 nigel 75 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 nigel 63 following negative numbers:
887 nigel 75 .sp
888     PCRE_ERROR_NULL the argument \fIcode\fP was NULL
889 nigel 63 PCRE_ERROR_BADMAGIC the "magic number" was not found
890 nigel 75 .sp
891     If the \fIoptptr\fP argument is not NULL, a copy of the options with which the
892 nigel 63 pattern was compiled is placed in the integer it points to (see
893     PCRE_INFO_OPTIONS above).
894 nigel 75 .P
895     If the pattern is not anchored and the \fIfirstcharptr\fP argument is not NULL,
896 nigel 63 it is used to pass back information about the first character of any matched
897     string (see PCRE_INFO_FIRSTBYTE above).
898 nigel 75 .
899     .
900 nigel 77 .SH "REFERENCE COUNTS"
901 nigel 63 .rs
902     .sp
903 nigel 77 .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     .
923     .SH "MATCHING A PATTERN: THE TRADITIONAL FUNCTION"
924     .rs
925     .sp
926 nigel 75 .B int pcre_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
927 nigel 63 .ti +5n
928 nigel 75 .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
929 nigel 63 .ti +5n
930 nigel 75 .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 nigel 63 pattern has been studied, the result of the study should be passed in the
935 nigel 77 \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 nigel 75 .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 nigel 63 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 nigel 75 ovector, /* vector of integers for substring information */
966 nigel 77 30); /* number of elements (NOT size in bytes) */
967 nigel 75 .
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 nigel 63 doesn't return NULL), but you can also create one for yourself, and pass
975 nigel 87 additional information in it. The \fBpcre_extra\fP block contains the following
976     fields (not necessarily in this order):
977 nigel 75 .sp
978     unsigned long int \fIflags\fP;
979     void *\fIstudy_data\fP;
980     unsigned long int \fImatch_limit\fP;
981 nigel 87 unsigned long int \fImatch_limit_recursion\fP;
982 nigel 75 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 nigel 63 are set. The flag bits are:
987 nigel 75 .sp
988 nigel 63 PCRE_EXTRA_STUDY_DATA
989     PCRE_EXTRA_MATCH_LIMIT
990 nigel 87 PCRE_EXTRA_MATCH_LIMIT_RECURSION
991 nigel 63 PCRE_EXTRA_CALLOUT_DATA
992 nigel 75 PCRE_EXTRA_TABLES
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 nigel 63 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 nigel 75 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 nigel 87 (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 nigel 75 .P
1011 nigel 87 The default value for the limit can be set when PCRE is built; the default
1012 nigel 63 default is 10 million, which handles all but the most extreme cases. You can
1013 nigel 87 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
1016     PCRE_ERROR_MATCHLIMIT.
1017 nigel 75 .P
1018 nigel 87 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 nigel 75 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,
1061     PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK and PCRE_PARTIAL.
1062     .sp
1063     PCRE_ANCHORED
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
1070 nigel 63 PCRE_NOTBOL
1071 nigel 75 .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
1078 nigel 63 PCRE_NOTEOL
1079 nigel 75 .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
1086 nigel 63 PCRE_NOTEMPTY
1087 nigel 75 .sp
1088 nigel 63 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 nigel 75 .sp
1092 nigel 63 a?b?
1093 nigel 75 .sp
1094 nigel 63 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 nigel 75 .P
1098 nigel 63 Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
1099 nigel 75 of a pattern match of the empty string within its \fBsplit()\fP function, and
1100 nigel 63 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 nigel 75 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
1106     PCRE_NO_UTF8_CHECK
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
1125     PCRE_PARTIAL
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 nigel 63 A non-zero starting offset is useful when searching for another match in the
1150 nigel 75 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 nigel 63 setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
1153     lookbehind. For example, consider the pattern
1154 nigel 75 .sp
1155     \eBiss\eB
1156     .sp
1157     which finds occurrences of "iss" in the middle of words. (\eB matches only if
1158 nigel 63 the current position in the subject is not a word boundary.) When applied to
1159 nigel 75 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 nigel 63 start of the subject, which is deemed to be a word boundary. However, if
1163 nigel 75 \fBpcre_exec()\fP is passed the entire string again, but with \fIstartoffset\fP
1164 nigel 63 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 nigel 75 .P
1167 nigel 63 If a non-zero starting offset is passed when the pattern is anchored, one
1168 nigel 75 attempt to match at the given offset is made. This can only succeed if the
1169 nigel 63 pattern does not require the match to be at the start of the subject.
1170 nigel 75 .
1171     .SS "How \fBpcre_exec()\fP returns captured substrings"
1172     .rs
1173     .sp
1174 nigel 63 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 nigel 75 .P
1181 nigel 63 Captured substrings are returned to the caller via a vector of integer offsets
1182 nigel 75 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 nigel 63 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 nigel 75 first pair, \fIovector[0]\fP and \fIovector[1]\fP, identify the portion of the
1199 nigel 63 subject string matched by the entire pattern. The next pair is used for the
1200 nigel 75 first capturing subpattern, and so on. The value returned by \fBpcre_exec()\fP
1201 nigel 63 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 nigel 75 .P
1205 nigel 63 Some convenience functions are provided for extracting the captured substrings
1206     as separate strings. These are described in the following section.
1207 nigel 75 .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 nigel 63 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 nigel 75 .P
1214 nigel 63 If a capturing subpattern is matched repeatedly, it is the last portion of the
1215 nigel 75 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 nigel 63 subpatterns there are in a compiled pattern. The smallest size for
1228 nigel 75 \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 nigel 77 .\" HTML <a name="errorlist"></a>
1232 nigel 75 .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 nigel 63 defined in the header file:
1237 nigel 75 .sp
1238 nigel 63 PCRE_ERROR_NOMATCH (-1)
1239 nigel 75 .sp
1240 nigel 63 The subject string did not match the pattern.
1241 nigel 75 .sp
1242 nigel 63 PCRE_ERROR_NULL (-2)
1243 nigel 75 .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
1247 nigel 63 PCRE_ERROR_BADOPTION (-3)
1248 nigel 75 .sp
1249     An unrecognized bit was set in the \fIoptions\fP argument.
1250     .sp
1251 nigel 63 PCRE_ERROR_BADMAGIC (-4)
1252 nigel 75 .sp
1253 nigel 63 PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
1254 nigel 75 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
1259 nigel 63 PCRE_ERROR_UNKNOWN_NODE (-5)
1260 nigel 75 .sp
1261 nigel 63 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 nigel 75 .sp
1265 nigel 63 PCRE_ERROR_NOMEMORY (-6)
1266 nigel 75 .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 nigel 63 gets a block of memory at the start of matching to use for this purpose. If the
1270 nigel 75 call via \fBpcre_malloc()\fP fails, this error is given. The memory is
1271     automatically freed at the end of matching.
1272     .sp
1273 nigel 63 PCRE_ERROR_NOSUBSTRING (-7)
1274 nigel 75 .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
1279 nigel 63 PCRE_ERROR_MATCHLIMIT (-8)
1280 nigel 75 .sp
1281 nigel 87 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
1285     PCRE_ERROR_RECURSIONLIMIT (-21)
1286     .sp
1287     The internal recursion limit, as specified by the \fImatch_limit_recursion\fP
1288 nigel 75 field in a \fBpcre_extra\fP structure (or defaulted) was reached. See the
1289 nigel 63 description above.
1290 nigel 75 .sp
1291 nigel 63 PCRE_ERROR_CALLOUT (-9)
1292 nigel 75 .sp
1293     This error is never generated by \fBpcre_exec()\fP itself. It is provided for
1294 nigel 63 use by callout functions that want to yield a distinctive error code. See the
1295 nigel 75 .\" HREF
1296     \fBpcrecallout\fP
1297     .\"
1298     documentation for details.
1299     .sp
1300 nigel 73 PCRE_ERROR_BADUTF8 (-10)
1301 nigel 75 .sp
1302 nigel 71 A string that contains an invalid UTF-8 byte sequence was passed as a subject.
1303 nigel 75 .sp
1304 nigel 73 PCRE_ERROR_BADUTF8_OFFSET (-11)
1305 nigel 75 .sp
1306 nigel 73 The UTF-8 byte sequence that was passed as a subject was valid, but the value
1307 nigel 75 of \fIstartoffset\fP did not point to the beginning of a UTF-8 character.
1308     .sp
1309 nigel 77 PCRE_ERROR_PARTIAL (-12)
1310 nigel 75 .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
1317 nigel 77 PCRE_ERROR_BADPARTIAL (-13)
1318 nigel 75 .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
1326 nigel 77 PCRE_ERROR_INTERNAL (-14)
1327 nigel 75 .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
1331 nigel 77 PCRE_ERROR_BADCOUNT (-15)
1332 nigel 75 .sp
1333     This error is given if the value of the \fIovecsize\fP argument is negative.
1334     .
1335     .
1336     .SH "EXTRACTING CAPTURED SUBSTRINGS BY NUMBER"
1337 nigel 63 .rs
1338     .sp
1339 nigel 75 .B int pcre_copy_substring(const char *\fIsubject\fP, int *\fIovector\fP,
1340 nigel 63 .ti +5n
1341 nigel 75 .B int \fIstringcount\fP, int \fIstringnumber\fP, char *\fIbuffer\fP,
1342 nigel 63 .ti +5n
1343 nigel 75 .B int \fIbuffersize\fP);
1344 nigel 63 .PP
1345     .br
1346 nigel 75 .B int pcre_get_substring(const char *\fIsubject\fP, int *\fIovector\fP,
1347 nigel 63 .ti +5n
1348 nigel 75 .B int \fIstringcount\fP, int \fIstringnumber\fP,
1349 nigel 63 .ti +5n
1350 nigel 75 .B const char **\fIstringptr\fP);
1351 nigel 63 .PP
1352     .br
1353 nigel 75 .B int pcre_get_substring_list(const char *\fIsubject\fP,
1354 nigel 63 .ti +5n
1355 nigel 75 .B int *\fIovector\fP, int \fIstringcount\fP, "const char ***\fIlistptr\fP);"
1356 nigel 63 .PP
1357     Captured substrings can be accessed directly by using the offsets returned by
1358 nigel 75 \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 nigel 63 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 nigel 75 .P
1367 nigel 63 The first three arguments are the same for all three of these functions:
1368 nigel 75 \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 nigel 63 captured by the match, including the substring that matched the entire regular
1372 nigel 75 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 nigel 63 including the terminating zero, or one of
1386 nigel 75 .sp
1387 nigel 63 PCRE_ERROR_NOMEMORY (-6)
1388 nigel 75 .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
1392 nigel 63 PCRE_ERROR_NOSUBSTRING (-7)
1393 nigel 75 .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 nigel 63 and builds a list of pointers to them. All this is done in a single block of
1398 nigel 75 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 nigel 63 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 nigel 75 .sp
1403 nigel 63 PCRE_ERROR_NOMEMORY (-6)
1404 nigel 75 .sp
1405 nigel 63 if the attempt to get the memory block failed.
1406 nigel 75 .P
1407 nigel 63 When any of these functions encounter a substring that is unset, which can
1408 nigel 75 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 nigel 63 string. This can be distinguished from a genuine zero-length substring by
1411 nigel 75 inspecting the appropriate offset in \fIovector\fP, which is negative for unset
1412 nigel 63 substrings.
1413 nigel 75 .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 nigel 63 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 nigel 75 \fBpcre_free\fP directly; it is for these cases that the functions are
1422 nigel 63 provided.
1423 nigel 75 .
1424     .
1425     .SH "EXTRACTING CAPTURED SUBSTRINGS BY NAME"
1426 nigel 63 .rs
1427     .sp
1428 nigel 75 .B int pcre_get_stringnumber(const pcre *\fIcode\fP,
1429 nigel 63 .ti +5n
1430 nigel 75 .B const char *\fIname\fP);
1431 nigel 63 .PP
1432     .br
1433 nigel 75 .B int pcre_copy_named_substring(const pcre *\fIcode\fP,
1434 nigel 63 .ti +5n
1435 nigel 75 .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 nigel 63 .PP
1441     .br
1442 nigel 75 .B int pcre_get_named_substring(const pcre *\fIcode\fP,
1443 nigel 63 .ti +5n
1444 nigel 75 .B const char *\fIsubject\fP, int *\fIovector\fP,
1445 nigel 63 .ti +5n
1446 nigel 75 .B int \fIstringcount\fP, const char *\fIstringname\fP,
1447 nigel 63 .ti +5n
1448 nigel 75 .B const char **\fIstringptr\fP);
1449 nigel 63 .PP
1450 nigel 75 To extract a substring by name, you first have to find associated number.
1451     For example, for this pattern
1452     .sp
1453 nigel 79 (a+)b(?P<xxx>\ed+)...
1454 nigel 75 .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 nigel 63 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 nigel 75 .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 nigel 63 appropriate.
1478 nigel 77 .
1479     .
1480     .SH "FINDING ALL POSSIBLE MATCHES"
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 nigel 75 .P
1495 nigel 77 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>
1503     .SH "MATCHING A PATTERN: THE ALTERNATIVE FUNCTION"
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 nigel 87 Here is an example of a simple call to \fBpcre_dfa_exec()\fP:
1537 nigel 77 .sp
1538     int rc;
1539     int ovector[10];
1540     int wspace[20];
1541 nigel 87 rc = pcre_dfa_exec(
1542 nigel 77 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,
1558     PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL,
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
1562     PCRE_PARTIAL
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
1572     PCRE_DFA_SHORTEST
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
1579     PCRE_DFA_RESTART
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
1639     PCRE_ERROR_DFA_UITEM (-16)
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
1644     PCRE_ERROR_DFA_UCOND (-17)
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
1649     PCRE_ERROR_DFA_UMLIMIT (-18)
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
1655     PCRE_ERROR_DFA_WSSIZE (-19)
1656     .sp
1657     This return is given if \fBpcre_dfa_exec()\fP runs out of space in the
1658     \fIworkspace\fP vector.
1659     .sp
1660     PCRE_ERROR_DFA_RECURSE (-20)
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 nigel 63 .in 0
1668 nigel 87 Last updated: 18 January 2006
1669 nigel 63 .br
1670 nigel 87 Copyright (c) 1997-2006 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12