ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

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


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

  ViewVC Help
Powered by ViewVC 1.1.5