ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 75 - (hide annotations) (download)
Sat Feb 24 21:40:37 2007 UTC (8 years, 1 month ago) by nigel
File size: 54232 byte(s)
Load pcre-5.0 into code/trunk.

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

ViewVC Help
Powered by ViewVC 1.1.12