ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 73 - (hide annotations) (download) (as text)
Sat Feb 24 21:40:30 2007 UTC (8 years, 2 months ago) by nigel
File MIME type: text/html
File size: 51200 byte(s)
Load pcre-4.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     This HTML document has been generated automatically from the original man page.
7     If there is any nonsense in it, please consult the man page, in case the
8     conversion went wrong.<br>
9     <ul>
10     <li><a name="TOC1" href="#SEC1">SYNOPSIS OF PCRE API</a>
11     <li><a name="TOC2" href="#SEC2">PCRE API</a>
12     <li><a name="TOC3" href="#SEC3">MULTITHREADING</a>
13     <li><a name="TOC4" href="#SEC4">CHECKING BUILD-TIME OPTIONS</a>
14     <li><a name="TOC5" href="#SEC5">COMPILING A PATTERN</a>
15     <li><a name="TOC6" href="#SEC6">STUDYING A PATTERN</a>
16     <li><a name="TOC7" href="#SEC7">LOCALE SUPPORT</a>
17     <li><a name="TOC8" href="#SEC8">INFORMATION ABOUT A PATTERN</a>
18     <li><a name="TOC9" href="#SEC9">OBSOLETE INFO FUNCTION</a>
19     <li><a name="TOC10" href="#SEC10">MATCHING A PATTERN</a>
20     <li><a name="TOC11" href="#SEC11">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a>
21     <li><a name="TOC12" href="#SEC12">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a>
22     </ul>
23     <br><a name="SEC1" href="#TOC1">SYNOPSIS OF PCRE API</a><br>
24     <P>
25     <b>#include &#60;pcre.h&#62;</b>
26     </P>
27     <P>
28     <b>pcre *pcre_compile(const char *<i>pattern</i>, int <i>options</i>,</b>
29     <b>const char **<i>errptr</i>, int *<i>erroffset</i>,</b>
30     <b>const unsigned char *<i>tableptr</i>);</b>
31     </P>
32     <P>
33     <b>pcre_extra *pcre_study(const pcre *<i>code</i>, int <i>options</i>,</b>
34     <b>const char **<i>errptr</i>);</b>
35     </P>
36     <P>
37     <b>int pcre_exec(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
38     <b>const char *<i>subject</i>, int <i>length</i>, int <i>startoffset</i>,</b>
39     <b>int <i>options</i>, int *<i>ovector</i>, int <i>ovecsize</i>);</b>
40     </P>
41     <P>
42     <b>int pcre_copy_named_substring(const pcre *<i>code</i>,</b>
43     <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
44     <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
45     <b>char *<i>buffer</i>, int <i>buffersize</i>);</b>
46     </P>
47     <P>
48     <b>int pcre_copy_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
49     <b>int <i>stringcount</i>, int <i>stringnumber</i>, char *<i>buffer</i>,</b>
50     <b>int <i>buffersize</i>);</b>
51     </P>
52     <P>
53     <b>int pcre_get_named_substring(const pcre *<i>code</i>,</b>
54     <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
55     <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
56     <b>const char **<i>stringptr</i>);</b>
57     </P>
58     <P>
59     <b>int pcre_get_stringnumber(const pcre *<i>code</i>,</b>
60     <b>const char *<i>name</i>);</b>
61     </P>
62     <P>
63     <b>int pcre_get_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
64     <b>int <i>stringcount</i>, int <i>stringnumber</i>,</b>
65     <b>const char **<i>stringptr</i>);</b>
66     </P>
67     <P>
68     <b>int pcre_get_substring_list(const char *<i>subject</i>,</b>
69     <b>int *<i>ovector</i>, int <i>stringcount</i>, const char ***<i>listptr</i>);</b>
70     </P>
71     <P>
72     <b>void pcre_free_substring(const char *<i>stringptr</i>);</b>
73     </P>
74     <P>
75     <b>void pcre_free_substring_list(const char **<i>stringptr</i>);</b>
76     </P>
77     <P>
78     <b>const unsigned char *pcre_maketables(void);</b>
79     </P>
80     <P>
81     <b>int pcre_fullinfo(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
82     <b>int <i>what</i>, void *<i>where</i>);</b>
83     </P>
84     <P>
85     <b>int pcre_info(const pcre *<i>code</i>, int *<i>optptr</i>, int</b>
86     <b>*<i>firstcharptr</i>);</b>
87     </P>
88     <P>
89     <b>int pcre_config(int <i>what</i>, void *<i>where</i>);</b>
90     </P>
91     <P>
92     <b>char *pcre_version(void);</b>
93     </P>
94     <P>
95     <b>void *(*pcre_malloc)(size_t);</b>
96     </P>
97     <P>
98     <b>void (*pcre_free)(void *);</b>
99     </P>
100     <P>
101 nigel 73 <b>void *(*pcre_stack_malloc)(size_t);</b>
102     </P>
103     <P>
104     <b>void (*pcre_stack_free)(void *);</b>
105     </P>
106     <P>
107 nigel 63 <b>int (*pcre_callout)(pcre_callout_block *);</b>
108     </P>
109     <br><a name="SEC2" href="#TOC1">PCRE API</a><br>
110     <P>
111     PCRE has its own native API, which is described in this document. There is also
112     a set of wrapper functions that correspond to the POSIX regular expression API.
113     These are described in the <b>pcreposix</b> documentation.
114     </P>
115     <P>
116     The native API function prototypes are defined in the header file <b>pcre.h</b>,
117     and on Unix systems the library itself is called <b>libpcre.a</b>, so can be
118     accessed by adding <b>-lpcre</b> to the command for linking an application which
119     calls it. The header file defines the macros PCRE_MAJOR and PCRE_MINOR to
120     contain the major and minor release numbers for the library. Applications can
121     use these to include support for different releases.
122     </P>
123     <P>
124     The functions <b>pcre_compile()</b>, <b>pcre_study()</b>, and <b>pcre_exec()</b>
125     are used for compiling and matching regular expressions. A sample program that
126     demonstrates the simplest way of using them is given in the file
127     <i>pcredemo.c</i>. The <b>pcresample</b> documentation describes how to run it.
128     </P>
129     <P>
130     There are convenience functions for extracting captured substrings from a
131     matched subject string. They are:
132     </P>
133     <P>
134     <pre>
135     <b>pcre_copy_substring()</b>
136     <b>pcre_copy_named_substring()</b>
137     <b>pcre_get_substring()</b>
138     <b>pcre_get_named_substring()</b>
139     <b>pcre_get_substring_list()</b>
140     </PRE>
141     </P>
142     <P>
143     <b>pcre_free_substring()</b> and <b>pcre_free_substring_list()</b> are also
144     provided, to free the memory used for extracted strings.
145     </P>
146     <P>
147     The function <b>pcre_maketables()</b> is used (optionally) to build a set of
148     character tables in the current locale for passing to <b>pcre_compile()</b>.
149     </P>
150     <P>
151     The function <b>pcre_fullinfo()</b> is used to find out information about a
152     compiled pattern; <b>pcre_info()</b> is an obsolete version which returns only
153     some of the available information, but is retained for backwards compatibility.
154     The function <b>pcre_version()</b> returns a pointer to a string containing the
155     version of PCRE and its date of release.
156     </P>
157     <P>
158     The global variables <b>pcre_malloc</b> and <b>pcre_free</b> initially contain
159     the entry points of the standard <b>malloc()</b> and <b>free()</b> functions
160     respectively. PCRE calls the memory management functions via these variables,
161     so a calling program can replace them if it wishes to intercept the calls. This
162     should be done before calling any PCRE functions.
163     </P>
164     <P>
165 nigel 73 The global variables <b>pcre_stack_malloc</b> and <b>pcre_stack_free</b> are also
166     indirections to memory management functions. These special functions are used
167     only when PCRE is compiled to use the heap for remembering data, instead of
168     recursive function calls. This is a non-standard way of building PCRE, for use
169     in environments that have limited stacks. Because of the greater use of memory
170     management, it runs more slowly. Separate functions are provided so that
171     special-purpose external code can be used for this case. When used, these
172     functions are always called in a stack-like manner (last obtained, first
173     freed), and always for memory blocks of the same size.
174     </P>
175     <P>
176 nigel 63 The global variable <b>pcre_callout</b> initially contains NULL. It can be set
177     by the caller to a "callout" function, which PCRE will then call at specified
178     points during a matching operation. Details are given in the <b>pcrecallout</b>
179     documentation.
180     </P>
181     <br><a name="SEC3" href="#TOC1">MULTITHREADING</a><br>
182     <P>
183     The PCRE functions can be used in multi-threading applications, with the
184 nigel 73 proviso that the memory management functions pointed to by <b>pcre_malloc</b>,
185     <b>pcre_free</b>, <b>pcre_stack_malloc</b>, and <b>pcre_stack_free</b>, and the
186     callout function pointed to by <b>pcre_callout</b>, are shared by all threads.
187 nigel 63 </P>
188     <P>
189     The compiled form of a regular expression is not altered during matching, so
190     the same compiled pattern can safely be used by several threads at once.
191     </P>
192     <br><a name="SEC4" href="#TOC1">CHECKING BUILD-TIME OPTIONS</a><br>
193     <P>
194     <b>int pcre_config(int <i>what</i>, void *<i>where</i>);</b>
195     </P>
196     <P>
197     The function <b>pcre_config()</b> makes it possible for a PCRE client to
198     discover which optional features have been compiled into the PCRE library. The
199     <a href="pcrebuild.html"><b>pcrebuild</b></a>
200     documentation has more details about these optional features.
201     </P>
202     <P>
203     The first argument for <b>pcre_config()</b> is an integer, specifying which
204     information is required; the second argument is a pointer to a variable into
205     which the information is placed. The following information is available:
206     </P>
207     <P>
208     <pre>
210     </PRE>
211     </P>
212     <P>
213     The output is an integer that is set to one if UTF-8 support is available;
214     otherwise it is set to zero.
215     </P>
216     <P>
217     <pre>
219     </PRE>
220     </P>
221     <P>
222     The output is an integer that is set to the value of the code that is used for
223     the newline character. It is either linefeed (10) or carriage return (13), and
224     should normally be the standard character for your operating system.
225     </P>
226     <P>
227     <pre>
229     </PRE>
230     </P>
231     <P>
232     The output is an integer that contains the number of bytes used for internal
233     linkage in compiled regular expressions. The value is 2, 3, or 4. Larger values
234     allow larger regular expressions to be compiled, at the expense of slower
235     matching. The default value of 2 is sufficient for all but the most massive
236     patterns, since it allows the compiled pattern to be up to 64K in size.
237     </P>
238     <P>
239     <pre>
241     </PRE>
242     </P>
243     <P>
244     The output is an integer that contains the threshold above which the POSIX
245     interface uses <b>malloc()</b> for output vectors. Further details are given in
246     the <b>pcreposix</b> documentation.
247     </P>
248     <P>
249     <pre>
251     </PRE>
252     </P>
253     <P>
254     The output is an integer that gives the default limit for the number of
255     internal matching function calls in a <b>pcre_exec()</b> execution. Further
256     details are given with <b>pcre_exec()</b> below.
257     </P>
258 nigel 73 <P>
259     <pre>
261     </PRE>
262     </P>
263     <P>
264     The output is an integer that is set to one if internal recursion is
265     implemented by recursive function calls that use the stack to remember their
266     state. This is the usual way that PCRE is compiled. The output is zero if PCRE
267     was compiled to use blocks of data on the heap instead of recursive function
268     calls. In this case, <b>pcre_stack_malloc</b> and <b>pcre_stack_free</b> are
269     called to manage memory blocks on the heap, thus avoiding the use of the stack.
270     </P>
271 nigel 63 <br><a name="SEC5" href="#TOC1">COMPILING A PATTERN</a><br>
272     <P>
273     <b>pcre *pcre_compile(const char *<i>pattern</i>, int <i>options</i>,</b>
274     <b>const char **<i>errptr</i>, int *<i>erroffset</i>,</b>
275     <b>const unsigned char *<i>tableptr</i>);</b>
276     </P>
277     <P>
278     The function <b>pcre_compile()</b> is called to compile a pattern into an
279     internal form. The pattern is a C string terminated by a binary zero, and
280     is passed in the argument <i>pattern</i>. A pointer to a single block of memory
281     that is obtained via <b>pcre_malloc</b> is returned. This contains the compiled
282     code and related data. The <b>pcre</b> type is defined for the returned block;
283     this is a typedef for a structure whose contents are not externally defined. It
284     is up to the caller to free the memory when it is no longer required.
285     </P>
286     <P>
287     Although the compiled code of a PCRE regex is relocatable, that is, it does not
288     depend on memory location, the complete <b>pcre</b> data block is not
289     fully relocatable, because it contains a copy of the <i>tableptr</i> argument,
290     which is an address (see below).
291     </P>
292     <P>
293     The <i>options</i> argument contains independent bits that affect the
294     compilation. It should be zero if no options are required. Some of the options,
295     in particular, those that are compatible with Perl, can also be set and unset
296     from within the pattern (see the detailed description of regular expressions
297     in the <b>pcrepattern</b> documentation). For these options, the contents of the
298     <i>options</i> argument specifies their initial settings at the start of
299     compilation and execution. The PCRE_ANCHORED option can be set at the time of
300     matching as well as at compile time.
301     </P>
302     <P>
303     If <i>errptr</i> is NULL, <b>pcre_compile()</b> returns NULL immediately.
304     Otherwise, if compilation of a pattern fails, <b>pcre_compile()</b> returns
305     NULL, and sets the variable pointed to by <i>errptr</i> to point to a textual
306     error message. The offset from the start of the pattern to the character where
307     the error was discovered is placed in the variable pointed to by
308     <i>erroffset</i>, which must not be NULL. If it is, an immediate error is given.
309     </P>
310     <P>
311     If the final argument, <i>tableptr</i>, is NULL, PCRE uses a default set of
312     character tables which are built when it is compiled, using the default C
313     locale. Otherwise, <i>tableptr</i> must be the result of a call to
314     <b>pcre_maketables()</b>. See the section on locale support below.
315     </P>
316     <P>
317     This code fragment shows a typical straightforward call to <b>pcre_compile()</b>:
318     </P>
319     <P>
320     <pre>
321     pcre *re;
322     const char *error;
323     int erroffset;
324     re = pcre_compile(
325     "^A.*Z", /* the pattern */
326     0, /* default options */
327     &error, /* for error message */
328     &erroffset, /* for error offset */
329     NULL); /* use default character tables */
330     </PRE>
331     </P>
332     <P>
333     The following option bits are defined:
334     </P>
335     <P>
336     <pre>
338     </PRE>
339     </P>
340     <P>
341     If this bit is set, the pattern is forced to be "anchored", that is, it is
342     constrained to match only at the first matching point in the string which is
343     being searched (the "subject string"). This effect can also be achieved by
344     appropriate constructs in the pattern itself, which is the only way to do it in
345     Perl.
346     </P>
347     <P>
348     <pre>
350     </PRE>
351     </P>
352     <P>
353     If this bit is set, letters in the pattern match both upper and lower case
354     letters. It is equivalent to Perl's /i option, and it can be changed within a
355     pattern by a (?i) option setting.
356     </P>
357     <P>
358     <pre>
360     </PRE>
361     </P>
362     <P>
363     If this bit is set, a dollar metacharacter in the pattern matches only at the
364     end of the subject string. Without this option, a dollar also matches
365     immediately before the final character if it is a newline (but not before any
366     other newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
367     set. There is no equivalent to this option in Perl, and no way to set it within
368     a pattern.
369     </P>
370     <P>
371     <pre>
373     </PRE>
374     </P>
375     <P>
376     If this bit is set, a dot metacharater in the pattern matches all characters,
377     including newlines. Without it, newlines are excluded. This option is
378     equivalent to Perl's /s option, and it can be changed within a pattern by a
379     (?s) option setting. A negative class such as [^a] always matches a newline
380     character, independent of the setting of this option.
381     </P>
382     <P>
383     <pre>
385     </PRE>
386     </P>
387     <P>
388     If this bit is set, whitespace data characters in the pattern are totally
389     ignored except when escaped or inside a character class. Whitespace does not
390     include the VT character (code 11). In addition, characters between an
391     unescaped # outside a character class and the next newline character,
392     inclusive, are also ignored. This is equivalent to Perl's /x option, and it can
393     be changed within a pattern by a (?x) option setting.
394     </P>
395     <P>
396     This option makes it possible to include comments inside complicated patterns.
397     Note, however, that this applies only to data characters. Whitespace characters
398     may never appear within special character sequences in a pattern, for example
399     within the sequence (?( which introduces a conditional subpattern.
400     </P>
401     <P>
402     <pre>
403     PCRE_EXTRA
404     </PRE>
405     </P>
406     <P>
407     This option was invented in order to turn on additional functionality of PCRE
408     that is incompatible with Perl, but it is currently of very little use. When
409     set, any backslash in a pattern that is followed by a letter that has no
410     special meaning causes an error, thus reserving these combinations for future
411     expansion. By default, as in Perl, a backslash followed by a letter with no
412     special meaning is treated as a literal. There are at present no other features
413     controlled by this option. It can also be set by a (?X) option setting within a
414     pattern.
415     </P>
416     <P>
417     <pre>
419     </PRE>
420     </P>
421     <P>
422     By default, PCRE treats the subject string as consisting of a single "line" of
423     characters (even if it actually contains several newlines). The "start of line"
424     metacharacter (^) matches only at the start of the string, while the "end of
425     line" metacharacter ($) matches only at the end of the string, or before a
426     terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
427     Perl.
428     </P>
429     <P>
430     When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
431     match immediately following or immediately before any newline in the subject
432     string, respectively, as well as at the very start and end. This is equivalent
433     to Perl's /m option, and it can be changed within a pattern by a (?m) option
434     setting. If there are no "\n" characters in a subject string, or no
435     occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no effect.
436     </P>
437     <P>
438     <pre>
440     </PRE>
441     </P>
442     <P>
443     If this option is set, it disables the use of numbered capturing parentheses in
444     the pattern. Any opening parenthesis that is not followed by ? behaves as if it
445     were followed by ?: but named parentheses can still be used for capturing (and
446     they acquire numbers in the usual way). There is no equivalent of this option
447     in Perl.
448     </P>
449     <P>
450     <pre>
452     </PRE>
453     </P>
454     <P>
455     This option inverts the "greediness" of the quantifiers so that they are not
456     greedy by default, but become greedy if followed by "?". It is not compatible
457     with Perl. It can also be set by a (?U) option setting within the pattern.
458     </P>
459     <P>
460     <pre>
461     PCRE_UTF8
462     </PRE>
463     </P>
464     <P>
465     This option causes PCRE to regard both the pattern and the subject as strings
466     of UTF-8 characters instead of single-byte character strings. However, it is
467     available only if PCRE has been built to include UTF-8 support. If not, the use
468     of this option provokes an error. Details of how this option changes the
469     behaviour of PCRE are given in the
470     <a href="pcre.html#utf8support">section on UTF-8 support</a>
471     in the main
472     <a href="pcre.html"><b>pcre</b></a>
473     page.
474     </P>
475 nigel 71 <P>
476     <pre>
478     </PRE>
479     </P>
480     <P>
481     When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
482     automatically checked. If an invalid UTF-8 sequence of bytes is found,
483     <b>pcre_compile()</b> returns an error. If you already know that your pattern is
484     valid, and you want to skip this check for performance reasons, you can set the
485     PCRE_NO_UTF8_CHECK option. When it is set, the effect of passing an invalid
486     UTF-8 string as a pattern is undefined. It may cause your program to crash.
487     Note that there is a similar option for suppressing the checking of subject
488     strings passed to <b>pcre_exec()</b>.
489     </P>
490 nigel 63 <br><a name="SEC6" href="#TOC1">STUDYING A PATTERN</a><br>
491     <P>
492     <b>pcre_extra *pcre_study(const pcre *<i>code</i>, int <i>options</i>,</b>
493     <b>const char **<i>errptr</i>);</b>
494     </P>
495     <P>
496     When a pattern is going to be used several times, it is worth spending more
497     time analyzing it in order to speed up the time taken for matching. The
498     function <b>pcre_study()</b> takes a pointer to a compiled pattern as its first
499     argument. If studing the pattern produces additional information that will help
500     speed up matching, <b>pcre_study()</b> returns a pointer to a <b>pcre_extra</b>
501     block, in which the <i>study_data</i> field points to the results of the study.
502     </P>
503     <P>
504     The returned value from a <b>pcre_study()</b> can be passed directly to
505     <b>pcre_exec()</b>. However, the <b>pcre_extra</b> block also contains other
506     fields that can be set by the caller before the block is passed; these are
507     described below. If studying the pattern does not produce any additional
508     information, <b>pcre_study()</b> returns NULL. In that circumstance, if the
509     calling program wants to pass some of the other fields to <b>pcre_exec()</b>, it
510     must set up its own <b>pcre_extra</b> block.
511     </P>
512     <P>
513     The second argument contains option bits. At present, no options are defined
514     for <b>pcre_study()</b>, and this argument should always be zero.
515     </P>
516     <P>
517     The third argument for <b>pcre_study()</b> is a pointer for an error message. If
518     studying succeeds (even if no data is returned), the variable it points to is
519     set to NULL. Otherwise it points to a textual error message. You should
520     therefore test the error pointer for NULL after calling <b>pcre_study()</b>, to
521     be sure that it has run successfully.
522     </P>
523     <P>
524     This is a typical call to <b>pcre_study</b>():
525     </P>
526     <P>
527     <pre>
528     pcre_extra *pe;
529     pe = pcre_study(
530     re, /* result of pcre_compile() */
531     0, /* no options exist */
532     &error); /* set to NULL or points to a message */
533     </PRE>
534     </P>
535     <P>
536     At present, studying a pattern is useful only for non-anchored patterns that do
537     not have a single fixed starting character. A bitmap of possible starting
538     characters is created.
539     </P>
540     <a name="localesupport"></a><br><a name="SEC7" href="#TOC1">LOCALE SUPPORT</a><br>
541     <P>
542     PCRE handles caseless matching, and determines whether characters are letters,
543     digits, or whatever, by reference to a set of tables. When running in UTF-8
544     mode, this applies only to characters with codes less than 256. The library
545     contains a default set of tables that is created in the default C locale when
546     PCRE is compiled. This is used when the final argument of <b>pcre_compile()</b>
547     is NULL, and is sufficient for many applications.
548     </P>
549     <P>
550     An alternative set of tables can, however, be supplied. Such tables are built
551     by calling the <b>pcre_maketables()</b> function, which has no arguments, in the
552     relevant locale. The result can then be passed to <b>pcre_compile()</b> as often
553     as necessary. For example, to build and use tables that are appropriate for the
554     French locale (where accented characters with codes greater than 128 are
555     treated as letters), the following code could be used:
556     </P>
557     <P>
558     <pre>
559     setlocale(LC_CTYPE, "fr");
560     tables = pcre_maketables();
561     re = pcre_compile(..., tables);
562     </PRE>
563     </P>
564     <P>
565     The tables are built in memory that is obtained via <b>pcre_malloc</b>. The
566     pointer that is passed to <b>pcre_compile</b> is saved with the compiled
567     pattern, and the same tables are used via this pointer by <b>pcre_study()</b>
568     and <b>pcre_exec()</b>. Thus, for any single pattern, compilation, studying and
569     matching all happen in the same locale, but different patterns can be compiled
570     in different locales. It is the caller's responsibility to ensure that the
571     memory containing the tables remains available for as long as it is needed.
572     </P>
573     <br><a name="SEC8" href="#TOC1">INFORMATION ABOUT A PATTERN</a><br>
574     <P>
575     <b>int pcre_fullinfo(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
576     <b>int <i>what</i>, void *<i>where</i>);</b>
577     </P>
578     <P>
579     The <b>pcre_fullinfo()</b> function returns information about a compiled
580     pattern. It replaces the obsolete <b>pcre_info()</b> function, which is
581     nevertheless retained for backwards compability (and is documented below).
582     </P>
583     <P>
584     The first argument for <b>pcre_fullinfo()</b> is a pointer to the compiled
585     pattern. The second argument is the result of <b>pcre_study()</b>, or NULL if
586     the pattern was not studied. The third argument specifies which piece of
587     information is required, and the fourth argument is a pointer to a variable
588     to receive the data. The yield of the function is zero for success, or one of
589     the following negative numbers:
590     </P>
591     <P>
592     <pre>
593     PCRE_ERROR_NULL the argument <i>code</i> was NULL
594     the argument <i>where</i> was NULL
595     PCRE_ERROR_BADMAGIC the "magic number" was not found
596     PCRE_ERROR_BADOPTION the value of <i>what</i> was invalid
597     </PRE>
598     </P>
599     <P>
600     Here is a typical call of <b>pcre_fullinfo()</b>, to obtain the length of the
601     compiled pattern:
602     </P>
603     <P>
604     <pre>
605     int rc;
606     unsigned long int length;
607     rc = pcre_fullinfo(
608     re, /* result of pcre_compile() */
609     pe, /* result of pcre_study(), or NULL */
610     PCRE_INFO_SIZE, /* what is required */
611     &length); /* where to put the data */
612     </PRE>
613     </P>
614     <P>
615     The possible values for the third argument are defined in <b>pcre.h</b>, and are
616     as follows:
617     </P>
618     <P>
619     <pre>
621     </PRE>
622     </P>
623     <P>
624     Return the number of the highest back reference in the pattern. The fourth
625     argument should point to an <b>int</b> variable. Zero is returned if there are
626     no back references.
627     </P>
628     <P>
629     <pre>
631     </PRE>
632     </P>
633     <P>
634     Return the number of capturing subpatterns in the pattern. The fourth argument
635     should point to an \fbint\fR variable.
636     </P>
637     <P>
638     <pre>
640     </PRE>
641     </P>
642     <P>
643     Return information about the first byte of any matched string, for a
644     non-anchored pattern. (This option used to be called PCRE_INFO_FIRSTCHAR; the
645     old name is still recognized for backwards compatibility.)
646     </P>
647     <P>
648     If there is a fixed first byte, e.g. from a pattern such as (cat|cow|coyote),
649     it is returned in the integer pointed to by <i>where</i>. Otherwise, if either
650     </P>
651     <P>
652     (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
653     starts with "^", or
654     </P>
655     <P>
656     (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
657     (if it were set, the pattern would be anchored),
658     </P>
659     <P>
660     -1 is returned, indicating that the pattern matches only at the start of a
661     subject string or after any newline within the string. Otherwise -2 is
662     returned. For anchored patterns, -2 is returned.
663     </P>
664     <P>
665     <pre>
667     </PRE>
668     </P>
669     <P>
670     If the pattern was studied, and this resulted in the construction of a 256-bit
671     table indicating a fixed set of bytes for the first byte in any matching
672     string, a pointer to the table is returned. Otherwise NULL is returned. The
673     fourth argument should point to an <b>unsigned char *</b> variable.
674     </P>
675     <P>
676     <pre>
678     </PRE>
679     </P>
680     <P>
681 nigel 65 Return the value of the rightmost literal byte that must exist in any matched
682     string, other than at its start, if such a byte has been recorded. The fourth
683     argument should point to an <b>int</b> variable. If there is no such byte, -1 is
684     returned. For anchored patterns, a last literal byte is recorded only if it
685     follows something of variable length. For example, for the pattern
686     /^a\d+z\d+/ the returned value is "z", but for /^a\dz\d/ the returned value
687     is -1.
688 nigel 63 </P>
689     <P>
690     <pre>
694     </PRE>
695     </P>
696     <P>
697     PCRE supports the use of named as well as numbered capturing parentheses. The
698     names are just an additional way of identifying the parentheses, which still
699     acquire a number. A caller that wants to extract data from a named subpattern
700     must convert the name to a number in order to access the correct pointers in
701     the output vector (described with <b>pcre_exec()</b> below). In order to do
702     this, it must first use these three values to obtain the name-to-number mapping
703     table for the pattern.
704     </P>
705     <P>
706     The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT gives
707     the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size of each
708     entry; both of these return an <b>int</b> value. The entry size depends on the
709     length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
710     entry of the table (a pointer to <b>char</b>). The first two bytes of each entry
711     are the number of the capturing parenthesis, most significant byte first. The
712     rest of the entry is the corresponding name, zero terminated. The names are in
713     alphabetical order. For example, consider the following pattern (assume
714     PCRE_EXTENDED is set, so white space - including newlines - is ignored):
715     </P>
716     <P>
717     <pre>
718     (?P&#60;date&#62; (?P&#60;year&#62;(\d\d)?\d\d) -
719     (?P&#60;month&#62;\d\d) - (?P&#60;day&#62;\d\d) )
720     </PRE>
721     </P>
722     <P>
723     There are four named subpatterns, so the table has four entries, and each entry
724     in the table is eight bytes long. The table is as follows, with non-printing
725     bytes shows in hex, and undefined bytes shown as ??:
726     </P>
727     <P>
728     <pre>
729     00 01 d a t e 00 ??
730     00 05 d a y 00 ?? ??
731     00 04 m o n t h 00
732     00 02 y e a r 00 ??
733     </PRE>
734     </P>
735     <P>
736     When writing code to extract data from named subpatterns, remember that the
737     length of each entry may be different for each compiled pattern.
738     </P>
739     <P>
740     <pre>
742     </PRE>
743     </P>
744     <P>
745     Return a copy of the options with which the pattern was compiled. The fourth
746     argument should point to an <b>unsigned long int</b> variable. These option bits
747     are those specified in the call to <b>pcre_compile()</b>, modified by any
748     top-level option settings within the pattern itself.
749     </P>
750     <P>
751     A pattern is automatically anchored by PCRE if all of its top-level
752     alternatives begin with one of the following:
753     </P>
754     <P>
755     <pre>
756     ^ unless PCRE_MULTILINE is set
757     \A always
758     \G always
759     .* if PCRE_DOTALL is set and there are no back
760     references to the subpattern in which .* appears
761     </PRE>
762     </P>
763     <P>
764     For such patterns, the PCRE_ANCHORED bit is set in the options returned by
765     <b>pcre_fullinfo()</b>.
766     </P>
767     <P>
768     <pre>
770     </PRE>
771     </P>
772     <P>
773     Return the size of the compiled pattern, that is, the value that was passed as
774     the argument to <b>pcre_malloc()</b> when PCRE was getting memory in which to
775     place the compiled data. The fourth argument should point to a <b>size_t</b>
776     variable.
777     </P>
778     <P>
779     <pre>
781     </PRE>
782     </P>
783     <P>
784     Returns the size of the data block pointed to by the <i>study_data</i> field in
785     a <b>pcre_extra</b> block. That is, it is the value that was passed to
786     <b>pcre_malloc()</b> when PCRE was getting memory into which to place the data
787     created by <b>pcre_study()</b>. The fourth argument should point to a
788     <b>size_t</b> variable.
789     </P>
790     <br><a name="SEC9" href="#TOC1">OBSOLETE INFO FUNCTION</a><br>
791     <P>
792     <b>int pcre_info(const pcre *<i>code</i>, int *<i>optptr</i>, int</b>
793     <b>*<i>firstcharptr</i>);</b>
794     </P>
795     <P>
796     The <b>pcre_info()</b> function is now obsolete because its interface is too
797     restrictive to return all the available data about a compiled pattern. New
798     programs should use <b>pcre_fullinfo()</b> instead. The yield of
799     <b>pcre_info()</b> is the number of capturing subpatterns, or one of the
800     following negative numbers:
801     </P>
802     <P>
803     <pre>
804     PCRE_ERROR_NULL the argument <i>code</i> was NULL
805     PCRE_ERROR_BADMAGIC the "magic number" was not found
806     </PRE>
807     </P>
808     <P>
809     If the <i>optptr</i> argument is not NULL, a copy of the options with which the
810     pattern was compiled is placed in the integer it points to (see
811     PCRE_INFO_OPTIONS above).
812     </P>
813     <P>
814     If the pattern is not anchored and the <i>firstcharptr</i> argument is not NULL,
815     it is used to pass back information about the first character of any matched
816     string (see PCRE_INFO_FIRSTBYTE above).
817     </P>
818     <br><a name="SEC10" href="#TOC1">MATCHING A PATTERN</a><br>
819     <P>
820     <b>int pcre_exec(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
821     <b>const char *<i>subject</i>, int <i>length</i>, int <i>startoffset</i>,</b>
822     <b>int <i>options</i>, int *<i>ovector</i>, int <i>ovecsize</i>);</b>
823     </P>
824     <P>
825     The function <b>pcre_exec()</b> is called to match a subject string against a
826     pre-compiled pattern, which is passed in the <i>code</i> argument. If the
827     pattern has been studied, the result of the study should be passed in the
828     <i>extra</i> argument.
829     </P>
830     <P>
831     Here is an example of a simple call to <b>pcre_exec()</b>:
832     </P>
833     <P>
834     <pre>
835     int rc;
836     int ovector[30];
837     rc = pcre_exec(
838     re, /* result of pcre_compile() */
839     NULL, /* we didn't study the pattern */
840     "some string", /* the subject string */
841     11, /* the length of the subject string */
842     0, /* start at offset 0 in the subject */
843     0, /* default options */
844     ovector, /* vector for substring information */
845     30); /* number of elements in the vector */
846     </PRE>
847     </P>
848     <P>
849     If the <i>extra</i> argument is not NULL, it must point to a <b>pcre_extra</b>
850     data block. The <b>pcre_study()</b> function returns such a block (when it
851     doesn't return NULL), but you can also create one for yourself, and pass
852     additional information in it. The fields in the block are as follows:
853     </P>
854     <P>
855     <pre>
856     unsigned long int <i>flags</i>;
857     void *<i>study_data</i>;
858     unsigned long int <i>match_limit</i>;
859     void *<i>callout_data</i>;
860     </PRE>
861     </P>
862     <P>
863     The <i>flags</i> field is a bitmap that specifies which of the other fields
864     are set. The flag bits are:
865     </P>
866     <P>
867     <pre>
871     </PRE>
872     </P>
873     <P>
874     Other flag bits should be set to zero. The <i>study_data</i> field is set in the
875     <b>pcre_extra</b> block that is returned by <b>pcre_study()</b>, together with
876     the appropriate flag bit. You should not set this yourself, but you can add to
877     the block by setting the other fields.
878     </P>
879     <P>
880     The <i>match_limit</i> field provides a means of preventing PCRE from using up a
881     vast amount of resources when running patterns that are not going to match,
882     but which have a very large number of possibilities in their search trees. The
883     classic example is the use of nested unlimited repeats. Internally, PCRE uses a
884     function called <b>match()</b> which it calls repeatedly (sometimes
885     recursively). The limit is imposed on the number of times this function is
886     called during a match, which has the effect of limiting the amount of recursion
887     and backtracking that can take place. For patterns that are not anchored, the
888     count starts from zero for each position in the subject string.
889     </P>
890     <P>
891     The default limit for the library can be set when PCRE is built; the default
892     default is 10 million, which handles all but the most extreme cases. You can
893     reduce the default by suppling <b>pcre_exec()</b> with a \fRpcre_extra\fR block
894     in which <i>match_limit</i> is set to a smaller value, and
895     PCRE_EXTRA_MATCH_LIMIT is set in the <i>flags</i> field. If the limit is
896     exceeded, <b>pcre_exec()</b> returns PCRE_ERROR_MATCHLIMIT.
897     </P>
898     <P>
899     The <i>pcre_callout</i> field is used in conjunction with the "callout" feature,
900     which is described in the <b>pcrecallout</b> documentation.
901     </P>
902     <P>
903     The PCRE_ANCHORED option can be passed in the <i>options</i> argument, whose
904     unused bits must be zero. This limits <b>pcre_exec()</b> to matching at the
905     first matching position. However, if a pattern was compiled with PCRE_ANCHORED,
906     or turned out to be anchored by virtue of its contents, it cannot be made
907     unachored at matching time.
908     </P>
909     <P>
910 nigel 71 When PCRE_UTF8 was set at compile time, the validity of the subject as a UTF-8
911 nigel 73 string is automatically checked, and the value of <i>startoffset</i> is also
912     checked to ensure that it points to the start of a UTF-8 character. If an
913     invalid UTF-8 sequence of bytes is found, <b>pcre_exec()</b> returns the error
914     PCRE_ERROR_BADUTF8. If <i>startoffset</i> contains an invalid value,
915     PCRE_ERROR_BADUTF8_OFFSET is returned.
916 nigel 71 </P>
917     <P>
918 nigel 73 If you already know that your subject is valid, and you want to skip these
919     checks for performance reasons, you can set the PCRE_NO_UTF8_CHECK option when
920     calling <b>pcre_exec()</b>. You might want to do this for the second and
921     subsequent calls to <b>pcre_exec()</b> if you are making repeated calls to find
922     all the matches in a single subject string. However, you should be sure that
923     the value of <i>startoffset</i> points to the start of a UTF-8 character. When
924     PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid UTF-8 string as a
925     subject, or a value of <i>startoffset</i> that does not point to the start of a
926     UTF-8 character, is undefined. Your program may crash.
927     </P>
928     <P>
929 nigel 63 There are also three further options that can be set only at matching time:
930     </P>
931     <P>
932     <pre>
934     </PRE>
935     </P>
936     <P>
937     The first character of the string is not the beginning of a line, so the
938     circumflex metacharacter should not match before it. Setting this without
939     PCRE_MULTILINE (at compile time) causes circumflex never to match.
940     </P>
941     <P>
942     <pre>
944     </PRE>
945     </P>
946     <P>
947     The end of the string is not the end of a line, so the dollar metacharacter
948     should not match it nor (except in multiline mode) a newline immediately before
949     it. Setting this without PCRE_MULTILINE (at compile time) causes dollar never
950     to match.
951     </P>
952     <P>
953     <pre>
955     </PRE>
956     </P>
957     <P>
958     An empty string is not considered to be a valid match if this option is set. If
959     there are alternatives in the pattern, they are tried. If all the alternatives
960     match the empty string, the entire match fails. For example, if the pattern
961     </P>
962     <P>
963     <pre>
964     a?b?
965     </PRE>
966     </P>
967     <P>
968     is applied to a string not beginning with "a" or "b", it matches the empty
969     string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
970     valid, so PCRE searches further into the string for occurrences of "a" or "b".
971     </P>
972     <P>
973     Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
974     of a pattern match of the empty string within its <b>split()</b> function, and
975     when using the /g modifier. It is possible to emulate Perl's behaviour after
976     matching a null string by first trying the match again at the same offset with
977     PCRE_NOTEMPTY set, and then if that fails by advancing the starting offset (see
978     below) and trying an ordinary match again.
979     </P>
980     <P>
981     The subject string is passed to <b>pcre_exec()</b> as a pointer in
982 nigel 73 <i>subject</i>, a length in <i>length</i>, and a starting byte offset in
983 nigel 63 <i>startoffset</i>. Unlike the pattern string, the subject may contain binary
984     zero bytes. When the starting offset is zero, the search for a match starts at
985     the beginning of the subject, and this is by far the most common case.
986     </P>
987     <P>
988     If the pattern was compiled with the PCRE_UTF8 option, the subject must be a
989 nigel 73 sequence of bytes that is a valid UTF-8 string, and the starting offset must
990     point to the beginning of a UTF-8 character. If an invalid UTF-8 string or
991     offset is passed, an error (either PCRE_ERROR_BADUTF8 or
992     PCRE_ERROR_BADUTF8_OFFSET) is returned, unless the option PCRE_NO_UTF8_CHECK is
993     set, in which case PCRE's behaviour is not defined.
994 nigel 63 </P>
995     <P>
996     A non-zero starting offset is useful when searching for another match in the
997     same subject by calling <b>pcre_exec()</b> again after a previous success.
998     Setting <i>startoffset</i> differs from just passing over a shortened string and
999     setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
1000     lookbehind. For example, consider the pattern
1001     </P>
1002     <P>
1003     <pre>
1004     \Biss\B
1005     </PRE>
1006     </P>
1007     <P>
1008     which finds occurrences of "iss" in the middle of words. (\B matches only if
1009     the current position in the subject is not a word boundary.) When applied to
1010     the string "Mississipi" the first call to <b>pcre_exec()</b> finds the first
1011     occurrence. If <b>pcre_exec()</b> is called again with just the remainder of the
1012     subject, namely "issipi", it does not match, because \B is always false at the
1013     start of the subject, which is deemed to be a word boundary. However, if
1014     <b>pcre_exec()</b> is passed the entire string again, but with <i>startoffset</i>
1015     set to 4, it finds the second occurrence of "iss" because it is able to look
1016     behind the starting point to discover that it is preceded by a letter.
1017     </P>
1018     <P>
1019     If a non-zero starting offset is passed when the pattern is anchored, one
1020     attempt to match at the given offset is tried. This can only succeed if the
1021     pattern does not require the match to be at the start of the subject.
1022     </P>
1023     <P>
1024     In general, a pattern matches a certain portion of the subject, and in
1025     addition, further substrings from the subject may be picked out by parts of the
1026     pattern. Following the usage in Jeffrey Friedl's book, this is called
1027     "capturing" in what follows, and the phrase "capturing subpattern" is used for
1028     a fragment of a pattern that picks out a substring. PCRE supports several other
1029     kinds of parenthesized subpattern that do not cause substrings to be captured.
1030     </P>
1031     <P>
1032     Captured substrings are returned to the caller via a vector of integer offsets
1033     whose address is passed in <i>ovector</i>. The number of elements in the vector
1034     is passed in <i>ovecsize</i>. The first two-thirds of the vector is used to pass
1035     back captured substrings, each substring using a pair of integers. The
1036     remaining third of the vector is used as workspace by <b>pcre_exec()</b> while
1037     matching capturing subpatterns, and is not available for passing back
1038     information. The length passed in <i>ovecsize</i> should always be a multiple of
1039     three. If it is not, it is rounded down.
1040     </P>
1041     <P>
1042     When a match has been successful, information about captured substrings is
1043     returned in pairs of integers, starting at the beginning of <i>ovector</i>, and
1044     continuing up to two-thirds of its length at the most. The first element of a
1045     pair is set to the offset of the first character in a substring, and the second
1046     is set to the offset of the first character after the end of a substring. The
1047     first pair, <i>ovector[0]</i> and <i>ovector[1]</i>, identify the portion of the
1048     subject string matched by the entire pattern. The next pair is used for the
1049     first capturing subpattern, and so on. The value returned by <b>pcre_exec()</b>
1050     is the number of pairs that have been set. If there are no capturing
1051     subpatterns, the return value from a successful match is 1, indicating that
1052     just the first pair of offsets has been set.
1053     </P>
1054     <P>
1055     Some convenience functions are provided for extracting the captured substrings
1056     as separate strings. These are described in the following section.
1057     </P>
1058     <P>
1059     It is possible for an capturing subpattern number <i>n+1</i> to match some
1060     part of the subject when subpattern <i>n</i> has not been used at all. For
1061     example, if the string "abc" is matched against the pattern (a|(z))(bc)
1062     subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
1063     values corresponding to the unused subpattern are set to -1.
1064     </P>
1065     <P>
1066     If a capturing subpattern is matched repeatedly, it is the last portion of the
1067     string that it matched that gets returned.
1068     </P>
1069     <P>
1070     If the vector is too small to hold all the captured substrings, it is used as
1071     far as possible (up to two-thirds of its length), and the function returns a
1072     value of zero. In particular, if the substring offsets are not of interest,
1073     <b>pcre_exec()</b> may be called with <i>ovector</i> passed as NULL and
1074     <i>ovecsize</i> as zero. However, if the pattern contains back references and
1075     the <i>ovector</i> isn't big enough to remember the related substrings, PCRE has
1076     to get additional memory for use during matching. Thus it is usually advisable
1077     to supply an <i>ovector</i>.
1078     </P>
1079     <P>
1080     Note that <b>pcre_info()</b> can be used to find out how many capturing
1081     subpatterns there are in a compiled pattern. The smallest size for
1082     <i>ovector</i> that will allow for <i>n</i> captured substrings, in addition to
1083     the offsets of the substring matched by the whole pattern, is (<i>n</i>+1)*3.
1084     </P>
1085     <P>
1086     If <b>pcre_exec()</b> fails, it returns a negative number. The following are
1087     defined in the header file:
1088     </P>
1089     <P>
1090     <pre>
1091     PCRE_ERROR_NOMATCH (-1)
1092     </PRE>
1093     </P>
1094     <P>
1095     The subject string did not match the pattern.
1096     </P>
1097     <P>
1098     <pre>
1099     PCRE_ERROR_NULL (-2)
1100     </PRE>
1101     </P>
1102     <P>
1103     Either <i>code</i> or <i>subject</i> was passed as NULL, or <i>ovector</i> was
1104     NULL and <i>ovecsize</i> was not zero.
1105     </P>
1106     <P>
1107     <pre>
1109     </PRE>
1110     </P>
1111     <P>
1112     An unrecognized bit was set in the <i>options</i> argument.
1113     </P>
1114     <P>
1115     <pre>
1117     </PRE>
1118     </P>
1119     <P>
1120     PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
1121     the case when it is passed a junk pointer. This is the error it gives when the
1122     magic number isn't present.
1123     </P>
1124     <P>
1125     <pre>
1127     </PRE>
1128     </P>
1129     <P>
1130     While running the pattern match, an unknown item was encountered in the
1131     compiled pattern. This error could be caused by a bug in PCRE or by overwriting
1132     of the compiled pattern.
1133     </P>
1134     <P>
1135     <pre>
1137     </PRE>
1138     </P>
1139     <P>
1140     If a pattern contains back references, but the <i>ovector</i> that is passed to
1141     <b>pcre_exec()</b> is not big enough to remember the referenced substrings, PCRE
1142     gets a block of memory at the start of matching to use for this purpose. If the
1143     call via <b>pcre_malloc()</b> fails, this error is given. The memory is freed at
1144     the end of matching.
1145     </P>
1146     <P>
1147     <pre>
1149     </PRE>
1150     </P>
1151     <P>
1152     This error is used by the <b>pcre_copy_substring()</b>,
1153     <b>pcre_get_substring()</b>, and <b>pcre_get_substring_list()</b> functions (see
1154     below). It is never returned by <b>pcre_exec()</b>.
1155     </P>
1156     <P>
1157     <pre>
1159     </PRE>
1160     </P>
1161     <P>
1162     The recursion and backtracking limit, as specified by the <i>match_limit</i>
1163     field in a <b>pcre_extra</b> structure (or defaulted) was reached. See the
1164     description above.
1165     </P>
1166     <P>
1167     <pre>
1168     PCRE_ERROR_CALLOUT (-9)
1169     </PRE>
1170     </P>
1171     <P>
1172     This error is never generated by <b>pcre_exec()</b> itself. It is provided for
1173     use by callout functions that want to yield a distinctive error code. See the
1174     <b>pcrecallout</b> documentation for details.
1175     </P>
1176 nigel 71 <P>
1177     <pre>
1178 nigel 73 PCRE_ERROR_BADUTF8 (-10)
1179 nigel 71 </PRE>
1180     </P>
1181     <P>
1182     A string that contains an invalid UTF-8 byte sequence was passed as a subject.
1183     </P>
1184 nigel 73 <P>
1185     <pre>
1187     </PRE>
1188     </P>
1189     <P>
1190     The UTF-8 byte sequence that was passed as a subject was valid, but the value
1191     of <i>startoffset</i> did not point to the beginning of a UTF-8 character.
1192     </P>
1193 nigel 63 <br><a name="SEC11" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a><br>
1194     <P>
1195     <b>int pcre_copy_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
1196     <b>int <i>stringcount</i>, int <i>stringnumber</i>, char *<i>buffer</i>,</b>
1197     <b>int <i>buffersize</i>);</b>
1198     </P>
1199     <P>
1200     <b>int pcre_get_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
1201     <b>int <i>stringcount</i>, int <i>stringnumber</i>,</b>
1202     <b>const char **<i>stringptr</i>);</b>
1203     </P>
1204     <P>
1205     <b>int pcre_get_substring_list(const char *<i>subject</i>,</b>
1206     <b>int *<i>ovector</i>, int <i>stringcount</i>, const char ***<i>listptr</i>);</b>
1207     </P>
1208     <P>
1209     Captured substrings can be accessed directly by using the offsets returned by
1210     <b>pcre_exec()</b> in <i>ovector</i>. For convenience, the functions
1211     <b>pcre_copy_substring()</b>, <b>pcre_get_substring()</b>, and
1212     <b>pcre_get_substring_list()</b> are provided for extracting captured substrings
1213     as new, separate, zero-terminated strings. These functions identify substrings
1214     by number. The next section describes functions for extracting named
1215     substrings. A substring that contains a binary zero is correctly extracted and
1216     has a further zero added on the end, but the result is not, of course,
1217     a C string.
1218     </P>
1219     <P>
1220     The first three arguments are the same for all three of these functions:
1221     <i>subject</i> is the subject string which has just been successfully matched,
1222     <i>ovector</i> is a pointer to the vector of integer offsets that was passed to
1223     <b>pcre_exec()</b>, and <i>stringcount</i> is the number of substrings that were
1224     captured by the match, including the substring that matched the entire regular
1225     expression. This is the value returned by <b>pcre_exec</b> if it is greater than
1226     zero. If <b>pcre_exec()</b> returned zero, indicating that it ran out of space
1227     in <i>ovector</i>, the value passed as <i>stringcount</i> should be the size of
1228     the vector divided by three.
1229     </P>
1230     <P>
1231     The functions <b>pcre_copy_substring()</b> and <b>pcre_get_substring()</b>
1232     extract a single substring, whose number is given as <i>stringnumber</i>. A
1233     value of zero extracts the substring that matched the entire pattern, while
1234     higher values extract the captured substrings. For <b>pcre_copy_substring()</b>,
1235     the string is placed in <i>buffer</i>, whose length is given by
1236     <i>buffersize</i>, while for <b>pcre_get_substring()</b> a new block of memory is
1237     obtained via <b>pcre_malloc</b>, and its address is returned via
1238     <i>stringptr</i>. The yield of the function is the length of the string, not
1239     including the terminating zero, or one of
1240     </P>
1241     <P>
1242     <pre>
1244     </PRE>
1245     </P>
1246     <P>
1247     The buffer was too small for <b>pcre_copy_substring()</b>, or the attempt to get
1248     memory failed for <b>pcre_get_substring()</b>.
1249     </P>
1250     <P>
1251     <pre>
1253     </PRE>
1254     </P>
1255     <P>
1256     There is no substring whose number is <i>stringnumber</i>.
1257     </P>
1258     <P>
1259     The <b>pcre_get_substring_list()</b> function extracts all available substrings
1260     and builds a list of pointers to them. All this is done in a single block of
1261     memory which is obtained via <b>pcre_malloc</b>. The address of the memory block
1262     is returned via <i>listptr</i>, which is also the start of the list of string
1263     pointers. The end of the list is marked by a NULL pointer. The yield of the
1264     function is zero if all went well, or
1265     </P>
1266     <P>
1267     <pre>
1269     </PRE>
1270     </P>
1271     <P>
1272     if the attempt to get the memory block failed.
1273     </P>
1274     <P>
1275     When any of these functions encounter a substring that is unset, which can
1276     happen when capturing subpattern number <i>n+1</i> matches some part of the
1277     subject, but subpattern <i>n</i> has not been used at all, they return an empty
1278     string. This can be distinguished from a genuine zero-length substring by
1279     inspecting the appropriate offset in <i>ovector</i>, which is negative for unset
1280     substrings.
1281     </P>
1282     <P>
1283     The two convenience functions <b>pcre_free_substring()</b> and
1284     <b>pcre_free_substring_list()</b> can be used to free the memory returned by
1285     a previous call of <b>pcre_get_substring()</b> or
1286     <b>pcre_get_substring_list()</b>, respectively. They do nothing more than call
1287     the function pointed to by <b>pcre_free</b>, which of course could be called
1288     directly from a C program. However, PCRE is used in some situations where it is
1289     linked via a special interface to another programming language which cannot use
1290     <b>pcre_free</b> directly; it is for these cases that the functions are
1291     provided.
1292     </P>
1293     <br><a name="SEC12" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a><br>
1294     <P>
1295     <b>int pcre_copy_named_substring(const pcre *<i>code</i>,</b>
1296     <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
1297     <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
1298     <b>char *<i>buffer</i>, int <i>buffersize</i>);</b>
1299     </P>
1300     <P>
1301     <b>int pcre_get_stringnumber(const pcre *<i>code</i>,</b>
1302     <b>const char *<i>name</i>);</b>
1303     </P>
1304     <P>
1305     <b>int pcre_get_named_substring(const pcre *<i>code</i>,</b>
1306     <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
1307     <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
1308     <b>const char **<i>stringptr</i>);</b>
1309     </P>
1310     <P>
1311     To extract a substring by name, you first have to find associated number. This
1312     can be done by calling <b>pcre_get_stringnumber()</b>. The first argument is the
1313     compiled pattern, and the second is the name. For example, for this pattern
1314     </P>
1315     <P>
1316     <pre>
1317     ab(?&#60;xxx&#62;\d+)...
1318     </PRE>
1319     </P>
1320     <P>
1321     the number of the subpattern called "xxx" is 1. Given the number, you can then
1322     extract the substring directly, or use one of the functions described in the
1323     previous section. For convenience, there are also two functions that do the
1324     whole job.
1325     </P>
1326     <P>
1327     Most of the arguments of <i>pcre_copy_named_substring()</i> and
1328     <i>pcre_get_named_substring()</i> are the same as those for the functions that
1329     extract by number, and so are not re-described here. There are just two
1330     differences.
1331     </P>
1332     <P>
1333     First, instead of a substring number, a substring name is given. Second, there
1334     is an extra argument, given at the start, which is a pointer to the compiled
1335     pattern. This is needed in order to gain access to the name-to-number
1336     translation table.
1337     </P>
1338     <P>
1339     These functions call <b>pcre_get_stringnumber()</b>, and if it succeeds, they
1340     then call <i>pcre_copy_substring()</i> or <i>pcre_get_substring()</i>, as
1341     appropriate.
1342     </P>
1343     <P>
1344 nigel 73 Last updated: 09 December 2003
1345 nigel 63 <br>
1346     Copyright &copy; 1997-2003 University of Cambridge.

ViewVC Help
Powered by ViewVC 1.1.12