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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (hide annotations) (download) (as text)
Sat Feb 24 21:41:21 2007 UTC (6 years, 2 months ago) by nigel
File MIME type: text/html
File size: 73924 byte(s)
Load pcre-6.5 into code/trunk.

1 nigel 63 <html>
2     <head>
3     <title>pcreapi specification</title>
4     </head>
5     <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6 nigel 75 <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 nigel 63 <ul>
16 nigel 75 <li><a name="TOC1" href="#SEC1">PCRE NATIVE API</a>
17     <li><a name="TOC2" href="#SEC2">PCRE API OVERVIEW</a>
18 nigel 63 <li><a name="TOC3" href="#SEC3">MULTITHREADING</a>
19 nigel 75 <li><a name="TOC4" href="#SEC4">SAVING PRECOMPILED PATTERNS FOR LATER USE</a>
20     <li><a name="TOC5" href="#SEC5">CHECKING BUILD-TIME OPTIONS</a>
21     <li><a name="TOC6" href="#SEC6">COMPILING A PATTERN</a>
22 nigel 77 <li><a name="TOC7" href="#SEC7">COMPILATION ERROR CODES</a>
23     <li><a name="TOC8" href="#SEC8">STUDYING A PATTERN</a>
24     <li><a name="TOC9" href="#SEC9">LOCALE SUPPORT</a>
25     <li><a name="TOC10" href="#SEC10">INFORMATION ABOUT A PATTERN</a>
26     <li><a name="TOC11" href="#SEC11">OBSOLETE INFO FUNCTION</a>
27     <li><a name="TOC12" href="#SEC12">REFERENCE COUNTS</a>
28     <li><a name="TOC13" href="#SEC13">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a>
29     <li><a name="TOC14" href="#SEC14">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a>
30     <li><a name="TOC15" href="#SEC15">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a>
31     <li><a name="TOC16" href="#SEC16">FINDING ALL POSSIBLE MATCHES</a>
32     <li><a name="TOC17" href="#SEC17">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a>
33 nigel 63 </ul>
34 nigel 75 <br><a name="SEC1" href="#TOC1">PCRE NATIVE API</a><br>
35 nigel 63 <P>
36     <b>#include &#60;pcre.h&#62;</b>
37     </P>
38     <P>
39     <b>pcre *pcre_compile(const char *<i>pattern</i>, int <i>options</i>,</b>
40     <b>const char **<i>errptr</i>, int *<i>erroffset</i>,</b>
41     <b>const unsigned char *<i>tableptr</i>);</b>
42     </P>
43     <P>
44 nigel 77 <b>pcre *pcre_compile2(const char *<i>pattern</i>, int <i>options</i>,</b>
45     <b>int *<i>errorcodeptr</i>,</b>
46     <b>const char **<i>errptr</i>, int *<i>erroffset</i>,</b>
47     <b>const unsigned char *<i>tableptr</i>);</b>
48     </P>
49     <P>
50 nigel 63 <b>pcre_extra *pcre_study(const pcre *<i>code</i>, int <i>options</i>,</b>
51     <b>const char **<i>errptr</i>);</b>
52     </P>
53     <P>
54     <b>int pcre_exec(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
55     <b>const char *<i>subject</i>, int <i>length</i>, int <i>startoffset</i>,</b>
56     <b>int <i>options</i>, int *<i>ovector</i>, int <i>ovecsize</i>);</b>
57     </P>
58     <P>
59 nigel 77 <b>int pcre_dfa_exec(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
60     <b>const char *<i>subject</i>, int <i>length</i>, int <i>startoffset</i>,</b>
61     <b>int <i>options</i>, int *<i>ovector</i>, int <i>ovecsize</i>,</b>
62     <b>int *<i>workspace</i>, int <i>wscount</i>);</b>
63     </P>
64     <P>
65 nigel 63 <b>int pcre_copy_named_substring(const pcre *<i>code</i>,</b>
66     <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
67     <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
68     <b>char *<i>buffer</i>, int <i>buffersize</i>);</b>
69     </P>
70     <P>
71     <b>int pcre_copy_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
72     <b>int <i>stringcount</i>, int <i>stringnumber</i>, char *<i>buffer</i>,</b>
73     <b>int <i>buffersize</i>);</b>
74     </P>
75     <P>
76     <b>int pcre_get_named_substring(const pcre *<i>code</i>,</b>
77     <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
78     <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
79     <b>const char **<i>stringptr</i>);</b>
80     </P>
81     <P>
82     <b>int pcre_get_stringnumber(const pcre *<i>code</i>,</b>
83     <b>const char *<i>name</i>);</b>
84     </P>
85     <P>
86     <b>int pcre_get_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
87     <b>int <i>stringcount</i>, int <i>stringnumber</i>,</b>
88     <b>const char **<i>stringptr</i>);</b>
89     </P>
90     <P>
91     <b>int pcre_get_substring_list(const char *<i>subject</i>,</b>
92     <b>int *<i>ovector</i>, int <i>stringcount</i>, const char ***<i>listptr</i>);</b>
93     </P>
94     <P>
95     <b>void pcre_free_substring(const char *<i>stringptr</i>);</b>
96     </P>
97     <P>
98     <b>void pcre_free_substring_list(const char **<i>stringptr</i>);</b>
99     </P>
100     <P>
101     <b>const unsigned char *pcre_maketables(void);</b>
102     </P>
103     <P>
104     <b>int pcre_fullinfo(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
105     <b>int <i>what</i>, void *<i>where</i>);</b>
106     </P>
107     <P>
108     <b>int pcre_info(const pcre *<i>code</i>, int *<i>optptr</i>, int</b>
109     <b>*<i>firstcharptr</i>);</b>
110     </P>
111     <P>
112 nigel 77 <b>int pcre_refcount(pcre *<i>code</i>, int <i>adjust</i>);</b>
113     </P>
114     <P>
115 nigel 63 <b>int pcre_config(int <i>what</i>, void *<i>where</i>);</b>
116     </P>
117     <P>
118     <b>char *pcre_version(void);</b>
119     </P>
120     <P>
121     <b>void *(*pcre_malloc)(size_t);</b>
122     </P>
123     <P>
124     <b>void (*pcre_free)(void *);</b>
125     </P>
126     <P>
127 nigel 73 <b>void *(*pcre_stack_malloc)(size_t);</b>
128     </P>
129     <P>
130     <b>void (*pcre_stack_free)(void *);</b>
131     </P>
132     <P>
133 nigel 63 <b>int (*pcre_callout)(pcre_callout_block *);</b>
134     </P>
135 nigel 75 <br><a name="SEC2" href="#TOC1">PCRE API OVERVIEW</a><br>
136 nigel 63 <P>
137 nigel 77 PCRE has its own native API, which is described in this document. There is
138     also a set of wrapper functions that correspond to the POSIX regular expression
139     API. These are described in the
140 nigel 75 <a href="pcreposix.html"><b>pcreposix</b></a>
141 nigel 77 documentation. Both of these APIs define a set of C function calls. A C++
142     wrapper is distributed with PCRE. It is documented in the
143     <a href="pcrecpp.html"><b>pcrecpp</b></a>
144     page.
145 nigel 63 </P>
146     <P>
147 nigel 77 The native API C function prototypes are defined in the header file
148     <b>pcre.h</b>, and on Unix systems the library itself is called <b>libpcre</b>.
149     It can normally be accessed by adding <b>-lpcre</b> to the command for linking
150     an application that uses PCRE. The header file defines the macros PCRE_MAJOR
151     and PCRE_MINOR to contain the major and minor release numbers for the library.
152 nigel 75 Applications can use these to include support for different releases of PCRE.
153 nigel 63 </P>
154     <P>
155 nigel 77 The functions <b>pcre_compile()</b>, <b>pcre_compile2()</b>, <b>pcre_study()</b>,
156     and <b>pcre_exec()</b> are used for compiling and matching regular expressions
157     in a Perl-compatible manner. A sample program that demonstrates the simplest
158     way of using them is provided in the file called <i>pcredemo.c</i> in the source
159     distribution. The
160 nigel 75 <a href="pcresample.html"><b>pcresample</b></a>
161     documentation describes how to run it.
162 nigel 63 </P>
163     <P>
164 nigel 77 A second matching function, <b>pcre_dfa_exec()</b>, which is not
165     Perl-compatible, is also provided. This uses a different algorithm for the
166     matching. This allows it to find all possible matches (at a given point in the
167     subject), not just one. However, this algorithm does not return captured
168     substrings. A description of the two matching algorithms and their advantages
169     and disadvantages is given in the
170     <a href="pcrematching.html"><b>pcrematching</b></a>
171     documentation.
172     </P>
173     <P>
174 nigel 75 In addition to the main compiling and matching functions, there are convenience
175 nigel 77 functions for extracting captured substrings from a subject string that is
176     matched by <b>pcre_exec()</b>. They are:
177 nigel 63 <pre>
178     <b>pcre_copy_substring()</b>
179     <b>pcre_copy_named_substring()</b>
180     <b>pcre_get_substring()</b>
181     <b>pcre_get_named_substring()</b>
182     <b>pcre_get_substring_list()</b>
183 nigel 75 <b>pcre_get_stringnumber()</b>
184     </pre>
185 nigel 63 <b>pcre_free_substring()</b> and <b>pcre_free_substring_list()</b> are also
186     provided, to free the memory used for extracted strings.
187     </P>
188     <P>
189 nigel 75 The function <b>pcre_maketables()</b> is used to build a set of character tables
190 nigel 77 in the current locale for passing to <b>pcre_compile()</b>, <b>pcre_exec()</b>,
191     or <b>pcre_dfa_exec()</b>. This is an optional facility that is provided for
192     specialist use. Most commonly, no special tables are passed, in which case
193     internal tables that are generated when PCRE is built are used.
194 nigel 63 </P>
195     <P>
196     The function <b>pcre_fullinfo()</b> is used to find out information about a
197 nigel 75 compiled pattern; <b>pcre_info()</b> is an obsolete version that returns only
198 nigel 63 some of the available information, but is retained for backwards compatibility.
199     The function <b>pcre_version()</b> returns a pointer to a string containing the
200     version of PCRE and its date of release.
201     </P>
202     <P>
203 nigel 77 The function <b>pcre_refcount()</b> maintains a reference count in a data block
204     containing a compiled pattern. This is provided for the benefit of
205     object-oriented applications.
206     </P>
207     <P>
208 nigel 63 The global variables <b>pcre_malloc</b> and <b>pcre_free</b> initially contain
209 nigel 75 the entry points of the standard <b>malloc()</b> and <b>free()</b> functions,
210 nigel 63 respectively. PCRE calls the memory management functions via these variables,
211     so a calling program can replace them if it wishes to intercept the calls. This
212     should be done before calling any PCRE functions.
213     </P>
214     <P>
215 nigel 73 The global variables <b>pcre_stack_malloc</b> and <b>pcre_stack_free</b> are also
216     indirections to memory management functions. These special functions are used
217     only when PCRE is compiled to use the heap for remembering data, instead of
218 nigel 77 recursive function calls, when running the <b>pcre_exec()</b> function. This is
219     a non-standard way of building PCRE, for use in environments that have limited
220     stacks. Because of the greater use of memory management, it runs more slowly.
221     Separate functions are provided so that special-purpose external code can be
222     used for this case. When used, these functions are always called in a
223     stack-like manner (last obtained, first freed), and always for memory blocks of
224     the same size.
225 nigel 73 </P>
226     <P>
227 nigel 63 The global variable <b>pcre_callout</b> initially contains NULL. It can be set
228     by the caller to a "callout" function, which PCRE will then call at specified
229 nigel 75 points during a matching operation. Details are given in the
230     <a href="pcrecallout.html"><b>pcrecallout</b></a>
231 nigel 63 documentation.
232     </P>
233     <br><a name="SEC3" href="#TOC1">MULTITHREADING</a><br>
234     <P>
235     The PCRE functions can be used in multi-threading applications, with the
236 nigel 73 proviso that the memory management functions pointed to by <b>pcre_malloc</b>,
237     <b>pcre_free</b>, <b>pcre_stack_malloc</b>, and <b>pcre_stack_free</b>, and the
238     callout function pointed to by <b>pcre_callout</b>, are shared by all threads.
239 nigel 63 </P>
240     <P>
241     The compiled form of a regular expression is not altered during matching, so
242     the same compiled pattern can safely be used by several threads at once.
243     </P>
244 nigel 75 <br><a name="SEC4" href="#TOC1">SAVING PRECOMPILED PATTERNS FOR LATER USE</a><br>
245 nigel 63 <P>
246 nigel 75 The compiled form of a regular expression can be saved and re-used at a later
247     time, possibly by a different program, and even on a host other than the one on
248     which it was compiled. Details are given in the
249     <a href="pcreprecompile.html"><b>pcreprecompile</b></a>
250     documentation.
251     </P>
252     <br><a name="SEC5" href="#TOC1">CHECKING BUILD-TIME OPTIONS</a><br>
253     <P>
254 nigel 63 <b>int pcre_config(int <i>what</i>, void *<i>where</i>);</b>
255     </P>
256     <P>
257     The function <b>pcre_config()</b> makes it possible for a PCRE client to
258     discover which optional features have been compiled into the PCRE library. The
259     <a href="pcrebuild.html"><b>pcrebuild</b></a>
260     documentation has more details about these optional features.
261     </P>
262     <P>
263     The first argument for <b>pcre_config()</b> is an integer, specifying which
264     information is required; the second argument is a pointer to a variable into
265     which the information is placed. The following information is available:
266     <pre>
267     PCRE_CONFIG_UTF8
268 nigel 75 </pre>
269 nigel 63 The output is an integer that is set to one if UTF-8 support is available;
270     otherwise it is set to zero.
271     <pre>
272 nigel 75 PCRE_CONFIG_UNICODE_PROPERTIES
273     </pre>
274     The output is an integer that is set to one if support for Unicode character
275     properties is available; otherwise it is set to zero.
276     <pre>
277 nigel 63 PCRE_CONFIG_NEWLINE
278 nigel 75 </pre>
279 nigel 63 The output is an integer that is set to the value of the code that is used for
280     the newline character. It is either linefeed (10) or carriage return (13), and
281     should normally be the standard character for your operating system.
282     <pre>
283     PCRE_CONFIG_LINK_SIZE
284 nigel 75 </pre>
285 nigel 63 The output is an integer that contains the number of bytes used for internal
286     linkage in compiled regular expressions. The value is 2, 3, or 4. Larger values
287     allow larger regular expressions to be compiled, at the expense of slower
288     matching. The default value of 2 is sufficient for all but the most massive
289     patterns, since it allows the compiled pattern to be up to 64K in size.
290     <pre>
291     PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
292 nigel 75 </pre>
293 nigel 63 The output is an integer that contains the threshold above which the POSIX
294     interface uses <b>malloc()</b> for output vectors. Further details are given in
295 nigel 75 the
296     <a href="pcreposix.html"><b>pcreposix</b></a>
297     documentation.
298 nigel 63 <pre>
299     PCRE_CONFIG_MATCH_LIMIT
300 nigel 75 </pre>
301 nigel 63 The output is an integer that gives the default limit for the number of
302     internal matching function calls in a <b>pcre_exec()</b> execution. Further
303     details are given with <b>pcre_exec()</b> below.
304 nigel 73 <pre>
305 nigel 87 PCRE_CONFIG_MATCH_LIMIT_RECURSION
306     </pre>
307     The output is an integer that gives the default limit for the depth of
308     recursion when calling the internal matching function in a <b>pcre_exec()</b>
309     execution. Further details are given with <b>pcre_exec()</b> below.
310     <pre>
311 nigel 73 PCRE_CONFIG_STACKRECURSE
312 nigel 75 </pre>
313 nigel 77 The output is an integer that is set to one if internal recursion when running
314     <b>pcre_exec()</b> is implemented by recursive function calls that use the stack
315     to remember their state. This is the usual way that PCRE is compiled. The
316     output is zero if PCRE was compiled to use blocks of data on the heap instead
317     of recursive function calls. In this case, <b>pcre_stack_malloc</b> and
318     <b>pcre_stack_free</b> are called to manage memory blocks on the heap, thus
319     avoiding the use of the stack.
320 nigel 73 </P>
321 nigel 75 <br><a name="SEC6" href="#TOC1">COMPILING A PATTERN</a><br>
322 nigel 63 <P>
323     <b>pcre *pcre_compile(const char *<i>pattern</i>, int <i>options</i>,</b>
324     <b>const char **<i>errptr</i>, int *<i>erroffset</i>,</b>
325     <b>const unsigned char *<i>tableptr</i>);</b>
326 nigel 77 <b>pcre *pcre_compile2(const char *<i>pattern</i>, int <i>options</i>,</b>
327     <b>int *<i>errorcodeptr</i>,</b>
328     <b>const char **<i>errptr</i>, int *<i>erroffset</i>,</b>
329     <b>const unsigned char *<i>tableptr</i>);</b>
330 nigel 63 </P>
331     <P>
332 nigel 77 Either of the functions <b>pcre_compile()</b> or <b>pcre_compile2()</b> can be
333     called to compile a pattern into an internal form. The only difference between
334     the two interfaces is that <b>pcre_compile2()</b> has an additional argument,
335     <i>errorcodeptr</i>, via which a numerical error code can be returned.
336 nigel 63 </P>
337     <P>
338 nigel 77 The pattern is a C string terminated by a binary zero, and is passed in the
339     <i>pattern</i> argument. A pointer to a single block of memory that is obtained
340     via <b>pcre_malloc</b> is returned. This contains the compiled code and related
341     data. The <b>pcre</b> type is defined for the returned block; this is a typedef
342     for a structure whose contents are not externally defined. It is up to the
343     caller to free the memory when it is no longer required.
344     </P>
345     <P>
346 nigel 63 Although the compiled code of a PCRE regex is relocatable, that is, it does not
347     depend on memory location, the complete <b>pcre</b> data block is not
348 nigel 75 fully relocatable, because it may contain a copy of the <i>tableptr</i>
349     argument, which is an address (see below).
350 nigel 63 </P>
351     <P>
352     The <i>options</i> argument contains independent bits that affect the
353 nigel 75 compilation. It should be zero if no options are required. The available
354     options are described below. Some of them, in particular, those that are
355     compatible with Perl, can also be set and unset from within the pattern (see
356     the detailed description in the
357     <a href="pcrepattern.html"><b>pcrepattern</b></a>
358     documentation). For these options, the contents of the <i>options</i> argument
359     specifies their initial settings at the start of compilation and execution. The
360     PCRE_ANCHORED option can be set at the time of matching as well as at compile
361     time.
362 nigel 63 </P>
363     <P>
364     If <i>errptr</i> is NULL, <b>pcre_compile()</b> returns NULL immediately.
365     Otherwise, if compilation of a pattern fails, <b>pcre_compile()</b> returns
366     NULL, and sets the variable pointed to by <i>errptr</i> to point to a textual
367 nigel 87 error message. This is a static string that is part of the library. You must
368     not try to free it. The offset from the start of the pattern to the character
369     where the error was discovered is placed in the variable pointed to by
370 nigel 63 <i>erroffset</i>, which must not be NULL. If it is, an immediate error is given.
371     </P>
372     <P>
373 nigel 77 If <b>pcre_compile2()</b> is used instead of <b>pcre_compile()</b>, and the
374     <i>errorcodeptr</i> argument is not NULL, a non-zero error code number is
375     returned via this argument in the event of an error. This is in addition to the
376     textual error message. Error codes and messages are listed below.
377     </P>
378     <P>
379 nigel 63 If the final argument, <i>tableptr</i>, is NULL, PCRE uses a default set of
380 nigel 75 character tables that are built when PCRE is compiled, using the default C
381     locale. Otherwise, <i>tableptr</i> must be an address that is the result of a
382     call to <b>pcre_maketables()</b>. This value is stored with the compiled
383     pattern, and used again by <b>pcre_exec()</b>, unless another table pointer is
384     passed to it. For more discussion, see the section on locale support below.
385 nigel 63 </P>
386     <P>
387     This code fragment shows a typical straightforward call to <b>pcre_compile()</b>:
388     <pre>
389     pcre *re;
390     const char *error;
391     int erroffset;
392     re = pcre_compile(
393     "^A.*Z", /* the pattern */
394     0, /* default options */
395     &error, /* for error message */
396     &erroffset, /* for error offset */
397     NULL); /* use default character tables */
398 nigel 75 </pre>
399     The following names for option bits are defined in the <b>pcre.h</b> header
400     file:
401 nigel 63 <pre>
402     PCRE_ANCHORED
403 nigel 75 </pre>
404 nigel 63 If this bit is set, the pattern is forced to be "anchored", that is, it is
405 nigel 75 constrained to match only at the first matching point in the string that is
406 nigel 63 being searched (the "subject string"). This effect can also be achieved by
407     appropriate constructs in the pattern itself, which is the only way to do it in
408     Perl.
409     <pre>
410 nigel 75 PCRE_AUTO_CALLOUT
411     </pre>
412     If this bit is set, <b>pcre_compile()</b> automatically inserts callout items,
413     all with number 255, before each pattern item. For discussion of the callout
414     facility, see the
415     <a href="pcrecallout.html"><b>pcrecallout</b></a>
416     documentation.
417     <pre>
418 nigel 63 PCRE_CASELESS
419 nigel 75 </pre>
420 nigel 63 If this bit is set, letters in the pattern match both upper and lower case
421     letters. It is equivalent to Perl's /i option, and it can be changed within a
422 nigel 77 pattern by a (?i) option setting. In UTF-8 mode, PCRE always understands the
423     concept of case for characters whose values are less than 128, so caseless
424     matching is always possible. For characters with higher values, the concept of
425     case is supported if PCRE is compiled with Unicode property support, but not
426     otherwise. If you want to use caseless matching for characters 128 and above,
427     you must ensure that PCRE is compiled with Unicode property support as well as
428     with UTF-8 support.
429 nigel 63 <pre>
430     PCRE_DOLLAR_ENDONLY
431 nigel 75 </pre>
432 nigel 63 If this bit is set, a dollar metacharacter in the pattern matches only at the
433     end of the subject string. Without this option, a dollar also matches
434     immediately before the final character if it is a newline (but not before any
435     other newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
436     set. There is no equivalent to this option in Perl, and no way to set it within
437     a pattern.
438     <pre>
439     PCRE_DOTALL
440 nigel 75 </pre>
441 nigel 63 If this bit is set, a dot metacharater in the pattern matches all characters,
442     including newlines. Without it, newlines are excluded. This option is
443     equivalent to Perl's /s option, and it can be changed within a pattern by a
444     (?s) option setting. A negative class such as [^a] always matches a newline
445     character, independent of the setting of this option.
446     <pre>
447     PCRE_EXTENDED
448 nigel 75 </pre>
449 nigel 63 If this bit is set, whitespace data characters in the pattern are totally
450     ignored except when escaped or inside a character class. Whitespace does not
451     include the VT character (code 11). In addition, characters between an
452     unescaped # outside a character class and the next newline character,
453     inclusive, are also ignored. This is equivalent to Perl's /x option, and it can
454     be changed within a pattern by a (?x) option setting.
455     </P>
456     <P>
457     This option makes it possible to include comments inside complicated patterns.
458     Note, however, that this applies only to data characters. Whitespace characters
459     may never appear within special character sequences in a pattern, for example
460     within the sequence (?( which introduces a conditional subpattern.
461     <pre>
462     PCRE_EXTRA
463 nigel 75 </pre>
464 nigel 63 This option was invented in order to turn on additional functionality of PCRE
465     that is incompatible with Perl, but it is currently of very little use. When
466     set, any backslash in a pattern that is followed by a letter that has no
467     special meaning causes an error, thus reserving these combinations for future
468     expansion. By default, as in Perl, a backslash followed by a letter with no
469     special meaning is treated as a literal. There are at present no other features
470     controlled by this option. It can also be set by a (?X) option setting within a
471     pattern.
472     <pre>
473 nigel 77 PCRE_FIRSTLINE
474     </pre>
475     If this option is set, an unanchored pattern is required to match before or at
476     the first newline character in the subject string, though the matched text may
477     continue over the newline.
478     <pre>
479 nigel 63 PCRE_MULTILINE
480 nigel 75 </pre>
481     By default, PCRE treats the subject string as consisting of a single line of
482     characters (even if it actually contains newlines). The "start of line"
483 nigel 63 metacharacter (^) matches only at the start of the string, while the "end of
484     line" metacharacter ($) matches only at the end of the string, or before a
485     terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
486     Perl.
487     </P>
488     <P>
489     When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
490     match immediately following or immediately before any newline in the subject
491     string, respectively, as well as at the very start and end. This is equivalent
492     to Perl's /m option, and it can be changed within a pattern by a (?m) option
493     setting. If there are no "\n" characters in a subject string, or no
494     occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no effect.
495     <pre>
496     PCRE_NO_AUTO_CAPTURE
497 nigel 75 </pre>
498 nigel 63 If this option is set, it disables the use of numbered capturing parentheses in
499     the pattern. Any opening parenthesis that is not followed by ? behaves as if it
500     were followed by ?: but named parentheses can still be used for capturing (and
501     they acquire numbers in the usual way). There is no equivalent of this option
502     in Perl.
503     <pre>
504     PCRE_UNGREEDY
505 nigel 75 </pre>
506 nigel 63 This option inverts the "greediness" of the quantifiers so that they are not
507     greedy by default, but become greedy if followed by "?". It is not compatible
508     with Perl. It can also be set by a (?U) option setting within the pattern.
509     <pre>
510     PCRE_UTF8
511 nigel 75 </pre>
512 nigel 63 This option causes PCRE to regard both the pattern and the subject as strings
513     of UTF-8 characters instead of single-byte character strings. However, it is
514 nigel 75 available only when PCRE is built to include UTF-8 support. If not, the use
515 nigel 63 of this option provokes an error. Details of how this option changes the
516     behaviour of PCRE are given in the
517     <a href="pcre.html#utf8support">section on UTF-8 support</a>
518     in the main
519     <a href="pcre.html"><b>pcre</b></a>
520     page.
521 nigel 71 <pre>
522     PCRE_NO_UTF8_CHECK
523 nigel 75 </pre>
524 nigel 71 When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
525     automatically checked. If an invalid UTF-8 sequence of bytes is found,
526     <b>pcre_compile()</b> returns an error. If you already know that your pattern is
527     valid, and you want to skip this check for performance reasons, you can set the
528     PCRE_NO_UTF8_CHECK option. When it is set, the effect of passing an invalid
529     UTF-8 string as a pattern is undefined. It may cause your program to crash.
530 nigel 77 Note that this option can also be passed to <b>pcre_exec()</b> and
531     <b>pcre_dfa_exec()</b>, to suppress the UTF-8 validity checking of subject
532     strings.
533 nigel 71 </P>
534 nigel 77 <br><a name="SEC7" href="#TOC1">COMPILATION ERROR CODES</a><br>
535 nigel 63 <P>
536 nigel 77 The following table lists the error codes than may be returned by
537     <b>pcre_compile2()</b>, along with the error messages that may be returned by
538     both compiling functions.
539     <pre>
540     0 no error
541     1 \ at end of pattern
542     2 \c at end of pattern
543     3 unrecognized character follows \
544     4 numbers out of order in {} quantifier
545     5 number too big in {} quantifier
546     6 missing terminating ] for character class
547     7 invalid escape sequence in character class
548     8 range out of order in character class
549     9 nothing to repeat
550     10 operand of unlimited repeat could match the empty string
551     11 internal error: unexpected repeat
552     12 unrecognized character after (?
553     13 POSIX named classes are supported only within a class
554     14 missing )
555     15 reference to non-existent subpattern
556     16 erroffset passed as NULL
557     17 unknown option bit(s) set
558     18 missing ) after comment
559     19 parentheses nested too deeply
560     20 regular expression too large
561     21 failed to get memory
562     22 unmatched parentheses
563     23 internal error: code overflow
564     24 unrecognized character after (?&#60;
565     25 lookbehind assertion is not fixed length
566     26 malformed number after (?(
567     27 conditional group contains more than two branches
568     28 assertion expected after (?(
569     29 (?R or (?digits must be followed by )
570     30 unknown POSIX class name
571     31 POSIX collating elements are not supported
572     32 this version of PCRE is not compiled with PCRE_UTF8 support
573     33 spare error
574     34 character value in \x{...} sequence is too large
575     35 invalid condition (?(0)
576     36 \C not allowed in lookbehind assertion
577     37 PCRE does not support \L, \l, \N, \U, or \u
578     38 number after (?C is &#62; 255
579     39 closing ) for (?C expected
580     40 recursive call could loop indefinitely
581     41 unrecognized character after (?P
582     42 syntax error after (?P
583     43 two named groups have the same name
584     44 invalid UTF-8 string
585     45 support for \P, \p, and \X has not been compiled
586     46 malformed \P or \p sequence
587     47 unknown property name after \P or \p
588     </PRE>
589     </P>
590     <br><a name="SEC8" href="#TOC1">STUDYING A PATTERN</a><br>
591     <P>
592     <b>pcre_extra *pcre_study(const pcre *<i>code</i>, int <i>options</i></b>
593 nigel 63 <b>const char **<i>errptr</i>);</b>
594     </P>
595     <P>
596 nigel 75 If a compiled pattern is going to be used several times, it is worth spending
597     more time analyzing it in order to speed up the time taken for matching. The
598 nigel 63 function <b>pcre_study()</b> takes a pointer to a compiled pattern as its first
599 nigel 75 argument. If studying the pattern produces additional information that will
600     help speed up matching, <b>pcre_study()</b> returns a pointer to a
601     <b>pcre_extra</b> block, in which the <i>study_data</i> field points to the
602     results of the study.
603 nigel 63 </P>
604     <P>
605 nigel 75 The returned value from <b>pcre_study()</b> can be passed directly to
606     <b>pcre_exec()</b>. However, a <b>pcre_extra</b> block also contains other
607 nigel 63 fields that can be set by the caller before the block is passed; these are
608 nigel 75 described
609     <a href="#extradata">below</a>
610     in the section on matching a pattern.
611 nigel 63 </P>
612     <P>
613 nigel 77 If studying the pattern does not produce any additional information
614 nigel 75 <b>pcre_study()</b> returns NULL. In that circumstance, if the calling program
615     wants to pass any of the other fields to <b>pcre_exec()</b>, it must set up its
616     own <b>pcre_extra</b> block.
617 nigel 63 </P>
618     <P>
619 nigel 75 The second argument of <b>pcre_study()</b> contains option bits. At present, no
620     options are defined, and this argument should always be zero.
621     </P>
622     <P>
623 nigel 63 The third argument for <b>pcre_study()</b> is a pointer for an error message. If
624     studying succeeds (even if no data is returned), the variable it points to is
625 nigel 87 set to NULL. Otherwise it is set to point to a textual error message. This is a
626     static string that is part of the library. You must not try to free it. You
627     should test the error pointer for NULL after calling <b>pcre_study()</b>, to be
628     sure that it has run successfully.
629 nigel 63 </P>
630     <P>
631     This is a typical call to <b>pcre_study</b>():
632     <pre>
633     pcre_extra *pe;
634     pe = pcre_study(
635     re, /* result of pcre_compile() */
636     0, /* no options exist */
637     &error); /* set to NULL or points to a message */
638 nigel 75 </pre>
639 nigel 63 At present, studying a pattern is useful only for non-anchored patterns that do
640     not have a single fixed starting character. A bitmap of possible starting
641 nigel 75 bytes is created.
642     <a name="localesupport"></a></P>
643 nigel 77 <br><a name="SEC9" href="#TOC1">LOCALE SUPPORT</a><br>
644 nigel 63 <P>
645 nigel 77 PCRE handles caseless matching, and determines whether characters are letters
646 nigel 75 digits, or whatever, by reference to a set of tables, indexed by character
647 nigel 77 value. When running in UTF-8 mode, this applies only to characters with codes
648 nigel 75 less than 128. Higher-valued codes never match escapes such as \w or \d, but
649     can be tested with \p if PCRE is built with Unicode character property
650 nigel 87 support. The use of locales with Unicode is discouraged.
651 nigel 63 </P>
652     <P>
653 nigel 75 An internal set of tables is created in the default C locale when PCRE is
654     built. This is used when the final argument of <b>pcre_compile()</b> is NULL,
655     and is sufficient for many applications. An alternative set of tables can,
656     however, be supplied. These may be created in a different locale from the
657     default. As more and more applications change to using Unicode, the need for
658     this locale support is expected to die away.
659 nigel 63 </P>
660     <P>
661 nigel 75 External tables are built by calling the <b>pcre_maketables()</b> function,
662     which has no arguments, in the relevant locale. The result can then be passed
663     to <b>pcre_compile()</b> or <b>pcre_exec()</b> as often as necessary. For
664     example, to build and use tables that are appropriate for the French locale
665     (where accented characters with values greater than 128 are treated as letters),
666     the following code could be used:
667 nigel 63 <pre>
668 nigel 75 setlocale(LC_CTYPE, "fr_FR");
669 nigel 63 tables = pcre_maketables();
670     re = pcre_compile(..., tables);
671 nigel 75 </pre>
672     When <b>pcre_maketables()</b> runs, the tables are built in memory that is
673     obtained via <b>pcre_malloc</b>. It is the caller's responsibility to ensure
674     that the memory containing the tables remains available for as long as it is
675     needed.
676 nigel 63 </P>
677     <P>
678 nigel 75 The pointer that is passed to <b>pcre_compile()</b> is saved with the compiled
679 nigel 63 pattern, and the same tables are used via this pointer by <b>pcre_study()</b>
680 nigel 75 and normally also by <b>pcre_exec()</b>. Thus, by default, for any single
681     pattern, compilation, studying and matching all happen in the same locale, but
682     different patterns can be compiled in different locales.
683 nigel 63 </P>
684     <P>
685 nigel 75 It is possible to pass a table pointer or NULL (indicating the use of the
686     internal tables) to <b>pcre_exec()</b>. Although not intended for this purpose,
687     this facility could be used to match a pattern in a different locale from the
688     one in which it was compiled. Passing table pointers at run time is discussed
689     below in the section on matching a pattern.
690     </P>
691 nigel 77 <br><a name="SEC10" href="#TOC1">INFORMATION ABOUT A PATTERN</a><br>
692 nigel 75 <P>
693 nigel 63 <b>int pcre_fullinfo(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
694     <b>int <i>what</i>, void *<i>where</i>);</b>
695     </P>
696     <P>
697     The <b>pcre_fullinfo()</b> function returns information about a compiled
698     pattern. It replaces the obsolete <b>pcre_info()</b> function, which is
699     nevertheless retained for backwards compability (and is documented below).
700     </P>
701     <P>
702     The first argument for <b>pcre_fullinfo()</b> is a pointer to the compiled
703     pattern. The second argument is the result of <b>pcre_study()</b>, or NULL if
704     the pattern was not studied. The third argument specifies which piece of
705     information is required, and the fourth argument is a pointer to a variable
706     to receive the data. The yield of the function is zero for success, or one of
707     the following negative numbers:
708     <pre>
709     PCRE_ERROR_NULL the argument <i>code</i> was NULL
710     the argument <i>where</i> was NULL
711     PCRE_ERROR_BADMAGIC the "magic number" was not found
712     PCRE_ERROR_BADOPTION the value of <i>what</i> was invalid
713 nigel 75 </pre>
714     The "magic number" is placed at the start of each compiled pattern as an simple
715     check against passing an arbitrary memory pointer. Here is a typical call of
716     <b>pcre_fullinfo()</b>, to obtain the length of the compiled pattern:
717 nigel 63 <pre>
718     int rc;
719     unsigned long int length;
720     rc = pcre_fullinfo(
721     re, /* result of pcre_compile() */
722     pe, /* result of pcre_study(), or NULL */
723     PCRE_INFO_SIZE, /* what is required */
724     &length); /* where to put the data */
725 nigel 75 </pre>
726 nigel 63 The possible values for the third argument are defined in <b>pcre.h</b>, and are
727     as follows:
728     <pre>
729     PCRE_INFO_BACKREFMAX
730 nigel 75 </pre>
731 nigel 63 Return the number of the highest back reference in the pattern. The fourth
732     argument should point to an <b>int</b> variable. Zero is returned if there are
733     no back references.
734     <pre>
735     PCRE_INFO_CAPTURECOUNT
736 nigel 75 </pre>
737 nigel 63 Return the number of capturing subpatterns in the pattern. The fourth argument
738 nigel 75 should point to an <b>int</b> variable.
739 nigel 63 <pre>
740 nigel 77 PCRE_INFO_DEFAULT_TABLES
741 nigel 75 </pre>
742     Return a pointer to the internal default character tables within PCRE. The
743     fourth argument should point to an <b>unsigned char *</b> variable. This
744     information call is provided for internal use by the <b>pcre_study()</b>
745     function. External callers can cause PCRE to use its internal tables by passing
746     a NULL table pointer.
747     <pre>
748 nigel 63 PCRE_INFO_FIRSTBYTE
749 nigel 75 </pre>
750 nigel 63 Return information about the first byte of any matched string, for a
751     non-anchored pattern. (This option used to be called PCRE_INFO_FIRSTCHAR; the
752     old name is still recognized for backwards compatibility.)
753     </P>
754     <P>
755 nigel 75 If there is a fixed first byte, for example, from a pattern such as
756     (cat|cow|coyote), it is returned in the integer pointed to by <i>where</i>.
757     Otherwise, if either
758     <br>
759     <br>
760 nigel 63 (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
761     starts with "^", or
762 nigel 75 <br>
763     <br>
764 nigel 63 (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
765     (if it were set, the pattern would be anchored),
766 nigel 75 <br>
767     <br>
768 nigel 63 -1 is returned, indicating that the pattern matches only at the start of a
769     subject string or after any newline within the string. Otherwise -2 is
770     returned. For anchored patterns, -2 is returned.
771     <pre>
772     PCRE_INFO_FIRSTTABLE
773 nigel 75 </pre>
774 nigel 63 If the pattern was studied, and this resulted in the construction of a 256-bit
775     table indicating a fixed set of bytes for the first byte in any matching
776     string, a pointer to the table is returned. Otherwise NULL is returned. The
777     fourth argument should point to an <b>unsigned char *</b> variable.
778     <pre>
779     PCRE_INFO_LASTLITERAL
780 nigel 75 </pre>
781 nigel 65 Return the value of the rightmost literal byte that must exist in any matched
782     string, other than at its start, if such a byte has been recorded. The fourth
783     argument should point to an <b>int</b> variable. If there is no such byte, -1 is
784     returned. For anchored patterns, a last literal byte is recorded only if it
785     follows something of variable length. For example, for the pattern
786     /^a\d+z\d+/ the returned value is "z", but for /^a\dz\d/ the returned value
787     is -1.
788 nigel 63 <pre>
789     PCRE_INFO_NAMECOUNT
790     PCRE_INFO_NAMEENTRYSIZE
791     PCRE_INFO_NAMETABLE
792 nigel 75 </pre>
793 nigel 63 PCRE supports the use of named as well as numbered capturing parentheses. The
794     names are just an additional way of identifying the parentheses, which still
795 nigel 75 acquire numbers. A convenience function called <b>pcre_get_named_substring()</b>
796     is provided for extracting an individual captured substring by name. It is also
797     possible to extract the data directly, by first converting the name to a number
798     in order to access the correct pointers in the output vector (described with
799     <b>pcre_exec()</b> below). To do the conversion, you need to use the
800     name-to-number map, which is described by these three values.
801 nigel 63 </P>
802     <P>
803     The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT gives
804     the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size of each
805     entry; both of these return an <b>int</b> value. The entry size depends on the
806     length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
807     entry of the table (a pointer to <b>char</b>). The first two bytes of each entry
808     are the number of the capturing parenthesis, most significant byte first. The
809     rest of the entry is the corresponding name, zero terminated. The names are in
810     alphabetical order. For example, consider the following pattern (assume
811     PCRE_EXTENDED is set, so white space - including newlines - is ignored):
812     <pre>
813 nigel 75 (?P&#60;date&#62; (?P&#60;year&#62;(\d\d)?\d\d) - (?P&#60;month&#62;\d\d) - (?P&#60;day&#62;\d\d) )
814     </pre>
815 nigel 63 There are four named subpatterns, so the table has four entries, and each entry
816     in the table is eight bytes long. The table is as follows, with non-printing
817 nigel 75 bytes shows in hexadecimal, and undefined bytes shown as ??:
818 nigel 63 <pre>
819     00 01 d a t e 00 ??
820     00 05 d a y 00 ?? ??
821     00 04 m o n t h 00
822     00 02 y e a r 00 ??
823 nigel 75 </pre>
824     When writing code to extract data from named subpatterns using the
825     name-to-number map, remember that the length of each entry is likely to be
826     different for each compiled pattern.
827 nigel 63 <pre>
828     PCRE_INFO_OPTIONS
829 nigel 75 </pre>
830 nigel 63 Return a copy of the options with which the pattern was compiled. The fourth
831     argument should point to an <b>unsigned long int</b> variable. These option bits
832     are those specified in the call to <b>pcre_compile()</b>, modified by any
833     top-level option settings within the pattern itself.
834     </P>
835     <P>
836     A pattern is automatically anchored by PCRE if all of its top-level
837     alternatives begin with one of the following:
838     <pre>
839     ^ unless PCRE_MULTILINE is set
840     \A always
841     \G always
842 nigel 75 .* if PCRE_DOTALL is set and there are no back references to the subpattern in which .* appears
843     </pre>
844 nigel 63 For such patterns, the PCRE_ANCHORED bit is set in the options returned by
845     <b>pcre_fullinfo()</b>.
846     <pre>
847     PCRE_INFO_SIZE
848 nigel 75 </pre>
849 nigel 63 Return the size of the compiled pattern, that is, the value that was passed as
850     the argument to <b>pcre_malloc()</b> when PCRE was getting memory in which to
851     place the compiled data. The fourth argument should point to a <b>size_t</b>
852     variable.
853     <pre>
854     PCRE_INFO_STUDYSIZE
855 nigel 75 </pre>
856     Return the size of the data block pointed to by the <i>study_data</i> field in
857 nigel 63 a <b>pcre_extra</b> block. That is, it is the value that was passed to
858     <b>pcre_malloc()</b> when PCRE was getting memory into which to place the data
859     created by <b>pcre_study()</b>. The fourth argument should point to a
860     <b>size_t</b> variable.
861     </P>
862 nigel 77 <br><a name="SEC11" href="#TOC1">OBSOLETE INFO FUNCTION</a><br>
863 nigel 63 <P>
864     <b>int pcre_info(const pcre *<i>code</i>, int *<i>optptr</i>, int</b>
865     <b>*<i>firstcharptr</i>);</b>
866     </P>
867     <P>
868     The <b>pcre_info()</b> function is now obsolete because its interface is too
869     restrictive to return all the available data about a compiled pattern. New
870     programs should use <b>pcre_fullinfo()</b> instead. The yield of
871     <b>pcre_info()</b> is the number of capturing subpatterns, or one of the
872     following negative numbers:
873     <pre>
874     PCRE_ERROR_NULL the argument <i>code</i> was NULL
875     PCRE_ERROR_BADMAGIC the "magic number" was not found
876 nigel 75 </pre>
877 nigel 63 If the <i>optptr</i> argument is not NULL, a copy of the options with which the
878     pattern was compiled is placed in the integer it points to (see
879     PCRE_INFO_OPTIONS above).
880     </P>
881     <P>
882     If the pattern is not anchored and the <i>firstcharptr</i> argument is not NULL,
883     it is used to pass back information about the first character of any matched
884     string (see PCRE_INFO_FIRSTBYTE above).
885     </P>
886 nigel 77 <br><a name="SEC12" href="#TOC1">REFERENCE COUNTS</a><br>
887 nigel 63 <P>
888 nigel 77 <b>int pcre_refcount(pcre *<i>code</i>, int <i>adjust</i>);</b>
889     </P>
890     <P>
891     The <b>pcre_refcount()</b> function is used to maintain a reference count in the
892     data block that contains a compiled pattern. It is provided for the benefit of
893     applications that operate in an object-oriented manner, where different parts
894     of the application may be using the same compiled pattern, but you want to free
895     the block when they are all done.
896     </P>
897     <P>
898     When a pattern is compiled, the reference count field is initialized to zero.
899     It is changed only by calling this function, whose action is to add the
900     <i>adjust</i> value (which may be positive or negative) to it. The yield of the
901     function is the new value. However, the value of the count is constrained to
902     lie between 0 and 65535, inclusive. If the new value is outside these limits,
903     it is forced to the appropriate limit value.
904     </P>
905     <P>
906     Except when it is zero, the reference count is not correctly preserved if a
907     pattern is compiled on one host and then transferred to a host whose byte-order
908     is different. (This seems a highly unlikely scenario.)
909     </P>
910     <br><a name="SEC13" href="#TOC1">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a><br>
911     <P>
912 nigel 63 <b>int pcre_exec(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
913     <b>const char *<i>subject</i>, int <i>length</i>, int <i>startoffset</i>,</b>
914     <b>int <i>options</i>, int *<i>ovector</i>, int <i>ovecsize</i>);</b>
915     </P>
916     <P>
917     The function <b>pcre_exec()</b> is called to match a subject string against a
918 nigel 75 compiled pattern, which is passed in the <i>code</i> argument. If the
919 nigel 63 pattern has been studied, the result of the study should be passed in the
920 nigel 77 <i>extra</i> argument. This function is the main matching facility of the
921     library, and it operates in a Perl-like manner. For specialist use there is
922     also an alternative matching function, which is described
923     <a href="#dfamatch">below</a>
924     in the section about the <b>pcre_dfa_exec()</b> function.
925 nigel 63 </P>
926     <P>
927 nigel 75 In most applications, the pattern will have been compiled (and optionally
928     studied) in the same process that calls <b>pcre_exec()</b>. However, it is
929     possible to save compiled patterns and study data, and then use them later
930     in different processes, possibly even on different hosts. For a discussion
931     about this, see the
932     <a href="pcreprecompile.html"><b>pcreprecompile</b></a>
933     documentation.
934 nigel 63 </P>
935     <P>
936 nigel 75 Here is an example of a simple call to <b>pcre_exec()</b>:
937 nigel 63 <pre>
938     int rc;
939     int ovector[30];
940     rc = pcre_exec(
941     re, /* result of pcre_compile() */
942     NULL, /* we didn't study the pattern */
943     "some string", /* the subject string */
944     11, /* the length of the subject string */
945     0, /* start at offset 0 in the subject */
946     0, /* default options */
947 nigel 75 ovector, /* vector of integers for substring information */
948 nigel 77 30); /* number of elements (NOT size in bytes) */
949 nigel 75 <a name="extradata"></a></PRE>
950 nigel 63 </P>
951 nigel 75 <br><b>
952     Extra data for <b>pcre_exec()</b>
953     </b><br>
954 nigel 63 <P>
955     If the <i>extra</i> argument is not NULL, it must point to a <b>pcre_extra</b>
956     data block. The <b>pcre_study()</b> function returns such a block (when it
957     doesn't return NULL), but you can also create one for yourself, and pass
958 nigel 87 additional information in it. The <b>pcre_extra</b> block contains the following
959     fields (not necessarily in this order):
960 nigel 63 <pre>
961     unsigned long int <i>flags</i>;
962     void *<i>study_data</i>;
963     unsigned long int <i>match_limit</i>;
964 nigel 87 unsigned long int <i>match_limit_recursion</i>;
965 nigel 63 void *<i>callout_data</i>;
966 nigel 75 const unsigned char *<i>tables</i>;
967     </pre>
968 nigel 63 The <i>flags</i> field is a bitmap that specifies which of the other fields
969     are set. The flag bits are:
970     <pre>
971     PCRE_EXTRA_STUDY_DATA
972     PCRE_EXTRA_MATCH_LIMIT
973 nigel 87 PCRE_EXTRA_MATCH_LIMIT_RECURSION
974 nigel 63 PCRE_EXTRA_CALLOUT_DATA
975 nigel 75 PCRE_EXTRA_TABLES
976     </pre>
977 nigel 63 Other flag bits should be set to zero. The <i>study_data</i> field is set in the
978     <b>pcre_extra</b> block that is returned by <b>pcre_study()</b>, together with
979 nigel 75 the appropriate flag bit. You should not set this yourself, but you may add to
980     the block by setting the other fields and their corresponding flag bits.
981 nigel 63 </P>
982     <P>
983     The <i>match_limit</i> field provides a means of preventing PCRE from using up a
984     vast amount of resources when running patterns that are not going to match,
985     but which have a very large number of possibilities in their search trees. The
986 nigel 75 classic example is the use of nested unlimited repeats.
987 nigel 63 </P>
988     <P>
989 nigel 75 Internally, PCRE uses a function called <b>match()</b> which it calls repeatedly
990 nigel 87 (sometimes recursively). The limit set by <i>match_limit</i> is imposed on the
991     number of times this function is called during a match, which has the effect of
992     limiting the amount of backtracking that can take place. For patterns that are
993     not anchored, the count restarts from zero for each position in the subject
994     string.
995 nigel 75 </P>
996     <P>
997 nigel 87 The default value for the limit can be set when PCRE is built; the default
998 nigel 63 default is 10 million, which handles all but the most extreme cases. You can
999 nigel 87 override the default by suppling <b>pcre_exec()</b> with a <b>pcre_extra</b>
1000     block in which <i>match_limit</i> is set, and PCRE_EXTRA_MATCH_LIMIT is set in
1001     the <i>flags</i> field. If the limit is exceeded, <b>pcre_exec()</b> returns
1002     PCRE_ERROR_MATCHLIMIT.
1003 nigel 63 </P>
1004     <P>
1005 nigel 87 The <i>match_limit_recursion</i> field is similar to <i>match_limit</i>, but
1006     instead of limiting the total number of times that <b>match()</b> is called, it
1007     limits the depth of recursion. The recursion depth is a smaller number than the
1008     total number of calls, because not all calls to <b>match()</b> are recursive.
1009     This limit is of use only if it is set smaller than <i>match_limit</i>.
1010     </P>
1011     <P>
1012     Limiting the recursion depth limits the amount of stack that can be used, or,
1013     when PCRE has been compiled to use memory on the heap instead of the stack, the
1014     amount of heap memory that can be used.
1015     </P>
1016     <P>
1017     The default value for <i>match_limit_recursion</i> can be set when PCRE is
1018     built; the default default is the same value as the default for
1019     <i>match_limit</i>. You can override the default by suppling <b>pcre_exec()</b>
1020     with a <b>pcre_extra</b> block in which <i>match_limit_recursion</i> is set, and
1021     PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the <i>flags</i> field. If the limit
1022     is exceeded, <b>pcre_exec()</b> returns PCRE_ERROR_RECURSIONLIMIT.
1023     </P>
1024     <P>
1025 nigel 63 The <i>pcre_callout</i> field is used in conjunction with the "callout" feature,
1026 nigel 75 which is described in the
1027     <a href="pcrecallout.html"><b>pcrecallout</b></a>
1028     documentation.
1029 nigel 63 </P>
1030     <P>
1031 nigel 75 The <i>tables</i> field is used to pass a character tables pointer to
1032     <b>pcre_exec()</b>; this overrides the value that is stored with the compiled
1033     pattern. A non-NULL value is stored with the compiled pattern only if custom
1034     tables were supplied to <b>pcre_compile()</b> via its <i>tableptr</i> argument.
1035     If NULL is passed to <b>pcre_exec()</b> using this mechanism, it forces PCRE's
1036     internal tables to be used. This facility is helpful when re-using patterns
1037     that have been saved after compiling with an external set of tables, because
1038     the external tables might be at a different address when <b>pcre_exec()</b> is
1039     called. See the
1040     <a href="pcreprecompile.html"><b>pcreprecompile</b></a>
1041     documentation for a discussion of saving compiled patterns for later use.
1042 nigel 63 </P>
1043 nigel 75 <br><b>
1044     Option bits for <b>pcre_exec()</b>
1045     </b><br>
1046 nigel 63 <P>
1047 nigel 75 The unused bits of the <i>options</i> argument for <b>pcre_exec()</b> must be
1048     zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL,
1049     PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK and PCRE_PARTIAL.
1050 nigel 63 <pre>
1051 nigel 75 PCRE_ANCHORED
1052     </pre>
1053     The PCRE_ANCHORED option limits <b>pcre_exec()</b> to matching at the first
1054     matching position. If a pattern was compiled with PCRE_ANCHORED, or turned out
1055     to be anchored by virtue of its contents, it cannot be made unachored at
1056     matching time.
1057     <pre>
1058 nigel 63 PCRE_NOTBOL
1059 nigel 75 </pre>
1060     This option specifies that first character of the subject string is not the
1061     beginning of a line, so the circumflex metacharacter should not match before
1062     it. Setting this without PCRE_MULTILINE (at compile time) causes circumflex
1063     never to match. This option affects only the behaviour of the circumflex
1064     metacharacter. It does not affect \A.
1065 nigel 63 <pre>
1066     PCRE_NOTEOL
1067 nigel 75 </pre>
1068     This option specifies that the end of the subject string is not the end of a
1069     line, so the dollar metacharacter should not match it nor (except in multiline
1070     mode) a newline immediately before it. Setting this without PCRE_MULTILINE (at
1071     compile time) causes dollar never to match. This option affects only the
1072     behaviour of the dollar metacharacter. It does not affect \Z or \z.
1073 nigel 63 <pre>
1074     PCRE_NOTEMPTY
1075 nigel 75 </pre>
1076 nigel 63 An empty string is not considered to be a valid match if this option is set. If
1077     there are alternatives in the pattern, they are tried. If all the alternatives
1078     match the empty string, the entire match fails. For example, if the pattern
1079     <pre>
1080     a?b?
1081 nigel 75 </pre>
1082 nigel 63 is applied to a string not beginning with "a" or "b", it matches the empty
1083     string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
1084     valid, so PCRE searches further into the string for occurrences of "a" or "b".
1085     </P>
1086     <P>
1087     Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
1088     of a pattern match of the empty string within its <b>split()</b> function, and
1089     when using the /g modifier. It is possible to emulate Perl's behaviour after
1090     matching a null string by first trying the match again at the same offset with
1091 nigel 75 PCRE_NOTEMPTY and PCRE_ANCHORED, and then if that fails by advancing the
1092     starting offset (see below) and trying an ordinary match again. There is some
1093     code that demonstrates how to do this in the <i>pcredemo.c</i> sample program.
1094     <pre>
1095     PCRE_NO_UTF8_CHECK
1096     </pre>
1097     When PCRE_UTF8 is set at compile time, the validity of the subject as a UTF-8
1098     string is automatically checked when <b>pcre_exec()</b> is subsequently called.
1099     The value of <i>startoffset</i> is also checked to ensure that it points to the
1100     start of a UTF-8 character. If an invalid UTF-8 sequence of bytes is found,
1101     <b>pcre_exec()</b> returns the error PCRE_ERROR_BADUTF8. If <i>startoffset</i>
1102     contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.
1103 nigel 63 </P>
1104     <P>
1105 nigel 75 If you already know that your subject is valid, and you want to skip these
1106     checks for performance reasons, you can set the PCRE_NO_UTF8_CHECK option when
1107     calling <b>pcre_exec()</b>. You might want to do this for the second and
1108     subsequent calls to <b>pcre_exec()</b> if you are making repeated calls to find
1109     all the matches in a single subject string. However, you should be sure that
1110     the value of <i>startoffset</i> points to the start of a UTF-8 character. When
1111     PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid UTF-8 string as a
1112     subject, or a value of <i>startoffset</i> that does not point to the start of a
1113     UTF-8 character, is undefined. Your program may crash.
1114     <pre>
1115     PCRE_PARTIAL
1116     </pre>
1117     This option turns on the partial matching feature. If the subject string fails
1118     to match the pattern, but at some point during the matching process the end of
1119     the subject was reached (that is, the subject partially matches the pattern and
1120     the failure to match occurred only because there were not enough subject
1121     characters), <b>pcre_exec()</b> returns PCRE_ERROR_PARTIAL instead of
1122     PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is used, there are restrictions on what
1123     may appear in the pattern. These are discussed in the
1124     <a href="pcrepartial.html"><b>pcrepartial</b></a>
1125     documentation.
1126     </P>
1127     <br><b>
1128     The string to be matched by <b>pcre_exec()</b>
1129     </b><br>
1130     <P>
1131 nigel 63 The subject string is passed to <b>pcre_exec()</b> as a pointer in
1132 nigel 73 <i>subject</i>, a length in <i>length</i>, and a starting byte offset in
1133 nigel 75 <i>startoffset</i>. In UTF-8 mode, the byte offset must point to the start of a
1134     UTF-8 character. Unlike the pattern string, the subject may contain binary zero
1135     bytes. When the starting offset is zero, the search for a match starts at the
1136     beginning of the subject, and this is by far the most common case.
1137 nigel 63 </P>
1138     <P>
1139     A non-zero starting offset is useful when searching for another match in the
1140     same subject by calling <b>pcre_exec()</b> again after a previous success.
1141     Setting <i>startoffset</i> differs from just passing over a shortened string and
1142     setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
1143     lookbehind. For example, consider the pattern
1144     <pre>
1145     \Biss\B
1146 nigel 75 </pre>
1147 nigel 63 which finds occurrences of "iss" in the middle of words. (\B matches only if
1148     the current position in the subject is not a word boundary.) When applied to
1149     the string "Mississipi" the first call to <b>pcre_exec()</b> finds the first
1150     occurrence. If <b>pcre_exec()</b> is called again with just the remainder of the
1151     subject, namely "issipi", it does not match, because \B is always false at the
1152     start of the subject, which is deemed to be a word boundary. However, if
1153     <b>pcre_exec()</b> is passed the entire string again, but with <i>startoffset</i>
1154     set to 4, it finds the second occurrence of "iss" because it is able to look
1155     behind the starting point to discover that it is preceded by a letter.
1156     </P>
1157     <P>
1158     If a non-zero starting offset is passed when the pattern is anchored, one
1159 nigel 75 attempt to match at the given offset is made. This can only succeed if the
1160 nigel 63 pattern does not require the match to be at the start of the subject.
1161     </P>
1162 nigel 75 <br><b>
1163     How <b>pcre_exec()</b> returns captured substrings
1164     </b><br>
1165 nigel 63 <P>
1166     In general, a pattern matches a certain portion of the subject, and in
1167     addition, further substrings from the subject may be picked out by parts of the
1168     pattern. Following the usage in Jeffrey Friedl's book, this is called
1169     "capturing" in what follows, and the phrase "capturing subpattern" is used for
1170     a fragment of a pattern that picks out a substring. PCRE supports several other
1171     kinds of parenthesized subpattern that do not cause substrings to be captured.
1172     </P>
1173     <P>
1174     Captured substrings are returned to the caller via a vector of integer offsets
1175     whose address is passed in <i>ovector</i>. The number of elements in the vector
1176 nigel 75 is passed in <i>ovecsize</i>, which must be a non-negative number. <b>Note</b>:
1177     this argument is NOT the size of <i>ovector</i> in bytes.
1178 nigel 63 </P>
1179     <P>
1180 nigel 75 The first two-thirds of the vector is used to pass back captured substrings,
1181     each substring using a pair of integers. The remaining third of the vector is
1182     used as workspace by <b>pcre_exec()</b> while matching capturing subpatterns,
1183     and is not available for passing back information. The length passed in
1184     <i>ovecsize</i> should always be a multiple of three. If it is not, it is
1185     rounded down.
1186     </P>
1187     <P>
1188     When a match is successful, information about captured substrings is returned
1189     in pairs of integers, starting at the beginning of <i>ovector</i>, and
1190 nigel 63 continuing up to two-thirds of its length at the most. The first element of a
1191     pair is set to the offset of the first character in a substring, and the second
1192     is set to the offset of the first character after the end of a substring. The
1193     first pair, <i>ovector[0]</i> and <i>ovector[1]</i>, identify the portion of the
1194     subject string matched by the entire pattern. The next pair is used for the
1195     first capturing subpattern, and so on. The value returned by <b>pcre_exec()</b>
1196     is the number of pairs that have been set. If there are no capturing
1197     subpatterns, the return value from a successful match is 1, indicating that
1198     just the first pair of offsets has been set.
1199     </P>
1200     <P>
1201     Some convenience functions are provided for extracting the captured substrings
1202     as separate strings. These are described in the following section.
1203     </P>
1204     <P>
1205     It is possible for an capturing subpattern number <i>n+1</i> to match some
1206     part of the subject when subpattern <i>n</i> has not been used at all. For
1207     example, if the string "abc" is matched against the pattern (a|(z))(bc)
1208     subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
1209     values corresponding to the unused subpattern are set to -1.
1210     </P>
1211     <P>
1212     If a capturing subpattern is matched repeatedly, it is the last portion of the
1213 nigel 75 string that it matched that is returned.
1214 nigel 63 </P>
1215     <P>
1216 nigel 75 If the vector is too small to hold all the captured substring offsets, it is
1217     used as far as possible (up to two-thirds of its length), and the function
1218     returns a value of zero. In particular, if the substring offsets are not of
1219     interest, <b>pcre_exec()</b> may be called with <i>ovector</i> passed as NULL and
1220 nigel 63 <i>ovecsize</i> as zero. However, if the pattern contains back references and
1221 nigel 75 the <i>ovector</i> is not big enough to remember the related substrings, PCRE
1222     has to get additional memory for use during matching. Thus it is usually
1223     advisable to supply an <i>ovector</i>.
1224 nigel 63 </P>
1225     <P>
1226     Note that <b>pcre_info()</b> can be used to find out how many capturing
1227     subpatterns there are in a compiled pattern. The smallest size for
1228     <i>ovector</i> that will allow for <i>n</i> captured substrings, in addition to
1229     the offsets of the substring matched by the whole pattern, is (<i>n</i>+1)*3.
1230 nigel 77 <a name="errorlist"></a></P>
1231 nigel 75 <br><b>
1232     Return values from <b>pcre_exec()</b>
1233     </b><br>
1234 nigel 63 <P>
1235     If <b>pcre_exec()</b> fails, it returns a negative number. The following are
1236     defined in the header file:
1237     <pre>
1238     PCRE_ERROR_NOMATCH (-1)
1239 nigel 75 </pre>
1240 nigel 63 The subject string did not match the pattern.
1241     <pre>
1242     PCRE_ERROR_NULL (-2)
1243 nigel 75 </pre>
1244 nigel 63 Either <i>code</i> or <i>subject</i> was passed as NULL, or <i>ovector</i> was
1245     NULL and <i>ovecsize</i> was not zero.
1246     <pre>
1247     PCRE_ERROR_BADOPTION (-3)
1248 nigel 75 </pre>
1249 nigel 63 An unrecognized bit was set in the <i>options</i> argument.
1250     <pre>
1251     PCRE_ERROR_BADMAGIC (-4)
1252 nigel 75 </pre>
1253 nigel 63 PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
1254 nigel 75 the case when it is passed a junk pointer and to detect when a pattern that was
1255     compiled in an environment of one endianness is run in an environment with the
1256     other endianness. This is the error that PCRE gives when the magic number is
1257     not present.
1258 nigel 63 <pre>
1259     PCRE_ERROR_UNKNOWN_NODE (-5)
1260 nigel 75 </pre>
1261 nigel 63 While running the pattern match, an unknown item was encountered in the
1262     compiled pattern. This error could be caused by a bug in PCRE or by overwriting
1263     of the compiled pattern.
1264     <pre>
1265     PCRE_ERROR_NOMEMORY (-6)
1266 nigel 75 </pre>
1267 nigel 63 If a pattern contains back references, but the <i>ovector</i> that is passed to
1268     <b>pcre_exec()</b> is not big enough to remember the referenced substrings, PCRE
1269     gets a block of memory at the start of matching to use for this purpose. If the
1270 nigel 75 call via <b>pcre_malloc()</b> fails, this error is given. The memory is
1271     automatically freed at the end of matching.
1272 nigel 63 <pre>
1273     PCRE_ERROR_NOSUBSTRING (-7)
1274 nigel 75 </pre>
1275 nigel 63 This error is used by the <b>pcre_copy_substring()</b>,
1276     <b>pcre_get_substring()</b>, and <b>pcre_get_substring_list()</b> functions (see
1277     below). It is never returned by <b>pcre_exec()</b>.
1278     <pre>
1279     PCRE_ERROR_MATCHLIMIT (-8)
1280 nigel 75 </pre>
1281 nigel 87 The backtracking limit, as specified by the <i>match_limit</i> field in a
1282     <b>pcre_extra</b> structure (or defaulted) was reached. See the description
1283     above.
1284     <pre>
1285     PCRE_ERROR_RECURSIONLIMIT (-21)
1286     </pre>
1287     The internal recursion limit, as specified by the <i>match_limit_recursion</i>
1288 nigel 63 field in a <b>pcre_extra</b> structure (or defaulted) was reached. See the
1289     description above.
1290     <pre>
1291     PCRE_ERROR_CALLOUT (-9)
1292 nigel 75 </pre>
1293 nigel 63 This error is never generated by <b>pcre_exec()</b> itself. It is provided for
1294     use by callout functions that want to yield a distinctive error code. See the
1295 nigel 75 <a href="pcrecallout.html"><b>pcrecallout</b></a>
1296     documentation for details.
1297 nigel 71 <pre>
1298 nigel 73 PCRE_ERROR_BADUTF8 (-10)
1299 nigel 75 </pre>
1300 nigel 71 A string that contains an invalid UTF-8 byte sequence was passed as a subject.
1301 nigel 73 <pre>
1302     PCRE_ERROR_BADUTF8_OFFSET (-11)
1303 nigel 75 </pre>
1304 nigel 73 The UTF-8 byte sequence that was passed as a subject was valid, but the value
1305     of <i>startoffset</i> did not point to the beginning of a UTF-8 character.
1306 nigel 75 <pre>
1307 nigel 77 PCRE_ERROR_PARTIAL (-12)
1308 nigel 75 </pre>
1309     The subject string did not match, but it did match partially. See the
1310     <a href="pcrepartial.html"><b>pcrepartial</b></a>
1311     documentation for details of partial matching.
1312     <pre>
1313 nigel 77 PCRE_ERROR_BADPARTIAL (-13)
1314 nigel 75 </pre>
1315     The PCRE_PARTIAL option was used with a compiled pattern containing items that
1316     are not supported for partial matching. See the
1317     <a href="pcrepartial.html"><b>pcrepartial</b></a>
1318     documentation for details of partial matching.
1319     <pre>
1320 nigel 77 PCRE_ERROR_INTERNAL (-14)
1321 nigel 75 </pre>
1322     An unexpected internal error has occurred. This error could be caused by a bug
1323     in PCRE or by overwriting of the compiled pattern.
1324     <pre>
1325 nigel 77 PCRE_ERROR_BADCOUNT (-15)
1326 nigel 75 </pre>
1327     This error is given if the value of the <i>ovecsize</i> argument is negative.
1328 nigel 73 </P>
1329 nigel 77 <br><a name="SEC14" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a><br>
1330 nigel 63 <P>
1331     <b>int pcre_copy_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
1332     <b>int <i>stringcount</i>, int <i>stringnumber</i>, char *<i>buffer</i>,</b>
1333     <b>int <i>buffersize</i>);</b>
1334     </P>
1335     <P>
1336     <b>int pcre_get_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
1337     <b>int <i>stringcount</i>, int <i>stringnumber</i>,</b>
1338     <b>const char **<i>stringptr</i>);</b>
1339     </P>
1340     <P>
1341     <b>int pcre_get_substring_list(const char *<i>subject</i>,</b>
1342     <b>int *<i>ovector</i>, int <i>stringcount</i>, const char ***<i>listptr</i>);</b>
1343     </P>
1344     <P>
1345     Captured substrings can be accessed directly by using the offsets returned by
1346     <b>pcre_exec()</b> in <i>ovector</i>. For convenience, the functions
1347     <b>pcre_copy_substring()</b>, <b>pcre_get_substring()</b>, and
1348     <b>pcre_get_substring_list()</b> are provided for extracting captured substrings
1349     as new, separate, zero-terminated strings. These functions identify substrings
1350     by number. The next section describes functions for extracting named
1351     substrings. A substring that contains a binary zero is correctly extracted and
1352     has a further zero added on the end, but the result is not, of course,
1353     a C string.
1354     </P>
1355     <P>
1356     The first three arguments are the same for all three of these functions:
1357 nigel 75 <i>subject</i> is the subject string that has just been successfully matched,
1358 nigel 63 <i>ovector</i> is a pointer to the vector of integer offsets that was passed to
1359     <b>pcre_exec()</b>, and <i>stringcount</i> is the number of substrings that were
1360     captured by the match, including the substring that matched the entire regular
1361 nigel 75 expression. This is the value returned by <b>pcre_exec()</b> if it is greater
1362     than zero. If <b>pcre_exec()</b> returned zero, indicating that it ran out of
1363     space in <i>ovector</i>, the value passed as <i>stringcount</i> should be the
1364     number of elements in the vector divided by three.
1365 nigel 63 </P>
1366     <P>
1367     The functions <b>pcre_copy_substring()</b> and <b>pcre_get_substring()</b>
1368     extract a single substring, whose number is given as <i>stringnumber</i>. A
1369 nigel 75 value of zero extracts the substring that matched the entire pattern, whereas
1370 nigel 63 higher values extract the captured substrings. For <b>pcre_copy_substring()</b>,
1371     the string is placed in <i>buffer</i>, whose length is given by
1372     <i>buffersize</i>, while for <b>pcre_get_substring()</b> a new block of memory is
1373     obtained via <b>pcre_malloc</b>, and its address is returned via
1374     <i>stringptr</i>. The yield of the function is the length of the string, not
1375     including the terminating zero, or one of
1376     <pre>
1377     PCRE_ERROR_NOMEMORY (-6)
1378 nigel 75 </pre>
1379 nigel 63 The buffer was too small for <b>pcre_copy_substring()</b>, or the attempt to get
1380     memory failed for <b>pcre_get_substring()</b>.
1381     <pre>
1382     PCRE_ERROR_NOSUBSTRING (-7)
1383 nigel 75 </pre>
1384 nigel 63 There is no substring whose number is <i>stringnumber</i>.
1385     </P>
1386     <P>
1387     The <b>pcre_get_substring_list()</b> function extracts all available substrings
1388     and builds a list of pointers to them. All this is done in a single block of
1389 nigel 75 memory that is obtained via <b>pcre_malloc</b>. The address of the memory block
1390 nigel 63 is returned via <i>listptr</i>, which is also the start of the list of string
1391     pointers. The end of the list is marked by a NULL pointer. The yield of the
1392     function is zero if all went well, or
1393     <pre>
1394     PCRE_ERROR_NOMEMORY (-6)
1395 nigel 75 </pre>
1396 nigel 63 if the attempt to get the memory block failed.
1397     </P>
1398     <P>
1399     When any of these functions encounter a substring that is unset, which can
1400     happen when capturing subpattern number <i>n+1</i> matches some part of the
1401     subject, but subpattern <i>n</i> has not been used at all, they return an empty
1402     string. This can be distinguished from a genuine zero-length substring by
1403     inspecting the appropriate offset in <i>ovector</i>, which is negative for unset
1404     substrings.
1405     </P>
1406     <P>
1407     The two convenience functions <b>pcre_free_substring()</b> and
1408     <b>pcre_free_substring_list()</b> can be used to free the memory returned by
1409     a previous call of <b>pcre_get_substring()</b> or
1410     <b>pcre_get_substring_list()</b>, respectively. They do nothing more than call
1411     the function pointed to by <b>pcre_free</b>, which of course could be called
1412     directly from a C program. However, PCRE is used in some situations where it is
1413     linked via a special interface to another programming language which cannot use
1414     <b>pcre_free</b> directly; it is for these cases that the functions are
1415     provided.
1416     </P>
1417 nigel 77 <br><a name="SEC15" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a><br>
1418 nigel 63 <P>
1419 nigel 75 <b>int pcre_get_stringnumber(const pcre *<i>code</i>,</b>
1420     <b>const char *<i>name</i>);</b>
1421     </P>
1422     <P>
1423 nigel 63 <b>int pcre_copy_named_substring(const pcre *<i>code</i>,</b>
1424     <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
1425     <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
1426     <b>char *<i>buffer</i>, int <i>buffersize</i>);</b>
1427     </P>
1428     <P>
1429     <b>int pcre_get_named_substring(const pcre *<i>code</i>,</b>
1430     <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
1431     <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
1432     <b>const char **<i>stringptr</i>);</b>
1433     </P>
1434     <P>
1435 nigel 75 To extract a substring by name, you first have to find associated number.
1436     For example, for this pattern
1437 nigel 63 <pre>
1438 nigel 79 (a+)b(?P&#60;xxx&#62;\d+)...
1439 nigel 75 </pre>
1440     the number of the subpattern called "xxx" is 2. You can find the number from
1441     the name by calling <b>pcre_get_stringnumber()</b>. The first argument is the
1442     compiled pattern, and the second is the name. The yield of the function is the
1443     subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no subpattern of
1444     that name.
1445 nigel 63 </P>
1446     <P>
1447 nigel 75 Given the number, you can extract the substring directly, or use one of the
1448     functions described in the previous section. For convenience, there are also
1449     two functions that do the whole job.
1450 nigel 63 </P>
1451     <P>
1452     Most of the arguments of <i>pcre_copy_named_substring()</i> and
1453 nigel 75 <i>pcre_get_named_substring()</i> are the same as those for the similarly named
1454     functions that extract by number. As these are described in the previous
1455     section, they are not re-described here. There are just two differences:
1456 nigel 63 </P>
1457     <P>
1458     First, instead of a substring number, a substring name is given. Second, there
1459     is an extra argument, given at the start, which is a pointer to the compiled
1460     pattern. This is needed in order to gain access to the name-to-number
1461     translation table.
1462     </P>
1463     <P>
1464     These functions call <b>pcre_get_stringnumber()</b>, and if it succeeds, they
1465     then call <i>pcre_copy_substring()</i> or <i>pcre_get_substring()</i>, as
1466     appropriate.
1467     </P>
1468 nigel 77 <br><a name="SEC16" href="#TOC1">FINDING ALL POSSIBLE MATCHES</a><br>
1469 nigel 63 <P>
1470 nigel 77 The traditional matching function uses a similar algorithm to Perl, which stops
1471     when it finds the first match, starting at a given point in the subject. If you
1472     want to find all possible matches, or the longest possible match, consider
1473     using the alternative matching function (see below) instead. If you cannot use
1474     the alternative function, but still need to find all possible matches, you
1475     can kludge it up by making use of the callout facility, which is described in
1476     the
1477     <a href="pcrecallout.html"><b>pcrecallout</b></a>
1478     documentation.
1479     </P>
1480     <P>
1481     What you have to do is to insert a callout right at the end of the pattern.
1482     When your callout function is called, extract and save the current matched
1483     substring. Then return 1, which forces <b>pcre_exec()</b> to backtrack and try
1484     other alternatives. Ultimately, when it runs out of matches, <b>pcre_exec()</b>
1485     will yield PCRE_ERROR_NOMATCH.
1486     <a name="dfamatch"></a></P>
1487     <br><a name="SEC17" href="#TOC1">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a><br>
1488     <P>
1489     <b>int pcre_dfa_exec(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
1490     <b>const char *<i>subject</i>, int <i>length</i>, int <i>startoffset</i>,</b>
1491     <b>int <i>options</i>, int *<i>ovector</i>, int <i>ovecsize</i>,</b>
1492     <b>int *<i>workspace</i>, int <i>wscount</i>);</b>
1493     </P>
1494     <P>
1495     The function <b>pcre_dfa_exec()</b> is called to match a subject string against
1496     a compiled pattern, using a "DFA" matching algorithm. This has different
1497     characteristics to the normal algorithm, and is not compatible with Perl. Some
1498     of the features of PCRE patterns are not supported. Nevertheless, there are
1499     times when this kind of matching can be useful. For a discussion of the two
1500     matching algorithms, see the
1501     <a href="pcrematching.html"><b>pcrematching</b></a>
1502     documentation.
1503     </P>
1504     <P>
1505     The arguments for the <b>pcre_dfa_exec()</b> function are the same as for
1506     <b>pcre_exec()</b>, plus two extras. The <i>ovector</i> argument is used in a
1507     different way, and this is described below. The other common arguments are used
1508     in the same way as for <b>pcre_exec()</b>, so their description is not repeated
1509     here.
1510     </P>
1511     <P>
1512     The two additional arguments provide workspace for the function. The workspace
1513     vector should contain at least 20 elements. It is used for keeping track of
1514     multiple paths through the pattern tree. More workspace will be needed for
1515     patterns and subjects where there are a lot of possible matches.
1516     </P>
1517     <P>
1518 nigel 87 Here is an example of a simple call to <b>pcre_dfa_exec()</b>:
1519 nigel 77 <pre>
1520     int rc;
1521     int ovector[10];
1522     int wspace[20];
1523 nigel 87 rc = pcre_dfa_exec(
1524 nigel 77 re, /* result of pcre_compile() */
1525     NULL, /* we didn't study the pattern */
1526     "some string", /* the subject string */
1527     11, /* the length of the subject string */
1528     0, /* start at offset 0 in the subject */
1529     0, /* default options */
1530     ovector, /* vector of integers for substring information */
1531     10, /* number of elements (NOT size in bytes) */
1532     wspace, /* working space vector */
1533     20); /* number of elements (NOT size in bytes) */
1534     </PRE>
1535     </P>
1536     <br><b>
1537     Option bits for <b>pcre_dfa_exec()</b>
1538     </b><br>
1539     <P>
1540     The unused bits of the <i>options</i> argument for <b>pcre_dfa_exec()</b> must be
1541     zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL,
1542     PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL,
1543     PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last three of these are
1544     the same as for <b>pcre_exec()</b>, so their description is not repeated here.
1545     <pre>
1546     PCRE_PARTIAL
1547     </pre>
1548     This has the same general effect as it does for <b>pcre_exec()</b>, but the
1549     details are slightly different. When PCRE_PARTIAL is set for
1550     <b>pcre_dfa_exec()</b>, the return code PCRE_ERROR_NOMATCH is converted into
1551     PCRE_ERROR_PARTIAL if the end of the subject is reached, there have been no
1552     complete matches, but there is still at least one matching possibility. The
1553     portion of the string that provided the partial match is set as the first
1554     matching string.
1555     <pre>
1556     PCRE_DFA_SHORTEST
1557     </pre>
1558     Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to stop as
1559     soon as it has found one match. Because of the way the DFA algorithm works,
1560     this is necessarily the shortest possible match at the first possible matching
1561     point in the subject string.
1562     <pre>
1563     PCRE_DFA_RESTART
1564     </pre>
1565     When <b>pcre_dfa_exec()</b> is called with the PCRE_PARTIAL option, and returns
1566     a partial match, it is possible to call it again, with additional subject
1567     characters, and have it continue with the same match. The PCRE_DFA_RESTART
1568     option requests this action; when it is set, the <i>workspace</i> and
1569     <i>wscount</i> options must reference the same vector as before because data
1570     about the match so far is left in them after a partial match. There is more
1571     discussion of this facility in the
1572     <a href="pcrepartial.html"><b>pcrepartial</b></a>
1573     documentation.
1574     </P>
1575     <br><b>
1576     Successful returns from <b>pcre_dfa_exec()</b>
1577     </b><br>
1578     <P>
1579     When <b>pcre_dfa_exec()</b> succeeds, it may have matched more than one
1580     substring in the subject. Note, however, that all the matches from one run of
1581     the function start at the same point in the subject. The shorter matches are
1582     all initial substrings of the longer matches. For example, if the pattern
1583     <pre>
1584     &#60;.*&#62;
1585     </pre>
1586     is matched against the string
1587     <pre>
1588     This is &#60;something&#62; &#60;something else&#62; &#60;something further&#62; no more
1589     </pre>
1590     the three matched strings are
1591     <pre>
1592     &#60;something&#62;
1593     &#60;something&#62; &#60;something else&#62;
1594     &#60;something&#62; &#60;something else&#62; &#60;something further&#62;
1595     </pre>
1596     On success, the yield of the function is a number greater than zero, which is
1597     the number of matched substrings. The substrings themselves are returned in
1598     <i>ovector</i>. Each string uses two elements; the first is the offset to the
1599     start, and the second is the offset to the end. All the strings have the same
1600     start offset. (Space could have been saved by giving this only once, but it was
1601     decided to retain some compatibility with the way <b>pcre_exec()</b> returns
1602     data, even though the meaning of the strings is different.)
1603     </P>
1604     <P>
1605     The strings are returned in reverse order of length; that is, the longest
1606     matching string is given first. If there were too many matches to fit into
1607     <i>ovector</i>, the yield of the function is zero, and the vector is filled with
1608     the longest matches.
1609     </P>
1610     <br><b>
1611     Error returns from <b>pcre_dfa_exec()</b>
1612     </b><br>
1613     <P>
1614     The <b>pcre_dfa_exec()</b> function returns a negative number when it fails.
1615     Many of the errors are the same as for <b>pcre_exec()</b>, and these are
1616     described
1617     <a href="#errorlist">above.</a>
1618     There are in addition the following errors that are specific to
1619     <b>pcre_dfa_exec()</b>:
1620     <pre>
1621     PCRE_ERROR_DFA_UITEM (-16)
1622     </pre>
1623     This return is given if <b>pcre_dfa_exec()</b> encounters an item in the pattern
1624     that it does not support, for instance, the use of \C or a back reference.
1625     <pre>
1626     PCRE_ERROR_DFA_UCOND (-17)
1627     </pre>
1628     This return is given if <b>pcre_dfa_exec()</b> encounters a condition item in a
1629     pattern that uses a back reference for the condition. This is not supported.
1630     <pre>
1631     PCRE_ERROR_DFA_UMLIMIT (-18)
1632     </pre>
1633     This return is given if <b>pcre_dfa_exec()</b> is called with an <i>extra</i>
1634     block that contains a setting of the <i>match_limit</i> field. This is not
1635     supported (it is meaningless).
1636     <pre>
1637     PCRE_ERROR_DFA_WSSIZE (-19)
1638     </pre>
1639     This return is given if <b>pcre_dfa_exec()</b> runs out of space in the
1640     <i>workspace</i> vector.
1641     <pre>
1642     PCRE_ERROR_DFA_RECURSE (-20)
1643     </pre>
1644     When a recursive subpattern is processed, the matching function calls itself
1645     recursively, using private vectors for <i>ovector</i> and <i>workspace</i>. This
1646     error is given if the output vector is not large enough. This should be
1647     extremely rare, as a vector of size 1000 is used.
1648     </P>
1649     <P>
1650 nigel 87 Last updated: 18 January 2006
1651 nigel 63 <br>
1652 nigel 87 Copyright &copy; 1997-2006 University of Cambridge.
1653 nigel 75 <p>
1654     Return to the <a href="index.html">PCRE index page</a>.
1655     </p>

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12