ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 65 - (show annotations)
Sat Feb 24 21:40:08 2007 UTC (8 years, 9 months ago) by nigel
File MIME type: text/html
File size: 47531 byte(s)
Error occurred while calculating annotation data.
Load pcre-4.1 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 <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>
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>
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>
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>
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>
633 </PRE>
634 </P>
635 <P>
636 Return the value of the rightmost literal byte that must exist in any matched
637 string, other than at its start, if such a byte has been recorded. The fourth
638 argument should point to an <b>int</b> variable. If there is no such byte, -1 is
639 returned. For anchored patterns, a last literal byte is recorded only if it
640 follows something of variable length. For example, for the pattern
641 /^a\d+z\d+/ the returned value is "z", but for /^a\dz\d/ the returned value
642 is -1.
643 </P>
644 <P>
645 <pre>
649 </PRE>
650 </P>
651 <P>
652 PCRE supports the use of named as well as numbered capturing parentheses. The
653 names are just an additional way of identifying the parentheses, which still
654 acquire a number. A caller that wants to extract data from a named subpattern
655 must convert the name to a number in order to access the correct pointers in
656 the output vector (described with <b>pcre_exec()</b> below). In order to do
657 this, it must first use these three values to obtain the name-to-number mapping
658 table for the pattern.
659 </P>
660 <P>
661 The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT gives
662 the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size of each
663 entry; both of these return an <b>int</b> value. The entry size depends on the
664 length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
665 entry of the table (a pointer to <b>char</b>). The first two bytes of each entry
666 are the number of the capturing parenthesis, most significant byte first. The
667 rest of the entry is the corresponding name, zero terminated. The names are in
668 alphabetical order. For example, consider the following pattern (assume
669 PCRE_EXTENDED is set, so white space - including newlines - is ignored):
670 </P>
671 <P>
672 <pre>
673 (?P&#60;date&#62; (?P&#60;year&#62;(\d\d)?\d\d) -
674 (?P&#60;month&#62;\d\d) - (?P&#60;day&#62;\d\d) )
675 </PRE>
676 </P>
677 <P>
678 There are four named subpatterns, so the table has four entries, and each entry
679 in the table is eight bytes long. The table is as follows, with non-printing
680 bytes shows in hex, and undefined bytes shown as ??:
681 </P>
682 <P>
683 <pre>
684 00 01 d a t e 00 ??
685 00 05 d a y 00 ?? ??
686 00 04 m o n t h 00
687 00 02 y e a r 00 ??
688 </PRE>
689 </P>
690 <P>
691 When writing code to extract data from named subpatterns, remember that the
692 length of each entry may be different for each compiled pattern.
693 </P>
694 <P>
695 <pre>
697 </PRE>
698 </P>
699 <P>
700 Return a copy of the options with which the pattern was compiled. The fourth
701 argument should point to an <b>unsigned long int</b> variable. These option bits
702 are those specified in the call to <b>pcre_compile()</b>, modified by any
703 top-level option settings within the pattern itself.
704 </P>
705 <P>
706 A pattern is automatically anchored by PCRE if all of its top-level
707 alternatives begin with one of the following:
708 </P>
709 <P>
710 <pre>
711 ^ unless PCRE_MULTILINE is set
712 \A always
713 \G always
714 .* if PCRE_DOTALL is set and there are no back
715 references to the subpattern in which .* appears
716 </PRE>
717 </P>
718 <P>
719 For such patterns, the PCRE_ANCHORED bit is set in the options returned by
720 <b>pcre_fullinfo()</b>.
721 </P>
722 <P>
723 <pre>
725 </PRE>
726 </P>
727 <P>
728 Return the size of the compiled pattern, that is, the value that was passed as
729 the argument to <b>pcre_malloc()</b> when PCRE was getting memory in which to
730 place the compiled data. The fourth argument should point to a <b>size_t</b>
731 variable.
732 </P>
733 <P>
734 <pre>
736 </PRE>
737 </P>
738 <P>
739 Returns the size of the data block pointed to by the <i>study_data</i> field in
740 a <b>pcre_extra</b> block. That is, it is the value that was passed to
741 <b>pcre_malloc()</b> when PCRE was getting memory into which to place the data
742 created by <b>pcre_study()</b>. The fourth argument should point to a
743 <b>size_t</b> variable.
744 </P>
745 <br><a name="SEC9" href="#TOC1">OBSOLETE INFO FUNCTION</a><br>
746 <P>
747 <b>int pcre_info(const pcre *<i>code</i>, int *<i>optptr</i>, int</b>
748 <b>*<i>firstcharptr</i>);</b>
749 </P>
750 <P>
751 The <b>pcre_info()</b> function is now obsolete because its interface is too
752 restrictive to return all the available data about a compiled pattern. New
753 programs should use <b>pcre_fullinfo()</b> instead. The yield of
754 <b>pcre_info()</b> is the number of capturing subpatterns, or one of the
755 following negative numbers:
756 </P>
757 <P>
758 <pre>
759 PCRE_ERROR_NULL the argument <i>code</i> was NULL
760 PCRE_ERROR_BADMAGIC the "magic number" was not found
761 </PRE>
762 </P>
763 <P>
764 If the <i>optptr</i> argument is not NULL, a copy of the options with which the
765 pattern was compiled is placed in the integer it points to (see
767 </P>
768 <P>
769 If the pattern is not anchored and the <i>firstcharptr</i> argument is not NULL,
770 it is used to pass back information about the first character of any matched
771 string (see PCRE_INFO_FIRSTBYTE above).
772 </P>
773 <br><a name="SEC10" href="#TOC1">MATCHING A PATTERN</a><br>
774 <P>
775 <b>int pcre_exec(const pcre *<i>code</i>, const pcre_extra *<i>extra</i>,</b>
776 <b>const char *<i>subject</i>, int <i>length</i>, int <i>startoffset</i>,</b>
777 <b>int <i>options</i>, int *<i>ovector</i>, int <i>ovecsize</i>);</b>
778 </P>
779 <P>
780 The function <b>pcre_exec()</b> is called to match a subject string against a
781 pre-compiled pattern, which is passed in the <i>code</i> argument. If the
782 pattern has been studied, the result of the study should be passed in the
783 <i>extra</i> argument.
784 </P>
785 <P>
786 Here is an example of a simple call to <b>pcre_exec()</b>:
787 </P>
788 <P>
789 <pre>
790 int rc;
791 int ovector[30];
792 rc = pcre_exec(
793 re, /* result of pcre_compile() */
794 NULL, /* we didn't study the pattern */
795 "some string", /* the subject string */
796 11, /* the length of the subject string */
797 0, /* start at offset 0 in the subject */
798 0, /* default options */
799 ovector, /* vector for substring information */
800 30); /* number of elements in the vector */
801 </PRE>
802 </P>
803 <P>
804 If the <i>extra</i> argument is not NULL, it must point to a <b>pcre_extra</b>
805 data block. The <b>pcre_study()</b> function returns such a block (when it
806 doesn't return NULL), but you can also create one for yourself, and pass
807 additional information in it. The fields in the block are as follows:
808 </P>
809 <P>
810 <pre>
811 unsigned long int <i>flags</i>;
812 void *<i>study_data</i>;
813 unsigned long int <i>match_limit</i>;
814 void *<i>callout_data</i>;
815 </PRE>
816 </P>
817 <P>
818 The <i>flags</i> field is a bitmap that specifies which of the other fields
819 are set. The flag bits are:
820 </P>
821 <P>
822 <pre>
826 </PRE>
827 </P>
828 <P>
829 Other flag bits should be set to zero. The <i>study_data</i> field is set in the
830 <b>pcre_extra</b> block that is returned by <b>pcre_study()</b>, together with
831 the appropriate flag bit. You should not set this yourself, but you can add to
832 the block by setting the other fields.
833 </P>
834 <P>
835 The <i>match_limit</i> field provides a means of preventing PCRE from using up a
836 vast amount of resources when running patterns that are not going to match,
837 but which have a very large number of possibilities in their search trees. The
838 classic example is the use of nested unlimited repeats. Internally, PCRE uses a
839 function called <b>match()</b> which it calls repeatedly (sometimes
840 recursively). The limit is imposed on the number of times this function is
841 called during a match, which has the effect of limiting the amount of recursion
842 and backtracking that can take place. For patterns that are not anchored, the
843 count starts from zero for each position in the subject string.
844 </P>
845 <P>
846 The default limit for the library can be set when PCRE is built; the default
847 default is 10 million, which handles all but the most extreme cases. You can
848 reduce the default by suppling <b>pcre_exec()</b> with a \fRpcre_extra\fR block
849 in which <i>match_limit</i> is set to a smaller value, and
850 PCRE_EXTRA_MATCH_LIMIT is set in the <i>flags</i> field. If the limit is
851 exceeded, <b>pcre_exec()</b> returns PCRE_ERROR_MATCHLIMIT.
852 </P>
853 <P>
854 The <i>pcre_callout</i> field is used in conjunction with the "callout" feature,
855 which is described in the <b>pcrecallout</b> documentation.
856 </P>
857 <P>
858 The PCRE_ANCHORED option can be passed in the <i>options</i> argument, whose
859 unused bits must be zero. This limits <b>pcre_exec()</b> to matching at the
860 first matching position. However, if a pattern was compiled with PCRE_ANCHORED,
861 or turned out to be anchored by virtue of its contents, it cannot be made
862 unachored at matching time.
863 </P>
864 <P>
865 There are also three further options that can be set only at matching time:
866 </P>
867 <P>
868 <pre>
870 </PRE>
871 </P>
872 <P>
873 The first character of the string is not the beginning of a line, so the
874 circumflex metacharacter should not match before it. Setting this without
875 PCRE_MULTILINE (at compile time) causes circumflex never to match.
876 </P>
877 <P>
878 <pre>
880 </PRE>
881 </P>
882 <P>
883 The end of the string is not the end of a line, so the dollar metacharacter
884 should not match it nor (except in multiline mode) a newline immediately before
885 it. Setting this without PCRE_MULTILINE (at compile time) causes dollar never
886 to match.
887 </P>
888 <P>
889 <pre>
891 </PRE>
892 </P>
893 <P>
894 An empty string is not considered to be a valid match if this option is set. If
895 there are alternatives in the pattern, they are tried. If all the alternatives
896 match the empty string, the entire match fails. For example, if the pattern
897 </P>
898 <P>
899 <pre>
900 a?b?
901 </PRE>
902 </P>
903 <P>
904 is applied to a string not beginning with "a" or "b", it matches the empty
905 string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
906 valid, so PCRE searches further into the string for occurrences of "a" or "b".
907 </P>
908 <P>
909 Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
910 of a pattern match of the empty string within its <b>split()</b> function, and
911 when using the /g modifier. It is possible to emulate Perl's behaviour after
912 matching a null string by first trying the match again at the same offset with
913 PCRE_NOTEMPTY set, and then if that fails by advancing the starting offset (see
914 below) and trying an ordinary match again.
915 </P>
916 <P>
917 The subject string is passed to <b>pcre_exec()</b> as a pointer in
918 <i>subject</i>, a length in <i>length</i>, and a starting offset in
919 <i>startoffset</i>. Unlike the pattern string, the subject may contain binary
920 zero bytes. When the starting offset is zero, the search for a match starts at
921 the beginning of the subject, and this is by far the most common case.
922 </P>
923 <P>
924 If the pattern was compiled with the PCRE_UTF8 option, the subject must be a
925 sequence of bytes that is a valid UTF-8 string. If an invalid UTF-8 string is
926 passed, PCRE's behaviour is not defined.
927 </P>
928 <P>
929 A non-zero starting offset is useful when searching for another match in the
930 same subject by calling <b>pcre_exec()</b> again after a previous success.
931 Setting <i>startoffset</i> differs from just passing over a shortened string and
932 setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
933 lookbehind. For example, consider the pattern
934 </P>
935 <P>
936 <pre>
937 \Biss\B
938 </PRE>
939 </P>
940 <P>
941 which finds occurrences of "iss" in the middle of words. (\B matches only if
942 the current position in the subject is not a word boundary.) When applied to
943 the string "Mississipi" the first call to <b>pcre_exec()</b> finds the first
944 occurrence. If <b>pcre_exec()</b> is called again with just the remainder of the
945 subject, namely "issipi", it does not match, because \B is always false at the
946 start of the subject, which is deemed to be a word boundary. However, if
947 <b>pcre_exec()</b> is passed the entire string again, but with <i>startoffset</i>
948 set to 4, it finds the second occurrence of "iss" because it is able to look
949 behind the starting point to discover that it is preceded by a letter.
950 </P>
951 <P>
952 If a non-zero starting offset is passed when the pattern is anchored, one
953 attempt to match at the given offset is tried. This can only succeed if the
954 pattern does not require the match to be at the start of the subject.
955 </P>
956 <P>
957 In general, a pattern matches a certain portion of the subject, and in
958 addition, further substrings from the subject may be picked out by parts of the
959 pattern. Following the usage in Jeffrey Friedl's book, this is called
960 "capturing" in what follows, and the phrase "capturing subpattern" is used for
961 a fragment of a pattern that picks out a substring. PCRE supports several other
962 kinds of parenthesized subpattern that do not cause substrings to be captured.
963 </P>
964 <P>
965 Captured substrings are returned to the caller via a vector of integer offsets
966 whose address is passed in <i>ovector</i>. The number of elements in the vector
967 is passed in <i>ovecsize</i>. The first two-thirds of the vector is used to pass
968 back captured substrings, each substring using a pair of integers. The
969 remaining third of the vector is used as workspace by <b>pcre_exec()</b> while
970 matching capturing subpatterns, and is not available for passing back
971 information. The length passed in <i>ovecsize</i> should always be a multiple of
972 three. If it is not, it is rounded down.
973 </P>
974 <P>
975 When a match has been successful, information about captured substrings is
976 returned in pairs of integers, starting at the beginning of <i>ovector</i>, and
977 continuing up to two-thirds of its length at the most. The first element of a
978 pair is set to the offset of the first character in a substring, and the second
979 is set to the offset of the first character after the end of a substring. The
980 first pair, <i>ovector[0]</i> and <i>ovector[1]</i>, identify the portion of the
981 subject string matched by the entire pattern. The next pair is used for the
982 first capturing subpattern, and so on. The value returned by <b>pcre_exec()</b>
983 is the number of pairs that have been set. If there are no capturing
984 subpatterns, the return value from a successful match is 1, indicating that
985 just the first pair of offsets has been set.
986 </P>
987 <P>
988 Some convenience functions are provided for extracting the captured substrings
989 as separate strings. These are described in the following section.
990 </P>
991 <P>
992 It is possible for an capturing subpattern number <i>n+1</i> to match some
993 part of the subject when subpattern <i>n</i> has not been used at all. For
994 example, if the string "abc" is matched against the pattern (a|(z))(bc)
995 subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
996 values corresponding to the unused subpattern are set to -1.
997 </P>
998 <P>
999 If a capturing subpattern is matched repeatedly, it is the last portion of the
1000 string that it matched that gets returned.
1001 </P>
1002 <P>
1003 If the vector is too small to hold all the captured substrings, it is used as
1004 far as possible (up to two-thirds of its length), and the function returns a
1005 value of zero. In particular, if the substring offsets are not of interest,
1006 <b>pcre_exec()</b> may be called with <i>ovector</i> passed as NULL and
1007 <i>ovecsize</i> as zero. However, if the pattern contains back references and
1008 the <i>ovector</i> isn't big enough to remember the related substrings, PCRE has
1009 to get additional memory for use during matching. Thus it is usually advisable
1010 to supply an <i>ovector</i>.
1011 </P>
1012 <P>
1013 Note that <b>pcre_info()</b> can be used to find out how many capturing
1014 subpatterns there are in a compiled pattern. The smallest size for
1015 <i>ovector</i> that will allow for <i>n</i> captured substrings, in addition to
1016 the offsets of the substring matched by the whole pattern, is (<i>n</i>+1)*3.
1017 </P>
1018 <P>
1019 If <b>pcre_exec()</b> fails, it returns a negative number. The following are
1020 defined in the header file:
1021 </P>
1022 <P>
1023 <pre>
1025 </PRE>
1026 </P>
1027 <P>
1028 The subject string did not match the pattern.
1029 </P>
1030 <P>
1031 <pre>
1033 </PRE>
1034 </P>
1035 <P>
1036 Either <i>code</i> or <i>subject</i> was passed as NULL, or <i>ovector</i> was
1037 NULL and <i>ovecsize</i> was not zero.
1038 </P>
1039 <P>
1040 <pre>
1042 </PRE>
1043 </P>
1044 <P>
1045 An unrecognized bit was set in the <i>options</i> argument.
1046 </P>
1047 <P>
1048 <pre>
1050 </PRE>
1051 </P>
1052 <P>
1053 PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
1054 the case when it is passed a junk pointer. This is the error it gives when the
1055 magic number isn't present.
1056 </P>
1057 <P>
1058 <pre>
1060 </PRE>
1061 </P>
1062 <P>
1063 While running the pattern match, an unknown item was encountered in the
1064 compiled pattern. This error could be caused by a bug in PCRE or by overwriting
1065 of the compiled pattern.
1066 </P>
1067 <P>
1068 <pre>
1070 </PRE>
1071 </P>
1072 <P>
1073 If a pattern contains back references, but the <i>ovector</i> that is passed to
1074 <b>pcre_exec()</b> is not big enough to remember the referenced substrings, PCRE
1075 gets a block of memory at the start of matching to use for this purpose. If the
1076 call via <b>pcre_malloc()</b> fails, this error is given. The memory is freed at
1077 the end of matching.
1078 </P>
1079 <P>
1080 <pre>
1082 </PRE>
1083 </P>
1084 <P>
1085 This error is used by the <b>pcre_copy_substring()</b>,
1086 <b>pcre_get_substring()</b>, and <b>pcre_get_substring_list()</b> functions (see
1087 below). It is never returned by <b>pcre_exec()</b>.
1088 </P>
1089 <P>
1090 <pre>
1092 </PRE>
1093 </P>
1094 <P>
1095 The recursion and backtracking limit, as specified by the <i>match_limit</i>
1096 field in a <b>pcre_extra</b> structure (or defaulted) was reached. See the
1097 description above.
1098 </P>
1099 <P>
1100 <pre>
1102 </PRE>
1103 </P>
1104 <P>
1105 This error is never generated by <b>pcre_exec()</b> itself. It is provided for
1106 use by callout functions that want to yield a distinctive error code. See the
1107 <b>pcrecallout</b> documentation for details.
1108 </P>
1109 <br><a name="SEC11" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a><br>
1110 <P>
1111 <b>int pcre_copy_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
1112 <b>int <i>stringcount</i>, int <i>stringnumber</i>, char *<i>buffer</i>,</b>
1113 <b>int <i>buffersize</i>);</b>
1114 </P>
1115 <P>
1116 <b>int pcre_get_substring(const char *<i>subject</i>, int *<i>ovector</i>,</b>
1117 <b>int <i>stringcount</i>, int <i>stringnumber</i>,</b>
1118 <b>const char **<i>stringptr</i>);</b>
1119 </P>
1120 <P>
1121 <b>int pcre_get_substring_list(const char *<i>subject</i>,</b>
1122 <b>int *<i>ovector</i>, int <i>stringcount</i>, const char ***<i>listptr</i>);</b>
1123 </P>
1124 <P>
1125 Captured substrings can be accessed directly by using the offsets returned by
1126 <b>pcre_exec()</b> in <i>ovector</i>. For convenience, the functions
1127 <b>pcre_copy_substring()</b>, <b>pcre_get_substring()</b>, and
1128 <b>pcre_get_substring_list()</b> are provided for extracting captured substrings
1129 as new, separate, zero-terminated strings. These functions identify substrings
1130 by number. The next section describes functions for extracting named
1131 substrings. A substring that contains a binary zero is correctly extracted and
1132 has a further zero added on the end, but the result is not, of course,
1133 a C string.
1134 </P>
1135 <P>
1136 The first three arguments are the same for all three of these functions:
1137 <i>subject</i> is the subject string which has just been successfully matched,
1138 <i>ovector</i> is a pointer to the vector of integer offsets that was passed to
1139 <b>pcre_exec()</b>, and <i>stringcount</i> is the number of substrings that were
1140 captured by the match, including the substring that matched the entire regular
1141 expression. This is the value returned by <b>pcre_exec</b> if it is greater than
1142 zero. If <b>pcre_exec()</b> returned zero, indicating that it ran out of space
1143 in <i>ovector</i>, the value passed as <i>stringcount</i> should be the size of
1144 the vector divided by three.
1145 </P>
1146 <P>
1147 The functions <b>pcre_copy_substring()</b> and <b>pcre_get_substring()</b>
1148 extract a single substring, whose number is given as <i>stringnumber</i>. A
1149 value of zero extracts the substring that matched the entire pattern, while
1150 higher values extract the captured substrings. For <b>pcre_copy_substring()</b>,
1151 the string is placed in <i>buffer</i>, whose length is given by
1152 <i>buffersize</i>, while for <b>pcre_get_substring()</b> a new block of memory is
1153 obtained via <b>pcre_malloc</b>, and its address is returned via
1154 <i>stringptr</i>. The yield of the function is the length of the string, not
1155 including the terminating zero, or one of
1156 </P>
1157 <P>
1158 <pre>
1160 </PRE>
1161 </P>
1162 <P>
1163 The buffer was too small for <b>pcre_copy_substring()</b>, or the attempt to get
1164 memory failed for <b>pcre_get_substring()</b>.
1165 </P>
1166 <P>
1167 <pre>
1169 </PRE>
1170 </P>
1171 <P>
1172 There is no substring whose number is <i>stringnumber</i>.
1173 </P>
1174 <P>
1175 The <b>pcre_get_substring_list()</b> function extracts all available substrings
1176 and builds a list of pointers to them. All this is done in a single block of
1177 memory which is obtained via <b>pcre_malloc</b>. The address of the memory block
1178 is returned via <i>listptr</i>, which is also the start of the list of string
1179 pointers. The end of the list is marked by a NULL pointer. The yield of the
1180 function is zero if all went well, or
1181 </P>
1182 <P>
1183 <pre>
1185 </PRE>
1186 </P>
1187 <P>
1188 if the attempt to get the memory block failed.
1189 </P>
1190 <P>
1191 When any of these functions encounter a substring that is unset, which can
1192 happen when capturing subpattern number <i>n+1</i> matches some part of the
1193 subject, but subpattern <i>n</i> has not been used at all, they return an empty
1194 string. This can be distinguished from a genuine zero-length substring by
1195 inspecting the appropriate offset in <i>ovector</i>, which is negative for unset
1196 substrings.
1197 </P>
1198 <P>
1199 The two convenience functions <b>pcre_free_substring()</b> and
1200 <b>pcre_free_substring_list()</b> can be used to free the memory returned by
1201 a previous call of <b>pcre_get_substring()</b> or
1202 <b>pcre_get_substring_list()</b>, respectively. They do nothing more than call
1203 the function pointed to by <b>pcre_free</b>, which of course could be called
1204 directly from a C program. However, PCRE is used in some situations where it is
1205 linked via a special interface to another programming language which cannot use
1206 <b>pcre_free</b> directly; it is for these cases that the functions are
1207 provided.
1208 </P>
1209 <br><a name="SEC12" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a><br>
1210 <P>
1211 <b>int pcre_copy_named_substring(const pcre *<i>code</i>,</b>
1212 <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
1213 <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
1214 <b>char *<i>buffer</i>, int <i>buffersize</i>);</b>
1215 </P>
1216 <P>
1217 <b>int pcre_get_stringnumber(const pcre *<i>code</i>,</b>
1218 <b>const char *<i>name</i>);</b>
1219 </P>
1220 <P>
1221 <b>int pcre_get_named_substring(const pcre *<i>code</i>,</b>
1222 <b>const char *<i>subject</i>, int *<i>ovector</i>,</b>
1223 <b>int <i>stringcount</i>, const char *<i>stringname</i>,</b>
1224 <b>const char **<i>stringptr</i>);</b>
1225 </P>
1226 <P>
1227 To extract a substring by name, you first have to find associated number. This
1228 can be done by calling <b>pcre_get_stringnumber()</b>. The first argument is the
1229 compiled pattern, and the second is the name. For example, for this pattern
1230 </P>
1231 <P>
1232 <pre>
1233 ab(?&#60;xxx&#62;\d+)...
1234 </PRE>
1235 </P>
1236 <P>
1237 the number of the subpattern called "xxx" is 1. Given the number, you can then
1238 extract the substring directly, or use one of the functions described in the
1239 previous section. For convenience, there are also two functions that do the
1240 whole job.
1241 </P>
1242 <P>
1243 Most of the arguments of <i>pcre_copy_named_substring()</i> and
1244 <i>pcre_get_named_substring()</i> are the same as those for the functions that
1245 extract by number, and so are not re-described here. There are just two
1246 differences.
1247 </P>
1248 <P>
1249 First, instead of a substring number, a substring name is given. Second, there
1250 is an extra argument, given at the start, which is a pointer to the compiled
1251 pattern. This is needed in order to gain access to the name-to-number
1252 translation table.
1253 </P>
1254 <P>
1255 These functions call <b>pcre_get_stringnumber()</b>, and if it succeeds, they
1256 then call <i>pcre_copy_substring()</i> or <i>pcre_get_substring()</i>, as
1257 appropriate.
1258 </P>
1259 <P>
1260 Last updated: 03 February 2003
1261 <br>
1262 Copyright &copy; 1997-2003 University of Cambridge.

  ViewVC Help
Powered by ViewVC 1.1.5