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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5