ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

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

  ViewVC Help
Powered by ViewVC 1.1.5