ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1433 - (show annotations)
Fri Jan 3 15:15:00 2014 UTC (7 years, 4 months ago) by ph10
File size: 128283 byte(s)
Reword pcretest messages and clarify "first char" meaning.
1 .TH PCREAPI 3 "03 January 2014" "PCRE 8.35"
3 PCRE - Perl-compatible regular expressions
4 .sp
5 .B #include <pcre.h>
6 .
7 .
9 .rs
10 .sp
11 .nf
12 .B pcre *pcre_compile(const char *\fIpattern\fP, int \fIoptions\fP,
13 .B " const char **\fIerrptr\fP, int *\fIerroffset\fP,"
14 .B " const unsigned char *\fItableptr\fP);"
15 .sp
16 .B pcre *pcre_compile2(const char *\fIpattern\fP, int \fIoptions\fP,
17 .B " int *\fIerrorcodeptr\fP,"
18 .B " const char **\fIerrptr\fP, int *\fIerroffset\fP,"
19 .B " const unsigned char *\fItableptr\fP);"
20 .sp
21 .B pcre_extra *pcre_study(const pcre *\fIcode\fP, int \fIoptions\fP,
22 .B " const char **\fIerrptr\fP);"
23 .sp
24 .B void pcre_free_study(pcre_extra *\fIextra\fP);
25 .sp
26 .B int pcre_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
27 .B " const char *\fIsubject\fP, int \fIlength\fP, int \fIstartoffset\fP,"
28 .B " int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP);"
29 .sp
30 .B int pcre_dfa_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
31 .B " const char *\fIsubject\fP, int \fIlength\fP, int \fIstartoffset\fP,"
32 .B " int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP,"
33 .B " int *\fIworkspace\fP, int \fIwscount\fP);"
34 .fi
35 .
36 .
38 .rs
39 .sp
40 .nf
41 .B int pcre_copy_named_substring(const pcre *\fIcode\fP,
42 .B " const char *\fIsubject\fP, int *\fIovector\fP,"
43 .B " int \fIstringcount\fP, const char *\fIstringname\fP,"
44 .B " char *\fIbuffer\fP, int \fIbuffersize\fP);"
45 .sp
46 .B int pcre_copy_substring(const char *\fIsubject\fP, int *\fIovector\fP,
47 .B " int \fIstringcount\fP, int \fIstringnumber\fP, char *\fIbuffer\fP,"
48 .B " int \fIbuffersize\fP);"
49 .sp
50 .B int pcre_get_named_substring(const pcre *\fIcode\fP,
51 .B " const char *\fIsubject\fP, int *\fIovector\fP,"
52 .B " int \fIstringcount\fP, const char *\fIstringname\fP,"
53 .B " const char **\fIstringptr\fP);"
54 .sp
55 .B int pcre_get_stringnumber(const pcre *\fIcode\fP,
56 .B " const char *\fIname\fP);"
57 .sp
58 .B int pcre_get_stringtable_entries(const pcre *\fIcode\fP,
59 .B " const char *\fIname\fP, char **\fIfirst\fP, char **\fIlast\fP);"
60 .sp
61 .B int pcre_get_substring(const char *\fIsubject\fP, int *\fIovector\fP,
62 .B " int \fIstringcount\fP, int \fIstringnumber\fP,"
63 .B " const char **\fIstringptr\fP);"
64 .sp
65 .B int pcre_get_substring_list(const char *\fIsubject\fP,
66 .B " int *\fIovector\fP, int \fIstringcount\fP, const char ***\fIlistptr\fP);"
67 .sp
68 .B void pcre_free_substring(const char *\fIstringptr\fP);
69 .sp
70 .B void pcre_free_substring_list(const char **\fIstringptr\fP);
71 .fi
72 .
73 .
75 .rs
76 .sp
77 .nf
78 .B int pcre_jit_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
79 .B " const char *\fIsubject\fP, int \fIlength\fP, int \fIstartoffset\fP,"
80 .B " int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP,"
81 .B " pcre_jit_stack *\fIjstack\fP);"
82 .sp
83 .B pcre_jit_stack *pcre_jit_stack_alloc(int \fIstartsize\fP, int \fImaxsize\fP);
84 .sp
85 .B void pcre_jit_stack_free(pcre_jit_stack *\fIstack\fP);
86 .sp
87 .B void pcre_assign_jit_stack(pcre_extra *\fIextra\fP,
88 .B " pcre_jit_callback \fIcallback\fP, void *\fIdata\fP);"
89 .sp
90 .B const unsigned char *pcre_maketables(void);
91 .sp
92 .B int pcre_fullinfo(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
93 .B " int \fIwhat\fP, void *\fIwhere\fP);"
94 .sp
95 .B int pcre_refcount(pcre *\fIcode\fP, int \fIadjust\fP);
96 .sp
97 .B int pcre_config(int \fIwhat\fP, void *\fIwhere\fP);
98 .sp
99 .B const char *pcre_version(void);
100 .sp
101 .B int pcre_pattern_to_host_byte_order(pcre *\fIcode\fP,
102 .B " pcre_extra *\fIextra\fP, const unsigned char *\fItables\fP);"
103 .fi
104 .
105 .
107 .rs
108 .sp
109 .nf
110 .B void *(*pcre_malloc)(size_t);
111 .sp
112 .B void (*pcre_free)(void *);
113 .sp
114 .B void *(*pcre_stack_malloc)(size_t);
115 .sp
116 .B void (*pcre_stack_free)(void *);
117 .sp
118 .B int (*pcre_callout)(pcre_callout_block *);
119 .fi
120 .
121 .
123 .rs
124 .sp
125 As well as support for 8-bit character strings, PCRE also supports 16-bit
126 strings (from release 8.30) and 32-bit strings (from release 8.32), by means of
127 two additional libraries. They can be built as well as, or instead of, the
128 8-bit library. To avoid too much complication, this document describes the
129 8-bit versions of the functions, with only occasional references to the 16-bit
130 and 32-bit libraries.
131 .P
132 The 16-bit and 32-bit functions operate in the same way as their 8-bit
133 counterparts; they just use different data types for their arguments and
134 results, and their names start with \fBpcre16_\fP or \fBpcre32_\fP instead of
135 \fBpcre_\fP. For every option that has UTF8 in its name (for example,
136 PCRE_UTF8), there are corresponding 16-bit and 32-bit names with UTF8 replaced
137 by UTF16 or UTF32, respectively. This facility is in fact just cosmetic; the
138 16-bit and 32-bit option names define the same bit values.
139 .P
140 References to bytes and UTF-8 in this document should be read as references to
141 16-bit data units and UTF-16 when using the 16-bit library, or 32-bit data
142 units and UTF-32 when using the 32-bit library, unless specified otherwise.
143 More details of the specific differences for the 16-bit and 32-bit libraries
144 are given in the
145 .\" HREF
146 \fBpcre16\fP
147 .\"
148 and
149 .\" HREF
150 \fBpcre32\fP
151 .\"
152 pages.
153 .
154 .
156 .rs
157 .sp
158 PCRE has its own native API, which is described in this document. There are
159 also some wrapper functions (for the 8-bit library only) that correspond to the
160 POSIX regular expression API, but they do not give access to all the
161 functionality. They are described in the
162 .\" HREF
163 \fBpcreposix\fP
164 .\"
165 documentation. Both of these APIs define a set of C function calls. A C++
166 wrapper (again for the 8-bit library only) is also distributed with PCRE. It is
167 documented in the
168 .\" HREF
169 \fBpcrecpp\fP
170 .\"
171 page.
172 .P
173 The native API C function prototypes are defined in the header file
174 \fBpcre.h\fP, and on Unix-like systems the (8-bit) library itself is called
175 \fBlibpcre\fP. It can normally be accessed by adding \fB-lpcre\fP to the
176 command for linking an application that uses PCRE. The header file defines the
177 macros PCRE_MAJOR and PCRE_MINOR to contain the major and minor release numbers
178 for the library. Applications can use these to include support for different
179 releases of PCRE.
180 .P
181 In a Windows environment, if you want to statically link an application program
182 against a non-dll \fBpcre.a\fP file, you must define PCRE_STATIC before
183 including \fBpcre.h\fP or \fBpcrecpp.h\fP, because otherwise the
184 \fBpcre_malloc()\fP and \fBpcre_free()\fP exported functions will be declared
185 \fB__declspec(dllimport)\fP, with unwanted results.
186 .P
187 The functions \fBpcre_compile()\fP, \fBpcre_compile2()\fP, \fBpcre_study()\fP,
188 and \fBpcre_exec()\fP are used for compiling and matching regular expressions
189 in a Perl-compatible manner. A sample program that demonstrates the simplest
190 way of using them is provided in the file called \fIpcredemo.c\fP in the PCRE
191 source distribution. A listing of this program is given in the
192 .\" HREF
193 \fBpcredemo\fP
194 .\"
195 documentation, and the
196 .\" HREF
197 \fBpcresample\fP
198 .\"
199 documentation describes how to compile and run it.
200 .P
201 Just-in-time compiler support is an optional feature of PCRE that can be built
202 in appropriate hardware environments. It greatly speeds up the matching
203 performance of many patterns. Simple programs can easily request that it be
204 used if available, by setting an option that is ignored when it is not
205 relevant. More complicated programs might need to make use of the functions
206 \fBpcre_jit_stack_alloc()\fP, \fBpcre_jit_stack_free()\fP, and
207 \fBpcre_assign_jit_stack()\fP in order to control the JIT code's memory usage.
208 .P
209 From release 8.32 there is also a direct interface for JIT execution, which
210 gives improved performance. The JIT-specific functions are discussed in the
211 .\" HREF
212 \fBpcrejit\fP
213 .\"
214 documentation.
215 .P
216 A second matching function, \fBpcre_dfa_exec()\fP, which is not
217 Perl-compatible, is also provided. This uses a different algorithm for the
218 matching. The alternative algorithm finds all possible matches (at a given
219 point in the subject), and scans the subject just once (unless there are
220 lookbehind assertions). However, this algorithm does not return captured
221 substrings. A description of the two matching algorithms and their advantages
222 and disadvantages is given in the
223 .\" HREF
224 \fBpcrematching\fP
225 .\"
226 documentation.
227 .P
228 In addition to the main compiling and matching functions, there are convenience
229 functions for extracting captured substrings from a subject string that is
230 matched by \fBpcre_exec()\fP. They are:
231 .sp
232 \fBpcre_copy_substring()\fP
233 \fBpcre_copy_named_substring()\fP
234 \fBpcre_get_substring()\fP
235 \fBpcre_get_named_substring()\fP
236 \fBpcre_get_substring_list()\fP
237 \fBpcre_get_stringnumber()\fP
238 \fBpcre_get_stringtable_entries()\fP
239 .sp
240 \fBpcre_free_substring()\fP and \fBpcre_free_substring_list()\fP are also
241 provided, to free the memory used for extracted strings.
242 .P
243 The function \fBpcre_maketables()\fP is used to build a set of character tables
244 in the current locale for passing to \fBpcre_compile()\fP, \fBpcre_exec()\fP,
245 or \fBpcre_dfa_exec()\fP. This is an optional facility that is provided for
246 specialist use. Most commonly, no special tables are passed, in which case
247 internal tables that are generated when PCRE is built are used.
248 .P
249 The function \fBpcre_fullinfo()\fP is used to find out information about a
250 compiled pattern. The function \fBpcre_version()\fP returns a pointer to a
251 string containing the version of PCRE and its date of release.
252 .P
253 The function \fBpcre_refcount()\fP maintains a reference count in a data block
254 containing a compiled pattern. This is provided for the benefit of
255 object-oriented applications.
256 .P
257 The global variables \fBpcre_malloc\fP and \fBpcre_free\fP initially contain
258 the entry points of the standard \fBmalloc()\fP and \fBfree()\fP functions,
259 respectively. PCRE calls the memory management functions via these variables,
260 so a calling program can replace them if it wishes to intercept the calls. This
261 should be done before calling any PCRE functions.
262 .P
263 The global variables \fBpcre_stack_malloc\fP and \fBpcre_stack_free\fP are also
264 indirections to memory management functions. These special functions are used
265 only when PCRE is compiled to use the heap for remembering data, instead of
266 recursive function calls, when running the \fBpcre_exec()\fP function. See the
267 .\" HREF
268 \fBpcrebuild\fP
269 .\"
270 documentation for details of how to do this. It is a non-standard way of
271 building PCRE, for use in environments that have limited stacks. Because of the
272 greater use of memory management, it runs more slowly. Separate functions are
273 provided so that special-purpose external code can be used for this case. When
274 used, these functions are always called in a stack-like manner (last obtained,
275 first freed), and always for memory blocks of the same size. There is a
276 discussion about PCRE's stack usage in the
277 .\" HREF
278 \fBpcrestack\fP
279 .\"
280 documentation.
281 .P
282 The global variable \fBpcre_callout\fP initially contains NULL. It can be set
283 by the caller to a "callout" function, which PCRE will then call at specified
284 points during a matching operation. Details are given in the
285 .\" HREF
286 \fBpcrecallout\fP
287 .\"
288 documentation.
289 .
290 .
291 .\" HTML <a name="newlines"></a>
293 .rs
294 .sp
295 PCRE supports five different conventions for indicating line breaks in
296 strings: a single CR (carriage return) character, a single LF (linefeed)
297 character, the two-character sequence CRLF, any of the three preceding, or any
298 Unicode newline sequence. The Unicode newline sequences are the three just
299 mentioned, plus the single characters VT (vertical tab, U+000B), FF (form feed,
300 U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and PS
301 (paragraph separator, U+2029).
302 .P
303 Each of the first three conventions is used by at least one operating system as
304 its standard newline sequence. When PCRE is built, a default can be specified.
305 The default default is LF, which is the Unix standard. When PCRE is run, the
306 default can be overridden, either when a pattern is compiled, or when it is
307 matched.
308 .P
309 At compile time, the newline convention can be specified by the \fIoptions\fP
310 argument of \fBpcre_compile()\fP, or it can be specified by special text at the
311 start of the pattern itself; this overrides any other settings. See the
312 .\" HREF
313 \fBpcrepattern\fP
314 .\"
315 page for details of the special character sequences.
316 .P
317 In the PCRE documentation the word "newline" is used to mean "the character or
318 pair of characters that indicate a line break". The choice of newline
319 convention affects the handling of the dot, circumflex, and dollar
320 metacharacters, the handling of #-comments in /x mode, and, when CRLF is a
321 recognized line ending sequence, the match position advancement for a
322 non-anchored pattern. There is more detail about this in the
323 .\" HTML <a href="#execoptions">
324 .\" </a>
325 section on \fBpcre_exec()\fP options
326 .\"
327 below.
328 .P
329 The choice of newline convention does not affect the interpretation of
330 the \en or \er escape sequences, nor does it affect what \eR matches, which is
331 controlled in a similar way, but by separate options.
332 .
333 .
335 .rs
336 .sp
337 The PCRE functions can be used in multi-threading applications, with the
338 proviso that the memory management functions pointed to by \fBpcre_malloc\fP,
339 \fBpcre_free\fP, \fBpcre_stack_malloc\fP, and \fBpcre_stack_free\fP, and the
340 callout function pointed to by \fBpcre_callout\fP, are shared by all threads.
341 .P
342 The compiled form of a regular expression is not altered during matching, so
343 the same compiled pattern can safely be used by several threads at once.
344 .P
345 If the just-in-time optimization feature is being used, it needs separate
346 memory stack areas for each thread. See the
347 .\" HREF
348 \fBpcrejit\fP
349 .\"
350 documentation for more details.
351 .
352 .
354 .rs
355 .sp
356 The compiled form of a regular expression can be saved and re-used at a later
357 time, possibly by a different program, and even on a host other than the one on
358 which it was compiled. Details are given in the
359 .\" HREF
360 \fBpcreprecompile\fP
361 .\"
362 documentation, which includes a description of the
363 \fBpcre_pattern_to_host_byte_order()\fP function. However, compiling a regular
364 expression with one version of PCRE for use with a different version is not
365 guaranteed to work and may cause crashes.
366 .
367 .
369 .rs
370 .sp
371 .B int pcre_config(int \fIwhat\fP, void *\fIwhere\fP);
372 .PP
373 The function \fBpcre_config()\fP makes it possible for a PCRE client to
374 discover which optional features have been compiled into the PCRE library. The
375 .\" HREF
376 \fBpcrebuild\fP
377 .\"
378 documentation has more details about these optional features.
379 .P
380 The first argument for \fBpcre_config()\fP is an integer, specifying which
381 information is required; the second argument is a pointer to a variable into
382 which the information is placed. The returned value is zero on success, or the
383 negative error code PCRE_ERROR_BADOPTION if the value in the first argument is
384 not recognized. The following information is available:
385 .sp
387 .sp
388 The output is an integer that is set to one if UTF-8 support is available;
389 otherwise it is set to zero. This value should normally be given to the 8-bit
390 version of this function, \fBpcre_config()\fP. If it is given to the 16-bit
391 or 32-bit version of this function, the result is PCRE_ERROR_BADOPTION.
392 .sp
394 .sp
395 The output is an integer that is set to one if UTF-16 support is available;
396 otherwise it is set to zero. This value should normally be given to the 16-bit
397 version of this function, \fBpcre16_config()\fP. If it is given to the 8-bit
398 or 32-bit version of this function, the result is PCRE_ERROR_BADOPTION.
399 .sp
401 .sp
402 The output is an integer that is set to one if UTF-32 support is available;
403 otherwise it is set to zero. This value should normally be given to the 32-bit
404 version of this function, \fBpcre32_config()\fP. If it is given to the 8-bit
405 or 16-bit version of this function, the result is PCRE_ERROR_BADOPTION.
406 .sp
408 .sp
409 The output is an integer that is set to one if support for Unicode character
410 properties is available; otherwise it is set to zero.
411 .sp
413 .sp
414 The output is an integer that is set to one if support for just-in-time
415 compiling is available; otherwise it is set to zero.
416 .sp
418 .sp
419 The output is a pointer to a zero-terminated "const char *" string. If JIT
420 support is available, the string contains the name of the architecture for
421 which the JIT compiler is configured, for example "x86 32bit (little endian +
422 unaligned)". If JIT support is not available, the result is NULL.
423 .sp
425 .sp
426 The output is an integer whose value specifies the default character sequence
427 that is recognized as meaning "newline". The values that are supported in
428 ASCII/Unicode environments are: 10 for LF, 13 for CR, 3338 for CRLF, -2 for
429 ANYCRLF, and -1 for ANY. In EBCDIC environments, CR, ANYCRLF, and ANY yield the
430 same values. However, the value for LF is normally 21, though some EBCDIC
431 environments use 37. The corresponding values for CRLF are 3349 and 3365. The
432 default should normally correspond to the standard sequence for your operating
433 system.
434 .sp
436 .sp
437 The output is an integer whose value indicates what character sequences the \eR
438 escape sequence matches by default. A value of 0 means that \eR matches any
439 Unicode line ending sequence; a value of 1 means that \eR matches only CR, LF,
440 or CRLF. The default can be overridden when a pattern is compiled or matched.
441 .sp
443 .sp
444 The output is an integer that contains the number of bytes used for internal
445 linkage in compiled regular expressions. For the 8-bit library, the value can
446 be 2, 3, or 4. For the 16-bit library, the value is either 2 or 4 and is still
447 a number of bytes. For the 32-bit library, the value is either 2 or 4 and is
448 still a number of bytes. The default value of 2 is sufficient for all but the
449 most massive patterns, since it allows the compiled pattern to be up to 64K in
450 size. Larger values allow larger regular expressions to be compiled, at the
451 expense of slower matching.
452 .sp
454 .sp
455 The output is an integer that contains the threshold above which the POSIX
456 interface uses \fBmalloc()\fP for output vectors. Further details are given in
457 the
458 .\" HREF
459 \fBpcreposix\fP
460 .\"
461 documentation.
462 .sp
464 .sp
465 The output is a long integer that gives the maximum depth of nesting of
466 parentheses (of any kind) in a pattern. This limit is imposed to cap the amount
467 of system stack used when a pattern is compiled. It is specified when PCRE is
468 built; the default is 250.
469 .sp
471 .sp
472 The output is a long integer that gives the default limit for the number of
473 internal matching function calls in a \fBpcre_exec()\fP execution. Further
474 details are given with \fBpcre_exec()\fP below.
475 .sp
477 .sp
478 The output is a long integer that gives the default limit for the depth of
479 recursion when calling the internal matching function in a \fBpcre_exec()\fP
480 execution. Further details are given with \fBpcre_exec()\fP below.
481 .sp
483 .sp
484 The output is an integer that is set to one if internal recursion when running
485 \fBpcre_exec()\fP is implemented by recursive function calls that use the stack
486 to remember their state. This is the usual way that PCRE is compiled. The
487 output is zero if PCRE was compiled to use blocks of data on the heap instead
488 of recursive function calls. In this case, \fBpcre_stack_malloc\fP and
489 \fBpcre_stack_free\fP are called to manage memory blocks on the heap, thus
490 avoiding the use of the stack.
491 .
492 .
494 .rs
495 .sp
496 .nf
497 .B pcre *pcre_compile(const char *\fIpattern\fP, int \fIoptions\fP,
498 .B " const char **\fIerrptr\fP, int *\fIerroffset\fP,"
499 .B " const unsigned char *\fItableptr\fP);"
500 .sp
501 .B pcre *pcre_compile2(const char *\fIpattern\fP, int \fIoptions\fP,
502 .B " int *\fIerrorcodeptr\fP,"
503 .B " const char **\fIerrptr\fP, int *\fIerroffset\fP,"
504 .B " const unsigned char *\fItableptr\fP);"
505 .fi
506 .P
507 Either of the functions \fBpcre_compile()\fP or \fBpcre_compile2()\fP can be
508 called to compile a pattern into an internal form. The only difference between
509 the two interfaces is that \fBpcre_compile2()\fP has an additional argument,
510 \fIerrorcodeptr\fP, via which a numerical error code can be returned. To avoid
511 too much repetition, we refer just to \fBpcre_compile()\fP below, but the
512 information applies equally to \fBpcre_compile2()\fP.
513 .P
514 The pattern is a C string terminated by a binary zero, and is passed in the
515 \fIpattern\fP argument. A pointer to a single block of memory that is obtained
516 via \fBpcre_malloc\fP is returned. This contains the compiled code and related
517 data. The \fBpcre\fP type is defined for the returned block; this is a typedef
518 for a structure whose contents are not externally defined. It is up to the
519 caller to free the memory (via \fBpcre_free\fP) when it is no longer required.
520 .P
521 Although the compiled code of a PCRE regex is relocatable, that is, it does not
522 depend on memory location, the complete \fBpcre\fP data block is not
523 fully relocatable, because it may contain a copy of the \fItableptr\fP
524 argument, which is an address (see below).
525 .P
526 The \fIoptions\fP argument contains various bit settings that affect the
527 compilation. It should be zero if no options are required. The available
528 options are described below. Some of them (in particular, those that are
529 compatible with Perl, but some others as well) can also be set and unset from
530 within the pattern (see the detailed description in the
531 .\" HREF
532 \fBpcrepattern\fP
533 .\"
534 documentation). For those options that can be different in different parts of
535 the pattern, the contents of the \fIoptions\fP argument specifies their
536 settings at the start of compilation and execution. The PCRE_ANCHORED,
538 PCRE_NO_START_OPTIMIZE options can be set at the time of matching as well as at
539 compile time.
540 .P
541 If \fIerrptr\fP is NULL, \fBpcre_compile()\fP returns NULL immediately.
542 Otherwise, if compilation of a pattern fails, \fBpcre_compile()\fP returns
543 NULL, and sets the variable pointed to by \fIerrptr\fP to point to a textual
544 error message. This is a static string that is part of the library. You must
545 not try to free it. Normally, the offset from the start of the pattern to the
546 data unit that was being processed when the error was discovered is placed in
547 the variable pointed to by \fIerroffset\fP, which must not be NULL (if it is,
548 an immediate error is given). However, for an invalid UTF-8 or UTF-16 string,
549 the offset is that of the first data unit of the failing character.
550 .P
551 Some errors are not detected until the whole pattern has been scanned; in these
552 cases, the offset passed back is the length of the pattern. Note that the
553 offset is in data units, not characters, even in a UTF mode. It may sometimes
554 point into the middle of a UTF-8 or UTF-16 character.
555 .P
556 If \fBpcre_compile2()\fP is used instead of \fBpcre_compile()\fP, and the
557 \fIerrorcodeptr\fP argument is not NULL, a non-zero error code number is
558 returned via this argument in the event of an error. This is in addition to the
559 textual error message. Error codes and messages are listed below.
560 .P
561 If the final argument, \fItableptr\fP, is NULL, PCRE uses a default set of
562 character tables that are built when PCRE is compiled, using the default C
563 locale. Otherwise, \fItableptr\fP must be an address that is the result of a
564 call to \fBpcre_maketables()\fP. This value is stored with the compiled
565 pattern, and used again by \fBpcre_exec()\fP and \fBpcre_dfa_exec()\fP when the
566 pattern is matched. For more discussion, see the section on locale support
567 below.
568 .P
569 This code fragment shows a typical straightforward call to \fBpcre_compile()\fP:
570 .sp
571 pcre *re;
572 const char *error;
573 int erroffset;
574 re = pcre_compile(
575 "^A.*Z", /* the pattern */
576 0, /* default options */
577 &error, /* for error message */
578 &erroffset, /* for error offset */
579 NULL); /* use default character tables */
580 .sp
581 The following names for option bits are defined in the \fBpcre.h\fP header
582 file:
583 .sp
585 .sp
586 If this bit is set, the pattern is forced to be "anchored", that is, it is
587 constrained to match only at the first matching point in the string that is
588 being searched (the "subject string"). This effect can also be achieved by
589 appropriate constructs in the pattern itself, which is the only way to do it in
590 Perl.
591 .sp
593 .sp
594 If this bit is set, \fBpcre_compile()\fP automatically inserts callout items,
595 all with number 255, before each pattern item. For discussion of the callout
596 facility, see the
597 .\" HREF
598 \fBpcrecallout\fP
599 .\"
600 documentation.
601 .sp
604 .sp
605 These options (which are mutually exclusive) control what the \eR escape
606 sequence matches. The choice is either to match only CR, LF, or CRLF, or to
607 match any Unicode newline sequence. The default is specified when PCRE is
608 built. It can be overridden from within the pattern, or by setting an option
609 when a compiled pattern is matched.
610 .sp
612 .sp
613 If this bit is set, letters in the pattern match both upper and lower case
614 letters. It is equivalent to Perl's /i option, and it can be changed within a
615 pattern by a (?i) option setting. In UTF-8 mode, PCRE always understands the
616 concept of case for characters whose values are less than 128, so caseless
617 matching is always possible. For characters with higher values, the concept of
618 case is supported if PCRE is compiled with Unicode property support, but not
619 otherwise. If you want to use caseless matching for characters 128 and above,
620 you must ensure that PCRE is compiled with Unicode property support as well as
621 with UTF-8 support.
622 .sp
624 .sp
625 If this bit is set, a dollar metacharacter in the pattern matches only at the
626 end of the subject string. Without this option, a dollar also matches
627 immediately before a newline at the end of the string (but not before any other
628 newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
629 There is no equivalent to this option in Perl, and no way to set it within a
630 pattern.
631 .sp
633 .sp
634 If this bit is set, a dot metacharacter in the pattern matches a character of
635 any value, including one that indicates a newline. However, it only ever
636 matches one character, even if newlines are coded as CRLF. Without this option,
637 a dot does not match when the current position is at a newline. This option is
638 equivalent to Perl's /s option, and it can be changed within a pattern by a
639 (?s) option setting. A negative class such as [^a] always matches newline
640 characters, independent of the setting of this option.
641 .sp
643 .sp
644 If this bit is set, names used to identify capturing subpatterns need not be
645 unique. This can be helpful for certain types of pattern when it is known that
646 only one instance of the named subpattern can ever be matched. There are more
647 details of named subpatterns below; see also the
648 .\" HREF
649 \fBpcrepattern\fP
650 .\"
651 documentation.
652 .sp
654 .sp
655 If this bit is set, most white space characters in the pattern are totally
656 ignored except when escaped or inside a character class. However, white space
657 is not allowed within sequences such as (?> that introduce various
658 parenthesized subpatterns, nor within a numerical quantifier such as {1,3}.
659 However, ignorable white space is permitted between an item and a following
660 quantifier and between a quantifier and a following + that indicates
661 possessiveness.
662 .P
663 White space did not used to include the VT character (code 11), because Perl
664 did not treat this character as white space. However, Perl changed at release
665 5.18, so PCRE followed at release 8.34, and VT is now treated as white space.
666 .P
667 PCRE_EXTENDED also causes characters between an unescaped # outside a character
668 class and the next newline, inclusive, to be ignored. PCRE_EXTENDED is
669 equivalent to Perl's /x option, and it can be changed within a pattern by a
670 (?x) option setting.
671 .P
672 Which characters are interpreted as newlines is controlled by the options
673 passed to \fBpcre_compile()\fP or by a special sequence at the start of the
674 pattern, as described in the section entitled
675 .\" HTML <a href="pcrepattern.html#newlines">
676 .\" </a>
677 "Newline conventions"
678 .\"
679 in the \fBpcrepattern\fP documentation. Note that the end of this type of
680 comment is a literal newline sequence in the pattern; escape sequences that
681 happen to represent a newline do not count.
682 .P
683 This option makes it possible to include comments inside complicated patterns.
684 Note, however, that this applies only to data characters. White space characters
685 may never appear within special character sequences in a pattern, for example
686 within the sequence (?( that introduces a conditional subpattern.
687 .sp
689 .sp
690 This option was invented in order to turn on additional functionality of PCRE
691 that is incompatible with Perl, but it is currently of very little use. When
692 set, any backslash in a pattern that is followed by a letter that has no
693 special meaning causes an error, thus reserving these combinations for future
694 expansion. By default, as in Perl, a backslash followed by a letter with no
695 special meaning is treated as a literal. (Perl can, however, be persuaded to
696 give an error for this, by running it with the -w option.) There are at present
697 no other features controlled by this option. It can also be set by a (?X)
698 option setting within a pattern.
699 .sp
701 .sp
702 If this option is set, an unanchored pattern is required to match before or at
703 the first newline in the subject string, though the matched text may continue
704 over the newline.
705 .sp
707 .sp
708 If this option is set, PCRE's behaviour is changed in some ways so that it is
709 compatible with JavaScript rather than Perl. The changes are as follows:
710 .P
711 (1) A lone closing square bracket in a pattern causes a compile-time error,
712 because this is illegal in JavaScript (by default it is treated as a data
713 character). Thus, the pattern AB]CD becomes illegal when this option is set.
714 .P
715 (2) At run time, a back reference to an unset subpattern group matches an empty
716 string (by default this causes the current matching alternative to fail). A
717 pattern such as (\e1)(a) succeeds when this option is set (assuming it can find
718 an "a" in the subject), whereas it fails by default, for Perl compatibility.
719 .P
720 (3) \eU matches an upper case "U" character; by default \eU causes a compile
721 time error (Perl uses \eU to upper case subsequent characters).
722 .P
723 (4) \eu matches a lower case "u" character unless it is followed by four
724 hexadecimal digits, in which case the hexadecimal number defines the code point
725 to match. By default, \eu causes a compile time error (Perl uses it to upper
726 case the following character).
727 .P
728 (5) \ex matches a lower case "x" character unless it is followed by two
729 hexadecimal digits, in which case the hexadecimal number defines the code point
730 to match. By default, as in Perl, a hexadecimal number is always expected after
731 \ex, but it may have zero, one, or two digits (so, for example, \exz matches a
732 binary zero character followed by z).
733 .sp
735 .sp
736 By default, for the purposes of matching "start of line" and "end of line",
737 PCRE treats the subject string as consisting of a single line of characters,
738 even if it actually contains newlines. The "start of line" metacharacter (^)
739 matches only at the start of the string, and the "end of line" metacharacter
740 ($) matches only at the end of the string, or before a terminating newline
741 (except when PCRE_DOLLAR_ENDONLY is set). Note, however, that unless
742 PCRE_DOTALL is set, the "any character" metacharacter (.) does not match at a
743 newline. This behaviour (for ^, $, and dot) is the same as Perl.
744 .P
745 When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
746 match immediately following or immediately before internal newlines in the
747 subject string, respectively, as well as at the very start and end. This is
748 equivalent to Perl's /m option, and it can be changed within a pattern by a
749 (?m) option setting. If there are no newlines in a subject string, or no
750 occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no effect.
751 .sp
753 .sp
754 This option locks out interpretation of the pattern as UTF-8 (or UTF-16 or
755 UTF-32 in the 16-bit and 32-bit libraries). In particular, it prevents the
756 creator of the pattern from switching to UTF interpretation by starting the
757 pattern with (*UTF). This may be useful in applications that process patterns
758 from external sources. The combination of PCRE_UTF8 and PCRE_NEVER_UTF also
759 causes an error.
760 .sp
766 .sp
767 These options override the default newline definition that was chosen when PCRE
768 was built. Setting the first or the second specifies that a newline is
769 indicated by a single character (CR or LF, respectively). Setting
770 PCRE_NEWLINE_CRLF specifies that a newline is indicated by the two-character
771 CRLF sequence. Setting PCRE_NEWLINE_ANYCRLF specifies that any of the three
772 preceding sequences should be recognized. Setting PCRE_NEWLINE_ANY specifies
773 that any Unicode newline sequence should be recognized.
774 .P
775 In an ASCII/Unicode environment, the Unicode newline sequences are the three
776 just mentioned, plus the single characters VT (vertical tab, U+000B), FF (form
777 feed, U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and PS
778 (paragraph separator, U+2029). For the 8-bit library, the last two are
779 recognized only in UTF-8 mode.
780 .P
781 When PCRE is compiled to run in an EBCDIC (mainframe) environment, the code for
782 CR is 0x0d, the same as ASCII. However, the character code for LF is normally
783 0x15, though in some EBCDIC environments 0x25 is used. Whichever of these is
784 not LF is made to correspond to Unicode's NEL character. EBCDIC codes are all
785 less than 256. For more details, see the
786 .\" HREF
787 \fBpcrebuild\fP
788 .\"
789 documentation.
790 .P
791 The newline setting in the options word uses three bits that are treated
792 as a number, giving eight possibilities. Currently only six are used (default
793 plus the five values above). This means that if you set more than one newline
794 option, the combination may or may not be sensible. For example,
796 other combinations may yield unused numbers and cause an error.
797 .P
798 The only time that a line break in a pattern is specially recognized when
799 compiling is when PCRE_EXTENDED is set. CR and LF are white space characters,
800 and so are ignored in this mode. Also, an unescaped # outside a character class
801 indicates a comment that lasts until after the next line break sequence. In
802 other circumstances, line break sequences in patterns are treated as literal
803 data.
804 .P
805 The newline option that is set at compile time becomes the default that is used
806 for \fBpcre_exec()\fP and \fBpcre_dfa_exec()\fP, but it can be overridden.
807 .sp
809 .sp
810 If this option is set, it disables the use of numbered capturing parentheses in
811 the pattern. Any opening parenthesis that is not followed by ? behaves as if it
812 were followed by ?: but named parentheses can still be used for capturing (and
813 they acquire numbers in the usual way). There is no equivalent of this option
814 in Perl.
815 .sp
817 .sp
818 If this option is set, it disables "auto-possessification". This is an
819 optimization that, for example, turns a+b into a++b in order to avoid
820 backtracks into a+ that can never be successful. However, if callouts are in
821 use, auto-possessification means that some of them are never taken. You can set
822 this option if you want the matching functions to do a full unoptimized search
823 and run all the callouts, but it is mainly provided for testing purposes.
824 .sp
826 .sp
827 This is an option that acts at matching time; that is, it is really an option
828 for \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP. If it is set at compile time,
829 it is remembered with the compiled pattern and assumed at matching time. This
830 is necessary if you want to use JIT execution, because the JIT compiler needs
831 to know whether or not this option is set. For details see the discussion of
833 .\" HTML <a href="#execoptions">
834 .\" </a>
835 below.
836 .\"
837 .sp
839 .sp
840 This option changes the way PCRE processes \eB, \eb, \eD, \ed, \eS, \es, \eW,
841 \ew, and some of the POSIX character classes. By default, only ASCII characters
842 are recognized, but if PCRE_UCP is set, Unicode properties are used instead to
843 classify characters. More details are given in the section on
844 .\" HTML <a href="pcre.html#genericchartypes">
845 .\" </a>
846 generic character types
847 .\"
848 in the
849 .\" HREF
850 \fBpcrepattern\fP
851 .\"
852 page. If you set PCRE_UCP, matching one of the items it affects takes much
853 longer. The option is available only if PCRE has been compiled with Unicode
854 property support.
855 .sp
857 .sp
858 This option inverts the "greediness" of the quantifiers so that they are not
859 greedy by default, but become greedy if followed by "?". It is not compatible
860 with Perl. It can also be set by a (?U) option setting within the pattern.
861 .sp
863 .sp
864 This option causes PCRE to regard both the pattern and the subject as strings
865 of UTF-8 characters instead of single-byte strings. However, it is available
866 only when PCRE is built to include UTF support. If not, the use of this option
867 provokes an error. Details of how this option changes the behaviour of PCRE are
868 given in the
869 .\" HREF
870 \fBpcreunicode\fP
871 .\"
872 page.
873 .sp
875 .sp
876 When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
877 automatically checked. There is a discussion about the
878 .\" HTML <a href="pcreunicode.html#utf8strings">
879 .\" </a>
880 validity of UTF-8 strings
881 .\"
882 in the
883 .\" HREF
884 \fBpcreunicode\fP
885 .\"
886 page. If an invalid UTF-8 sequence is found, \fBpcre_compile()\fP returns an
887 error. If you already know that your pattern is valid, and you want to skip
888 this check for performance reasons, you can set the PCRE_NO_UTF8_CHECK option.
889 When it is set, the effect of passing an invalid UTF-8 string as a pattern is
890 undefined. It may cause your program to crash or loop. Note that this option
891 can also be passed to \fBpcre_exec()\fP and \fBpcre_dfa_exec()\fP, to suppress
892 the validity checking of subject strings only. If the same string is being
893 matched many times, the option can be safely set for the second and subsequent
894 matchings to improve performance.
895 .
896 .
898 .rs
899 .sp
900 The following table lists the error codes than may be returned by
901 \fBpcre_compile2()\fP, along with the error messages that may be returned by
902 both compiling functions. Note that error messages are always 8-bit ASCII
903 strings, even in 16-bit or 32-bit mode. As PCRE has developed, some error codes
904 have fallen out of use. To avoid confusion, they have not been re-used.
905 .sp
906 0 no error
907 1 \e at end of pattern
908 2 \ec at end of pattern
909 3 unrecognized character follows \e
910 4 numbers out of order in {} quantifier
911 5 number too big in {} quantifier
912 6 missing terminating ] for character class
913 7 invalid escape sequence in character class
914 8 range out of order in character class
915 9 nothing to repeat
916 10 [this code is not in use]
917 11 internal error: unexpected repeat
918 12 unrecognized character after (? or (?-
919 13 POSIX named classes are supported only within a class
920 14 missing )
921 15 reference to non-existent subpattern
922 16 erroffset passed as NULL
923 17 unknown option bit(s) set
924 18 missing ) after comment
925 19 [this code is not in use]
926 20 regular expression is too large
927 21 failed to get memory
928 22 unmatched parentheses
929 23 internal error: code overflow
930 24 unrecognized character after (?<
931 25 lookbehind assertion is not fixed length
932 26 malformed number or name after (?(
933 27 conditional group contains more than two branches
934 28 assertion expected after (?(
935 29 (?R or (?[+-]digits must be followed by )
936 30 unknown POSIX class name
937 31 POSIX collating elements are not supported
938 32 this version of PCRE is compiled without UTF support
939 33 [this code is not in use]
940 34 character value in \ex{} or \eo{} is too large
941 35 invalid condition (?(0)
942 36 \eC not allowed in lookbehind assertion
943 37 PCRE does not support \eL, \el, \eN{name}, \eU, or \eu
944 38 number after (?C is > 255
945 39 closing ) for (?C expected
946 40 recursive call could loop indefinitely
947 41 unrecognized character after (?P
948 42 syntax error in subpattern name (missing terminator)
949 43 two named subpatterns have the same name
950 44 invalid UTF-8 string (specifically UTF-8)
951 45 support for \eP, \ep, and \eX has not been compiled
952 46 malformed \eP or \ep sequence
953 47 unknown property name after \eP or \ep
954 48 subpattern name is too long (maximum 32 characters)
955 49 too many named subpatterns (maximum 10000)
956 50 [this code is not in use]
957 51 octal value is greater than \e377 in 8-bit non-UTF-8 mode
958 52 internal error: overran compiling workspace
959 53 internal error: previously-checked referenced subpattern
960 not found
961 54 DEFINE group contains more than one branch
962 55 repeating a DEFINE group is not allowed
963 56 inconsistent NEWLINE options
964 57 \eg is not followed by a braced, angle-bracketed, or quoted
965 name/number or by a plain number
966 58 a numbered reference must not be zero
967 59 an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)
968 60 (*VERB) not recognized or malformed
969 61 number is too big
970 62 subpattern name expected
971 63 digit expected after (?+
972 64 ] is an invalid data character in JavaScript compatibility mode
973 65 different names for subpatterns of the same number are
974 not allowed
975 66 (*MARK) must have an argument
976 67 this version of PCRE is not compiled with Unicode property
977 support
978 68 \ec must be followed by an ASCII character
979 69 \ek is not followed by a braced, angle-bracketed, or quoted name
980 70 internal error: unknown opcode in find_fixedlength()
981 71 \eN is not supported in a class
982 72 too many forward references
983 73 disallowed Unicode code point (>= 0xd800 && <= 0xdfff)
984 74 invalid UTF-16 string (specifically UTF-16)
985 75 name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)
986 76 character value in \eu.... sequence is too large
987 77 invalid UTF-32 string (specifically UTF-32)
988 78 setting UTF is disabled by the application
989 79 non-hex character in \ex{} (closing brace missing?)
990 80 non-octal character in \eo{} (closing brace missing?)
991 81 missing opening brace after \eo
992 82 parentheses are too deeply nested
993 83 invalid range in character class
994 .sp
995 The numbers 32 and 10000 in errors 48 and 49 are defaults; different values may
996 be used if the limits were changed when PCRE was built.
997 .
998 .
999 .\" HTML <a name="studyingapattern"></a>
1001 .rs
1002 .sp
1003 .nf
1004 .B pcre_extra *pcre_study(const pcre *\fIcode\fP, int \fIoptions\fP,
1005 .B " const char **\fIerrptr\fP);"
1006 .fi
1007 .PP
1008 If a compiled pattern is going to be used several times, it is worth spending
1009 more time analyzing it in order to speed up the time taken for matching. The
1010 function \fBpcre_study()\fP takes a pointer to a compiled pattern as its first
1011 argument. If studying the pattern produces additional information that will
1012 help speed up matching, \fBpcre_study()\fP returns a pointer to a
1013 \fBpcre_extra\fP block, in which the \fIstudy_data\fP field points to the
1014 results of the study.
1015 .P
1016 The returned value from \fBpcre_study()\fP can be passed directly to
1017 \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP. However, a \fBpcre_extra\fP block
1018 also contains other fields that can be set by the caller before the block is
1019 passed; these are described
1020 .\" HTML <a href="#extradata">
1021 .\" </a>
1022 below
1023 .\"
1024 in the section on matching a pattern.
1025 .P
1026 If studying the pattern does not produce any useful information,
1027 \fBpcre_study()\fP returns NULL by default. In that circumstance, if the
1028 calling program wants to pass any of the other fields to \fBpcre_exec()\fP or
1029 \fBpcre_dfa_exec()\fP, it must set up its own \fBpcre_extra\fP block. However,
1030 if \fBpcre_study()\fP is called with the PCRE_STUDY_EXTRA_NEEDED option, it
1031 returns a \fBpcre_extra\fP block even if studying did not find any additional
1032 information. It may still return NULL, however, if an error occurs in
1033 \fBpcre_study()\fP.
1034 .P
1035 The second argument of \fBpcre_study()\fP contains option bits. There are three
1036 further options in addition to PCRE_STUDY_EXTRA_NEEDED:
1037 .sp
1041 .sp
1042 If any of these are set, and the just-in-time compiler is available, the
1043 pattern is further compiled into machine code that executes much faster than
1044 the \fBpcre_exec()\fP interpretive matching function. If the just-in-time
1045 compiler is not available, these options are ignored. All undefined bits in the
1046 \fIoptions\fP argument must be zero.
1047 .P
1048 JIT compilation is a heavyweight optimization. It can take some time for
1049 patterns to be analyzed, and for one-off matches and simple patterns the
1050 benefit of faster execution might be offset by a much slower study time.
1051 Not all patterns can be optimized by the JIT compiler. For those that cannot be
1052 handled, matching automatically falls back to the \fBpcre_exec()\fP
1053 interpreter. For more details, see the
1054 .\" HREF
1055 \fBpcrejit\fP
1056 .\"
1057 documentation.
1058 .P
1059 The third argument for \fBpcre_study()\fP is a pointer for an error message. If
1060 studying succeeds (even if no data is returned), the variable it points to is
1061 set to NULL. Otherwise it is set to point to a textual error message. This is a
1062 static string that is part of the library. You must not try to free it. You
1063 should test the error pointer for NULL after calling \fBpcre_study()\fP, to be
1064 sure that it has run successfully.
1065 .P
1066 When you are finished with a pattern, you can free the memory used for the
1067 study data by calling \fBpcre_free_study()\fP. This function was added to the
1068 API for release 8.20. For earlier versions, the memory could be freed with
1069 \fBpcre_free()\fP, just like the pattern itself. This will still work in cases
1070 where JIT optimization is not used, but it is advisable to change to the new
1071 function when convenient.
1072 .P
1073 This is a typical way in which \fBpcre_study\fP() is used (except that in a
1074 real application there should be tests for errors):
1075 .sp
1076 int rc;
1077 pcre *re;
1078 pcre_extra *sd;
1079 re = pcre_compile("pattern", 0, &error, &erroroffset, NULL);
1080 sd = pcre_study(
1081 re, /* result of pcre_compile() */
1082 0, /* no options */
1083 &error); /* set to NULL or points to a message */
1084 rc = pcre_exec( /* see below for details of pcre_exec() options */
1085 re, sd, "subject", 7, 0, 0, ovector, 30);
1086 ...
1087 pcre_free_study(sd);
1088 pcre_free(re);
1089 .sp
1090 Studying a pattern does two things: first, a lower bound for the length of
1091 subject string that is needed to match the pattern is computed. This does not
1092 mean that there are any strings of that length that match, but it does
1093 guarantee that no shorter strings match. The value is used to avoid wasting
1094 time by trying to match strings that are shorter than the lower bound. You can
1095 find out the value in a calling program via the \fBpcre_fullinfo()\fP function.
1096 .P
1097 Studying a pattern is also useful for non-anchored patterns that do not have a
1098 single fixed starting character. A bitmap of possible starting bytes is
1099 created. This speeds up finding a position in the subject at which to start
1100 matching. (In 16-bit mode, the bitmap is used for 16-bit values less than 256.
1101 In 32-bit mode, the bitmap is used for 32-bit values less than 256.)
1102 .P
1103 These two optimizations apply to both \fBpcre_exec()\fP and
1104 \fBpcre_dfa_exec()\fP, and the information is also used by the JIT compiler.
1105 The optimizations can be disabled by setting the PCRE_NO_START_OPTIMIZE option.
1106 You might want to do this if your pattern contains callouts or (*MARK) and you
1107 want to make use of these facilities in cases where matching fails.
1108 .P
1109 PCRE_NO_START_OPTIMIZE can be specified at either compile time or execution
1110 time. However, if PCRE_NO_START_OPTIMIZE is passed to \fBpcre_exec()\fP, (that
1111 is, after any JIT compilation has happened) JIT execution is disabled. For JIT
1112 execution to work with PCRE_NO_START_OPTIMIZE, the option must be set at
1113 compile time.
1114 .P
1115 There is a longer discussion of PCRE_NO_START_OPTIMIZE
1116 .\" HTML <a href="#execoptions">
1117 .\" </a>
1118 below.
1119 .\"
1120 .
1121 .
1122 .\" HTML <a name="localesupport"></a>
1124 .rs
1125 .sp
1126 PCRE handles caseless matching, and determines whether characters are letters,
1127 digits, or whatever, by reference to a set of tables, indexed by character
1128 code point. When running in UTF-8 mode, or in the 16- or 32-bit libraries, this
1129 applies only to characters with code points less than 256. By default,
1130 higher-valued code points never match escapes such as \ew or \ed. However, if
1131 PCRE is built with Unicode property support, all characters can be tested with
1132 \ep and \eP, or, alternatively, the PCRE_UCP option can be set when a pattern
1133 is compiled; this causes \ew and friends to use Unicode property support
1134 instead of the built-in tables.
1135 .P
1136 The use of locales with Unicode is discouraged. If you are handling characters
1137 with code points greater than 128, you should either use Unicode support, or
1138 use locales, but not try to mix the two.
1139 .P
1140 PCRE contains an internal set of tables that are used when the final argument
1141 of \fBpcre_compile()\fP is NULL. These are sufficient for many applications.
1142 Normally, the internal tables recognize only ASCII characters. However, when
1143 PCRE is built, it is possible to cause the internal tables to be rebuilt in the
1144 default "C" locale of the local system, which may cause them to be different.
1145 .P
1146 The internal tables can always be overridden by tables supplied by the
1147 application that calls PCRE. These may be created in a different locale from
1148 the default. As more and more applications change to using Unicode, the need
1149 for this locale support is expected to die away.
1150 .P
1151 External tables are built by calling the \fBpcre_maketables()\fP function,
1152 which has no arguments, in the relevant locale. The result can then be passed
1153 to \fBpcre_compile()\fP as often as necessary. For example, to build and use
1154 tables that are appropriate for the French locale (where accented characters
1155 with values greater than 128 are treated as letters), the following code could
1156 be used:
1157 .sp
1158 setlocale(LC_CTYPE, "fr_FR");
1159 tables = pcre_maketables();
1160 re = pcre_compile(..., tables);
1161 .sp
1162 The locale name "fr_FR" is used on Linux and other Unix-like systems; if you
1163 are using Windows, the name for the French locale is "french".
1164 .P
1165 When \fBpcre_maketables()\fP runs, the tables are built in memory that is
1166 obtained via \fBpcre_malloc\fP. It is the caller's responsibility to ensure
1167 that the memory containing the tables remains available for as long as it is
1168 needed.
1169 .P
1170 The pointer that is passed to \fBpcre_compile()\fP is saved with the compiled
1171 pattern, and the same tables are used via this pointer by \fBpcre_study()\fP
1172 and also by \fBpcre_exec()\fP and \fBpcre_dfa_exec()\fP. Thus, for any single
1173 pattern, compilation, studying and matching all happen in the same locale, but
1174 different patterns can be processed in different locales.
1175 .P
1176 It is possible to pass a table pointer or NULL (indicating the use of the
1177 internal tables) to \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP (see the
1178 discussion below in the section on matching a pattern). This facility is
1179 provided for use with pre-compiled patterns that have been saved and reloaded.
1180 Character tables are not saved with patterns, so if a non-standard table was
1181 used at compile time, it must be provided again when the reloaded pattern is
1182 matched. Attempting to use this facility to match a pattern in a different
1183 locale from the one in which it was compiled is likely to lead to anomalous
1184 (usually incorrect) results.
1185 .
1186 .
1187 .\" HTML <a name="infoaboutpattern"></a>
1189 .rs
1190 .sp
1191 .nf
1192 .B int pcre_fullinfo(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
1193 .B " int \fIwhat\fP, void *\fIwhere\fP);"
1194 .fi
1195 .PP
1196 The \fBpcre_fullinfo()\fP function returns information about a compiled
1197 pattern. It replaces the \fBpcre_info()\fP function, which was removed from the
1198 library at version 8.30, after more than 10 years of obsolescence.
1199 .P
1200 The first argument for \fBpcre_fullinfo()\fP is a pointer to the compiled
1201 pattern. The second argument is the result of \fBpcre_study()\fP, or NULL if
1202 the pattern was not studied. The third argument specifies which piece of
1203 information is required, and the fourth argument is a pointer to a variable
1204 to receive the data. The yield of the function is zero for success, or one of
1205 the following negative numbers:
1206 .sp
1207 PCRE_ERROR_NULL the argument \fIcode\fP was NULL
1208 the argument \fIwhere\fP was NULL
1209 PCRE_ERROR_BADMAGIC the "magic number" was not found
1210 PCRE_ERROR_BADENDIANNESS the pattern was compiled with different
1211 endianness
1212 PCRE_ERROR_BADOPTION the value of \fIwhat\fP was invalid
1213 PCRE_ERROR_UNSET the requested field is not set
1214 .sp
1215 The "magic number" is placed at the start of each compiled pattern as an simple
1216 check against passing an arbitrary memory pointer. The endianness error can
1217 occur if a compiled pattern is saved and reloaded on a different host. Here is
1218 a typical call of \fBpcre_fullinfo()\fP, to obtain the length of the compiled
1219 pattern:
1220 .sp
1221 int rc;
1222 size_t length;
1223 rc = pcre_fullinfo(
1224 re, /* result of pcre_compile() */
1225 sd, /* result of pcre_study(), or NULL */
1226 PCRE_INFO_SIZE, /* what is required */
1227 &length); /* where to put the data */
1228 .sp
1229 The possible values for the third argument are defined in \fBpcre.h\fP, and are
1230 as follows:
1231 .sp
1233 .sp
1234 Return the number of the highest back reference in the pattern. The fourth
1235 argument should point to an \fBint\fP variable. Zero is returned if there are
1236 no back references.
1237 .sp
1239 .sp
1240 Return the number of capturing subpatterns in the pattern. The fourth argument
1241 should point to an \fBint\fP variable.
1242 .sp
1244 .sp
1245 Return a pointer to the internal default character tables within PCRE. The
1246 fourth argument should point to an \fBunsigned char *\fP variable. This
1247 information call is provided for internal use by the \fBpcre_study()\fP
1248 function. External callers can cause PCRE to use its internal tables by passing
1249 a NULL table pointer.
1250 .sp
1251 PCRE_INFO_FIRSTBYTE (deprecated)
1252 .sp
1253 Return information about the first data unit of any matched string, for a
1254 non-anchored pattern. The name of this option refers to the 8-bit library,
1255 where data units are bytes. The fourth argument should point to an \fBint\fP
1256 variable. Negative values are used for special cases. However, this means that
1257 when the 32-bit library is in non-UTF-32 mode, the full 32-bit range of
1258 characters cannot be returned. For this reason, this value is deprecated; use
1260 .P
1261 If there is a fixed first value, for example, the letter "c" from a pattern
1262 such as (cat|cow|coyote), its value is returned. In the 8-bit library, the
1263 value is always less than 256. In the 16-bit library the value can be up to
1264 0xffff. In the 32-bit library the value can be up to 0x10ffff.
1265 .P
1266 If there is no fixed first value, and if either
1267 .sp
1268 (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
1269 starts with "^", or
1270 .sp
1271 (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
1272 (if it were set, the pattern would be anchored),
1273 .sp
1274 -1 is returned, indicating that the pattern matches only at the start of a
1275 subject string or after any newline within the string. Otherwise -2 is
1276 returned. For anchored patterns, -2 is returned.
1277 .sp
1279 .sp
1280 Return the value of the first data unit (non-UTF character) of any matched
1281 string in the situation where PCRE_INFO_FIRSTCHARACTERFLAGS returns 1;
1282 otherwise return 0. The fourth argument should point to an \fBuint_t\fP
1283 variable.
1284 .P
1285 In the 8-bit library, the value is always less than 256. In the 16-bit library
1286 the value can be up to 0xffff. In the 32-bit library in UTF-32 mode the value
1287 can be up to 0x10ffff, and up to 0xffffffff when not using UTF-32 mode.
1288 .sp
1290 .sp
1291 Return information about the first data unit of any matched string, for a
1292 non-anchored pattern. The fourth argument should point to an \fBint\fP
1293 variable.
1294 .P
1295 If there is a fixed first value, for example, the letter "c" from a pattern
1296 such as (cat|cow|coyote), 1 is returned, and the character value can be
1297 retrieved using PCRE_INFO_FIRSTCHARACTER. If there is no fixed first value, and
1298 if either
1299 .sp
1300 (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
1301 starts with "^", or
1302 .sp
1303 (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
1304 (if it were set, the pattern would be anchored),
1305 .sp
1306 2 is returned, indicating that the pattern matches only at the start of a
1307 subject string or after any newline within the string. Otherwise 0 is
1308 returned. For anchored patterns, 0 is returned.
1309 .sp
1311 .sp
1312 If the pattern was studied, and this resulted in the construction of a 256-bit
1313 table indicating a fixed set of values for the first data unit in any matching
1314 string, a pointer to the table is returned. Otherwise NULL is returned. The
1315 fourth argument should point to an \fBunsigned char *\fP variable.
1316 .sp
1318 .sp
1319 Return 1 if the pattern contains any explicit matches for CR or LF characters,
1320 otherwise 0. The fourth argument should point to an \fBint\fP variable. An
1321 explicit match is either a literal CR or LF character, or \er or \en.
1322 .sp
1324 .sp
1325 Return 1 if the (?J) or (?-J) option setting is used in the pattern, otherwise
1326 0. The fourth argument should point to an \fBint\fP variable. (?J) and
1327 (?-J) set and unset the local PCRE_DUPNAMES option, respectively.
1328 .sp
1330 .sp
1331 Return 1 if the pattern was studied with one of the JIT options, and
1332 just-in-time compiling was successful. The fourth argument should point to an
1333 \fBint\fP variable. A return value of 0 means that JIT support is not available
1334 in this version of PCRE, or that the pattern was not studied with a JIT option,
1335 or that the JIT compiler could not handle this particular pattern. See the
1336 .\" HREF
1337 \fBpcrejit\fP
1338 .\"
1339 documentation for details of what can and cannot be handled.
1340 .sp
1342 .sp
1343 If the pattern was successfully studied with a JIT option, return the size of
1344 the JIT compiled code, otherwise return zero. The fourth argument should point
1345 to a \fBsize_t\fP variable.
1346 .sp
1348 .sp
1349 Return the value of the rightmost literal data unit that must exist in any
1350 matched string, other than at its start, if such a value has been recorded. The
1351 fourth argument should point to an \fBint\fP variable. If there is no such
1352 value, -1 is returned. For anchored patterns, a last literal value is recorded
1353 only if it follows something of variable length. For example, for the pattern
1354 /^a\ed+z\ed+/ the returned value is "z", but for /^a\edz\ed/ the returned value
1355 is -1.
1356 .P
1357 Since for the 32-bit library using the non-UTF-32 mode, this function is unable
1358 to return the full 32-bit range of characters, this value is deprecated;
1360 be used.
1361 .sp
1363 .sp
1364 Return 1 if the pattern can match an empty string, otherwise 0. The fourth
1365 argument should point to an \fBint\fP variable.
1366 .sp
1368 .sp
1369 If the pattern set a match limit by including an item of the form
1370 (*LIMIT_MATCH=nnnn) at the start, the value is returned. The fourth argument
1371 should point to an unsigned 32-bit integer. If no such value has been set, the
1372 call to \fBpcre_fullinfo()\fP returns the error PCRE_ERROR_UNSET.
1373 .sp
1375 .sp
1376 Return the number of characters (NB not data units) in the longest lookbehind
1377 assertion in the pattern. This information is useful when doing multi-segment
1378 matching using the partial matching facilities. Note that the simple assertions
1379 \eb and \eB require a one-character lookbehind. \eA also registers a
1380 one-character lookbehind, though it does not actually inspect the previous
1381 character. This is to ensure that at least one character from the old segment
1382 is retained when a new segment is processed. Otherwise, if there are no
1383 lookbehinds in the pattern, \eA might match incorrectly at the start of a new
1384 segment.
1385 .sp
1387 .sp
1388 If the pattern was studied and a minimum length for matching subject strings
1389 was computed, its value is returned. Otherwise the returned value is -1. The
1390 value is a number of characters, which in UTF mode may be different from the
1391 number of data units. The fourth argument should point to an \fBint\fP
1392 variable. A non-negative value is a lower bound to the length of any matching
1393 string. There may not be any strings of that length that do actually match, but
1394 every string that does match is at least that long.
1395 .sp
1399 .sp
1400 PCRE supports the use of named as well as numbered capturing parentheses. The
1401 names are just an additional way of identifying the parentheses, which still
1402 acquire numbers. Several convenience functions such as
1403 \fBpcre_get_named_substring()\fP are provided for extracting captured
1404 substrings by name. It is also possible to extract the data directly, by first
1405 converting the name to a number in order to access the correct pointers in the
1406 output vector (described with \fBpcre_exec()\fP below). To do the conversion,
1407 you need to use the name-to-number map, which is described by these three
1408 values.
1409 .P
1410 The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT gives
1411 the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size of each
1412 entry; both of these return an \fBint\fP value. The entry size depends on the
1413 length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
1414 entry of the table. This is a pointer to \fBchar\fP in the 8-bit library, where
1415 the first two bytes of each entry are the number of the capturing parenthesis,
1416 most significant byte first. In the 16-bit library, the pointer points to
1417 16-bit data units, the first of which contains the parenthesis number. In the
1418 32-bit library, the pointer points to 32-bit data units, the first of which
1419 contains the parenthesis number. The rest of the entry is the corresponding
1420 name, zero terminated.
1421 .P
1422 The names are in alphabetical order. If (?| is used to create multiple groups
1423 with the same number, as described in the
1424 .\" HTML <a href="pcrepattern.html#dupsubpatternnumber">
1425 .\" </a>
1426 section on duplicate subpattern numbers
1427 .\"
1428 in the
1429 .\" HREF
1430 \fBpcrepattern\fP
1431 .\"
1432 page, the groups may be given the same name, but there is only one entry in the
1433 table. Different names for groups of the same number are not permitted.
1434 Duplicate names for subpatterns with different numbers are permitted,
1435 but only if PCRE_DUPNAMES is set. They appear in the table in the order in
1436 which they were found in the pattern. In the absence of (?| this is the order
1437 of increasing number; when (?| is used this is not necessarily the case because
1438 later subpatterns may have lower numbers.
1439 .P
1440 As a simple example of the name/number table, consider the following pattern
1441 after compilation by the 8-bit library (assume PCRE_EXTENDED is set, so white
1442 space - including newlines - is ignored):
1443 .sp
1444 .\" JOIN
1445 (?<date> (?<year>(\ed\ed)?\ed\ed) -
1446 (?<month>\ed\ed) - (?<day>\ed\ed) )
1447 .sp
1448 There are four named subpatterns, so the table has four entries, and each entry
1449 in the table is eight bytes long. The table is as follows, with non-printing
1450 bytes shows in hexadecimal, and undefined bytes shown as ??:
1451 .sp
1452 00 01 d a t e 00 ??
1453 00 05 d a y 00 ?? ??
1454 00 04 m o n t h 00
1455 00 02 y e a r 00 ??
1456 .sp
1457 When writing code to extract data from named subpatterns using the
1458 name-to-number map, remember that the length of the entries is likely to be
1459 different for each compiled pattern.
1460 .sp
1462 .sp
1463 Return 1 if the pattern can be used for partial matching with
1464 \fBpcre_exec()\fP, otherwise 0. The fourth argument should point to an
1465 \fBint\fP variable. From release 8.00, this always returns 1, because the
1466 restrictions that previously applied to partial matching have been lifted. The
1467 .\" HREF
1468 \fBpcrepartial\fP
1469 .\"
1470 documentation gives details of partial matching.
1471 .sp
1473 .sp
1474 Return a copy of the options with which the pattern was compiled. The fourth
1475 argument should point to an \fBunsigned long int\fP variable. These option bits
1476 are those specified in the call to \fBpcre_compile()\fP, modified by any
1477 top-level option settings at the start of the pattern itself. In other words,
1478 they are the options that will be in force when matching starts. For example,
1479 if the pattern /(?im)abc(?-i)d/ is compiled with the PCRE_EXTENDED option, the
1481 .P
1482 A pattern is automatically anchored by PCRE if all of its top-level
1483 alternatives begin with one of the following:
1484 .sp
1485 ^ unless PCRE_MULTILINE is set
1486 \eA always
1487 \eG always
1488 .\" JOIN
1489 .* if PCRE_DOTALL is set and there are no back
1490 references to the subpattern in which .* appears
1491 .sp
1492 For such patterns, the PCRE_ANCHORED bit is set in the options returned by
1493 \fBpcre_fullinfo()\fP.
1494 .sp
1496 .sp
1497 If the pattern set a recursion limit by including an item of the form
1498 (*LIMIT_RECURSION=nnnn) at the start, the value is returned. The fourth
1499 argument should point to an unsigned 32-bit integer. If no such value has been
1500 set, the call to \fBpcre_fullinfo()\fP returns the error PCRE_ERROR_UNSET.
1501 .sp
1503 .sp
1504 Return the size of the compiled pattern in bytes (for all three libraries). The
1505 fourth argument should point to a \fBsize_t\fP variable. This value does not
1506 include the size of the \fBpcre\fP structure that is returned by
1507 \fBpcre_compile()\fP. The value that is passed as the argument to
1508 \fBpcre_malloc()\fP when \fBpcre_compile()\fP is getting memory in which to
1509 place the compiled data is the value returned by this option plus the size of
1510 the \fBpcre\fP structure. Studying a compiled pattern, with or without JIT,
1511 does not alter the value returned by this option.
1512 .sp
1514 .sp
1515 Return the size in bytes (for all three libraries) of the data block pointed to
1516 by the \fIstudy_data\fP field in a \fBpcre_extra\fP block. If \fBpcre_extra\fP
1517 is NULL, or there is no study data, zero is returned. The fourth argument
1518 should point to a \fBsize_t\fP variable. The \fIstudy_data\fP field is set by
1519 \fBpcre_study()\fP to record information that will speed up matching (see the
1520 section entitled
1521 .\" HTML <a href="#studyingapattern">
1522 .\" </a>
1523 "Studying a pattern"
1524 .\"
1525 above). The format of the \fIstudy_data\fP block is private, but its length
1526 is made available via this option so that it can be saved and restored (see the
1527 .\" HREF
1528 \fBpcreprecompile\fP
1529 .\"
1530 documentation for details).
1531 .sp
1533 .sp
1534 Returns 1 if there is a rightmost literal data unit that must exist in any
1535 matched string, other than at its start. The fourth argument should point to
1536 an \fBint\fP variable. If there is no such value, 0 is returned. If returning
1537 1, the character value itself can be retrieved using PCRE_INFO_REQUIREDCHAR.
1538 .P
1539 For anchored patterns, a last literal value is recorded only if it follows
1540 something of variable length. For example, for the pattern /^a\ed+z\ed+/ the
1541 returned value 1 (with "z" returned from PCRE_INFO_REQUIREDCHAR), but for
1542 /^a\edz\ed/ the returned value is 0.
1543 .sp
1545 .sp
1546 Return the value of the rightmost literal data unit that must exist in any
1547 matched string, other than at its start, if such a value has been recorded. The
1548 fourth argument should point to an \fBuint32_t\fP variable. If there is no such
1549 value, 0 is returned.
1550 .
1551 .
1553 .rs
1554 .sp
1555 .B int pcre_refcount(pcre *\fIcode\fP, int \fIadjust\fP);
1556 .PP
1557 The \fBpcre_refcount()\fP function is used to maintain a reference count in the
1558 data block that contains a compiled pattern. It is provided for the benefit of
1559 applications that operate in an object-oriented manner, where different parts
1560 of the application may be using the same compiled pattern, but you want to free
1561 the block when they are all done.
1562 .P
1563 When a pattern is compiled, the reference count field is initialized to zero.
1564 It is changed only by calling this function, whose action is to add the
1565 \fIadjust\fP value (which may be positive or negative) to it. The yield of the
1566 function is the new value. However, the value of the count is constrained to
1567 lie between 0 and 65535, inclusive. If the new value is outside these limits,
1568 it is forced to the appropriate limit value.
1569 .P
1570 Except when it is zero, the reference count is not correctly preserved if a
1571 pattern is compiled on one host and then transferred to a host whose byte-order
1572 is different. (This seems a highly unlikely scenario.)
1573 .
1574 .
1576 .rs
1577 .sp
1578 .nf
1579 .B int pcre_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
1580 .B " const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
1581 .B " int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP);"
1582 .fi
1583 .P
1584 The function \fBpcre_exec()\fP is called to match a subject string against a
1585 compiled pattern, which is passed in the \fIcode\fP argument. If the
1586 pattern was studied, the result of the study should be passed in the
1587 \fIextra\fP argument. You can call \fBpcre_exec()\fP with the same \fIcode\fP
1588 and \fIextra\fP arguments as many times as you like, in order to match
1589 different subject strings with the same pattern.
1590 .P
1591 This function is the main matching facility of the library, and it operates in
1592 a Perl-like manner. For specialist use there is also an alternative matching
1593 function, which is described
1594 .\" HTML <a href="#dfamatch">
1595 .\" </a>
1596 below
1597 .\"
1598 in the section about the \fBpcre_dfa_exec()\fP function.
1599 .P
1600 In most applications, the pattern will have been compiled (and optionally
1601 studied) in the same process that calls \fBpcre_exec()\fP. However, it is
1602 possible to save compiled patterns and study data, and then use them later
1603 in different processes, possibly even on different hosts. For a discussion
1604 about this, see the
1605 .\" HREF
1606 \fBpcreprecompile\fP
1607 .\"
1608 documentation.
1609 .P
1610 Here is an example of a simple call to \fBpcre_exec()\fP:
1611 .sp
1612 int rc;
1613 int ovector[30];
1614 rc = pcre_exec(
1615 re, /* result of pcre_compile() */
1616 NULL, /* we didn't study the pattern */
1617 "some string", /* the subject string */
1618 11, /* the length of the subject string */
1619 0, /* start at offset 0 in the subject */
1620 0, /* default options */
1621 ovector, /* vector of integers for substring information */
1622 30); /* number of elements (NOT size in bytes) */
1623 .
1624 .
1625 .\" HTML <a name="extradata"></a>
1626 .SS "Extra data for \fBpcre_exec()\fR"
1627 .rs
1628 .sp
1629 If the \fIextra\fP argument is not NULL, it must point to a \fBpcre_extra\fP
1630 data block. The \fBpcre_study()\fP function returns such a block (when it
1631 doesn't return NULL), but you can also create one for yourself, and pass
1632 additional information in it. The \fBpcre_extra\fP block contains the following
1633 fields (not necessarily in this order):
1634 .sp
1635 unsigned long int \fIflags\fP;
1636 void *\fIstudy_data\fP;
1637 void *\fIexecutable_jit\fP;
1638 unsigned long int \fImatch_limit\fP;
1639 unsigned long int \fImatch_limit_recursion\fP;
1640 void *\fIcallout_data\fP;
1641 const unsigned char *\fItables\fP;
1642 unsigned char **\fImark\fP;
1643 .sp
1644 In the 16-bit version of this structure, the \fImark\fP field has type
1645 "PCRE_UCHAR16 **".
1646 .sp
1647 In the 32-bit version of this structure, the \fImark\fP field has type
1648 "PCRE_UCHAR32 **".
1649 .P
1650 The \fIflags\fP field is used to specify which of the other fields are set. The
1651 flag bits are:
1652 .sp
1660 .sp
1661 Other flag bits should be set to zero. The \fIstudy_data\fP field and sometimes
1662 the \fIexecutable_jit\fP field are set in the \fBpcre_extra\fP block that is
1663 returned by \fBpcre_study()\fP, together with the appropriate flag bits. You
1664 should not set these yourself, but you may add to the block by setting other
1665 fields and their corresponding flag bits.
1666 .P
1667 The \fImatch_limit\fP field provides a means of preventing PCRE from using up a
1668 vast amount of resources when running patterns that are not going to match,
1669 but which have a very large number of possibilities in their search trees. The
1670 classic example is a pattern that uses nested unlimited repeats.
1671 .P
1672 Internally, \fBpcre_exec()\fP uses a function called \fBmatch()\fP, which it
1673 calls repeatedly (sometimes recursively). The limit set by \fImatch_limit\fP is
1674 imposed on the number of times this function is called during a match, which
1675 has the effect of limiting the amount of backtracking that can take place. For
1676 patterns that are not anchored, the count restarts from zero for each position
1677 in the subject string.
1678 .P
1679 When \fBpcre_exec()\fP is called with a pattern that was successfully studied
1680 with a JIT option, the way that the matching is executed is entirely different.
1681 However, there is still the possibility of runaway matching that goes on for a
1682 very long time, and so the \fImatch_limit\fP value is also used in this case
1683 (but in a different way) to limit how long the matching can continue.
1684 .P
1685 The default value for the limit can be set when PCRE is built; the default
1686 default is 10 million, which handles all but the most extreme cases. You can
1687 override the default by suppling \fBpcre_exec()\fP with a \fBpcre_extra\fP
1688 block in which \fImatch_limit\fP is set, and PCRE_EXTRA_MATCH_LIMIT is set in
1689 the \fIflags\fP field. If the limit is exceeded, \fBpcre_exec()\fP returns
1691 .P
1692 A value for the match limit may also be supplied by an item at the start of a
1693 pattern of the form
1694 .sp
1695 (*LIMIT_MATCH=d)
1696 .sp
1697 where d is a decimal number. However, such a setting is ignored unless d is
1698 less than the limit set by the caller of \fBpcre_exec()\fP or, if no such limit
1699 is set, less than the default.
1700 .P
1701 The \fImatch_limit_recursion\fP field is similar to \fImatch_limit\fP, but
1702 instead of limiting the total number of times that \fBmatch()\fP is called, it
1703 limits the depth of recursion. The recursion depth is a smaller number than the
1704 total number of calls, because not all calls to \fBmatch()\fP are recursive.
1705 This limit is of use only if it is set smaller than \fImatch_limit\fP.
1706 .P
1707 Limiting the recursion depth limits the amount of machine stack that can be
1708 used, or, when PCRE has been compiled to use memory on the heap instead of the
1709 stack, the amount of heap memory that can be used. This limit is not relevant,
1710 and is ignored, when matching is done using JIT compiled code.
1711 .P
1712 The default value for \fImatch_limit_recursion\fP can be set when PCRE is
1713 built; the default default is the same value as the default for
1714 \fImatch_limit\fP. You can override the default by suppling \fBpcre_exec()\fP
1715 with a \fBpcre_extra\fP block in which \fImatch_limit_recursion\fP is set, and
1716 PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the \fIflags\fP field. If the limit
1717 is exceeded, \fBpcre_exec()\fP returns PCRE_ERROR_RECURSIONLIMIT.
1718 .P
1719 A value for the recursion limit may also be supplied by an item at the start of
1720 a pattern of the form
1721 .sp
1723 .sp
1724 where d is a decimal number. However, such a setting is ignored unless d is
1725 less than the limit set by the caller of \fBpcre_exec()\fP or, if no such limit
1726 is set, less than the default.
1727 .P
1728 The \fIcallout_data\fP field is used in conjunction with the "callout" feature,
1729 and is described in the
1730 .\" HREF
1731 \fBpcrecallout\fP
1732 .\"
1733 documentation.
1734 .P
1735 The \fItables\fP field is provided for use with patterns that have been
1736 pre-compiled using custom character tables, saved to disc or elsewhere, and
1737 then reloaded, because the tables that were used to compile a pattern are not
1738 saved with it. See the
1739 .\" HREF
1740 \fBpcreprecompile\fP
1741 .\"
1742 documentation for a discussion of saving compiled patterns for later use. If
1743 NULL is passed using this mechanism, it forces PCRE's internal tables to be
1744 used.
1745 .P
1746 \fBWarning:\fP The tables that \fBpcre_exec()\fP uses must be the same as those
1747 that were used when the pattern was compiled. If this is not the case, the
1748 behaviour of \fBpcre_exec()\fP is undefined. Therefore, when a pattern is
1749 compiled and matched in the same process, this field should never be set. In
1750 this (the most common) case, the correct table pointer is automatically passed
1751 with the compiled pattern from \fBpcre_compile()\fP to \fBpcre_exec()\fP.
1752 .P
1753 If PCRE_EXTRA_MARK is set in the \fIflags\fP field, the \fImark\fP field must
1754 be set to point to a suitable variable. If the pattern contains any
1755 backtracking control verbs such as (*MARK:NAME), and the execution ends up with
1756 a name to pass back, a pointer to the name string (zero terminated) is placed
1757 in the variable pointed to by the \fImark\fP field. The names are within the
1758 compiled pattern; if you wish to retain such a name you must copy it before
1759 freeing the memory of a compiled pattern. If there is no name to pass back, the
1760 variable pointed to by the \fImark\fP field is set to NULL. For details of the
1761 backtracking control verbs, see the section entitled
1762 .\" HTML <a href="pcrepattern#backtrackcontrol">
1763 .\" </a>
1764 "Backtracking control"
1765 .\"
1766 in the
1767 .\" HREF
1768 \fBpcrepattern\fP
1769 .\"
1770 documentation.
1771 .
1772 .
1773 .\" HTML <a name="execoptions"></a>
1774 .SS "Option bits for \fBpcre_exec()\fP"
1775 .rs
1776 .sp
1777 The unused bits of the \fIoptions\fP argument for \fBpcre_exec()\fP must be
1778 zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_\fIxxx\fP,
1782 .P
1783 If the pattern was successfully studied with one of the just-in-time (JIT)
1784 compile options, the only supported options for JIT execution are
1787 unsupported option is used, JIT execution is disabled and the normal
1788 interpretive code in \fBpcre_exec()\fP is run.
1789 .sp
1791 .sp
1792 The PCRE_ANCHORED option limits \fBpcre_exec()\fP to matching at the first
1793 matching position. If a pattern was compiled with PCRE_ANCHORED, or turned out
1794 to be anchored by virtue of its contents, it cannot be made unachored at
1795 matching time.
1796 .sp
1799 .sp
1800 These options (which are mutually exclusive) control what the \eR escape
1801 sequence matches. The choice is either to match only CR, LF, or CRLF, or to
1802 match any Unicode newline sequence. These options override the choice that was
1803 made or defaulted when the pattern was compiled.
1804 .sp
1810 .sp
1811 These options override the newline definition that was chosen or defaulted when
1812 the pattern was compiled. For details, see the description of
1813 \fBpcre_compile()\fP above. During matching, the newline choice affects the
1814 behaviour of the dot, circumflex, and dollar metacharacters. It may also alter
1815 the way the match position is advanced after a match failure for an unanchored
1816 pattern.
1817 .P
1819 match attempt for an unanchored pattern fails when the current position is at a
1820 CRLF sequence, and the pattern contains no explicit matches for CR or LF
1821 characters, the match position is advanced by two characters instead of one, in
1822 other words, to after the CRLF.
1823 .P
1824 The above rule is a compromise that makes the most common cases work as
1825 expected. For example, if the pattern is .+A (and the PCRE_DOTALL option is not
1826 set), it does not match the string "\er\enA" because, after failing at the
1827 start, it skips both the CR and the LF before retrying. However, the pattern
1828 [\er\en]A does match that string, because it contains an explicit CR or LF
1829 reference, and so advances only by one character after the first failure.
1830 .P
1831 An explicit match for CR of LF is either a literal appearance of one of those
1832 characters, or one of the \er or \en escape sequences. Implicit matches such as
1833 [^X] do not count, nor does \es (which includes CR and LF in the characters
1834 that it matches).
1835 .P
1836 Notwithstanding the above, anomalous effects may still occur when CRLF is a
1837 valid newline sequence and explicit \er or \en escapes appear in the pattern.
1838 .sp
1840 .sp
1841 This option specifies that first character of the subject string is not the
1842 beginning of a line, so the circumflex metacharacter should not match before
1843 it. Setting this without PCRE_MULTILINE (at compile time) causes circumflex
1844 never to match. This option affects only the behaviour of the circumflex
1845 metacharacter. It does not affect \eA.
1846 .sp
1848 .sp
1849 This option specifies that the end of the subject string is not the end of a
1850 line, so the dollar metacharacter should not match it nor (except in multiline
1851 mode) a newline immediately before it. Setting this without PCRE_MULTILINE (at
1852 compile time) causes dollar never to match. This option affects only the
1853 behaviour of the dollar metacharacter. It does not affect \eZ or \ez.
1854 .sp
1856 .sp
1857 An empty string is not considered to be a valid match if this option is set. If
1858 there are alternatives in the pattern, they are tried. If all the alternatives
1859 match the empty string, the entire match fails. For example, if the pattern
1860 .sp
1861 a?b?
1862 .sp
1863 is applied to a string not beginning with "a" or "b", it matches an empty
1864 string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
1865 valid, so PCRE searches further into the string for occurrences of "a" or "b".
1866 .sp
1868 .sp
1869 This is like PCRE_NOTEMPTY, except that an empty string match that is not at
1870 the start of the subject is permitted. If the pattern is anchored, such a match
1871 can occur only if the pattern contains \eK.
1872 .P
1873 Perl has no direct equivalent of PCRE_NOTEMPTY or PCRE_NOTEMPTY_ATSTART, but it
1874 does make a special case of a pattern match of the empty string within its
1875 \fBsplit()\fP function, and when using the /g modifier. It is possible to
1876 emulate Perl's behaviour after matching a null string by first trying the match
1877 again at the same offset with PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED, and then
1878 if that fails, by advancing the starting offset (see below) and trying an
1879 ordinary match again. There is some code that demonstrates how to do this in
1880 the
1881 .\" HREF
1882 \fBpcredemo\fP
1883 .\"
1884 sample program. In the most general case, you have to check to see if the
1885 newline convention recognizes CRLF as a newline, and if so, and the current
1886 character is CR followed by LF, advance the starting offset by two characters
1887 instead of one.
1888 .sp
1890 .sp
1891 There are a number of optimizations that \fBpcre_exec()\fP uses at the start of
1892 a match, in order to speed up the process. For example, if it is known that an
1893 unanchored match must start with a specific character, it searches the subject
1894 for that character, and fails immediately if it cannot find it, without
1895 actually running the main matching function. This means that a special item
1896 such as (*COMMIT) at the start of a pattern is not considered until after a
1897 suitable starting point for the match has been found. Also, when callouts or
1898 (*MARK) items are in use, these "start-up" optimizations can cause them to be
1899 skipped if the pattern is never actually used. The start-up optimizations are
1900 in effect a pre-scan of the subject that takes place before the pattern is run.
1901 .P
1902 The PCRE_NO_START_OPTIMIZE option disables the start-up optimizations, possibly
1903 causing performance to suffer, but ensuring that in cases where the result is
1904 "no match", the callouts do occur, and that items such as (*COMMIT) and (*MARK)
1905 are considered at every possible starting position in the subject string. If
1906 PCRE_NO_START_OPTIMIZE is set at compile time, it cannot be unset at matching
1907 time. The use of PCRE_NO_START_OPTIMIZE at matching time (that is, passing it
1908 to \fBpcre_exec()\fP) disables JIT execution; in this situation, matching is
1909 always done using interpretively.
1910 .P
1911 Setting PCRE_NO_START_OPTIMIZE can change the outcome of a matching operation.
1912 Consider the pattern
1913 .sp
1915 .sp
1916 When this is compiled, PCRE records the fact that a match must start with the
1917 character "A". Suppose the subject string is "DEFABC". The start-up
1918 optimization scans along the subject, finds "A" and runs the first match
1919 attempt from there. The (*COMMIT) item means that the pattern must match the
1920 current starting position, which in this case, it does. However, if the same
1921 match is run with PCRE_NO_START_OPTIMIZE set, the initial scan along the
1922 subject string does not happen. The first match attempt is run starting from
1923 "D" and when this fails, (*COMMIT) prevents any further matches being tried, so
1924 the overall result is "no match". If the pattern is studied, more start-up
1925 optimizations may be used. For example, a minimum length for the subject may be
1926 recorded. Consider the pattern
1927 .sp
1928 (*MARK:A)(X|Y)
1929 .sp
1930 The minimum length for a match is one character. If the subject is "ABC", there
1931 will be attempts to match "ABC", "BC", "C", and then finally an empty string.
1932 If the pattern is studied, the final attempt does not take place, because PCRE
1933 knows that the subject is too short, and so the (*MARK) is never encountered.
1934 In this case, studying the pattern does not affect the overall match result,
1935 which is still "no match", but it does affect the auxiliary information that is
1936 returned.
1937 .sp
1939 .sp
1940 When PCRE_UTF8 is set at compile time, the validity of the subject as a UTF-8
1941 string is automatically checked when \fBpcre_exec()\fP is subsequently called.
1942 The entire string is checked before any other processing takes place. The value
1943 of \fIstartoffset\fP is also checked to ensure that it points to the start of a
1944 UTF-8 character. There is a discussion about the
1945 .\" HTML <a href="pcreunicode.html#utf8strings">
1946 .\" </a>
1947 validity of UTF-8 strings
1948 .\"
1949 in the
1950 .\" HREF
1951 \fBpcreunicode\fP
1952 .\"
1953 page. If an invalid sequence of bytes is found, \fBpcre_exec()\fP returns the
1954 error PCRE_ERROR_BADUTF8 or, if PCRE_PARTIAL_HARD is set and the problem is a
1955 truncated character at the end of the subject, PCRE_ERROR_SHORTUTF8. In both
1956 cases, information about the precise nature of the error may also be returned
1957 (see the descriptions of these errors in the section entitled \fIError return
1958 values from\fP \fBpcre_exec()\fP
1959 .\" HTML <a href="#errorlist">
1960 .\" </a>
1961 below).
1962 .\"
1963 If \fIstartoffset\fP contains a value that does not point to the start of a
1964 UTF-8 character (or to the end of the subject), PCRE_ERROR_BADUTF8_OFFSET is
1965 returned.
1966 .P
1967 If you already know that your subject is valid, and you want to skip these
1968 checks for performance reasons, you can set the PCRE_NO_UTF8_CHECK option when
1969 calling \fBpcre_exec()\fP. You might want to do this for the second and
1970 subsequent calls to \fBpcre_exec()\fP if you are making repeated calls to find
1971 all the matches in a single subject string. However, you should be sure that
1972 the value of \fIstartoffset\fP points to the start of a character (or the end
1973 of the subject). When PCRE_NO_UTF8_CHECK is set, the effect of passing an
1974 invalid string as a subject or an invalid value of \fIstartoffset\fP is
1975 undefined. Your program may crash or loop.
1976 .sp
1979 .sp
1980 These options turn on the partial matching feature. For backwards
1981 compatibility, PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. A partial match
1982 occurs if the end of the subject string is reached successfully, but there are
1983 not enough subject characters to complete the match. If this happens when
1984 PCRE_PARTIAL_SOFT (but not PCRE_PARTIAL_HARD) is set, matching continues by
1985 testing any remaining alternatives. Only if no complete match can be found is
1986 PCRE_ERROR_PARTIAL returned instead of PCRE_ERROR_NOMATCH. In other words,
1987 PCRE_PARTIAL_SOFT says that the caller is prepared to handle a partial match,
1988 but only if no complete match can be found.
1989 .P
1990 If PCRE_PARTIAL_HARD is set, it overrides PCRE_PARTIAL_SOFT. In this case, if a
1991 partial match is found, \fBpcre_exec()\fP immediately returns
1992 PCRE_ERROR_PARTIAL, without considering any other alternatives. In other words,
1993 when PCRE_PARTIAL_HARD is set, a partial match is considered to be more
1994 important that an alternative complete match.
1995 .P
1996 In both cases, the portion of the string that was inspected when the partial
1997 match was found is set as the first matching string. There is a more detailed
1998 discussion of partial and multi-segment matching, with examples, in the
1999 .\" HREF
2000 \fBpcrepartial\fP
2001 .\"
2002 documentation.
2003 .
2004 .
2005 .SS "The string to be matched by \fBpcre_exec()\fP"
2006 .rs
2007 .sp
2008 The subject string is passed to \fBpcre_exec()\fP as a pointer in
2009 \fIsubject\fP, a length in \fIlength\fP, and a starting offset in
2010 \fIstartoffset\fP. The units for \fIlength\fP and \fIstartoffset\fP are bytes
2011 for the 8-bit library, 16-bit data items for the 16-bit library, and 32-bit
2012 data items for the 32-bit library.
2013 .P
2014 If \fIstartoffset\fP is negative or greater than the length of the subject,
2015 \fBpcre_exec()\fP returns PCRE_ERROR_BADOFFSET. When the starting offset is
2016 zero, the search for a match starts at the beginning of the subject, and this
2017 is by far the most common case. In UTF-8 or UTF-16 mode, the offset must point
2018 to the start of a character, or the end of the subject (in UTF-32 mode, one
2019 data unit equals one character, so all offsets are valid). Unlike the pattern
2020 string, the subject may contain binary zeroes.
2021 .P
2022 A non-zero starting offset is useful when searching for another match in the
2023 same subject by calling \fBpcre_exec()\fP again after a previous success.
2024 Setting \fIstartoffset\fP differs from just passing over a shortened string and
2025 setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
2026 lookbehind. For example, consider the pattern
2027 .sp
2028 \eBiss\eB
2029 .sp
2030 which finds occurrences of "iss" in the middle of words. (\eB matches only if
2031 the current position in the subject is not a word boundary.) When applied to
2032 the string "Mississipi" the first call to \fBpcre_exec()\fP finds the first
2033 occurrence. If \fBpcre_exec()\fP is called again with just the remainder of the
2034 subject, namely "issipi", it does not match, because \eB is always false at the
2035 start of the subject, which is deemed to be a word boundary. However, if
2036 \fBpcre_exec()\fP is passed the entire string again, but with \fIstartoffset\fP
2037 set to 4, it finds the second occurrence of "iss" because it is able to look
2038 behind the starting point to discover that it is preceded by a letter.
2039 .P
2040 Finding all the matches in a subject is tricky when the pattern can match an
2041 empty string. It is possible to emulate Perl's /g behaviour by first trying the
2042 match again at the same offset, with the PCRE_NOTEMPTY_ATSTART and
2043 PCRE_ANCHORED options, and then if that fails, advancing the starting offset
2044 and trying an ordinary match again. There is some code that demonstrates how to
2045 do this in the
2046 .\" HREF
2047 \fBpcredemo\fP
2048 .\"
2049 sample program. In the most general case, you have to check to see if the
2050 newline convention recognizes CRLF as a newline, and if so, and the current
2051 character is CR followed by LF, advance the starting offset by two characters
2052 instead of one.
2053 .P
2054 If a non-zero starting offset is passed when the pattern is anchored, one
2055 attempt to match at the given offset is made. This can only succeed if the
2056 pattern does not require the match to be at the start of the subject.
2057 .
2058 .
2059 .SS "How \fBpcre_exec()\fP returns captured substrings"
2060 .rs
2061 .sp
2062 In general, a pattern matches a certain portion of the subject, and in
2063 addition, further substrings from the subject may be picked out by parts of the
2064 pattern. Following the usage in Jeffrey Friedl's book, this is called
2065 "capturing" in what follows, and the phrase "capturing subpattern" is used for
2066 a fragment of a pattern that picks out a substring. PCRE supports several other
2067 kinds of parenthesized subpattern that do not cause substrings to be captured.
2068 .P
2069 Captured substrings are returned to the caller via a vector of integers whose
2070 address is passed in \fIovector\fP. The number of elements in the vector is
2071 passed in \fIovecsize\fP, which must be a non-negative number. \fBNote\fP: this
2072 argument is NOT the size of \fIovector\fP in bytes.
2073 .P
2074 The first two-thirds of the vector is used to pass back captured substrings,
2075 each substring using a pair of integers. The remaining third of the vector is
2076 used as workspace by \fBpcre_exec()\fP while matching capturing subpatterns,
2077 and is not available for passing back information. The number passed in
2078 \fIovecsize\fP should always be a multiple of three. If it is not, it is
2079 rounded down.
2080 .P
2081 When a match is successful, information about captured substrings is returned
2082 in pairs of integers, starting at the beginning of \fIovector\fP, and
2083 continuing up to two-thirds of its length at the most. The first element of
2084 each pair is set to the offset of the first character in a substring, and the
2085 second is set to the offset of the first character after the end of a
2086 substring. These values are always data unit offsets, even in UTF mode. They
2087 are byte offsets in the 8-bit library, 16-bit data item offsets in the 16-bit
2088 library, and 32-bit data item offsets in the 32-bit library. \fBNote\fP: they
2089 are not character counts.
2090 .P
2091 The first pair of integers, \fIovector[0]\fP and \fIovector[1]\fP, identify the
2092 portion of the subject string matched by the entire pattern. The next pair is
2093 used for the first capturing subpattern, and so on. The value returned by
2094 \fBpcre_exec()\fP is one more than the highest numbered pair that has been set.
2095 For example, if two substrings have been captured, the returned value is 3. If
2096 there are no capturing subpatterns, the return value from a successful match is
2097 1, indicating that just the first pair of offsets has been set.
2098 .P
2099 If a capturing subpattern is matched repeatedly, it is the last portion of the
2100 string that it matched that is returned.
2101 .P
2102 If the vector is too small to hold all the captured substring offsets, it is
2103 used as far as possible (up to two-thirds of its length), and the function
2104 returns a value of zero. If neither the actual string matched nor any captured
2105 substrings are of interest, \fBpcre_exec()\fP may be called with \fIovector\fP
2106 passed as NULL and \fIovecsize\fP as zero. However, if the pattern contains
2107 back references and the \fIovector\fP is not big enough to remember the related
2108 substrings, PCRE has to get additional memory for use during matching. Thus it
2109 is usually advisable to supply an \fIovector\fP of reasonable size.
2110 .P
2111 There are some cases where zero is returned (indicating vector overflow) when
2112 in fact the vector is exactly the right size for the final match. For example,
2113 consider the pattern
2114 .sp
2115 (a)(?:(b)c|bd)
2116 .sp
2117 If a vector of 6 elements (allowing for only 1 captured substring) is given
2118 with subject string "abd", \fBpcre_exec()\fP will try to set the second
2119 captured string, thereby recording a vector overflow, before failing to match
2120 "c" and backing up to try the second alternative. The zero return, however,
2121 does correctly indicate that the maximum number of slots (namely 2) have been
2122 filled. In similar cases where there is temporary overflow, but the final
2123 number of used slots is actually less than the maximum, a non-zero value is
2124 returned.
2125 .P
2126 The \fBpcre_fullinfo()\fP function can be used to find out how many capturing
2127 subpatterns there are in a compiled pattern. The smallest size for
2128 \fIovector\fP that will allow for \fIn\fP captured substrings, in addition to
2129 the offsets of the substring matched by the whole pattern, is (\fIn\fP+1)*3.
2130 .P
2131 It is possible for capturing subpattern number \fIn+1\fP to match some part of
2132 the subject when subpattern \fIn\fP has not been used at all. For example, if
2133 the string "abc" is matched against the pattern (a|(z))(bc) the return from the
2134 function is 4, and subpatterns 1 and 3 are matched, but 2 is not. When this
2135 happens, both values in the offset pairs corresponding to unused subpatterns
2136 are set to -1.
2137 .P
2138 Offset values that correspond to unused subpatterns at the end of the
2139 expression are also set to -1. For example, if the string "abc" is matched
2140 against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not matched. The
2141 return from the function is 2, because the highest used capturing subpattern
2142 number is 1, and the offsets for for the second and third capturing subpatterns
2143 (assuming the vector is large enough, of course) are set to -1.
2144 .P
2145 \fBNote\fP: Elements in the first two-thirds of \fIovector\fP that do not
2146 correspond to capturing parentheses in the pattern are never changed. That is,
2147 if a pattern contains \fIn\fP capturing parentheses, no more than
2148 \fIovector[0]\fP to \fIovector[2n+1]\fP are set by \fBpcre_exec()\fP. The other
2149 elements (in the first two-thirds) retain whatever values they previously had.
2150 .P
2151 Some convenience functions are provided for extracting the captured substrings
2152 as separate strings. These are described below.
2153 .
2154 .
2155 .\" HTML <a name="errorlist"></a>
2156 .SS "Error return values from \fBpcre_exec()\fP"
2157 .rs
2158 .sp
2159 If \fBpcre_exec()\fP fails, it returns a negative number. The following are
2160 defined in the header file:
2161 .sp
2163 .sp
2164 The subject string did not match the pattern.
2165 .sp
2167 .sp
2168 Either \fIcode\fP or \fIsubject\fP was passed as NULL, or \fIovector\fP was
2169 NULL and \fIovecsize\fP was not zero.
2170 .sp
2172 .sp
2173 An unrecognized bit was set in the \fIoptions\fP argument.
2174 .sp
2176 .sp
2177 PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
2178 the case when it is passed a junk pointer and to detect when a pattern that was
2179 compiled in an environment of one endianness is run in an environment with the
2180 other endianness. This is the error that PCRE gives when the magic number is
2181 not present.
2182 .sp
2184 .sp
2185 While running the pattern match, an unknown item was encountered in the
2186 compiled pattern. This error could be caused by a bug in PCRE or by overwriting
2187 of the compiled pattern.
2188 .sp
2190 .sp
2191 If a pattern contains back references, but the \fIovector\fP that is passed to
2192 \fBpcre_exec()\fP is not big enough to remember the referenced substrings, PCRE
2193 gets a block of memory at the start of matching to use for this purpose. If the
2194 call via \fBpcre_malloc()\fP fails, this error is given. The memory is
2195 automatically freed at the end of matching.
2196 .P
2197 This error is also given if \fBpcre_stack_malloc()\fP fails in
2198 \fBpcre_exec()\fP. This can happen only when PCRE has been compiled with
2199 \fB--disable-stack-for-recursion\fP.
2200 .sp
2202 .sp
2203 This error is used by the \fBpcre_copy_substring()\fP,
2204 \fBpcre_get_substring()\fP, and \fBpcre_get_substring_list()\fP functions (see
2205 below). It is never returned by \fBpcre_exec()\fP.
2206 .sp
2208 .sp
2209 The backtracking limit, as specified by the \fImatch_limit\fP field in a
2210 \fBpcre_extra\fP structure (or defaulted) was reached. See the description
2211 above.
2212 .sp
2214 .sp
2215 This error is never generated by \fBpcre_exec()\fP itself. It is provided for
2216 use by callout functions that want to yield a distinctive error code. See the
2217 .\" HREF
2218 \fBpcrecallout\fP
2219 .\"
2220 documentation for details.
2221 .sp
2223 .sp
2224 A string that contains an invalid UTF-8 byte sequence was passed as a subject,
2225 and the PCRE_NO_UTF8_CHECK option was not set. If the size of the output vector
2226 (\fIovecsize\fP) is at least 2, the byte offset to the start of the the invalid
2227 UTF-8 character is placed in the first element, and a reason code is placed in
2228 the second element. The reason codes are listed in the
2229 .\" HTML <a href="#badutf8reasons">
2230 .\" </a>
2231 following section.
2232 .\"
2233 For backward compatibility, if PCRE_PARTIAL_HARD is set and the problem is a
2234 truncated UTF-8 character at the end of the subject (reason codes 1 to 5),
2235 PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8.
2236 .sp
2238 .sp
2239 The UTF-8 byte sequence that was passed as a subject was checked and found to
2240 be valid (the PCRE_NO_UTF8_CHECK option was not set), but the value of
2241 \fIstartoffset\fP did not point to the beginning of a UTF-8 character or the
2242 end of the subject.
2243 .sp
2245 .sp
2246 The subject string did not match, but it did match partially. See the
2247 .\" HREF
2248 \fBpcrepartial\fP
2249 .\"
2250 documentation for details of partial matching.
2251 .sp
2253 .sp
2254 This code is no longer in use. It was formerly returned when the PCRE_PARTIAL
2255 option was used with a compiled pattern containing items that were not
2256 supported for partial matching. From release 8.00 onwards, there are no
2257 restrictions on partial matching.
2258 .sp
2260 .sp
2261 An unexpected internal error has occurred. This error could be caused by a bug
2262 in PCRE or by overwriting of the compiled pattern.
2263 .sp
2265 .sp
2266 This error is given if the value of the \fIovecsize\fP argument is negative.
2267 .sp
2269 .sp
2270 The internal recursion limit, as specified by the \fImatch_limit_recursion\fP
2271 field in a \fBpcre_extra\fP structure (or defaulted) was reached. See the
2272 description above.
2273 .sp
2275 .sp
2276 An invalid combination of PCRE_NEWLINE_\fIxxx\fP options was given.
2277 .sp
2279 .sp
2280 The value of \fIstartoffset\fP was negative or greater than the length of the
2281 subject, that is, the value in \fIlength\fP.
2282 .sp
2284 .sp
2285 This error is returned instead of PCRE_ERROR_BADUTF8 when the subject string
2286 ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD option is set.
2287 Information about the failure is returned as for PCRE_ERROR_BADUTF8. It is in
2288 fact sufficient to detect this case, but this special error code for
2289 PCRE_PARTIAL_HARD precedes the implementation of returned information; it is
2290 retained for backwards compatibility.
2291 .sp
2293 .sp
2294 This error is returned when \fBpcre_exec()\fP detects a recursion loop within
2295 the pattern. Specifically, it means that either the whole pattern or a
2296 subpattern has been called recursively for the second time at the same position
2297 in the subject string. Some simple patterns that might do this are detected and
2298 faulted at compile time, but more complicated cases, in particular mutual
2299 recursions between two different subpatterns, cannot be detected until run
2300 time.
2301 .sp
2303 .sp
2304 This error is returned when a pattern that was successfully studied using a
2305 JIT compile option is being matched, but the memory available for the
2306 just-in-time processing stack is not large enough. See the
2307 .\" HREF
2308 \fBpcrejit\fP
2309 .\"
2310 documentation for more details.
2311 .sp
2313 .sp
2314 This error is given if a pattern that was compiled by the 8-bit library is
2315 passed to a 16-bit or 32-bit library function, or vice versa.
2316 .sp
2318 .sp
2319 This error is given if a pattern that was compiled and saved is reloaded on a
2320 host with different endianness. The utility function
2321 \fBpcre_pattern_to_host_byte_order()\fP can be used to convert such a pattern
2322 so that it runs on the new host.
2323 .sp
2325 .sp
2326 This error is returned when a pattern that was successfully studied using a JIT
2327 compile option is being matched, but the matching mode (partial or complete
2328 match) does not correspond to any JIT compilation mode. When the JIT fast path
2329 function is used, this error may be also given for invalid options. See the
2330 .\" HREF
2331 \fBpcrejit\fP
2332 .\"
2333 documentation for more details.
2334 .sp
2336 .sp
2337 This error is given if \fBpcre_exec()\fP is called with a negative value for
2338 the \fIlength\fP argument.
2339 .P
2340 Error numbers -16 to -20, -22, and 30 are not used by \fBpcre_exec()\fP.
2341 .
2342 .
2343 .\" HTML <a name="badutf8reasons"></a>
2344 .SS "Reason codes for invalid UTF-8 strings"
2345 .rs
2346 .sp
2347 This section applies only to the 8-bit library. The corresponding information
2348 for the 16-bit and 32-bit libraries is given in the
2349 .\" HREF
2350 \fBpcre16\fP
2351 .\"
2352 and
2353 .\" HREF
2354 \fBpcre32\fP
2355 .\"
2356 pages.
2357 .P
2358 When \fBpcre_exec()\fP returns either PCRE_ERROR_BADUTF8 or
2359 PCRE_ERROR_SHORTUTF8, and the size of the output vector (\fIovecsize\fP) is at
2360 least 2, the offset of the start of the invalid UTF-8 character is placed in
2361 the first output vector element (\fIovector[0]\fP) and a reason code is placed
2362 in the second element (\fIovector[1]\fP). The reason codes are given names in
2363 the \fBpcre.h\fP header file:
2364 .sp
2370 .sp
2371 The string ends with a truncated UTF-8 character; the code specifies how many
2372 bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8 characters to be
2373 no longer than 4 bytes, the encoding scheme (originally defined by RFC 2279)
2374 allows for up to 6 bytes, and this is checked first; hence the possibility of
2375 4 or 5 missing bytes.
2376 .sp
2381 PCRE_UTF8_ERR10
2382 .sp
2383 The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of the
2384 character do not have the binary value 0b10 (that is, either the most
2385 significant bit is 0, or the next bit is 1).
2386 .sp
2387 PCRE_UTF8_ERR11
2388 PCRE_UTF8_ERR12
2389 .sp
2390 A character that is valid by the RFC 2279 rules is either 5 or 6 bytes long;
2391 these code points are excluded by RFC 3629.
2392 .sp
2393 PCRE_UTF8_ERR13
2394 .sp
2395 A 4-byte character has a value greater than 0x10fff; these code points are
2396 excluded by RFC 3629.
2397 .sp
2398 PCRE_UTF8_ERR14
2399 .sp
2400 A 3-byte character has a value in the range 0xd800 to 0xdfff; this range of
2401 code points are reserved by RFC 3629 for use with UTF-16, and so are excluded
2402 from UTF-8.
2403 .sp
2404 PCRE_UTF8_ERR15
2405 PCRE_UTF8_ERR16
2406 PCRE_UTF8_ERR17
2407 PCRE_UTF8_ERR18
2408 PCRE_UTF8_ERR19
2409 .sp
2410 A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes for a
2411 value that can be represented by fewer bytes, which is invalid. For example,
2412 the two bytes 0xc0, 0xae give the value 0x2e, whose correct coding uses just
2413 one byte.
2414 .sp
2415 PCRE_UTF8_ERR20
2416 .sp
2417 The two most significant bits of the first byte of a character have the binary
2418 value 0b10 (that is, the most significant bit is 1 and the second is 0). Such a
2419 byte can only validly occur as the second or subsequent byte of a multi-byte
2420 character.
2421 .sp
2422 PCRE_UTF8_ERR21
2423 .sp
2424 The first byte of a character has the value 0xfe or 0xff. These values can
2425 never occur in a valid UTF-8 string.
2426 .sp
2427 PCRE_UTF8_ERR22
2428 .sp
2429 This error code was formerly used when the presence of a so-called
2430 "non-character" caused an error. Unicode corrigendum #9 makes it clear that
2431 such characters should not cause a string to be rejected, and so this code is
2432 no longer in use and is never returned.
2433 .
2434 .
2436 .rs
2437 .sp
2438 .nf
2439 .B int pcre_copy_substring(const char *\fIsubject\fP, int *\fIovector\fP,
2440 .B " int \fIstringcount\fP, int \fIstringnumber\fP, char *\fIbuffer\fP,"
2441 .B " int \fIbuffersize\fP);"
2442 .sp
2443 .B int pcre_get_substring(const char *\fIsubject\fP, int *\fIovector\fP,
2444 .B " int \fIstringcount\fP, int \fIstringnumber\fP,"
2445 .B " const char **\fIstringptr\fP);"
2446 .sp
2447 .B int pcre_get_substring_list(const char *\fIsubject\fP,
2448 .B " int *\fIovector\fP, int \fIstringcount\fP, const char ***\fIlistptr\fP);"
2449 .fi
2450 .PP
2451 Captured substrings can be accessed directly by using the offsets returned by
2452 \fBpcre_exec()\fP in \fIovector\fP. For convenience, the functions
2453 \fBpcre_copy_substring()\fP, \fBpcre_get_substring()\fP, and
2454 \fBpcre_get_substring_list()\fP are provided for extracting captured substrings
2455 as new, separate, zero-terminated strings. These functions identify substrings
2456 by number. The next section describes functions for extracting named
2457 substrings.
2458 .P
2459 A substring that contains a binary zero is correctly extracted and has a
2460 further zero added on the end, but the result is not, of course, a C string.
2461 However, you can process such a string by referring to the length that is
2462 returned by \fBpcre_copy_substring()\fP and \fBpcre_get_substring()\fP.
2463 Unfortunately, the interface to \fBpcre_get_substring_list()\fP is not adequate
2464 for handling strings containing binary zeros, because the end of the final
2465 string is not independently indicated.
2466 .P
2467 The first three arguments are the same for all three of these functions:
2468 \fIsubject\fP is the subject string that has just been successfully matched,
2469 \fIovector\fP is a pointer to the vector of integer offsets that was passed to
2470 \fBpcre_exec()\fP, and \fIstringcount\fP is the number of substrings that were
2471 captured by the match, including the substring that matched the entire regular
2472 expression. This is the value returned by \fBpcre_exec()\fP if it is greater
2473 than zero. If \fBpcre_exec()\fP returned zero, indicating that it ran out of
2474 space in \fIovector\fP, the value passed as \fIstringcount\fP should be the
2475 number of elements in the vector divided by three.
2476 .P
2477 The functions \fBpcre_copy_substring()\fP and \fBpcre_get_substring()\fP
2478 extract a single substring, whose number is given as \fIstringnumber\fP. A
2479 value of zero extracts the substring that matched the entire pattern, whereas
2480 higher values extract the captured substrings. For \fBpcre_copy_substring()\fP,
2481 the string is placed in \fIbuffer\fP, whose length is given by
2482 \fIbuffersize\fP, while for \fBpcre_get_substring()\fP a new block of memory is
2483 obtained via \fBpcre_malloc\fP, and its address is returned via
2484 \fIstringptr\fP. The yield of the function is the length of the string, not
2485 including the terminating zero, or one of these error codes:
2486 .sp
2488 .sp
2489 The buffer was too small for \fBpcre_copy_substring()\fP, or the attempt to get
2490 memory failed for \fBpcre_get_substring()\fP.
2491 .sp
2493 .sp
2494 There is no substring whose number is \fIstringnumber\fP.
2495 .P
2496 The \fBpcre_get_substring_list()\fP function extracts all available substrings
2497 and builds a list of pointers to them. All this is done in a single block of
2498 memory that is obtained via \fBpcre_malloc\fP. The address of the memory block
2499 is returned via \fIlistptr\fP, which is also the start of the list of string
2500 pointers. The end of the list is marked by a NULL pointer. The yield of the
2501 function is zero if all went well, or the error code
2502 .sp
2504 .sp
2505 if the attempt to get the memory block failed.
2506 .P
2507 When any of these functions encounter a substring that is unset, which can
2508 happen when capturing subpattern number \fIn+1\fP matches some part of the
2509 subject, but subpattern \fIn\fP has not been used at all, they return an empty
2510 string. This can be distinguished from a genuine zero-length substring by
2511 inspecting the appropriate offset in \fIovector\fP, which is negative for unset
2512 substrings.
2513 .P
2514 The two convenience functions \fBpcre_free_substring()\fP and
2515 \fBpcre_free_substring_list()\fP can be used to free the memory returned by
2516 a previous call of \fBpcre_get_substring()\fP or
2517 \fBpcre_get_substring_list()\fP, respectively. They do nothing more than call
2518 the function pointed to by \fBpcre_free\fP, which of course could be called
2519 directly from a C program. However, PCRE is used in some situations where it is
2520 linked via a special interface to another programming language that cannot use
2521 \fBpcre_free\fP directly; it is for these cases that the functions are
2522 provided.
2523 .
2524 .
2526 .rs
2527 .sp
2528 .nf
2529 .B int pcre_get_stringnumber(const pcre *\fIcode\fP,
2530 .B " const char *\fIname\fP);"
2531 .sp
2532 .B int pcre_copy_named_substring(const pcre *\fIcode\fP,
2533 .B " const char *\fIsubject\fP, int *\fIovector\fP,"
2534 .B " int \fIstringcount\fP, const char *\fIstringname\fP,"
2535 .B " char *\fIbuffer\fP, int \fIbuffersize\fP);"
2536 .sp
2537 .B int pcre_get_named_substring(const pcre *\fIcode\fP,
2538 .B " const char *\fIsubject\fP, int *\fIovector\fP,"
2539 .B " int \fIstringcount\fP, const char *\fIstringname\fP,"
2540 .B " const char **\fIstringptr\fP);"
2541 .fi
2542 .PP
2543 To extract a substring by name, you first have to find associated number.
2544 For example, for this pattern
2545 .sp
2546 (a+)b(?<xxx>\ed+)...
2547 .sp
2548 the number of the subpattern called "xxx" is 2. If the name is known to be
2549 unique (PCRE_DUPNAMES was not set), you can find the number from the name by
2550 calling \fBpcre_get_stringnumber()\fP. The first argument is the compiled
2551 pattern, and the second is the name. The yield of the function is the
2552 subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no subpattern of
2553 that name.
2554 .P
2555 Given the number, you can extract the substring directly, or use one of the
2556 functions described in the previous section. For convenience, there are also
2557 two functions that do the whole job.
2558 .P
2559 Most of the arguments of \fBpcre_copy_named_substring()\fP and
2560 \fBpcre_get_named_substring()\fP are the same as those for the similarly named
2561 functions that extract by number. As these are described in the previous
2562 section, they are not re-described here. There are just two differences:
2563 .P
2564 First, instead of a substring number, a substring name is given. Second, there
2565 is an extra argument, given at the start, which is a pointer to the compiled
2566 pattern. This is needed in order to gain access to the name-to-number
2567 translation table.
2568 .P
2569 These functions call \fBpcre_get_stringnumber()\fP, and if it succeeds, they
2570 then call \fBpcre_copy_substring()\fP or \fBpcre_get_substring()\fP, as
2571 appropriate. \fBNOTE:\fP If PCRE_DUPNAMES is set and there are duplicate names,
2572 the behaviour may not be what you want (see the next section).
2573 .P
2574 \fBWarning:\fP If the pattern uses the (?| feature to set up multiple
2575 subpatterns with the same number, as described in the
2576 .\" HTML <a href="pcrepattern.html#dupsubpatternnumber">
2577 .\" </a>
2578 section on duplicate subpattern numbers
2579 .\"
2580 in the
2581 .\" HREF
2582 \fBpcrepattern\fP
2583 .\"
2584 page, you cannot use names to distinguish the different subpatterns, because
2585 names are not included in the compiled code. The matching process uses only
2586 numbers. For this reason, the use of different names for subpatterns of the
2587 same number causes an error at compile time.
2588 .
2589 .
2591 .rs
2592 .sp
2593 .nf
2594 .B int pcre_get_stringtable_entries(const pcre *\fIcode\fP,
2595 .B " const char *\fIname\fP, char **\fIfirst\fP, char **\fIlast\fP);"
2596 .fi
2597 .PP
2598 When a pattern is compiled with the PCRE_DUPNAMES option, names for subpatterns
2599 are not required to be unique. (Duplicate names are always allowed for
2600 subpatterns with the same number, created by using the (?| feature. Indeed, if
2601 such subpatterns are named, they are required to use the same names.)
2602 .P
2603 Normally, patterns with duplicate names are such that in any one match, only
2604 one of the named subpatterns participates. An example is shown in the
2605 .\" HREF
2606 \fBpcrepattern\fP
2607 .\"
2608 documentation.
2609 .P
2610 When duplicates are present, \fBpcre_copy_named_substring()\fP and
2611 \fBpcre_get_named_substring()\fP return the first substring corresponding to
2612 the given name that is set. If none are set, PCRE_ERROR_NOSUBSTRING (-7) is
2613 returned; no data is returned. The \fBpcre_get_stringnumber()\fP function
2614 returns one of the numbers that are associated with the name, but it is not
2615 defined which it is.
2616 .P
2617 If you want to get full details of all captured substrings for a given name,
2618 you must use the \fBpcre_get_stringtable_entries()\fP function. The first
2619 argument is the compiled pattern, and the second is the name. The third and
2620 fourth are pointers to variables which are updated by the function. After it
2621 has run, they point to the first and last entries in the name-to-number table
2622 for the given name. The function itself returns the length of each entry, or
2623 PCRE_ERROR_NOSUBSTRING (-7) if there are none. The format of the table is
2624 described above in the section entitled \fIInformation about a pattern\fP
2625 .\" HTML <a href="#infoaboutpattern">
2626 .\" </a>
2627 above.
2628 .\"
2629 Given all the relevant entries for the name, you can extract each of their
2630 numbers, and hence the captured data, if any.
2631 .
2632 .
2634 .rs
2635 .sp
2636 The traditional matching function uses a similar algorithm to Perl, which stops
2637 when it finds the first match, starting at a given point in the subject. If you
2638 want to find all possible matches, or the longest possible match, consider
2639 using the alternative matching function (see below) instead. If you cannot use
2640 the alternative function, but still need to find all possible matches, you
2641 can kludge it up by making use of the callout facility, which is described in
2642 the
2643 .\" HREF
2644 \fBpcrecallout\fP
2645 .\"
2646 documentation.
2647 .P
2648 What you have to do is to insert a callout right at the end of the pattern.
2649 When your callout function is called, extract and save the current matched
2650 substring. Then return 1, which forces \fBpcre_exec()\fP to backtrack and try
2651 other alternatives. Ultimately, when it runs out of matches, \fBpcre_exec()\fP
2652 will yield PCRE_ERROR_NOMATCH.
2653 .
2654 .
2656 .rs
2657 .sp
2658 Matching certain patterns using \fBpcre_exec()\fP can use a lot of process
2659 stack, which in certain environments can be rather limited in size. Some users
2660 find it helpful to have an estimate of the amount of stack that is used by
2661 \fBpcre_exec()\fP, to help them set recursion limits, as described in the
2662 .\" HREF
2663 \fBpcrestack\fP
2664 .\"
2665 documentation. The estimate that is output by \fBpcretest\fP when called with
2666 the \fB-m\fP and \fB-C\fP options is obtained by calling \fBpcre_exec\fP with
2667 the values NULL, NULL, NULL, -999, and -999 for its first five arguments.
2668 .P
2669 Normally, if its first argument is NULL, \fBpcre_exec()\fP immediately returns
2670 the negative error code PCRE_ERROR_NULL, but with this special combination of
2671 arguments, it returns instead a negative number whose absolute value is the
2672 approximate stack frame size in bytes. (A negative number is used so that it is
2673 clear that no match has happened.) The value is approximate because in some
2674 cases, recursive calls to \fBpcre_exec()\fP occur when there are one or two
2675 additional variables on the stack.
2676 .P
2677 If PCRE has been compiled to use the heap instead of the stack for recursion,
2678 the value returned is the size of each block that is obtained from the heap.
2679 .
2680 .
2681 .\" HTML <a name="dfamatch"></a>
2683 .rs
2684 .sp
2685 .nf
2686 .B int pcre_dfa_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
2687 .B " const char *\fIsubject\fP, int \fIlength\fP, int \fIstartoffset\fP,"
2688 .B " int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP,"
2689 .B " int *\fIworkspace\fP, int \fIwscount\fP);"
2690 .fi
2691 .P
2692 The function \fBpcre_dfa_exec()\fP is called to match a subject string against
2693 a compiled pattern, using a matching algorithm that scans the subject string
2694 just once, and does not backtrack. This has different characteristics to the
2695 normal algorithm, and is not compatible with Perl. Some of the features of PCRE
2696 patterns are not supported. Nevertheless, there are times when this kind of
2697 matching can be useful. For a discussion of the two matching algorithms, and a
2698 list of features that \fBpcre_dfa_exec()\fP does not support, see the
2699 .\" HREF
2700 \fBpcrematching\fP
2701 .\"
2702 documentation.
2703 .P
2704 The arguments for the \fBpcre_dfa_exec()\fP function are the same as for
2705 \fBpcre_exec()\fP, plus two extras. The \fIovector\fP argument is used in a
2706 different way, and this is described below. The other common arguments are used
2707 in the same way as for \fBpcre_exec()\fP, so their description is not repeated
2708 here.
2709 .P
2710 The two additional arguments provide workspace for the function. The workspace
2711 vector should contain at least 20 elements. It is used for keeping track of
2712 multiple paths through the pattern tree. More workspace will be needed for
2713 patterns and subjects where there are a lot of potential matches.
2714 .P
2715 Here is an example of a simple call to \fBpcre_dfa_exec()\fP:
2716 .sp
2717 int rc;
2718 int ovector[10];
2719 int wspace[20];
2720 rc = pcre_dfa_exec(
2721 re, /* result of pcre_compile() */
2722 NULL, /* we didn't study the pattern */
2723 "some string", /* the subject string */
2724 11, /* the length of the subject string */
2725 0, /* start at offset 0 in the subject */
2726 0, /* default options */
2727 ovector, /* vector of integers for substring information */
2728 10, /* number of elements (NOT size in bytes) */
2729 wspace, /* working space vector */
2730 20); /* number of elements (NOT size in bytes) */
2731 .
2732 .SS "Option bits for \fBpcre_dfa_exec()\fP"
2733 .rs
2734 .sp
2735 The unused bits of the \fIoptions\fP argument for \fBpcre_dfa_exec()\fP must be
2736 zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_\fIxxx\fP,
2740 All but the last four of these are exactly the same as for \fBpcre_exec()\fP,
2741 so their description is not repeated here.
2742 .sp
2745 .sp
2746 These have the same general effect as they do for \fBpcre_exec()\fP, but the
2747 details are slightly different. When PCRE_PARTIAL_HARD is set for
2748 \fBpcre_dfa_exec()\fP, it returns PCRE_ERROR_PARTIAL if the end of the subject
2749 is reached and there is still at least one matching possibility that requires
2750 additional characters. This happens even if some complete matches have also
2751 been found. When PCRE_PARTIAL_SOFT is set, the return code PCRE_ERROR_NOMATCH
2752 is converted into PCRE_ERROR_PARTIAL if the end of the subject is reached,
2753 there have been no complete matches, but there is still at least one matching
2754 possibility. The portion of the string that was inspected when the longest
2755 partial match was found is set as the first matching string in both cases.
2756 There is a more detailed discussion of partial and multi-segment matching, with
2757 examples, in the
2758 .\" HREF
2759 \fBpcrepartial\fP
2760 .\"
2761 documentation.
2762 .sp
2764 .sp
2765 Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to stop as
2766 soon as it has found one match. Because of the way the alternative algorithm
2767 works, this is necessarily the shortest possible match at the first possible
2768 matching point in the subject string.
2769 .sp
2771 .sp
2772 When \fBpcre_dfa_exec()\fP returns a partial match, it is possible to call it
2773 again, with additional subject characters, and have it continue with the same
2774 match. The PCRE_DFA_RESTART option requests this action; when it is set, the
2775 \fIworkspace\fP and \fIwscount\fP options must reference the same vector as
2776 before because data about the match so far is left in them after a partial
2777 match. There is more discussion of this facility in the
2778 .\" HREF
2779 \fBpcrepartial\fP
2780 .\"
2781 documentation.
2782 .
2783 .
2784 .SS "Successful returns from \fBpcre_dfa_exec()\fP"
2785 .rs
2786 .sp
2787 When \fBpcre_dfa_exec()\fP succeeds, it may have matched more than one
2788 substring in the subject. Note, however, that all the matches from one run of
2789 the function start at the same point in the subject. The shorter matches are
2790 all initial substrings of the longer matches. For example, if the pattern
2791 .sp
2792 <.*>
2793 .sp
2794 is matched against the string
2795 .sp
2796 This is <something> <something else> <something further> no more
2797 .sp
2798 the three matched strings are
2799 .sp
2800 <something>
2801 <something> <something else>
2802 <something> <something else> <something further>
2803 .sp
2804 On success, the yield of the function is a number greater than zero, which is
2805 the number of matched substrings. The substrings themselves are returned in
2806 \fIovector\fP. Each string uses two elements; the first is the offset to the
2807 start, and the second is the offset to the end. In fact, all the strings have
2808 the same start offset. (Space could have been saved by giving this only once,
2809 but it was decided to retain some compatibility with the way \fBpcre_exec()\fP
2810 returns data, even though the meaning of the strings is different.)
2811 .P
2812 The strings are returned in reverse order of length; that is, the longest
2813 matching string is given first. If there were too many matches to fit into
2814 \fIovector\fP, the yield of the function is zero, and the vector is filled with
2815 the longest matches. Unlike \fBpcre_exec()\fP, \fBpcre_dfa_exec()\fP can use
2816 the entire \fIovector\fP for returning matched strings.
2817 .P
2818 NOTE: PCRE's "auto-possessification" optimization usually applies to character
2819 repeats at the end of a pattern (as well as internally). For example, the
2820 pattern "a\ed+" is compiled as if it were "a\ed++" because there is no point
2821 even considering the possibility of backtracking into the repeated digits. For
2822 DFA matching, this means that only one possible match is found. If you really
2823 do want multiple matches in such cases, either use an ungreedy repeat
2824 ("a\ed+?") or set the PCRE_NO_AUTO_POSSESS option when compiling.
2825 .
2826 .
2827 .SS "Error returns from \fBpcre_dfa_exec()\fP"
2828 .rs
2829 .sp
2830 The \fBpcre_dfa_exec()\fP function returns a negative number when it fails.
2831 Many of the errors are the same as for \fBpcre_exec()\fP, and these are
2832 described
2833 .\" HTML <a href="#errorlist">
2834 .\" </a>
2835 above.
2836 .\"
2837 There are in addition the following errors that are specific to
2838 \fBpcre_dfa_exec()\fP:
2839 .sp
2841 .sp
2842 This return is given if \fBpcre_dfa_exec()\fP encounters an item in the pattern
2843 that it does not support, for instance, the use of \eC or a back reference.
2844 .sp
2846 .sp
2847 This return is given if \fBpcre_dfa_exec()\fP encounters a condition item that
2848 uses a back reference for the condition, or a test for recursion in a specific
2849 group. These are not supported.
2850 .sp
2852 .sp
2853 This return is given if \fBpcre_dfa_exec()\fP is called with an \fIextra\fP
2854 block that contains a setting of the \fImatch_limit\fP or
2855 \fImatch_limit_recursion\fP fields. This is not supported (these fields are
2856 meaningless for DFA matching).
2857 .sp
2859 .sp
2860 This return is given if \fBpcre_dfa_exec()\fP runs out of space in the
2861 \fIworkspace\fP vector.
2862 .sp
2864 .sp
2865 When a recursive subpattern is processed, the matching function calls itself
2866 recursively, using private vectors for \fIovector\fP and \fIworkspace\fP. This
2867 error is given if the output vector is not large enough. This should be
2868 extremely rare, as a vector of size 1000 is used.
2869 .sp
2871 .sp
2872 When \fBpcre_dfa_exec()\fP is called with the \fBPCRE_DFA_RESTART\fP option,
2873 some plausibility checks are made on the contents of the workspace, which
2874 should contain data about the previous partial match. If any of these checks
2875 fail, this error is given.
2876 .
2877 .
2878 .SH "SEE ALSO"
2879 .rs
2880 .sp
2881 \fBpcre16\fP(3), \fBpcre32\fP(3), \fBpcrebuild\fP(3), \fBpcrecallout\fP(3),
2882 \fBpcrecpp(3)\fP(3), \fBpcrematching\fP(3), \fBpcrepartial\fP(3),
2883 \fBpcreposix\fP(3), \fBpcreprecompile\fP(3), \fBpcresample\fP(3),
2884 \fBpcrestack\fP(3).
2885 .
2886 .
2888 .rs
2889 .sp
2890 .nf
2891 Philip Hazel
2892 University Computing Service
2893 Cambridge CB2 3QH, England.
2894 .fi
2895 .
2896 .
2898 .rs
2899 .sp
2900 .nf
2901 Last updated: 03 January 2014
2902 Copyright (c) 1997-2014 University of Cambridge.
2903 .fi


Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

  ViewVC Help
Powered by ViewVC 1.1.5