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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (show annotations) (download) (as text)
Sat Feb 24 21:40:03 2007 UTC (7 years, 7 months ago) by nigel
File MIME type: text/html
File size: 47408 byte(s)
Load pcre-4.0 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>
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 <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>
192 PCRE_CONFIG_UTF8
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>
201 PCRE_CONFIG_NEWLINE
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>
211 PCRE_CONFIG_LINK_SIZE
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>
223 PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
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>
233 PCRE_CONFIG_MATCH_LIMIT
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>
307 PCRE_ANCHORED
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>
319 PCRE_CASELESS
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>
329 PCRE_DOLLAR_ENDONLY
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>
342 PCRE_DOTALL
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>
354 PCRE_EXTENDED
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>
373 PCRE_EXTRA
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>
388 PCRE_MULTILINE
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>
409 PCRE_NO_AUTO_CAPTURE
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>
421 PCRE_UNGREEDY
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>
431 PCRE_UTF8
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 <br><a name="SEC6" href="#TOC1">STUDYING A PATTERN</a><br>
446 <P>
447 <b>pcre_extra *pcre_study(const pcre *<i>code</i>, int <i>options</i>,</b>
448 <b>const char **<i>errptr</i>);</b>
449 </P>
450 <P>
451 When a pattern is going to be used several times, it is worth spending more
452 time analyzing it in order to speed up the time taken for matching. The
453 function <b>pcre_study()</b> takes a pointer to a compiled pattern as its first
454 argument. If studing the pattern produces additional information that will help
455 speed up matching, <b>pcre_study()</b> returns a pointer to a <b>pcre_extra</b>
456 block, in which the <i>study_data</i> field points to the results of the study.
457 </P>
458 <P>
459 The returned value from a <b>pcre_study()</b> can be passed directly to
460 <b>pcre_exec()</b>. However, the <b>pcre_extra</b> block also contains other
461 fields that can be set by the caller before the block is passed; these are
462 described below. If studying the pattern does not produce any additional
463 information, <b>pcre_study()</b> returns NULL. In that circumstance, if the
464 calling program wants to pass some of the other fields to <b>pcre_exec()</b>, it
465 must set up its own <b>pcre_extra</b> block.
466 </P>
467 <P>
468 The second argument contains option bits. At present, no options are defined
469 for <b>pcre_study()</b>, and this argument should always be zero.
470 </P>
471 <P>
472 The third argument for <b>pcre_study()</b> is a pointer for an error message. If
473 studying succeeds (even if no data is returned), the variable it points to is
474 set to NULL. Otherwise it points to a textual error message. You should
475 therefore test the error pointer for NULL after calling <b>pcre_study()</b>, to
476 be sure that it has run successfully.
477 </P>
478 <P>
479 This is a typical call to <b>pcre_study</b>():
480 </P>
481 <P>
482 <pre>
483 pcre_extra *pe;
484 pe = pcre_study(
485 re, /* result of pcre_compile() */
486 0, /* no options exist */
487 &error); /* set to NULL or points to a message */
488 </PRE>
489 </P>
490 <P>
491 At present, studying a pattern is useful only for non-anchored patterns that do
492 not have a single fixed starting character. A bitmap of possible starting
493 characters is created.
494 </P>
495 <a name="localesupport"></a><br><a name="SEC7" href="#TOC1">LOCALE SUPPORT</a><br>
496 <P>
497 PCRE handles caseless matching, and determines whether characters are letters,
498 digits, or whatever, by reference to a set of tables. When running in UTF-8
499 mode, this applies only to characters with codes less than 256. The library
500 contains a default set of tables that is created in the default C locale when
501 PCRE is compiled. This is used when the final argument of <b>pcre_compile()</b>
502 is NULL, and is sufficient for many applications.
503 </P>
504 <P>
505 An alternative set of tables can, however, be supplied. Such tables are built
506 by calling the <b>pcre_maketables()</b> function, which has no arguments, in the
507 relevant locale. The result can then be passed to <b>pcre_compile()</b> as often
508 as necessary. For example, to build and use tables that are appropriate for the
509 French locale (where accented characters with codes greater than 128 are
510 treated as letters), the following code could be used:
511 </P>
512 <P>
513 <pre>
514 setlocale(LC_CTYPE, "fr");
515 tables = pcre_maketables();
516 re = pcre_compile(..., tables);
517 </PRE>
518 </P>
519 <P>
520 The tables are built in memory that is obtained via <b>pcre_malloc</b>. The
521 pointer that is passed to <b>pcre_compile</b> is saved with the compiled
522 pattern, and the same tables are used via this pointer by <b>pcre_study()</b>
523 and <b>pcre_exec()</b>. Thus, for any single pattern, compilation, studying and
524 matching all happen in the same locale, but different patterns can be compiled
525 in different locales. It is the caller's responsibility to ensure that the
526 memory containing the tables remains available for as long as it is needed.
527 </P>
528 <br><a name="SEC8" href="#TOC1">INFORMATION ABOUT A PATTERN</a><br>
529 <P>
530 <b>int pcre_fullinfo(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
531 <b>int <i>what</i>, void *<i>where</i>);</b>
532 </P>
533 <P>
534 The <b>pcre_fullinfo()</b> function returns information about a compiled
535 pattern. It replaces the obsolete <b>pcre_info()</b> function, which is
536 nevertheless retained for backwards compability (and is documented below).
537 </P>
538 <P>
539 The first argument for <b>pcre_fullinfo()</b> is a pointer to the compiled
540 pattern. The second argument is the result of <b>pcre_study()</b>, or NULL if
541 the pattern was not studied. The third argument specifies which piece of
542 information is required, and the fourth argument is a pointer to a variable
543 to receive the data. The yield of the function is zero for success, or one of
544 the following negative numbers:
545 </P>
546 <P>
547 <pre>
548 PCRE_ERROR_NULL the argument <i>code</i> was NULL
549 the argument <i>where</i> was NULL
550 PCRE_ERROR_BADMAGIC the "magic number" was not found
551 PCRE_ERROR_BADOPTION the value of <i>what</i> was invalid
552 </PRE>
553 </P>
554 <P>
555 Here is a typical call of <b>pcre_fullinfo()</b>, to obtain the length of the
556 compiled pattern:
557 </P>
558 <P>
559 <pre>
560 int rc;
561 unsigned long int length;
562 rc = pcre_fullinfo(
563 re, /* result of pcre_compile() */
564 pe, /* result of pcre_study(), or NULL */
565 PCRE_INFO_SIZE, /* what is required */
566 &length); /* where to put the data */
567 </PRE>
568 </P>
569 <P>
570 The possible values for the third argument are defined in <b>pcre.h</b>, and are
571 as follows:
572 </P>
573 <P>
574 <pre>
575 PCRE_INFO_BACKREFMAX
576 </PRE>
577 </P>
578 <P>
579 Return the number of the highest back reference in the pattern. The fourth
580 argument should point to an <b>int</b> variable. Zero is returned if there are
581 no back references.
582 </P>
583 <P>
584 <pre>
585 PCRE_INFO_CAPTURECOUNT
586 </PRE>
587 </P>
588 <P>
589 Return the number of capturing subpatterns in the pattern. The fourth argument
590 should point to an \fbint\fR variable.
591 </P>
592 <P>
593 <pre>
594 PCRE_INFO_FIRSTBYTE
595 </PRE>
596 </P>
597 <P>
598 Return information about the first byte of any matched string, for a
599 non-anchored pattern. (This option used to be called PCRE_INFO_FIRSTCHAR; the
600 old name is still recognized for backwards compatibility.)
601 </P>
602 <P>
603 If there is a fixed first byte, e.g. from a pattern such as (cat|cow|coyote),
604 it is returned in the integer pointed to by <i>where</i>. Otherwise, if either
605 </P>
606 <P>
607 (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
608 starts with "^", or
609 </P>
610 <P>
611 (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
612 (if it were set, the pattern would be anchored),
613 </P>
614 <P>
615 -1 is returned, indicating that the pattern matches only at the start of a
616 subject string or after any newline within the string. Otherwise -2 is
617 returned. For anchored patterns, -2 is returned.
618 </P>
619 <P>
620 <pre>
621 PCRE_INFO_FIRSTTABLE
622 </PRE>
623 </P>
624 <P>
625 If the pattern was studied, and this resulted in the construction of a 256-bit
626 table indicating a fixed set of bytes for the first byte in any matching
627 string, a pointer to the table is returned. Otherwise NULL is returned. The
628 fourth argument should point to an <b>unsigned char *</b> variable.
629 </P>
630 <P>
631 <pre>
632 PCRE_INFO_LASTLITERAL
633 </PRE>
634 </P>
635 <P>
636 For a non-anchored pattern, return the value of the rightmost literal byte
637 which must exist in any matched string, other than at its start. The fourth
638 argument should point to an <b>int</b> variable. If there is no such byte, or if
639 the pattern is anchored, -1 is returned. For example, for the pattern
640 /a\d+z\d+/ the returned value is 'z'.
641 </P>
642 <P>
643 <pre>
644 PCRE_INFO_NAMECOUNT
645 PCRE_INFO_NAMEENTRYSIZE
646 PCRE_INFO_NAMETABLE
647 </PRE>
648 </P>
649 <P>
650 PCRE supports the use of named as well as numbered capturing parentheses. The
651 names are just an additional way of identifying the parentheses, which still
652 acquire a number. A caller that wants to extract data from a named subpattern
653 must convert the name to a number in order to access the correct pointers in
654 the output vector (described with <b>pcre_exec()</b> below). In order to do
655 this, it must first use these three values to obtain the name-to-number mapping
656 table for the pattern.
657 </P>
658 <P>
659 The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT gives
660 the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size of each
661 entry; both of these return an <b>int</b> value. The entry size depends on the
662 length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
663 entry of the table (a pointer to <b>char</b>). The first two bytes of each entry
664 are the number of the capturing parenthesis, most significant byte first. The
665 rest of the entry is the corresponding name, zero terminated. The names are in
666 alphabetical order. For example, consider the following pattern (assume
667 PCRE_EXTENDED is set, so white space - including newlines - is ignored):
668 </P>
669 <P>
670 <pre>
671 (?P&#60;date&#62; (?P&#60;year&#62;(\d\d)?\d\d) -
672 (?P&#60;month&#62;\d\d) - (?P&#60;day&#62;\d\d) )
673 </PRE>
674 </P>
675 <P>
676 There are four named subpatterns, so the table has four entries, and each entry
677 in the table is eight bytes long. The table is as follows, with non-printing
678 bytes shows in hex, and undefined bytes shown as ??:
679 </P>
680 <P>
681 <pre>
682 00 01 d a t e 00 ??
683 00 05 d a y 00 ?? ??
684 00 04 m o n t h 00
685 00 02 y e a r 00 ??
686 </PRE>
687 </P>
688 <P>
689 When writing code to extract data from named subpatterns, remember that the
690 length of each entry may be different for each compiled pattern.
691 </P>
692 <P>
693 <pre>
694 PCRE_INFO_OPTIONS
695 </PRE>
696 </P>
697 <P>
698 Return a copy of the options with which the pattern was compiled. The fourth
699 argument should point to an <b>unsigned long int</b> variable. These option bits
700 are those specified in the call to <b>pcre_compile()</b>, modified by any
701 top-level option settings within the pattern itself.
702 </P>
703 <P>
704 A pattern is automatically anchored by PCRE if all of its top-level
705 alternatives begin with one of the following:
706 </P>
707 <P>
708 <pre>
709 ^ unless PCRE_MULTILINE is set
710 \A always
711 \G always
712 .* if PCRE_DOTALL is set and there are no back
713 references to the subpattern in which .* appears
714 </PRE>
715 </P>
716 <P>
717 For such patterns, the PCRE_ANCHORED bit is set in the options returned by
718 <b>pcre_fullinfo()</b>.
719 </P>
720 <P>
721 <pre>
722 PCRE_INFO_SIZE
723 </PRE>
724 </P>
725 <P>
726 Return the size of the compiled pattern, that is, the value that was passed as
727 the argument to <b>pcre_malloc()</b> when PCRE was getting memory in which to
728 place the compiled data. The fourth argument should point to a <b>size_t</b>
729 variable.
730 </P>
731 <P>
732 <pre>
733 PCRE_INFO_STUDYSIZE
734 </PRE>
735 </P>
736 <P>
737 Returns the size of the data block pointed to by the <i>study_data</i> field in
738 a <b>pcre_extra</b> block. That is, it is the value that was passed to
739 <b>pcre_malloc()</b> when PCRE was getting memory into which to place the data
740 created by <b>pcre_study()</b>. The fourth argument should point to a
741 <b>size_t</b> variable.
742 </P>
743 <br><a name="SEC9" href="#TOC1">OBSOLETE INFO FUNCTION</a><br>
744 <P>
745 <b>int pcre_info(const pcre *<i>code</i>, int *<i>optptr</i>, int</b>
746 <b>*<i>firstcharptr</i>);</b>
747 </P>
748 <P>
749 The <b>pcre_info()</b> function is now obsolete because its interface is too
750 restrictive to return all the available data about a compiled pattern. New
751 programs should use <b>pcre_fullinfo()</b> instead. The yield of
752 <b>pcre_info()</b> is the number of capturing subpatterns, or one of the
753 following negative numbers:
754 </P>
755 <P>
756 <pre>
757 PCRE_ERROR_NULL the argument <i>code</i> was NULL
758 PCRE_ERROR_BADMAGIC the "magic number" was not found
759 </PRE>
760 </P>
761 <P>
762 If the <i>optptr</i> argument is not NULL, a copy of the options with which the
763 pattern was compiled is placed in the integer it points to (see
764 PCRE_INFO_OPTIONS above).
765 </P>
766 <P>
767 If the pattern is not anchored and the <i>firstcharptr</i> argument is not NULL,
768 it is used to pass back information about the first character of any matched
769 string (see PCRE_INFO_FIRSTBYTE above).
770 </P>
771 <br><a name="SEC10" href="#TOC1">MATCHING A PATTERN</a><br>
772 <P>
773 <b>int pcre_exec(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
774 <b>const char *<i>subject</i>, int <i>length</i>, int <i>startoffset</i>,</b>
775 <b>int <i>options</i>, int *<i>ovector</i>, int <i>ovecsize</i>);</b>
776 </P>
777 <P>
778 The function <b>pcre_exec()</b> is called to match a subject string against a
779 pre-compiled pattern, which is passed in the <i>code</i> argument. If the
780 pattern has been studied, the result of the study should be passed in the
781 <i>extra</i> argument.
782 </P>
783 <P>
784 Here is an example of a simple call to <b>pcre_exec()</b>:
785 </P>
786 <P>
787 <pre>
788 int rc;
789 int ovector[30];
790 rc = pcre_exec(
791 re, /* result of pcre_compile() */
792 NULL, /* we didn't study the pattern */
793 "some string", /* the subject string */
794 11, /* the length of the subject string */
795 0, /* start at offset 0 in the subject */
796 0, /* default options */
797 ovector, /* vector for substring information */
798 30); /* number of elements in the vector */
799 </PRE>
800 </P>
801 <P>
802 If the <i>extra</i> argument is not NULL, it must point to a <b>pcre_extra</b>
803 data block. The <b>pcre_study()</b> function returns such a block (when it
804 doesn't return NULL), but you can also create one for yourself, and pass
805 additional information in it. The fields in the block are as follows:
806 </P>
807 <P>
808 <pre>
809 unsigned long int <i>flags</i>;
810 void *<i>study_data</i>;
811 unsigned long int <i>match_limit</i>;
812 void *<i>callout_data</i>;
813 </PRE>
814 </P>
815 <P>
816 The <i>flags</i> field is a bitmap that specifies which of the other fields
817 are set. The flag bits are:
818 </P>
819 <P>
820 <pre>
821 PCRE_EXTRA_STUDY_DATA
822 PCRE_EXTRA_MATCH_LIMIT
823 PCRE_EXTRA_CALLOUT_DATA
824 </PRE>
825 </P>
826 <P>
827 Other flag bits should be set to zero. The <i>study_data</i> field is set in the
828 <b>pcre_extra</b> block that is returned by <b>pcre_study()</b>, together with
829 the appropriate flag bit. You should not set this yourself, but you can add to
830 the block by setting the other fields.
831 </P>
832 <P>
833 The <i>match_limit</i> field provides a means of preventing PCRE from using up a
834 vast amount of resources when running patterns that are not going to match,
835 but which have a very large number of possibilities in their search trees. The
836 classic example is the use of nested unlimited repeats. Internally, PCRE uses a
837 function called <b>match()</b> which it calls repeatedly (sometimes
838 recursively). The limit is imposed on the number of times this function is
839 called during a match, which has the effect of limiting the amount of recursion
840 and backtracking that can take place. For patterns that are not anchored, the
841 count starts from zero for each position in the subject string.
842 </P>
843 <P>
844 The default limit for the library can be set when PCRE is built; the default
845 default is 10 million, which handles all but the most extreme cases. You can
846 reduce the default by suppling <b>pcre_exec()</b> with a \fRpcre_extra\fR block
847 in which <i>match_limit</i> is set to a smaller value, and
848 PCRE_EXTRA_MATCH_LIMIT is set in the <i>flags</i> field. If the limit is
849 exceeded, <b>pcre_exec()</b> returns PCRE_ERROR_MATCHLIMIT.
850 </P>
851 <P>
852 The <i>pcre_callout</i> field is used in conjunction with the "callout" feature,
853 which is described in the <b>pcrecallout</b> documentation.
854 </P>
855 <P>
856 The PCRE_ANCHORED option can be passed in the <i>options</i> argument, whose
857 unused bits must be zero. This limits <b>pcre_exec()</b> to matching at the
858 first matching position. However, if a pattern was compiled with PCRE_ANCHORED,
859 or turned out to be anchored by virtue of its contents, it cannot be made
860 unachored at matching time.
861 </P>
862 <P>
863 There are also three further options that can be set only at matching time:
864 </P>
865 <P>
866 <pre>
867 PCRE_NOTBOL
868 </PRE>
869 </P>
870 <P>
871 The first character of the string is not the beginning of a line, so the
872 circumflex metacharacter should not match before it. Setting this without
873 PCRE_MULTILINE (at compile time) causes circumflex never to match.
874 </P>
875 <P>
876 <pre>
877 PCRE_NOTEOL
878 </PRE>
879 </P>
880 <P>
881 The end of the string is not the end of a line, so the dollar metacharacter
882 should not match it nor (except in multiline mode) a newline immediately before
883 it. Setting this without PCRE_MULTILINE (at compile time) causes dollar never
884 to match.
885 </P>
886 <P>
887 <pre>
888 PCRE_NOTEMPTY
889 </PRE>
890 </P>
891 <P>
892 An empty string is not considered to be a valid match if this option is set. If
893 there are alternatives in the pattern, they are tried. If all the alternatives
894 match the empty string, the entire match fails. For example, if the pattern
895 </P>
896 <P>
897 <pre>
898 a?b?
899 </PRE>
900 </P>
901 <P>
902 is applied to a string not beginning with "a" or "b", it matches the empty
903 string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
904 valid, so PCRE searches further into the string for occurrences of "a" or "b".
905 </P>
906 <P>
907 Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
908 of a pattern match of the empty string within its <b>split()</b> function, and
909 when using the /g modifier. It is possible to emulate Perl's behaviour after
910 matching a null string by first trying the match again at the same offset with
911 PCRE_NOTEMPTY set, and then if that fails by advancing the starting offset (see
912 below) and trying an ordinary match again.
913 </P>
914 <P>
915 The subject string is passed to <b>pcre_exec()</b> as a pointer in
916 <i>subject</i>, a length in <i>length</i>, and a starting offset in
917 <i>startoffset</i>. Unlike the pattern string, the subject may contain binary
918 zero bytes. When the starting offset is zero, the search for a match starts at
919 the beginning of the subject, and this is by far the most common case.
920 </P>
921 <P>
922 If the pattern was compiled with the PCRE_UTF8 option, the subject must be a
923 sequence of bytes that is a valid UTF-8 string. If an invalid UTF-8 string is
924 passed, PCRE's behaviour is not defined.
925 </P>
926 <P>
927 A non-zero starting offset is useful when searching for another match in the
928 same subject by calling <b>pcre_exec()</b> again after a previous success.
929 Setting <i>startoffset</i> differs from just passing over a shortened string and
930 setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
931 lookbehind. For example, consider the pattern
932 </P>
933 <P>
934 <pre>
935 \Biss\B
936 </PRE>
937 </P>
938 <P>
939 which finds occurrences of "iss" in the middle of words. (\B matches only if
940 the current position in the subject is not a word boundary.) When applied to
941 the string "Mississipi" the first call to <b>pcre_exec()</b> finds the first
942 occurrence. If <b>pcre_exec()</b> is called again with just the remainder of the
943 subject, namely "issipi", it does not match, because \B is always false at the
944 start of the subject, which is deemed to be a word boundary. However, if
945 <b>pcre_exec()</b> is passed the entire string again, but with <i>startoffset</i>
946 set to 4, it finds the second occurrence of "iss" because it is able to look
947 behind the starting point to discover that it is preceded by a letter.
948 </P>
949 <P>
950 If a non-zero starting offset is passed when the pattern is anchored, one
951 attempt to match at the given offset is tried. This can only succeed if the
952 pattern does not require the match to be at the start of the subject.
953 </P>
954 <P>
955 In general, a pattern matches a certain portion of the subject, and in
956 addition, further substrings from the subject may be picked out by parts of the
957 pattern. Following the usage in Jeffrey Friedl's book, this is called
958 "capturing" in what follows, and the phrase "capturing subpattern" is used for
959 a fragment of a pattern that picks out a substring. PCRE supports several other
960 kinds of parenthesized subpattern that do not cause substrings to be captured.
961 </P>
962 <P>
963 Captured substrings are returned to the caller via a vector of integer offsets
964 whose address is passed in <i>ovector</i>. The number of elements in the vector
965 is passed in <i>ovecsize</i>. The first two-thirds of the vector is used to pass
966 back captured substrings, each substring using a pair of integers. The
967 remaining third of the vector is used as workspace by <b>pcre_exec()</b> while
968 matching capturing subpatterns, and is not available for passing back
969 information. The length passed in <i>ovecsize</i> should always be a multiple of
970 three. If it is not, it is rounded down.
971 </P>
972 <P>
973 When a match has been successful, information about captured substrings is
974 returned in pairs of integers, starting at the beginning of <i>ovector</i>, and
975 continuing up to two-thirds of its length at the most. The first element of a
976 pair is set to the offset of the first character in a substring, and the second
977 is set to the offset of the first character after the end of a substring. The
978 first pair, <i>ovector[0]</i> and <i>ovector[1]</i>, identify the portion of the
979 subject string matched by the entire pattern. The next pair is used for the
980 first capturing subpattern, and so on. The value returned by <b>pcre_exec()</b>
981 is the number of pairs that have been set. If there are no capturing
982 subpatterns, the return value from a successful match is 1, indicating that
983 just the first pair of offsets has been set.
984 </P>
985 <P>
986 Some convenience functions are provided for extracting the captured substrings
987 as separate strings. These are described in the following section.
988 </P>
989 <P>
990 It is possible for an capturing subpattern number <i>n+1</i> to match some
991 part of the subject when subpattern <i>n</i> has not been used at all. For
992 example, if the string "abc" is matched against the pattern (a|(z))(bc)
993 subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
994 values corresponding to the unused subpattern are set to -1.
995 </P>
996 <P>
997 If a capturing subpattern is matched repeatedly, it is the last portion of the
998 string that it matched that gets returned.
999 </P>
1000 <P>
1001 If the vector is too small to hold all the captured substrings, it is used as
1002 far as possible (up to two-thirds of its length), and the function returns a
1003 value of zero. In particular, if the substring offsets are not of interest,
1004 <b>pcre_exec()</b> may be called with <i>ovector</i> passed as NULL and
1005 <i>ovecsize</i> as zero. However, if the pattern contains back references and
1006 the <i>ovector</i> isn't big enough to remember the related substrings, PCRE has
1007 to get additional memory for use during matching. Thus it is usually advisable
1008 to supply an <i>ovector</i>.
1009 </P>
1010 <P>
1011 Note that <b>pcre_info()</b> can be used to find out how many capturing
1012 subpatterns there are in a compiled pattern. The smallest size for
1013 <i>ovector</i> that will allow for <i>n</i> captured substrings, in addition to
1014 the offsets of the substring matched by the whole pattern, is (<i>n</i>+1)*3.
1015 </P>
1016 <P>
1017 If <b>pcre_exec()</b> fails, it returns a negative number. The following are
1018 defined in the header file:
1019 </P>
1020 <P>
1021 <pre>
1022 PCRE_ERROR_NOMATCH (-1)
1023 </PRE>
1024 </P>
1025 <P>
1026 The subject string did not match the pattern.
1027 </P>
1028 <P>
1029 <pre>
1030 PCRE_ERROR_NULL (-2)
1031 </PRE>
1032 </P>
1033 <P>
1034 Either <i>code</i> or <i>subject</i> was passed as NULL, or <i>ovector</i> was
1035 NULL and <i>ovecsize</i> was not zero.
1036 </P>
1037 <P>
1038 <pre>
1039 PCRE_ERROR_BADOPTION (-3)
1040 </PRE>
1041 </P>
1042 <P>
1043 An unrecognized bit was set in the <i>options</i> argument.
1044 </P>
1045 <P>
1046 <pre>
1047 PCRE_ERROR_BADMAGIC (-4)
1048 </PRE>
1049 </P>
1050 <P>
1051 PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
1052 the case when it is passed a junk pointer. This is the error it gives when the
1053 magic number isn't present.
1054 </P>
1055 <P>
1056 <pre>
1057 PCRE_ERROR_UNKNOWN_NODE (-5)
1058 </PRE>
1059 </P>
1060 <P>
1061 While running the pattern match, an unknown item was encountered in the
1062 compiled pattern. This error could be caused by a bug in PCRE or by overwriting
1063 of the compiled pattern.
1064 </P>
1065 <P>
1066 <pre>
1067 PCRE_ERROR_NOMEMORY (-6)
1068 </PRE>
1069 </P>
1070 <P>
1071 If a pattern contains back references, but the <i>ovector</i> that is passed to
1072 <b>pcre_exec()</b> is not big enough to remember the referenced substrings, PCRE
1073 gets a block of memory at the start of matching to use for this purpose. If the
1074 call via <b>pcre_malloc()</b> fails, this error is given. The memory is freed at
1075 the end of matching.
1076 </P>
1077 <P>
1078 <pre>
1079 PCRE_ERROR_NOSUBSTRING (-7)
1080 </PRE>
1081 </P>
1082 <P>
1083 This error is used by the <b>pcre_copy_substring()</b>,
1084 <b>pcre_get_substring()</b>, and <b>pcre_get_substring_list()</b> functions (see
1085 below). It is never returned by <b>pcre_exec()</b>.
1086 </P>
1087 <P>
1088 <pre>
1089 PCRE_ERROR_MATCHLIMIT (-8)
1090 </PRE>
1091 </P>
1092 <P>
1093 The recursion and backtracking limit, as specified by the <i>match_limit</i>
1094 field in a <b>pcre_extra</b> structure (or defaulted) was reached. See the
1095 description above.
1096 </P>
1097 <P>
1098 <pre>
1099 PCRE_ERROR_CALLOUT (-9)
1100 </PRE>
1101 </P>
1102 <P>
1103 This error is never generated by <b>pcre_exec()</b> itself. It is provided for
1104 use by callout functions that want to yield a distinctive error code. See the
1105 <b>pcrecallout</b> documentation for details.
1106 </P>
1107 <br><a name="SEC11" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a><br>
1108 <P>
1109 <b>int pcre_copy_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
1110 <b>int <i>stringcount</i>, int <i>stringnumber</i>, char *<i>buffer</i>,</b>
1111 <b>int <i>buffersize</i>);</b>
1112 </P>
1113 <P>
1114 <b>int pcre_get_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
1115 <b>int <i>stringcount</i>, int <i>stringnumber</i>,</b>
1116 <b>const char **<i>stringptr</i>);</b>
1117 </P>
1118 <P>
1119 <b>int pcre_get_substring_list(const char *<i>subject</i>,</b>
1120 <b>int *<i>ovector</i>, int <i>stringcount</i>, const char ***<i>listptr</i>);</b>
1121 </P>
1122 <P>
1123 Captured substrings can be accessed directly by using the offsets returned by
1124 <b>pcre_exec()</b> in <i>ovector</i>. For convenience, the functions
1125 <b>pcre_copy_substring()</b>, <b>pcre_get_substring()</b>, and
1126 <b>pcre_get_substring_list()</b> are provided for extracting captured substrings
1127 as new, separate, zero-terminated strings. These functions identify substrings
1128 by number. The next section describes functions for extracting named
1129 substrings. A substring that contains a binary zero is correctly extracted and
1130 has a further zero added on the end, but the result is not, of course,
1131 a C string.
1132 </P>
1133 <P>
1134 The first three arguments are the same for all three of these functions:
1135 <i>subject</i> is the subject string which has just been successfully matched,
1136 <i>ovector</i> is a pointer to the vector of integer offsets that was passed to
1137 <b>pcre_exec()</b>, and <i>stringcount</i> is the number of substrings that were
1138 captured by the match, including the substring that matched the entire regular
1139 expression. This is the value returned by <b>pcre_exec</b> if it is greater than
1140 zero. If <b>pcre_exec()</b> returned zero, indicating that it ran out of space
1141 in <i>ovector</i>, the value passed as <i>stringcount</i> should be the size of
1142 the vector divided by three.
1143 </P>
1144 <P>
1145 The functions <b>pcre_copy_substring()</b> and <b>pcre_get_substring()</b>
1146 extract a single substring, whose number is given as <i>stringnumber</i>. A
1147 value of zero extracts the substring that matched the entire pattern, while
1148 higher values extract the captured substrings. For <b>pcre_copy_substring()</b>,
1149 the string is placed in <i>buffer</i>, whose length is given by
1150 <i>buffersize</i>, while for <b>pcre_get_substring()</b> a new block of memory is
1151 obtained via <b>pcre_malloc</b>, and its address is returned via
1152 <i>stringptr</i>. The yield of the function is the length of the string, not
1153 including the terminating zero, or one of
1154 </P>
1155 <P>
1156 <pre>
1157 PCRE_ERROR_NOMEMORY (-6)
1158 </PRE>
1159 </P>
1160 <P>
1161 The buffer was too small for <b>pcre_copy_substring()</b>, or the attempt to get
1162 memory failed for <b>pcre_get_substring()</b>.
1163 </P>
1164 <P>
1165 <pre>
1166 PCRE_ERROR_NOSUBSTRING (-7)
1167 </PRE>
1168 </P>
1169 <P>
1170 There is no substring whose number is <i>stringnumber</i>.
1171 </P>
1172 <P>
1173 The <b>pcre_get_substring_list()</b> function extracts all available substrings
1174 and builds a list of pointers to them. All this is done in a single block of
1175 memory which is obtained via <b>pcre_malloc</b>. The address of the memory block
1176 is returned via <i>listptr</i>, which is also the start of the list of string
1177 pointers. The end of the list is marked by a NULL pointer. The yield of the
1178 function is zero if all went well, or
1179 </P>
1180 <P>
1181 <pre>
1182 PCRE_ERROR_NOMEMORY (-6)
1183 </PRE>
1184 </P>
1185 <P>
1186 if the attempt to get the memory block failed.
1187 </P>
1188 <P>
1189 When any of these functions encounter a substring that is unset, which can
1190 happen when capturing subpattern number <i>n+1</i> matches some part of the
1191 subject, but subpattern <i>n</i> has not been used at all, they return an empty
1192 string. This can be distinguished from a genuine zero-length substring by
1193 inspecting the appropriate offset in <i>ovector</i>, which is negative for unset
1194 substrings.
1195 </P>
1196 <P>
1197 The two convenience functions <b>pcre_free_substring()</b> and
1198 <b>pcre_free_substring_list()</b> can be used to free the memory returned by
1199 a previous call of <b>pcre_get_substring()</b> or
1200 <b>pcre_get_substring_list()</b>, respectively. They do nothing more than call
1201 the function pointed to by <b>pcre_free</b>, which of course could be called
1202 directly from a C program. However, PCRE is used in some situations where it is
1203 linked via a special interface to another programming language which cannot use
1204 <b>pcre_free</b> directly; it is for these cases that the functions are
1205 provided.
1206 </P>
1207 <br><a name="SEC12" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a><br>
1208 <P>
1209 <b>int pcre_copy_named_substring(const pcre *<i>code</i>,</b>
1210 <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
1211 <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
1212 <b>char *<i>buffer</i>, int <i>buffersize</i>);</b>
1213 </P>
1214 <P>
1215 <b>int pcre_get_stringnumber(const pcre *<i>code</i>,</b>
1216 <b>const char *<i>name</i>);</b>
1217 </P>
1218 <P>
1219 <b>int pcre_get_named_substring(const pcre *<i>code</i>,</b>
1220 <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
1221 <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
1222 <b>const char **<i>stringptr</i>);</b>
1223 </P>
1224 <P>
1225 To extract a substring by name, you first have to find associated number. This
1226 can be done by calling <b>pcre_get_stringnumber()</b>. The first argument is the
1227 compiled pattern, and the second is the name. For example, for this pattern
1228 </P>
1229 <P>
1230 <pre>
1231 ab(?&#60;xxx&#62;\d+)...
1232 </PRE>
1233 </P>
1234 <P>
1235 the number of the subpattern called "xxx" is 1. Given the number, you can then
1236 extract the substring directly, or use one of the functions described in the
1237 previous section. For convenience, there are also two functions that do the
1238 whole job.
1239 </P>
1240 <P>
1241 Most of the arguments of <i>pcre_copy_named_substring()</i> and
1242 <i>pcre_get_named_substring()</i> are the same as those for the functions that
1243 extract by number, and so are not re-described here. There are just two
1244 differences.
1245 </P>
1246 <P>
1247 First, instead of a substring number, a substring name is given. Second, there
1248 is an extra argument, given at the start, which is a pointer to the compiled
1249 pattern. This is needed in order to gain access to the name-to-number
1250 translation table.
1251 </P>
1252 <P>
1253 These functions call <b>pcre_get_stringnumber()</b>, and if it succeeds, they
1254 then call <i>pcre_copy_substring()</i> or <i>pcre_get_substring()</i>, as
1255 appropriate.
1256 </P>
1257 <P>
1258 Last updated: 03 February 2003
1259 <br>
1260 Copyright &copy; 1997-2003 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12