ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 91 - (hide annotations) (download)
Sat Feb 24 21:41:34 2007 UTC (8 years, 2 months ago) by nigel
File size: 75554 byte(s)
Load pcre-6.7 into code/trunk.

1 nigel 79 .TH PCREAPI 3
2 nigel 63 .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 77 .B pcre *pcre_compile2(const char *\fIpattern\fP, int \fIoptions\fP,
19     .ti +5n
20     .B int *\fIerrorcodeptr\fP,
21     .ti +5n
22     .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
23     .ti +5n
24     .B const unsigned char *\fItableptr\fP);
25     .PP
26     .br
27 nigel 75 .B pcre_extra *pcre_study(const pcre *\fIcode\fP, int \fIoptions\fP,
28 nigel 63 .ti +5n
29 nigel 75 .B const char **\fIerrptr\fP);
30 nigel 63 .PP
31     .br
32 nigel 75 .B int pcre_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
33 nigel 63 .ti +5n
34 nigel 75 .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
35 nigel 63 .ti +5n
36 nigel 75 .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP);
37 nigel 63 .PP
38     .br
39 nigel 77 .B int pcre_dfa_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
40     .ti +5n
41     .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
42     .ti +5n
43     .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP,
44     .ti +5n
45     .B int *\fIworkspace\fP, int \fIwscount\fP);
46     .PP
47     .br
48 nigel 75 .B int pcre_copy_named_substring(const pcre *\fIcode\fP,
49 nigel 63 .ti +5n
50 nigel 75 .B const char *\fIsubject\fP, int *\fIovector\fP,
51 nigel 63 .ti +5n
52 nigel 75 .B int \fIstringcount\fP, const char *\fIstringname\fP,
53 nigel 63 .ti +5n
54 nigel 75 .B char *\fIbuffer\fP, int \fIbuffersize\fP);
55 nigel 63 .PP
56     .br
57 nigel 75 .B int pcre_copy_substring(const char *\fIsubject\fP, int *\fIovector\fP,
58 nigel 63 .ti +5n
59 nigel 75 .B int \fIstringcount\fP, int \fIstringnumber\fP, char *\fIbuffer\fP,
60 nigel 63 .ti +5n
61 nigel 75 .B int \fIbuffersize\fP);
62 nigel 63 .PP
63     .br
64 nigel 75 .B int pcre_get_named_substring(const pcre *\fIcode\fP,
65 nigel 63 .ti +5n
66 nigel 75 .B const char *\fIsubject\fP, int *\fIovector\fP,
67 nigel 63 .ti +5n
68 nigel 75 .B int \fIstringcount\fP, const char *\fIstringname\fP,
69 nigel 63 .ti +5n
70 nigel 75 .B const char **\fIstringptr\fP);
71 nigel 63 .PP
72     .br
73 nigel 75 .B int pcre_get_stringnumber(const pcre *\fIcode\fP,
74 nigel 63 .ti +5n
75 nigel 75 .B const char *\fIname\fP);
76 nigel 63 .PP
77     .br
78 nigel 91 .B int pcre_get_stringtable_entries(const pcre *\fIcode\fP,
79     .ti +5n
80     .B const char *\fIname\fP, char **\fIfirst\fP, char **\fIlast\fP);
81     .PP
82     .br
83 nigel 75 .B int pcre_get_substring(const char *\fIsubject\fP, int *\fIovector\fP,
84 nigel 63 .ti +5n
85 nigel 75 .B int \fIstringcount\fP, int \fIstringnumber\fP,
86 nigel 63 .ti +5n
87 nigel 75 .B const char **\fIstringptr\fP);
88 nigel 63 .PP
89     .br
90 nigel 75 .B int pcre_get_substring_list(const char *\fIsubject\fP,
91 nigel 63 .ti +5n
92 nigel 75 .B int *\fIovector\fP, int \fIstringcount\fP, "const char ***\fIlistptr\fP);"
93 nigel 63 .PP
94     .br
95 nigel 75 .B void pcre_free_substring(const char *\fIstringptr\fP);
96 nigel 63 .PP
97     .br
98 nigel 75 .B void pcre_free_substring_list(const char **\fIstringptr\fP);
99 nigel 63 .PP
100     .br
101     .B const unsigned char *pcre_maketables(void);
102     .PP
103     .br
104 nigel 75 .B int pcre_fullinfo(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
105 nigel 63 .ti +5n
106 nigel 75 .B int \fIwhat\fP, void *\fIwhere\fP);
107 nigel 63 .PP
108     .br
109 nigel 75 .B int pcre_info(const pcre *\fIcode\fP, int *\fIoptptr\fP, int
110     .B *\fIfirstcharptr\fP);
111 nigel 63 .PP
112     .br
113 nigel 77 .B int pcre_refcount(pcre *\fIcode\fP, int \fIadjust\fP);
114     .PP
115     .br
116 nigel 75 .B int pcre_config(int \fIwhat\fP, void *\fIwhere\fP);
117 nigel 63 .PP
118     .br
119     .B char *pcre_version(void);
120     .PP
121     .br
122     .B void *(*pcre_malloc)(size_t);
123     .PP
124     .br
125     .B void (*pcre_free)(void *);
126     .PP
127     .br
128 nigel 73 .B void *(*pcre_stack_malloc)(size_t);
129     .PP
130     .br
131     .B void (*pcre_stack_free)(void *);
132     .PP
133     .br
134 nigel 63 .B int (*pcre_callout)(pcre_callout_block *);
135 nigel 75 .
136     .
138 nigel 63 .rs
139     .sp
140 nigel 77 PCRE has its own native API, which is described in this document. There is
141     also a set of wrapper functions that correspond to the POSIX regular expression
142     API. These are described in the
143 nigel 75 .\" HREF
144     \fBpcreposix\fP
145     .\"
146 nigel 77 documentation. Both of these APIs define a set of C function calls. A C++
147     wrapper is distributed with PCRE. It is documented in the
148     .\" HREF
149     \fBpcrecpp\fP
150     .\"
151     page.
152 nigel 75 .P
153 nigel 77 The native API C function prototypes are defined in the header file
154     \fBpcre.h\fP, and on Unix systems the library itself is called \fBlibpcre\fP.
155     It can normally be accessed by adding \fB-lpcre\fP to the command for linking
156     an application that uses PCRE. The header file defines the macros PCRE_MAJOR
157     and PCRE_MINOR to contain the major and minor release numbers for the library.
158 nigel 75 Applications can use these to include support for different releases of PCRE.
159     .P
160 nigel 77 The functions \fBpcre_compile()\fP, \fBpcre_compile2()\fP, \fBpcre_study()\fP,
161     and \fBpcre_exec()\fP are used for compiling and matching regular expressions
162     in a Perl-compatible manner. A sample program that demonstrates the simplest
163     way of using them is provided in the file called \fIpcredemo.c\fP in the source
164     distribution. The
165 nigel 75 .\" HREF
166     \fBpcresample\fP
167     .\"
168     documentation describes how to run it.
169     .P
170 nigel 77 A second matching function, \fBpcre_dfa_exec()\fP, which is not
171     Perl-compatible, is also provided. This uses a different algorithm for the
172 nigel 91 matching. The alternative algorithm finds all possible matches (at a given
173     point in the subject). However, this algorithm does not return captured
174 nigel 77 substrings. A description of the two matching algorithms and their advantages
175     and disadvantages is given in the
176     .\" HREF
177     \fBpcrematching\fP
178     .\"
179     documentation.
180     .P
181 nigel 75 In addition to the main compiling and matching functions, there are convenience
182 nigel 77 functions for extracting captured substrings from a subject string that is
183     matched by \fBpcre_exec()\fP. They are:
184 nigel 75 .sp
185     \fBpcre_copy_substring()\fP
186     \fBpcre_copy_named_substring()\fP
187     \fBpcre_get_substring()\fP
188     \fBpcre_get_named_substring()\fP
189     \fBpcre_get_substring_list()\fP
190     \fBpcre_get_stringnumber()\fP
191 nigel 91 \fBpcre_get_stringtable_entries()\fP
192 nigel 75 .sp
193     \fBpcre_free_substring()\fP and \fBpcre_free_substring_list()\fP are also
194 nigel 63 provided, to free the memory used for extracted strings.
195 nigel 75 .P
196     The function \fBpcre_maketables()\fP is used to build a set of character tables
197 nigel 77 in the current locale for passing to \fBpcre_compile()\fP, \fBpcre_exec()\fP,
198     or \fBpcre_dfa_exec()\fP. This is an optional facility that is provided for
199     specialist use. Most commonly, no special tables are passed, in which case
200     internal tables that are generated when PCRE is built are used.
201 nigel 75 .P
202     The function \fBpcre_fullinfo()\fP is used to find out information about a
203     compiled pattern; \fBpcre_info()\fP is an obsolete version that returns only
204 nigel 63 some of the available information, but is retained for backwards compatibility.
205 nigel 75 The function \fBpcre_version()\fP returns a pointer to a string containing the
206 nigel 63 version of PCRE and its date of release.
207 nigel 75 .P
208 nigel 77 The function \fBpcre_refcount()\fP maintains a reference count in a data block
209     containing a compiled pattern. This is provided for the benefit of
210     object-oriented applications.
211     .P
212 nigel 75 The global variables \fBpcre_malloc\fP and \fBpcre_free\fP initially contain
213     the entry points of the standard \fBmalloc()\fP and \fBfree()\fP functions,
214 nigel 63 respectively. PCRE calls the memory management functions via these variables,
215     so a calling program can replace them if it wishes to intercept the calls. This
216     should be done before calling any PCRE functions.
217 nigel 75 .P
218     The global variables \fBpcre_stack_malloc\fP and \fBpcre_stack_free\fP are also
219 nigel 73 indirections to memory management functions. These special functions are used
220     only when PCRE is compiled to use the heap for remembering data, instead of
221 nigel 91 recursive function calls, when running the \fBpcre_exec()\fP function. See the
222     .\" HREF
223     \fBpcrebuild\fP
224     .\"
225     documentation for details of how to do this. It is a non-standard way of
226     building PCRE, for use in environments that have limited stacks. Because of the
227     greater use of memory management, it runs more slowly. Separate functions are
228     provided so that special-purpose external code can be used for this case. When
229     used, these functions are always called in a stack-like manner (last obtained,
230     first freed), and always for memory blocks of the same size. There is a
231     discussion about PCRE's stack usage in the
232     .\" HREF
233     \fBpcrestack\fP
234     .\"
235     documentation.
236 nigel 75 .P
237     The global variable \fBpcre_callout\fP initially contains NULL. It can be set
238 nigel 63 by the caller to a "callout" function, which PCRE will then call at specified
239 nigel 75 points during a matching operation. Details are given in the
240     .\" HREF
241     \fBpcrecallout\fP
242     .\"
243 nigel 63 documentation.
244 nigel 75 .
245     .
246 nigel 91 .SH NEWLINES
247     PCRE supports three different conventions for indicating line breaks in
248     strings: a single CR character, a single LF character, or the two-character
249     sequence CRLF. All three are used as "standard" by different operating systems.
250     When PCRE is built, a default can be specified. The default default is LF,
251     which is the Unix standard. When PCRE is run, the default can be overridden,
252     either when a pattern is compiled, or when it is matched.
253     .sp
254     In the PCRE documentation the word "newline" is used to mean "the character or
255     pair of characters that indicate a line break".
256     .
257     .
259     .rs
260     .sp
261     The PCRE functions can be used in multi-threading applications, with the
262 nigel 75 proviso that the memory management functions pointed to by \fBpcre_malloc\fP,
263     \fBpcre_free\fP, \fBpcre_stack_malloc\fP, and \fBpcre_stack_free\fP, and the
264     callout function pointed to by \fBpcre_callout\fP, are shared by all threads.
265     .P
266 nigel 63 The compiled form of a regular expression is not altered during matching, so
267     the same compiled pattern can safely be used by several threads at once.
268 nigel 75 .
269     .
271 nigel 63 .rs
272     .sp
273 nigel 75 The compiled form of a regular expression can be saved and re-used at a later
274     time, possibly by a different program, and even on a host other than the one on
275     which it was compiled. Details are given in the
276     .\" HREF
277     \fBpcreprecompile\fP
278     .\"
279     documentation.
280     .
281     .
283     .rs
284     .sp
285     .B int pcre_config(int \fIwhat\fP, void *\fIwhere\fP);
286 nigel 63 .PP
287 nigel 75 The function \fBpcre_config()\fP makes it possible for a PCRE client to
288 nigel 63 discover which optional features have been compiled into the PCRE library. The
289     .\" HREF
290 nigel 75 \fBpcrebuild\fP
291 nigel 63 .\"
292     documentation has more details about these optional features.
293 nigel 75 .P
294     The first argument for \fBpcre_config()\fP is an integer, specifying which
295 nigel 63 information is required; the second argument is a pointer to a variable into
296     which the information is placed. The following information is available:
297 nigel 75 .sp
298 nigel 63 PCRE_CONFIG_UTF8
299 nigel 75 .sp
300 nigel 63 The output is an integer that is set to one if UTF-8 support is available;
301     otherwise it is set to zero.
302 nigel 75 .sp
304     .sp
305     The output is an integer that is set to one if support for Unicode character
306     properties is available; otherwise it is set to zero.
307     .sp
309 nigel 75 .sp
310 nigel 91 The output is an integer whose value specifies the default character sequence
311     that is recognized as meaning "newline". The three values that are supported
312     are: 10 for LF, 13 for CR, and 3338 for CRLF. The default should normally be
313     the standard sequence for your operating system.
314 nigel 75 .sp
316 nigel 75 .sp
317 nigel 63 The output is an integer that contains the number of bytes used for internal
318     linkage in compiled regular expressions. The value is 2, 3, or 4. Larger values
319     allow larger regular expressions to be compiled, at the expense of slower
320     matching. The default value of 2 is sufficient for all but the most massive
321     patterns, since it allows the compiled pattern to be up to 64K in size.
322 nigel 75 .sp
324 nigel 75 .sp
325 nigel 63 The output is an integer that contains the threshold above which the POSIX
326 nigel 75 interface uses \fBmalloc()\fP for output vectors. Further details are given in
327     the
328     .\" HREF
329     \fBpcreposix\fP
330     .\"
331     documentation.
332     .sp
334 nigel 75 .sp
335 nigel 63 The output is an integer that gives the default limit for the number of
336 nigel 75 internal matching function calls in a \fBpcre_exec()\fP execution. Further
337     details are given with \fBpcre_exec()\fP below.
338     .sp
340     .sp
341     The output is an integer that gives the default limit for the depth of
342     recursion when calling the internal matching function in a \fBpcre_exec()\fP
343     execution. Further details are given with \fBpcre_exec()\fP below.
344     .sp
346 nigel 75 .sp
347 nigel 77 The output is an integer that is set to one if internal recursion when running
348     \fBpcre_exec()\fP is implemented by recursive function calls that use the stack
349     to remember their state. This is the usual way that PCRE is compiled. The
350     output is zero if PCRE was compiled to use blocks of data on the heap instead
351     of recursive function calls. In this case, \fBpcre_stack_malloc\fP and
352     \fBpcre_stack_free\fP are called to manage memory blocks on the heap, thus
353     avoiding the use of the stack.
354 nigel 75 .
355     .
357 nigel 63 .rs
358     .sp
359 nigel 75 .B pcre *pcre_compile(const char *\fIpattern\fP, int \fIoptions\fP,
360 nigel 63 .ti +5n
361 nigel 75 .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
362 nigel 63 .ti +5n
363 nigel 75 .B const unsigned char *\fItableptr\fP);
364 nigel 77 .sp
365     .B pcre *pcre_compile2(const char *\fIpattern\fP, int \fIoptions\fP,
366     .ti +5n
367     .B int *\fIerrorcodeptr\fP,
368     .ti +5n
369     .B const char **\fIerrptr\fP, int *\fIerroffset\fP,
370     .ti +5n
371     .B const unsigned char *\fItableptr\fP);
372 nigel 75 .P
373 nigel 77 Either of the functions \fBpcre_compile()\fP or \fBpcre_compile2()\fP can be
374     called to compile a pattern into an internal form. The only difference between
375     the two interfaces is that \fBpcre_compile2()\fP has an additional argument,
376     \fIerrorcodeptr\fP, via which a numerical error code can be returned.
377 nigel 75 .P
378 nigel 77 The pattern is a C string terminated by a binary zero, and is passed in the
379     \fIpattern\fP argument. A pointer to a single block of memory that is obtained
380     via \fBpcre_malloc\fP is returned. This contains the compiled code and related
381     data. The \fBpcre\fP type is defined for the returned block; this is a typedef
382     for a structure whose contents are not externally defined. It is up to the
383 nigel 91 caller to free the memory (via \fBpcre_free\fP) when it is no longer required.
384 nigel 77 .P
385 nigel 63 Although the compiled code of a PCRE regex is relocatable, that is, it does not
386 nigel 75 depend on memory location, the complete \fBpcre\fP data block is not
387     fully relocatable, because it may contain a copy of the \fItableptr\fP
388     argument, which is an address (see below).
389     .P
390     The \fIoptions\fP argument contains independent bits that affect the
391     compilation. It should be zero if no options are required. The available
392     options are described below. Some of them, in particular, those that are
393     compatible with Perl, can also be set and unset from within the pattern (see
394     the detailed description in the
395     .\" HREF
396     \fBpcrepattern\fP
397     .\"
398     documentation). For these options, the contents of the \fIoptions\fP argument
399     specifies their initial settings at the start of compilation and execution. The
400 nigel 91 PCRE_ANCHORED and PCRE_NEWLINE_\fIxxx\fP options can be set at the time of
401     matching as well as at compile time.
402 nigel 75 .P
403     If \fIerrptr\fP is NULL, \fBpcre_compile()\fP returns NULL immediately.
404     Otherwise, if compilation of a pattern fails, \fBpcre_compile()\fP returns
405     NULL, and sets the variable pointed to by \fIerrptr\fP to point to a textual
406 nigel 87 error message. This is a static string that is part of the library. You must
407     not try to free it. The offset from the start of the pattern to the character
408     where the error was discovered is placed in the variable pointed to by
409 nigel 75 \fIerroffset\fP, which must not be NULL. If it is, an immediate error is given.
410     .P
411 nigel 77 If \fBpcre_compile2()\fP is used instead of \fBpcre_compile()\fP, and the
412     \fIerrorcodeptr\fP argument is not NULL, a non-zero error code number is
413     returned via this argument in the event of an error. This is in addition to the
414     textual error message. Error codes and messages are listed below.
415     .P
416 nigel 75 If the final argument, \fItableptr\fP, is NULL, PCRE uses a default set of
417     character tables that are built when PCRE is compiled, using the default C
418     locale. Otherwise, \fItableptr\fP must be an address that is the result of a
419     call to \fBpcre_maketables()\fP. This value is stored with the compiled
420     pattern, and used again by \fBpcre_exec()\fP, unless another table pointer is
421     passed to it. For more discussion, see the section on locale support below.
422     .P
423     This code fragment shows a typical straightforward call to \fBpcre_compile()\fP:
424     .sp
425 nigel 63 pcre *re;
426     const char *error;
427     int erroffset;
428     re = pcre_compile(
429     "^A.*Z", /* the pattern */
430     0, /* default options */
431     &error, /* for error message */
432     &erroffset, /* for error offset */
433     NULL); /* use default character tables */
434 nigel 75 .sp
435     The following names for option bits are defined in the \fBpcre.h\fP header
436     file:
437     .sp
438 nigel 63 PCRE_ANCHORED
439 nigel 75 .sp
440 nigel 63 If this bit is set, the pattern is forced to be "anchored", that is, it is
441 nigel 75 constrained to match only at the first matching point in the string that is
442 nigel 63 being searched (the "subject string"). This effect can also be achieved by
443     appropriate constructs in the pattern itself, which is the only way to do it in
444     Perl.
445 nigel 75 .sp
447     .sp
448     If this bit is set, \fBpcre_compile()\fP automatically inserts callout items,
449     all with number 255, before each pattern item. For discussion of the callout
450     facility, see the
451     .\" HREF
452     \fBpcrecallout\fP
453     .\"
454     documentation.
455     .sp
456 nigel 63 PCRE_CASELESS
457 nigel 75 .sp
458 nigel 63 If this bit is set, letters in the pattern match both upper and lower case
459     letters. It is equivalent to Perl's /i option, and it can be changed within a
460 nigel 77 pattern by a (?i) option setting. In UTF-8 mode, PCRE always understands the
461     concept of case for characters whose values are less than 128, so caseless
462     matching is always possible. For characters with higher values, the concept of
463     case is supported if PCRE is compiled with Unicode property support, but not
464     otherwise. If you want to use caseless matching for characters 128 and above,
465     you must ensure that PCRE is compiled with Unicode property support as well as
466     with UTF-8 support.
467 nigel 75 .sp
469 nigel 75 .sp
470 nigel 63 If this bit is set, a dollar metacharacter in the pattern matches only at the
471     end of the subject string. Without this option, a dollar also matches
472 nigel 91 immediately before a newline at the end of the string (but not before any other
473     newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
474     There is no equivalent to this option in Perl, and no way to set it within a
475     pattern.
476 nigel 75 .sp
477 nigel 63 PCRE_DOTALL
478 nigel 75 .sp
479 nigel 63 If this bit is set, a dot metacharater in the pattern matches all characters,
480 nigel 91 including those that indicate newline. Without it, a dot does not match when
481     the current position is at a newline. This option is equivalent to Perl's /s
482     option, and it can be changed within a pattern by a (?s) option setting. A
483     negative class such as [^a] always matches newlines, independent of the setting
484     of this option.
485 nigel 75 .sp
486 nigel 91 PCRE_DUPNAMES
487     .sp
488     If this bit is set, names used to identify capturing subpatterns need not be
489     unique. This can be helpful for certain types of pattern when it is known that
490     only one instance of the named subpattern can ever be matched. There are more
491     details of named subpatterns below; see also the
492     .\" HREF
493     \fBpcrepattern\fP
494     .\"
495     documentation.
496     .sp
497 nigel 63 PCRE_EXTENDED
498 nigel 75 .sp
499 nigel 63 If this bit is set, whitespace data characters in the pattern are totally
500     ignored except when escaped or inside a character class. Whitespace does not
501     include the VT character (code 11). In addition, characters between an
502 nigel 91 unescaped # outside a character class and the next newline, inclusive, are also
503     ignored. This is equivalent to Perl's /x option, and it can be changed within a
504     pattern by a (?x) option setting.
505 nigel 75 .P
506 nigel 63 This option makes it possible to include comments inside complicated patterns.
507     Note, however, that this applies only to data characters. Whitespace characters
508     may never appear within special character sequences in a pattern, for example
509     within the sequence (?( which introduces a conditional subpattern.
510 nigel 75 .sp
511 nigel 63 PCRE_EXTRA
512 nigel 75 .sp
513 nigel 63 This option was invented in order to turn on additional functionality of PCRE
514     that is incompatible with Perl, but it is currently of very little use. When
515     set, any backslash in a pattern that is followed by a letter that has no
516     special meaning causes an error, thus reserving these combinations for future
517     expansion. By default, as in Perl, a backslash followed by a letter with no
518 nigel 91 special meaning is treated as a literal. (Perl can, however, be persuaded to
519     give a warning for this.) There are at present no other features controlled by
520     this option. It can also be set by a (?X) option setting within a pattern.
521 nigel 75 .sp
522 nigel 77 PCRE_FIRSTLINE
523     .sp
524     If this option is set, an unanchored pattern is required to match before or at
525 nigel 91 the first newline in the subject string, though the matched text may continue
526     over the newline.
527 nigel 77 .sp
528 nigel 63 PCRE_MULTILINE
529 nigel 75 .sp
530     By default, PCRE treats the subject string as consisting of a single line of
531     characters (even if it actually contains newlines). The "start of line"
532 nigel 63 metacharacter (^) matches only at the start of the string, while the "end of
533     line" metacharacter ($) matches only at the end of the string, or before a
534     terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
535     Perl.
536 nigel 75 .P
537 nigel 63 When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
538 nigel 91 match immediately following or immediately before internal newlines in the
539     subject string, respectively, as well as at the very start and end. This is
540     equivalent to Perl's /m option, and it can be changed within a pattern by a
541     (?m) option setting. If there are no newlines in a subject string, or no
542 nigel 63 occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no effect.
543 nigel 75 .sp
544 nigel 91 PCRE_NEWLINE_CR
547     .sp
548     These options override the default newline definition that was chosen when PCRE
549     was built. Setting the first or the second specifies that a newline is
550     indicated by a single character (CR or LF, respectively). Setting both of them
551     specifies that a newline is indicated by the two-character CRLF sequence. For
552     convenience, PCRE_NEWLINE_CRLF is defined to contain both bits. The only time
553     that a line break is relevant when compiling a pattern is if PCRE_EXTENDED is
554     set, and an unescaped # outside a character class is encountered. This
555     indicates a comment that lasts until after the next newline.
556     .P
557     The newline option set at compile time becomes the default that is used for
558     \fBpcre_exec()\fP and \fBpcre_dfa_exec()\fP, but it can be overridden.
559     .sp
561 nigel 75 .sp
562 nigel 63 If this option is set, it disables the use of numbered capturing parentheses in
563     the pattern. Any opening parenthesis that is not followed by ? behaves as if it
564     were followed by ?: but named parentheses can still be used for capturing (and
565     they acquire numbers in the usual way). There is no equivalent of this option
566     in Perl.
567 nigel 75 .sp
568 nigel 63 PCRE_UNGREEDY
569 nigel 75 .sp
570 nigel 63 This option inverts the "greediness" of the quantifiers so that they are not
571     greedy by default, but become greedy if followed by "?". It is not compatible
572     with Perl. It can also be set by a (?U) option setting within the pattern.
573 nigel 75 .sp
574 nigel 63 PCRE_UTF8
575 nigel 75 .sp
576 nigel 63 This option causes PCRE to regard both the pattern and the subject as strings
577     of UTF-8 characters instead of single-byte character strings. However, it is
578 nigel 75 available only when PCRE is built to include UTF-8 support. If not, the use
579 nigel 63 of this option provokes an error. Details of how this option changes the
580     behaviour of PCRE are given in the
581     .\" HTML <a href="pcre.html#utf8support">
582     .\" </a>
583     section on UTF-8 support
584     .\"
585     in the main
586     .\" HREF
587 nigel 75 \fBpcre\fP
588 nigel 63 .\"
589     page.
590 nigel 75 .sp
591 nigel 71 PCRE_NO_UTF8_CHECK
592 nigel 75 .sp
593 nigel 71 When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
594     automatically checked. If an invalid UTF-8 sequence of bytes is found,
595 nigel 75 \fBpcre_compile()\fP returns an error. If you already know that your pattern is
596 nigel 71 valid, and you want to skip this check for performance reasons, you can set the
597     PCRE_NO_UTF8_CHECK option. When it is set, the effect of passing an invalid
598     UTF-8 string as a pattern is undefined. It may cause your program to crash.
599 nigel 77 Note that this option can also be passed to \fBpcre_exec()\fP and
600     \fBpcre_dfa_exec()\fP, to suppress the UTF-8 validity checking of subject
601     strings.
602 nigel 75 .
603     .
605     .rs
606     .sp
607     The following table lists the error codes than may be returned by
608     \fBpcre_compile2()\fP, along with the error messages that may be returned by
609     both compiling functions.
610     .sp
611     0 no error
612     1 \e at end of pattern
613     2 \ec at end of pattern
614     3 unrecognized character follows \e
615     4 numbers out of order in {} quantifier
616     5 number too big in {} quantifier
617     6 missing terminating ] for character class
618     7 invalid escape sequence in character class
619     8 range out of order in character class
620     9 nothing to repeat
621     10 operand of unlimited repeat could match the empty string
622     11 internal error: unexpected repeat
623     12 unrecognized character after (?
624     13 POSIX named classes are supported only within a class
625     14 missing )
626     15 reference to non-existent subpattern
627     16 erroffset passed as NULL
628     17 unknown option bit(s) set
629     18 missing ) after comment
630     19 parentheses nested too deeply
631     20 regular expression too large
632     21 failed to get memory
633     22 unmatched parentheses
634     23 internal error: code overflow
635     24 unrecognized character after (?<
636     25 lookbehind assertion is not fixed length
637 nigel 91 26 malformed number or name after (?(
638 nigel 77 27 conditional group contains more than two branches
639     28 assertion expected after (?(
640     29 (?R or (?digits must be followed by )
641     30 unknown POSIX class name
642     31 POSIX collating elements are not supported
643     32 this version of PCRE is not compiled with PCRE_UTF8 support
644     33 spare error
645     34 character value in \ex{...} sequence is too large
646     35 invalid condition (?(0)
647     36 \eC not allowed in lookbehind assertion
648     37 PCRE does not support \eL, \el, \eN, \eU, or \eu
649     38 number after (?C is > 255
650     39 closing ) for (?C expected
651     40 recursive call could loop indefinitely
652     41 unrecognized character after (?P
653     42 syntax error after (?P
654 nigel 91 43 two named subpatterns have the same name
655 nigel 77 44 invalid UTF-8 string
656     45 support for \eP, \ep, and \eX has not been compiled
657     46 malformed \eP or \ep sequence
658     47 unknown property name after \eP or \ep
659 nigel 91 48 subpattern name is too long (maximum 32 characters)
660     49 too many named subpatterns (maximum 10,000)
661     50 repeated subpattern is too long
662     51 octal value is greater than \e377 (not in UTF-8 mode)
663 nigel 77 .
664     .
665 nigel 75 .SH "STUDYING A PATTERN"
666 nigel 63 .rs
667     .sp
668 nigel 77 .B pcre_extra *pcre_study(const pcre *\fIcode\fP, int \fIoptions\fP
669 nigel 63 .ti +5n
670 nigel 75 .B const char **\fIerrptr\fP);
671 nigel 63 .PP
672 nigel 75 If a compiled pattern is going to be used several times, it is worth spending
673     more time analyzing it in order to speed up the time taken for matching. The
674     function \fBpcre_study()\fP takes a pointer to a compiled pattern as its first
675     argument. If studying the pattern produces additional information that will
676     help speed up matching, \fBpcre_study()\fP returns a pointer to a
677     \fBpcre_extra\fP block, in which the \fIstudy_data\fP field points to the
678     results of the study.
679     .P
680     The returned value from \fBpcre_study()\fP can be passed directly to
681     \fBpcre_exec()\fP. However, a \fBpcre_extra\fP block also contains other
682 nigel 63 fields that can be set by the caller before the block is passed; these are
683 nigel 75 described
684     .\" HTML <a href="#extradata">
685     .\" </a>
686     below
687     .\"
688     in the section on matching a pattern.
689     .P
690 nigel 77 If studying the pattern does not produce any additional information
691 nigel 75 \fBpcre_study()\fP returns NULL. In that circumstance, if the calling program
692     wants to pass any of the other fields to \fBpcre_exec()\fP, it must set up its
693     own \fBpcre_extra\fP block.
694     .P
695     The second argument of \fBpcre_study()\fP contains option bits. At present, no
696     options are defined, and this argument should always be zero.
697     .P
698     The third argument for \fBpcre_study()\fP is a pointer for an error message. If
699 nigel 63 studying succeeds (even if no data is returned), the variable it points to is
700 nigel 87 set to NULL. Otherwise it is set to point to a textual error message. This is a
701     static string that is part of the library. You must not try to free it. You
702     should test the error pointer for NULL after calling \fBpcre_study()\fP, to be
703     sure that it has run successfully.
704 nigel 75 .P
705     This is a typical call to \fBpcre_study\fP():
706     .sp
707 nigel 63 pcre_extra *pe;
708     pe = pcre_study(
709     re, /* result of pcre_compile() */
710     0, /* no options exist */
711     &error); /* set to NULL or points to a message */
712 nigel 75 .sp
713 nigel 63 At present, studying a pattern is useful only for non-anchored patterns that do
714     not have a single fixed starting character. A bitmap of possible starting
715 nigel 75 bytes is created.
716     .
717     .
718 nigel 63 .\" HTML <a name="localesupport"></a>
719 nigel 75 .SH "LOCALE SUPPORT"
720 nigel 63 .rs
721     .sp
722 nigel 77 PCRE handles caseless matching, and determines whether characters are letters
723 nigel 75 digits, or whatever, by reference to a set of tables, indexed by character
724 nigel 77 value. When running in UTF-8 mode, this applies only to characters with codes
725 nigel 75 less than 128. Higher-valued codes never match escapes such as \ew or \ed, but
726     can be tested with \ep if PCRE is built with Unicode character property
727 nigel 87 support. The use of locales with Unicode is discouraged.
728 nigel 75 .P
729     An internal set of tables is created in the default C locale when PCRE is
730     built. This is used when the final argument of \fBpcre_compile()\fP is NULL,
731     and is sufficient for many applications. An alternative set of tables can,
732     however, be supplied. These may be created in a different locale from the
733     default. As more and more applications change to using Unicode, the need for
734     this locale support is expected to die away.
735     .P
736     External tables are built by calling the \fBpcre_maketables()\fP function,
737     which has no arguments, in the relevant locale. The result can then be passed
738     to \fBpcre_compile()\fP or \fBpcre_exec()\fP as often as necessary. For
739     example, to build and use tables that are appropriate for the French locale
740     (where accented characters with values greater than 128 are treated as letters),
741     the following code could be used:
742     .sp
743     setlocale(LC_CTYPE, "fr_FR");
744 nigel 63 tables = pcre_maketables();
745     re = pcre_compile(..., tables);
746 nigel 75 .sp
747     When \fBpcre_maketables()\fP runs, the tables are built in memory that is
748     obtained via \fBpcre_malloc\fP. It is the caller's responsibility to ensure
749     that the memory containing the tables remains available for as long as it is
750     needed.
751     .P
752     The pointer that is passed to \fBpcre_compile()\fP is saved with the compiled
753     pattern, and the same tables are used via this pointer by \fBpcre_study()\fP
754     and normally also by \fBpcre_exec()\fP. Thus, by default, for any single
755     pattern, compilation, studying and matching all happen in the same locale, but
756     different patterns can be compiled in different locales.
757     .P
758     It is possible to pass a table pointer or NULL (indicating the use of the
759     internal tables) to \fBpcre_exec()\fP. Although not intended for this purpose,
760     this facility could be used to match a pattern in a different locale from the
761     one in which it was compiled. Passing table pointers at run time is discussed
762     below in the section on matching a pattern.
763     .
764     .
766 nigel 63 .rs
767     .sp
768 nigel 75 .B int pcre_fullinfo(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
769 nigel 63 .ti +5n
770 nigel 75 .B int \fIwhat\fP, void *\fIwhere\fP);
771 nigel 63 .PP
772 nigel 75 The \fBpcre_fullinfo()\fP function returns information about a compiled
773     pattern. It replaces the obsolete \fBpcre_info()\fP function, which is
774 nigel 63 nevertheless retained for backwards compability (and is documented below).
775 nigel 75 .P
776     The first argument for \fBpcre_fullinfo()\fP is a pointer to the compiled
777     pattern. The second argument is the result of \fBpcre_study()\fP, or NULL if
778 nigel 63 the pattern was not studied. The third argument specifies which piece of
779     information is required, and the fourth argument is a pointer to a variable
780     to receive the data. The yield of the function is zero for success, or one of
781     the following negative numbers:
782 nigel 75 .sp
783     PCRE_ERROR_NULL the argument \fIcode\fP was NULL
784     the argument \fIwhere\fP was NULL
785 nigel 63 PCRE_ERROR_BADMAGIC the "magic number" was not found
786 nigel 75 PCRE_ERROR_BADOPTION the value of \fIwhat\fP was invalid
787     .sp
788     The "magic number" is placed at the start of each compiled pattern as an simple
789     check against passing an arbitrary memory pointer. Here is a typical call of
790     \fBpcre_fullinfo()\fP, to obtain the length of the compiled pattern:
791     .sp
792 nigel 63 int rc;
793 nigel 91 size_t length;
794 nigel 63 rc = pcre_fullinfo(
795     re, /* result of pcre_compile() */
796     pe, /* result of pcre_study(), or NULL */
797     PCRE_INFO_SIZE, /* what is required */
798     &length); /* where to put the data */
799 nigel 75 .sp
800     The possible values for the third argument are defined in \fBpcre.h\fP, and are
801 nigel 63 as follows:
802 nigel 75 .sp
804 nigel 75 .sp
805 nigel 63 Return the number of the highest back reference in the pattern. The fourth
806 nigel 75 argument should point to an \fBint\fP variable. Zero is returned if there are
807 nigel 63 no back references.
808 nigel 75 .sp
810 nigel 75 .sp
811 nigel 63 Return the number of capturing subpatterns in the pattern. The fourth argument
812 nigel 75 should point to an \fBint\fP variable.
813     .sp
815 nigel 75 .sp
816     Return a pointer to the internal default character tables within PCRE. The
817     fourth argument should point to an \fBunsigned char *\fP variable. This
818     information call is provided for internal use by the \fBpcre_study()\fP
819     function. External callers can cause PCRE to use its internal tables by passing
820     a NULL table pointer.
821     .sp
823 nigel 75 .sp
824 nigel 63 Return information about the first byte of any matched string, for a
825 nigel 91 non-anchored pattern. The fourth argument should point to an \fBint\fP
826     variable. (This option used to be called PCRE_INFO_FIRSTCHAR; the old name is
827     still recognized for backwards compatibility.)
828 nigel 75 .P
829     If there is a fixed first byte, for example, from a pattern such as
830 nigel 91 (cat|cow|coyote). Otherwise, if either
831 nigel 75 .sp
832 nigel 63 (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
833     starts with "^", or
834 nigel 75 .sp
835 nigel 63 (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
836     (if it were set, the pattern would be anchored),
837 nigel 75 .sp
838 nigel 63 -1 is returned, indicating that the pattern matches only at the start of a
839     subject string or after any newline within the string. Otherwise -2 is
840     returned. For anchored patterns, -2 is returned.
841 nigel 75 .sp
843 nigel 75 .sp
844 nigel 63 If the pattern was studied, and this resulted in the construction of a 256-bit
845     table indicating a fixed set of bytes for the first byte in any matching
846     string, a pointer to the table is returned. Otherwise NULL is returned. The
847 nigel 75 fourth argument should point to an \fBunsigned char *\fP variable.
848     .sp
850 nigel 75 .sp
851 nigel 65 Return the value of the rightmost literal byte that must exist in any matched
852     string, other than at its start, if such a byte has been recorded. The fourth
853 nigel 75 argument should point to an \fBint\fP variable. If there is no such byte, -1 is
854 nigel 65 returned. For anchored patterns, a last literal byte is recorded only if it
855     follows something of variable length. For example, for the pattern
856 nigel 75 /^a\ed+z\ed+/ the returned value is "z", but for /^a\edz\ed/ the returned value
857 nigel 65 is -1.
858 nigel 75 .sp
862 nigel 75 .sp
863 nigel 63 PCRE supports the use of named as well as numbered capturing parentheses. The
864     names are just an additional way of identifying the parentheses, which still
865 nigel 91 acquire numbers. Several convenience functions such as
866     \fBpcre_get_named_substring()\fP are provided for extracting captured
867     substrings by name. It is also possible to extract the data directly, by first
868     converting the name to a number in order to access the correct pointers in the
869     output vector (described with \fBpcre_exec()\fP below). To do the conversion,
870     you need to use the name-to-number map, which is described by these three
871     values.
872 nigel 75 .P
873 nigel 63 The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT gives
874     the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size of each
875 nigel 75 entry; both of these return an \fBint\fP value. The entry size depends on the
876 nigel 63 length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
877 nigel 75 entry of the table (a pointer to \fBchar\fP). The first two bytes of each entry
878 nigel 63 are the number of the capturing parenthesis, most significant byte first. The
879     rest of the entry is the corresponding name, zero terminated. The names are in
880 nigel 91 alphabetical order. When PCRE_DUPNAMES is set, duplicate names are in order of
881     their parentheses numbers. For example, consider the following pattern (assume
882 nigel 63 PCRE_EXTENDED is set, so white space - including newlines - is ignored):
883 nigel 75 .sp
884     .\" JOIN
885     (?P<date> (?P<year>(\ed\ed)?\ed\ed) -
886     (?P<month>\ed\ed) - (?P<day>\ed\ed) )
887     .sp
888 nigel 63 There are four named subpatterns, so the table has four entries, and each entry
889     in the table is eight bytes long. The table is as follows, with non-printing
890 nigel 75 bytes shows in hexadecimal, and undefined bytes shown as ??:
891     .sp
892 nigel 63 00 01 d a t e 00 ??
893     00 05 d a y 00 ?? ??
894     00 04 m o n t h 00
895     00 02 y e a r 00 ??
896 nigel 75 .sp
897     When writing code to extract data from named subpatterns using the
898 nigel 91 name-to-number map, remember that the length of the entries is likely to be
899 nigel 75 different for each compiled pattern.
900     .sp
901 nigel 63 PCRE_INFO_OPTIONS
902 nigel 75 .sp
903 nigel 63 Return a copy of the options with which the pattern was compiled. The fourth
904 nigel 75 argument should point to an \fBunsigned long int\fP variable. These option bits
905     are those specified in the call to \fBpcre_compile()\fP, modified by any
906 nigel 63 top-level option settings within the pattern itself.
907 nigel 75 .P
908 nigel 63 A pattern is automatically anchored by PCRE if all of its top-level
909     alternatives begin with one of the following:
910 nigel 75 .sp
911 nigel 63 ^ unless PCRE_MULTILINE is set
912 nigel 75 \eA always
913     \eG always
914     .\" JOIN
915 nigel 63 .* if PCRE_DOTALL is set and there are no back
916     references to the subpattern in which .* appears
917 nigel 75 .sp
918 nigel 63 For such patterns, the PCRE_ANCHORED bit is set in the options returned by
919 nigel 75 \fBpcre_fullinfo()\fP.
920     .sp
921 nigel 63 PCRE_INFO_SIZE
922 nigel 75 .sp
923 nigel 63 Return the size of the compiled pattern, that is, the value that was passed as
924 nigel 75 the argument to \fBpcre_malloc()\fP when PCRE was getting memory in which to
925     place the compiled data. The fourth argument should point to a \fBsize_t\fP
926 nigel 63 variable.
927 nigel 75 .sp
929 nigel 75 .sp
930     Return the size of the data block pointed to by the \fIstudy_data\fP field in
931     a \fBpcre_extra\fP block. That is, it is the value that was passed to
932     \fBpcre_malloc()\fP when PCRE was getting memory into which to place the data
933     created by \fBpcre_study()\fP. The fourth argument should point to a
934     \fBsize_t\fP variable.
935     .
936     .
938 nigel 63 .rs
939     .sp
940 nigel 75 .B int pcre_info(const pcre *\fIcode\fP, int *\fIoptptr\fP, int
941     .B *\fIfirstcharptr\fP);
942 nigel 63 .PP
943 nigel 75 The \fBpcre_info()\fP function is now obsolete because its interface is too
944 nigel 63 restrictive to return all the available data about a compiled pattern. New
945 nigel 75 programs should use \fBpcre_fullinfo()\fP instead. The yield of
946     \fBpcre_info()\fP is the number of capturing subpatterns, or one of the
947 nigel 63 following negative numbers:
948 nigel 75 .sp
949     PCRE_ERROR_NULL the argument \fIcode\fP was NULL
950 nigel 63 PCRE_ERROR_BADMAGIC the "magic number" was not found
951 nigel 75 .sp
952     If the \fIoptptr\fP argument is not NULL, a copy of the options with which the
953 nigel 63 pattern was compiled is placed in the integer it points to (see
954     PCRE_INFO_OPTIONS above).
955 nigel 75 .P
956     If the pattern is not anchored and the \fIfirstcharptr\fP argument is not NULL,
957 nigel 63 it is used to pass back information about the first character of any matched
958     string (see PCRE_INFO_FIRSTBYTE above).
959 nigel 75 .
960     .
961 nigel 77 .SH "REFERENCE COUNTS"
962 nigel 63 .rs
963     .sp
964 nigel 77 .B int pcre_refcount(pcre *\fIcode\fP, int \fIadjust\fP);
965     .PP
966     The \fBpcre_refcount()\fP function is used to maintain a reference count in the
967     data block that contains a compiled pattern. It is provided for the benefit of
968     applications that operate in an object-oriented manner, where different parts
969     of the application may be using the same compiled pattern, but you want to free
970     the block when they are all done.
971     .P
972     When a pattern is compiled, the reference count field is initialized to zero.
973     It is changed only by calling this function, whose action is to add the
974     \fIadjust\fP value (which may be positive or negative) to it. The yield of the
975     function is the new value. However, the value of the count is constrained to
976     lie between 0 and 65535, inclusive. If the new value is outside these limits,
977     it is forced to the appropriate limit value.
978     .P
979     Except when it is zero, the reference count is not correctly preserved if a
980     pattern is compiled on one host and then transferred to a host whose byte-order
981     is different. (This seems a highly unlikely scenario.)
982     .
983     .
985     .rs
986     .sp
987 nigel 75 .B int pcre_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
988 nigel 63 .ti +5n
989 nigel 75 .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
990 nigel 63 .ti +5n
991 nigel 75 .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP);
992     .P
993     The function \fBpcre_exec()\fP is called to match a subject string against a
994     compiled pattern, which is passed in the \fIcode\fP argument. If the
995 nigel 63 pattern has been studied, the result of the study should be passed in the
996 nigel 77 \fIextra\fP argument. This function is the main matching facility of the
997     library, and it operates in a Perl-like manner. For specialist use there is
998     also an alternative matching function, which is described
999     .\" HTML <a href="#dfamatch">
1000     .\" </a>
1001     below
1002     .\"
1003     in the section about the \fBpcre_dfa_exec()\fP function.
1004 nigel 75 .P
1005     In most applications, the pattern will have been compiled (and optionally
1006     studied) in the same process that calls \fBpcre_exec()\fP. However, it is
1007     possible to save compiled patterns and study data, and then use them later
1008     in different processes, possibly even on different hosts. For a discussion
1009     about this, see the
1010     .\" HREF
1011     \fBpcreprecompile\fP
1012     .\"
1013     documentation.
1014     .P
1015     Here is an example of a simple call to \fBpcre_exec()\fP:
1016     .sp
1017 nigel 63 int rc;
1018     int ovector[30];
1019     rc = pcre_exec(
1020     re, /* result of pcre_compile() */
1021     NULL, /* we didn't study the pattern */
1022     "some string", /* the subject string */
1023     11, /* the length of the subject string */
1024     0, /* start at offset 0 in the subject */
1025     0, /* default options */
1026 nigel 75 ovector, /* vector of integers for substring information */
1027 nigel 77 30); /* number of elements (NOT size in bytes) */
1028 nigel 75 .
1029     .\" HTML <a name="extradata"></a>
1030     .SS "Extra data for \fBpcre_exec()\fR"
1031     .rs
1032     .sp
1033     If the \fIextra\fP argument is not NULL, it must point to a \fBpcre_extra\fP
1034     data block. The \fBpcre_study()\fP function returns such a block (when it
1035 nigel 63 doesn't return NULL), but you can also create one for yourself, and pass
1036 nigel 87 additional information in it. The \fBpcre_extra\fP block contains the following
1037     fields (not necessarily in this order):
1038 nigel 75 .sp
1039     unsigned long int \fIflags\fP;
1040     void *\fIstudy_data\fP;
1041     unsigned long int \fImatch_limit\fP;
1042 nigel 87 unsigned long int \fImatch_limit_recursion\fP;
1043 nigel 75 void *\fIcallout_data\fP;
1044     const unsigned char *\fItables\fP;
1045     .sp
1046     The \fIflags\fP field is a bitmap that specifies which of the other fields
1047 nigel 63 are set. The flag bits are:
1048 nigel 75 .sp
1053 nigel 75 PCRE_EXTRA_TABLES
1054     .sp
1055     Other flag bits should be set to zero. The \fIstudy_data\fP field is set in the
1056     \fBpcre_extra\fP block that is returned by \fBpcre_study()\fP, together with
1057     the appropriate flag bit. You should not set this yourself, but you may add to
1058     the block by setting the other fields and their corresponding flag bits.
1059     .P
1060     The \fImatch_limit\fP field provides a means of preventing PCRE from using up a
1061 nigel 63 vast amount of resources when running patterns that are not going to match,
1062     but which have a very large number of possibilities in their search trees. The
1063 nigel 75 classic example is the use of nested unlimited repeats.
1064     .P
1065     Internally, PCRE uses a function called \fBmatch()\fP which it calls repeatedly
1066 nigel 87 (sometimes recursively). The limit set by \fImatch_limit\fP is imposed on the
1067     number of times this function is called during a match, which has the effect of
1068     limiting the amount of backtracking that can take place. For patterns that are
1069     not anchored, the count restarts from zero for each position in the subject
1070     string.
1071 nigel 75 .P
1072 nigel 87 The default value for the limit can be set when PCRE is built; the default
1073 nigel 63 default is 10 million, which handles all but the most extreme cases. You can
1074 nigel 87 override the default by suppling \fBpcre_exec()\fP with a \fBpcre_extra\fP
1075     block in which \fImatch_limit\fP is set, and PCRE_EXTRA_MATCH_LIMIT is set in
1076     the \fIflags\fP field. If the limit is exceeded, \fBpcre_exec()\fP returns
1078 nigel 75 .P
1079 nigel 87 The \fImatch_limit_recursion\fP field is similar to \fImatch_limit\fP, but
1080     instead of limiting the total number of times that \fBmatch()\fP is called, it
1081     limits the depth of recursion. The recursion depth is a smaller number than the
1082     total number of calls, because not all calls to \fBmatch()\fP are recursive.
1083     This limit is of use only if it is set smaller than \fImatch_limit\fP.
1084     .P
1085     Limiting the recursion depth limits the amount of stack that can be used, or,
1086     when PCRE has been compiled to use memory on the heap instead of the stack, the
1087     amount of heap memory that can be used.
1088     .P
1089     The default value for \fImatch_limit_recursion\fP can be set when PCRE is
1090     built; the default default is the same value as the default for
1091     \fImatch_limit\fP. You can override the default by suppling \fBpcre_exec()\fP
1092     with a \fBpcre_extra\fP block in which \fImatch_limit_recursion\fP is set, and
1093     PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the \fIflags\fP field. If the limit
1094     is exceeded, \fBpcre_exec()\fP returns PCRE_ERROR_RECURSIONLIMIT.
1095     .P
1096 nigel 75 The \fIpcre_callout\fP field is used in conjunction with the "callout" feature,
1097     which is described in the
1098     .\" HREF
1099     \fBpcrecallout\fP
1100     .\"
1101     documentation.
1102     .P
1103     The \fItables\fP field is used to pass a character tables pointer to
1104     \fBpcre_exec()\fP; this overrides the value that is stored with the compiled
1105     pattern. A non-NULL value is stored with the compiled pattern only if custom
1106     tables were supplied to \fBpcre_compile()\fP via its \fItableptr\fP argument.
1107     If NULL is passed to \fBpcre_exec()\fP using this mechanism, it forces PCRE's
1108     internal tables to be used. This facility is helpful when re-using patterns
1109     that have been saved after compiling with an external set of tables, because
1110     the external tables might be at a different address when \fBpcre_exec()\fP is
1111     called. See the
1112     .\" HREF
1113     \fBpcreprecompile\fP
1114     .\"
1115     documentation for a discussion of saving compiled patterns for later use.
1116     .
1117     .SS "Option bits for \fBpcre_exec()\fP"
1118     .rs
1119     .sp
1120     The unused bits of the \fIoptions\fP argument for \fBpcre_exec()\fP must be
1121 nigel 91 zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_\fIxxx\fP,
1123 nigel 75 .sp
1125     .sp
1126     The PCRE_ANCHORED option limits \fBpcre_exec()\fP to matching at the first
1127     matching position. If a pattern was compiled with PCRE_ANCHORED, or turned out
1128     to be anchored by virtue of its contents, it cannot be made unachored at
1129     matching time.
1130     .sp
1131 nigel 91 PCRE_NEWLINE_CR
1134     .sp
1135     These options override the newline definition that was chosen or defaulted when
1136     the pattern was compiled. For details, see the description \fBpcre_compile()\fP
1137     above. During matching, the newline choice affects the behaviour of the dot,
1138     circumflex, and dollar metacharacters.
1139     .sp
1140 nigel 63 PCRE_NOTBOL
1141 nigel 75 .sp
1142     This option specifies that first character of the subject string is not the
1143     beginning of a line, so the circumflex metacharacter should not match before
1144     it. Setting this without PCRE_MULTILINE (at compile time) causes circumflex
1145     never to match. This option affects only the behaviour of the circumflex
1146     metacharacter. It does not affect \eA.
1147     .sp
1148 nigel 63 PCRE_NOTEOL
1149 nigel 75 .sp
1150     This option specifies that the end of the subject string is not the end of a
1151     line, so the dollar metacharacter should not match it nor (except in multiline
1152     mode) a newline immediately before it. Setting this without PCRE_MULTILINE (at
1153     compile time) causes dollar never to match. This option affects only the
1154     behaviour of the dollar metacharacter. It does not affect \eZ or \ez.
1155     .sp
1156 nigel 63 PCRE_NOTEMPTY
1157 nigel 75 .sp
1158 nigel 63 An empty string is not considered to be a valid match if this option is set. If
1159     there are alternatives in the pattern, they are tried. If all the alternatives
1160     match the empty string, the entire match fails. For example, if the pattern
1161 nigel 75 .sp
1162 nigel 63 a?b?
1163 nigel 75 .sp
1164 nigel 63 is applied to a string not beginning with "a" or "b", it matches the empty
1165     string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
1166     valid, so PCRE searches further into the string for occurrences of "a" or "b".
1167 nigel 75 .P
1168 nigel 63 Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
1169 nigel 75 of a pattern match of the empty string within its \fBsplit()\fP function, and
1170 nigel 63 when using the /g modifier. It is possible to emulate Perl's behaviour after
1171     matching a null string by first trying the match again at the same offset with
1172 nigel 75 PCRE_NOTEMPTY and PCRE_ANCHORED, and then if that fails by advancing the
1173     starting offset (see below) and trying an ordinary match again. There is some
1174     code that demonstrates how to do this in the \fIpcredemo.c\fP sample program.
1175     .sp
1177     .sp
1178     When PCRE_UTF8 is set at compile time, the validity of the subject as a UTF-8
1179     string is automatically checked when \fBpcre_exec()\fP is subsequently called.
1180     The value of \fIstartoffset\fP is also checked to ensure that it points to the
1181     start of a UTF-8 character. If an invalid UTF-8 sequence of bytes is found,
1182     \fBpcre_exec()\fP returns the error PCRE_ERROR_BADUTF8. If \fIstartoffset\fP
1183     contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.
1184     .P
1185     If you already know that your subject is valid, and you want to skip these
1186     checks for performance reasons, you can set the PCRE_NO_UTF8_CHECK option when
1187     calling \fBpcre_exec()\fP. You might want to do this for the second and
1188     subsequent calls to \fBpcre_exec()\fP if you are making repeated calls to find
1189     all the matches in a single subject string. However, you should be sure that
1190     the value of \fIstartoffset\fP points to the start of a UTF-8 character. When
1191     PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid UTF-8 string as a
1192     subject, or a value of \fIstartoffset\fP that does not point to the start of a
1193     UTF-8 character, is undefined. Your program may crash.
1194     .sp
1196     .sp
1197     This option turns on the partial matching feature. If the subject string fails
1198     to match the pattern, but at some point during the matching process the end of
1199     the subject was reached (that is, the subject partially matches the pattern and
1200     the failure to match occurred only because there were not enough subject
1201     characters), \fBpcre_exec()\fP returns PCRE_ERROR_PARTIAL instead of
1202     PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is used, there are restrictions on what
1203     may appear in the pattern. These are discussed in the
1204     .\" HREF
1205     \fBpcrepartial\fP
1206     .\"
1207     documentation.
1208     .
1209     .SS "The string to be matched by \fBpcre_exec()\fP"
1210     .rs
1211     .sp
1212     The subject string is passed to \fBpcre_exec()\fP as a pointer in
1213     \fIsubject\fP, a length in \fIlength\fP, and a starting byte offset in
1214     \fIstartoffset\fP. In UTF-8 mode, the byte offset must point to the start of a
1215     UTF-8 character. Unlike the pattern string, the subject may contain binary zero
1216     bytes. When the starting offset is zero, the search for a match starts at the
1217     beginning of the subject, and this is by far the most common case.
1218     .P
1219 nigel 63 A non-zero starting offset is useful when searching for another match in the
1220 nigel 75 same subject by calling \fBpcre_exec()\fP again after a previous success.
1221     Setting \fIstartoffset\fP differs from just passing over a shortened string and
1222 nigel 63 setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
1223     lookbehind. For example, consider the pattern
1224 nigel 75 .sp
1225     \eBiss\eB
1226     .sp
1227     which finds occurrences of "iss" in the middle of words. (\eB matches only if
1228 nigel 63 the current position in the subject is not a word boundary.) When applied to
1229 nigel 75 the string "Mississipi" the first call to \fBpcre_exec()\fP finds the first
1230     occurrence. If \fBpcre_exec()\fP is called again with just the remainder of the
1231     subject, namely "issipi", it does not match, because \eB is always false at the
1232 nigel 63 start of the subject, which is deemed to be a word boundary. However, if
1233 nigel 75 \fBpcre_exec()\fP is passed the entire string again, but with \fIstartoffset\fP
1234 nigel 63 set to 4, it finds the second occurrence of "iss" because it is able to look
1235     behind the starting point to discover that it is preceded by a letter.
1236 nigel 75 .P
1237 nigel 63 If a non-zero starting offset is passed when the pattern is anchored, one
1238 nigel 75 attempt to match at the given offset is made. This can only succeed if the
1239 nigel 63 pattern does not require the match to be at the start of the subject.
1240 nigel 75 .
1241     .SS "How \fBpcre_exec()\fP returns captured substrings"
1242     .rs
1243     .sp
1244 nigel 63 In general, a pattern matches a certain portion of the subject, and in
1245     addition, further substrings from the subject may be picked out by parts of the
1246     pattern. Following the usage in Jeffrey Friedl's book, this is called
1247     "capturing" in what follows, and the phrase "capturing subpattern" is used for
1248     a fragment of a pattern that picks out a substring. PCRE supports several other
1249     kinds of parenthesized subpattern that do not cause substrings to be captured.
1250 nigel 75 .P
1251 nigel 63 Captured substrings are returned to the caller via a vector of integer offsets
1252 nigel 75 whose address is passed in \fIovector\fP. The number of elements in the vector
1253     is passed in \fIovecsize\fP, which must be a non-negative number. \fBNote\fP:
1254     this argument is NOT the size of \fIovector\fP in bytes.
1255     .P
1256     The first two-thirds of the vector is used to pass back captured substrings,
1257     each substring using a pair of integers. The remaining third of the vector is
1258     used as workspace by \fBpcre_exec()\fP while matching capturing subpatterns,
1259     and is not available for passing back information. The length passed in
1260     \fIovecsize\fP should always be a multiple of three. If it is not, it is
1261     rounded down.
1262     .P
1263     When a match is successful, information about captured substrings is returned
1264     in pairs of integers, starting at the beginning of \fIovector\fP, and
1265 nigel 63 continuing up to two-thirds of its length at the most. The first element of a
1266     pair is set to the offset of the first character in a substring, and the second
1267     is set to the offset of the first character after the end of a substring. The
1268 nigel 75 first pair, \fIovector[0]\fP and \fIovector[1]\fP, identify the portion of the
1269 nigel 63 subject string matched by the entire pattern. The next pair is used for the
1270 nigel 75 first capturing subpattern, and so on. The value returned by \fBpcre_exec()\fP
1271 nigel 91 is one more than the highest numbered pair that has been set. For example, if
1272     two substrings have been captured, the returned value is 3. If there are no
1273     capturing subpatterns, the return value from a successful match is 1,
1274     indicating that just the first pair of offsets has been set.
1275 nigel 75 .P
1276 nigel 63 If a capturing subpattern is matched repeatedly, it is the last portion of the
1277 nigel 75 string that it matched that is returned.
1278     .P
1279     If the vector is too small to hold all the captured substring offsets, it is
1280     used as far as possible (up to two-thirds of its length), and the function
1281     returns a value of zero. In particular, if the substring offsets are not of
1282     interest, \fBpcre_exec()\fP may be called with \fIovector\fP passed as NULL and
1283     \fIovecsize\fP as zero. However, if the pattern contains back references and
1284     the \fIovector\fP is not big enough to remember the related substrings, PCRE
1285     has to get additional memory for use during matching. Thus it is usually
1286     advisable to supply an \fIovector\fP.
1287     .P
1288 nigel 91 The \fBpcre_info()\fP function can be used to find out how many capturing
1289 nigel 63 subpatterns there are in a compiled pattern. The smallest size for
1290 nigel 75 \fIovector\fP that will allow for \fIn\fP captured substrings, in addition to
1291     the offsets of the substring matched by the whole pattern, is (\fIn\fP+1)*3.
1292 nigel 91 .P
1293     It is possible for capturing subpattern number \fIn+1\fP to match some part of
1294     the subject when subpattern \fIn\fP has not been used at all. For example, if
1295     the string "abc" is matched against the pattern (a|(z))(bc) the return from the
1296     function is 4, and subpatterns 1 and 3 are matched, but 2 is not. When this
1297     happens, both values in the offset pairs corresponding to unused subpatterns
1298     are set to -1.
1299     .P
1300     Offset values that correspond to unused subpatterns at the end of the
1301     expression are also set to -1. For example, if the string "abc" is matched
1302     against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not matched. The
1303     return from the function is 2, because the highest used capturing subpattern
1304     number is 1. However, you can refer to the offsets for the second and third
1305     capturing subpatterns if you wish (assuming the vector is large enough, of
1306     course).
1307     .P
1308     Some convenience functions are provided for extracting the captured substrings
1309     as separate strings. These are described below.
1310 nigel 75 .
1311 nigel 77 .\" HTML <a name="errorlist"></a>
1312 nigel 91 .SS "Error return values from \fBpcre_exec()\fP"
1313 nigel 75 .rs
1314     .sp
1315     If \fBpcre_exec()\fP fails, it returns a negative number. The following are
1316 nigel 63 defined in the header file:
1317 nigel 75 .sp
1318 nigel 63 PCRE_ERROR_NOMATCH (-1)
1319 nigel 75 .sp
1320 nigel 63 The subject string did not match the pattern.
1321 nigel 75 .sp
1322 nigel 63 PCRE_ERROR_NULL (-2)
1323 nigel 75 .sp
1324     Either \fIcode\fP or \fIsubject\fP was passed as NULL, or \fIovector\fP was
1325     NULL and \fIovecsize\fP was not zero.
1326     .sp
1327 nigel 63 PCRE_ERROR_BADOPTION (-3)
1328 nigel 75 .sp
1329     An unrecognized bit was set in the \fIoptions\fP argument.
1330     .sp
1331 nigel 63 PCRE_ERROR_BADMAGIC (-4)
1332 nigel 75 .sp
1333 nigel 63 PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
1334 nigel 75 the case when it is passed a junk pointer and to detect when a pattern that was
1335     compiled in an environment of one endianness is run in an environment with the
1336     other endianness. This is the error that PCRE gives when the magic number is
1337     not present.
1338     .sp
1339 nigel 63 PCRE_ERROR_UNKNOWN_NODE (-5)
1340 nigel 75 .sp
1341 nigel 63 While running the pattern match, an unknown item was encountered in the
1342     compiled pattern. This error could be caused by a bug in PCRE or by overwriting
1343     of the compiled pattern.
1344 nigel 75 .sp
1345 nigel 63 PCRE_ERROR_NOMEMORY (-6)
1346 nigel 75 .sp
1347     If a pattern contains back references, but the \fIovector\fP that is passed to
1348     \fBpcre_exec()\fP is not big enough to remember the referenced substrings, PCRE
1349 nigel 63 gets a block of memory at the start of matching to use for this purpose. If the
1350 nigel 75 call via \fBpcre_malloc()\fP fails, this error is given. The memory is
1351     automatically freed at the end of matching.
1352     .sp
1353 nigel 63 PCRE_ERROR_NOSUBSTRING (-7)
1354 nigel 75 .sp
1355     This error is used by the \fBpcre_copy_substring()\fP,
1356     \fBpcre_get_substring()\fP, and \fBpcre_get_substring_list()\fP functions (see
1357     below). It is never returned by \fBpcre_exec()\fP.
1358     .sp
1359 nigel 63 PCRE_ERROR_MATCHLIMIT (-8)
1360 nigel 75 .sp
1361 nigel 87 The backtracking limit, as specified by the \fImatch_limit\fP field in a
1362     \fBpcre_extra\fP structure (or defaulted) was reached. See the description
1363     above.
1364     .sp
1366     .sp
1367     The internal recursion limit, as specified by the \fImatch_limit_recursion\fP
1368 nigel 75 field in a \fBpcre_extra\fP structure (or defaulted) was reached. See the
1369 nigel 63 description above.
1370 nigel 75 .sp
1371 nigel 63 PCRE_ERROR_CALLOUT (-9)
1372 nigel 75 .sp
1373     This error is never generated by \fBpcre_exec()\fP itself. It is provided for
1374 nigel 63 use by callout functions that want to yield a distinctive error code. See the
1375 nigel 75 .\" HREF
1376     \fBpcrecallout\fP
1377     .\"
1378     documentation for details.
1379     .sp
1380 nigel 73 PCRE_ERROR_BADUTF8 (-10)
1381 nigel 75 .sp
1382 nigel 71 A string that contains an invalid UTF-8 byte sequence was passed as a subject.
1383 nigel 75 .sp
1384 nigel 73 PCRE_ERROR_BADUTF8_OFFSET (-11)
1385 nigel 75 .sp
1386 nigel 73 The UTF-8 byte sequence that was passed as a subject was valid, but the value
1387 nigel 75 of \fIstartoffset\fP did not point to the beginning of a UTF-8 character.
1388     .sp
1389 nigel 77 PCRE_ERROR_PARTIAL (-12)
1390 nigel 75 .sp
1391     The subject string did not match, but it did match partially. See the
1392     .\" HREF
1393     \fBpcrepartial\fP
1394     .\"
1395     documentation for details of partial matching.
1396     .sp
1397 nigel 77 PCRE_ERROR_BADPARTIAL (-13)
1398 nigel 75 .sp
1399     The PCRE_PARTIAL option was used with a compiled pattern containing items that
1400     are not supported for partial matching. See the
1401     .\" HREF
1402     \fBpcrepartial\fP
1403     .\"
1404     documentation for details of partial matching.
1405     .sp
1406 nigel 77 PCRE_ERROR_INTERNAL (-14)
1407 nigel 75 .sp
1408     An unexpected internal error has occurred. This error could be caused by a bug
1409     in PCRE or by overwriting of the compiled pattern.
1410     .sp
1411 nigel 77 PCRE_ERROR_BADCOUNT (-15)
1412 nigel 75 .sp
1413     This error is given if the value of the \fIovecsize\fP argument is negative.
1414     .
1415     .
1417 nigel 63 .rs
1418     .sp
1419 nigel 75 .B int pcre_copy_substring(const char *\fIsubject\fP, int *\fIovector\fP,
1420 nigel 63 .ti +5n
1421 nigel 75 .B int \fIstringcount\fP, int \fIstringnumber\fP, char *\fIbuffer\fP,
1422 nigel 63 .ti +5n
1423 nigel 75 .B int \fIbuffersize\fP);
1424 nigel 63 .PP
1425     .br
1426 nigel 75 .B int pcre_get_substring(const char *\fIsubject\fP, int *\fIovector\fP,
1427 nigel 63 .ti +5n
1428 nigel 75 .B int \fIstringcount\fP, int \fIstringnumber\fP,
1429 nigel 63 .ti +5n
1430 nigel 75 .B const char **\fIstringptr\fP);
1431 nigel 63 .PP
1432     .br
1433 nigel 75 .B int pcre_get_substring_list(const char *\fIsubject\fP,
1434 nigel 63 .ti +5n
1435 nigel 75 .B int *\fIovector\fP, int \fIstringcount\fP, "const char ***\fIlistptr\fP);"
1436 nigel 63 .PP
1437     Captured substrings can be accessed directly by using the offsets returned by
1438 nigel 75 \fBpcre_exec()\fP in \fIovector\fP. For convenience, the functions
1439     \fBpcre_copy_substring()\fP, \fBpcre_get_substring()\fP, and
1440     \fBpcre_get_substring_list()\fP are provided for extracting captured substrings
1441 nigel 63 as new, separate, zero-terminated strings. These functions identify substrings
1442     by number. The next section describes functions for extracting named
1443 nigel 91 substrings.
1444 nigel 75 .P
1445 nigel 91 A substring that contains a binary zero is correctly extracted and has a
1446     further zero added on the end, but the result is not, of course, a C string.
1447     However, you can process such a string by referring to the length that is
1448     returned by \fBpcre_copy_substring()\fP and \fBpcre_get_substring()\fP.
1449     Unfortunately, the interface to \fBpcre_get_substring_list()\fP is not adequate
1450     for handling strings containing binary zeros, because the end of the final
1451     string is not independently indicated.
1452     .P
1453 nigel 63 The first three arguments are the same for all three of these functions:
1454 nigel 75 \fIsubject\fP is the subject string that has just been successfully matched,
1455     \fIovector\fP is a pointer to the vector of integer offsets that was passed to
1456     \fBpcre_exec()\fP, and \fIstringcount\fP is the number of substrings that were
1457 nigel 63 captured by the match, including the substring that matched the entire regular
1458 nigel 75 expression. This is the value returned by \fBpcre_exec()\fP if it is greater
1459     than zero. If \fBpcre_exec()\fP returned zero, indicating that it ran out of
1460     space in \fIovector\fP, the value passed as \fIstringcount\fP should be the
1461     number of elements in the vector divided by three.
1462     .P
1463     The functions \fBpcre_copy_substring()\fP and \fBpcre_get_substring()\fP
1464     extract a single substring, whose number is given as \fIstringnumber\fP. A
1465     value of zero extracts the substring that matched the entire pattern, whereas
1466     higher values extract the captured substrings. For \fBpcre_copy_substring()\fP,
1467     the string is placed in \fIbuffer\fP, whose length is given by
1468     \fIbuffersize\fP, while for \fBpcre_get_substring()\fP a new block of memory is
1469     obtained via \fBpcre_malloc\fP, and its address is returned via
1470     \fIstringptr\fP. The yield of the function is the length of the string, not
1471 nigel 63 including the terminating zero, or one of
1472 nigel 75 .sp
1473 nigel 63 PCRE_ERROR_NOMEMORY (-6)
1474 nigel 75 .sp
1475     The buffer was too small for \fBpcre_copy_substring()\fP, or the attempt to get
1476     memory failed for \fBpcre_get_substring()\fP.
1477     .sp
1478 nigel 63 PCRE_ERROR_NOSUBSTRING (-7)
1479 nigel 75 .sp
1480     There is no substring whose number is \fIstringnumber\fP.
1481     .P
1482     The \fBpcre_get_substring_list()\fP function extracts all available substrings
1483 nigel 63 and builds a list of pointers to them. All this is done in a single block of
1484 nigel 75 memory that is obtained via \fBpcre_malloc\fP. The address of the memory block
1485     is returned via \fIlistptr\fP, which is also the start of the list of string
1486 nigel 63 pointers. The end of the list is marked by a NULL pointer. The yield of the
1487     function is zero if all went well, or
1488 nigel 75 .sp
1489 nigel 63 PCRE_ERROR_NOMEMORY (-6)
1490 nigel 75 .sp
1491 nigel 63 if the attempt to get the memory block failed.
1492 nigel 75 .P
1493 nigel 63 When any of these functions encounter a substring that is unset, which can
1494 nigel 75 happen when capturing subpattern number \fIn+1\fP matches some part of the
1495     subject, but subpattern \fIn\fP has not been used at all, they return an empty
1496 nigel 63 string. This can be distinguished from a genuine zero-length substring by
1497 nigel 75 inspecting the appropriate offset in \fIovector\fP, which is negative for unset
1498 nigel 63 substrings.
1499 nigel 75 .P
1500     The two convenience functions \fBpcre_free_substring()\fP and
1501     \fBpcre_free_substring_list()\fP can be used to free the memory returned by
1502     a previous call of \fBpcre_get_substring()\fP or
1503     \fBpcre_get_substring_list()\fP, respectively. They do nothing more than call
1504     the function pointed to by \fBpcre_free\fP, which of course could be called
1505 nigel 63 directly from a C program. However, PCRE is used in some situations where it is
1506 nigel 91 linked via a special interface to another programming language that cannot use
1507 nigel 75 \fBpcre_free\fP directly; it is for these cases that the functions are
1508 nigel 63 provided.
1509 nigel 75 .
1510     .
1512 nigel 63 .rs
1513     .sp
1514 nigel 75 .B int pcre_get_stringnumber(const pcre *\fIcode\fP,
1515 nigel 63 .ti +5n
1516 nigel 75 .B const char *\fIname\fP);
1517 nigel 63 .PP
1518     .br
1519 nigel 75 .B int pcre_copy_named_substring(const pcre *\fIcode\fP,
1520 nigel 63 .ti +5n
1521 nigel 75 .B const char *\fIsubject\fP, int *\fIovector\fP,
1522     .ti +5n
1523     .B int \fIstringcount\fP, const char *\fIstringname\fP,
1524     .ti +5n
1525     .B char *\fIbuffer\fP, int \fIbuffersize\fP);
1526 nigel 63 .PP
1527     .br
1528 nigel 75 .B int pcre_get_named_substring(const pcre *\fIcode\fP,
1529 nigel 63 .ti +5n
1530 nigel 75 .B const char *\fIsubject\fP, int *\fIovector\fP,
1531 nigel 63 .ti +5n
1532 nigel 75 .B int \fIstringcount\fP, const char *\fIstringname\fP,
1533 nigel 63 .ti +5n
1534 nigel 75 .B const char **\fIstringptr\fP);
1535 nigel 63 .PP
1536 nigel 75 To extract a substring by name, you first have to find associated number.
1537     For example, for this pattern
1538     .sp
1539 nigel 79 (a+)b(?P<xxx>\ed+)...
1540 nigel 75 .sp
1541 nigel 91 the number of the subpattern called "xxx" is 2. If the name is known to be
1542     unique (PCRE_DUPNAMES was not set), you can find the number from the name by
1543     calling \fBpcre_get_stringnumber()\fP. The first argument is the compiled
1544     pattern, and the second is the name. The yield of the function is the
1545 nigel 75 subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no subpattern of
1546     that name.
1547     .P
1548     Given the number, you can extract the substring directly, or use one of the
1549     functions described in the previous section. For convenience, there are also
1550     two functions that do the whole job.
1551     .P
1552 nigel 91 Most of the arguments of \fBpcre_copy_named_substring()\fP and
1553     \fBpcre_get_named_substring()\fP are the same as those for the similarly named
1554 nigel 75 functions that extract by number. As these are described in the previous
1555     section, they are not re-described here. There are just two differences:
1556     .P
1557 nigel 63 First, instead of a substring number, a substring name is given. Second, there
1558     is an extra argument, given at the start, which is a pointer to the compiled
1559     pattern. This is needed in order to gain access to the name-to-number
1560     translation table.
1561 nigel 75 .P
1562     These functions call \fBpcre_get_stringnumber()\fP, and if it succeeds, they
1563     then call \fIpcre_copy_substring()\fP or \fIpcre_get_substring()\fP, as
1564 nigel 63 appropriate.
1565 nigel 77 .
1566     .
1568     .rs
1569     .sp
1570     .B int pcre_get_stringtable_entries(const pcre *\fIcode\fP,
1571     .ti +5n
1572     .B const char *\fIname\fP, char **\fIfirst\fP, char **\fIlast\fP);
1573     .PP
1574     When a pattern is compiled with the PCRE_DUPNAMES option, names for subpatterns
1575     are not required to be unique. Normally, patterns with duplicate names are such
1576     that in any one match, only one of the named subpatterns participates. An
1577     example is shown in the
1578     .\" HREF
1579     \fBpcrepattern\fP
1580     .\"
1581     documentation. When duplicates are present, \fBpcre_copy_named_substring()\fP
1582     and \fBpcre_get_named_substring()\fP return the first substring corresponding
1583     to the given name that is set. If none are set, an empty string is returned.
1584     The \fBpcre_get_stringnumber()\fP function returns one of the numbers that are
1585     associated with the name, but it is not defined which it is.
1586     .sp
1587     If you want to get full details of all captured substrings for a given name,
1588     you must use the \fBpcre_get_stringtable_entries()\fP function. The first
1589     argument is the compiled pattern, and the second is the name. The third and
1590     fourth are pointers to variables which are updated by the function. After it
1591     has run, they point to the first and last entries in the name-to-number table
1592     for the given name. The function itself returns the length of each entry, or
1593     PCRE_ERROR_NOSUBSTRING if there are none. The format of the table is described
1594     above in the section entitled \fIInformation about a pattern\fP. Given all the
1595     relevant entries for the name, you can extract each of their numbers, and hence
1596     the captured data, if any.
1597     .
1598     .
1600     .rs
1601     .sp
1602     The traditional matching function uses a similar algorithm to Perl, which stops
1603     when it finds the first match, starting at a given point in the subject. If you
1604     want to find all possible matches, or the longest possible match, consider
1605     using the alternative matching function (see below) instead. If you cannot use
1606     the alternative function, but still need to find all possible matches, you
1607     can kludge it up by making use of the callout facility, which is described in
1608     the
1609     .\" HREF
1610     \fBpcrecallout\fP
1611     .\"
1612     documentation.
1613 nigel 75 .P
1614 nigel 77 What you have to do is to insert a callout right at the end of the pattern.
1615     When your callout function is called, extract and save the current matched
1616     substring. Then return 1, which forces \fBpcre_exec()\fP to backtrack and try
1617     other alternatives. Ultimately, when it runs out of matches, \fBpcre_exec()\fP
1618     will yield PCRE_ERROR_NOMATCH.
1619     .
1620     .
1621     .\" HTML <a name="dfamatch"></a>
1623     .rs
1624     .sp
1625     .B int pcre_dfa_exec(const pcre *\fIcode\fP, "const pcre_extra *\fIextra\fP,"
1626     .ti +5n
1627     .B "const char *\fIsubject\fP," int \fIlength\fP, int \fIstartoffset\fP,
1628     .ti +5n
1629     .B int \fIoptions\fP, int *\fIovector\fP, int \fIovecsize\fP,
1630     .ti +5n
1631     .B int *\fIworkspace\fP, int \fIwscount\fP);
1632     .P
1633     The function \fBpcre_dfa_exec()\fP is called to match a subject string against
1634     a compiled pattern, using a "DFA" matching algorithm. This has different
1635     characteristics to the normal algorithm, and is not compatible with Perl. Some
1636     of the features of PCRE patterns are not supported. Nevertheless, there are
1637     times when this kind of matching can be useful. For a discussion of the two
1638     matching algorithms, see the
1639     .\" HREF
1640     \fBpcrematching\fP
1641     .\"
1642     documentation.
1643     .P
1644     The arguments for the \fBpcre_dfa_exec()\fP function are the same as for
1645     \fBpcre_exec()\fP, plus two extras. The \fIovector\fP argument is used in a
1646     different way, and this is described below. The other common arguments are used
1647     in the same way as for \fBpcre_exec()\fP, so their description is not repeated
1648     here.
1649     .P
1650     The two additional arguments provide workspace for the function. The workspace
1651     vector should contain at least 20 elements. It is used for keeping track of
1652     multiple paths through the pattern tree. More workspace will be needed for
1653 nigel 91 patterns and subjects where there are a lot of potential matches.
1654 nigel 77 .P
1655 nigel 87 Here is an example of a simple call to \fBpcre_dfa_exec()\fP:
1656 nigel 77 .sp
1657     int rc;
1658     int ovector[10];
1659     int wspace[20];
1660 nigel 87 rc = pcre_dfa_exec(
1661 nigel 77 re, /* result of pcre_compile() */
1662     NULL, /* we didn't study the pattern */
1663     "some string", /* the subject string */
1664     11, /* the length of the subject string */
1665     0, /* start at offset 0 in the subject */
1666     0, /* default options */
1667     ovector, /* vector of integers for substring information */
1668     10, /* number of elements (NOT size in bytes) */
1669     wspace, /* working space vector */
1670     20); /* number of elements (NOT size in bytes) */
1671     .
1672     .SS "Option bits for \fBpcre_dfa_exec()\fP"
1673     .rs
1674     .sp
1675     The unused bits of the \fIoptions\fP argument for \fBpcre_dfa_exec()\fP must be
1676 nigel 91 zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_\fIxxx\fP,
1678 nigel 77 PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last three of these are
1679     the same as for \fBpcre_exec()\fP, so their description is not repeated here.
1680     .sp
1682     .sp
1683     This has the same general effect as it does for \fBpcre_exec()\fP, but the
1684     details are slightly different. When PCRE_PARTIAL is set for
1685     \fBpcre_dfa_exec()\fP, the return code PCRE_ERROR_NOMATCH is converted into
1686     PCRE_ERROR_PARTIAL if the end of the subject is reached, there have been no
1687     complete matches, but there is still at least one matching possibility. The
1688     portion of the string that provided the partial match is set as the first
1689     matching string.
1690     .sp
1692     .sp
1693     Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to stop as
1694     soon as it has found one match. Because of the way the DFA algorithm works,
1695     this is necessarily the shortest possible match at the first possible matching
1696     point in the subject string.
1697     .sp
1699     .sp
1700     When \fBpcre_dfa_exec()\fP is called with the PCRE_PARTIAL option, and returns
1701     a partial match, it is possible to call it again, with additional subject
1702     characters, and have it continue with the same match. The PCRE_DFA_RESTART
1703     option requests this action; when it is set, the \fIworkspace\fP and
1704     \fIwscount\fP options must reference the same vector as before because data
1705     about the match so far is left in them after a partial match. There is more
1706     discussion of this facility in the
1707     .\" HREF
1708     \fBpcrepartial\fP
1709     .\"
1710     documentation.
1711     .
1712     .SS "Successful returns from \fBpcre_dfa_exec()\fP"
1713     .rs
1714     .sp
1715     When \fBpcre_dfa_exec()\fP succeeds, it may have matched more than one
1716     substring in the subject. Note, however, that all the matches from one run of
1717     the function start at the same point in the subject. The shorter matches are
1718     all initial substrings of the longer matches. For example, if the pattern
1719     .sp
1720     <.*>
1721     .sp
1722     is matched against the string
1723     .sp
1724     This is <something> <something else> <something further> no more
1725     .sp
1726     the three matched strings are
1727     .sp
1728     <something>
1729     <something> <something else>
1730     <something> <something else> <something further>
1731     .sp
1732     On success, the yield of the function is a number greater than zero, which is
1733     the number of matched substrings. The substrings themselves are returned in
1734     \fIovector\fP. Each string uses two elements; the first is the offset to the
1735     start, and the second is the offset to the end. All the strings have the same
1736     start offset. (Space could have been saved by giving this only once, but it was
1737     decided to retain some compatibility with the way \fBpcre_exec()\fP returns
1738     data, even though the meaning of the strings is different.)
1739     .P
1740     The strings are returned in reverse order of length; that is, the longest
1741     matching string is given first. If there were too many matches to fit into
1742     \fIovector\fP, the yield of the function is zero, and the vector is filled with
1743     the longest matches.
1744     .
1745     .SS "Error returns from \fBpcre_dfa_exec()\fP"
1746     .rs
1747     .sp
1748     The \fBpcre_dfa_exec()\fP function returns a negative number when it fails.
1749     Many of the errors are the same as for \fBpcre_exec()\fP, and these are
1750     described
1751     .\" HTML <a href="#errorlist">
1752     .\" </a>
1753     above.
1754     .\"
1755     There are in addition the following errors that are specific to
1756     \fBpcre_dfa_exec()\fP:
1757     .sp
1758     PCRE_ERROR_DFA_UITEM (-16)
1759     .sp
1760     This return is given if \fBpcre_dfa_exec()\fP encounters an item in the pattern
1761     that it does not support, for instance, the use of \eC or a back reference.
1762     .sp
1763     PCRE_ERROR_DFA_UCOND (-17)
1764     .sp
1765     This return is given if \fBpcre_dfa_exec()\fP encounters a condition item in a
1766     pattern that uses a back reference for the condition. This is not supported.
1767     .sp
1769     .sp
1770     This return is given if \fBpcre_dfa_exec()\fP is called with an \fIextra\fP
1771     block that contains a setting of the \fImatch_limit\fP field. This is not
1772     supported (it is meaningless).
1773     .sp
1774     PCRE_ERROR_DFA_WSSIZE (-19)
1775     .sp
1776     This return is given if \fBpcre_dfa_exec()\fP runs out of space in the
1777     \fIworkspace\fP vector.
1778     .sp
1780     .sp
1781     When a recursive subpattern is processed, the matching function calls itself
1782     recursively, using private vectors for \fIovector\fP and \fIworkspace\fP. This
1783     error is given if the output vector is not large enough. This should be
1784     extremely rare, as a vector of size 1000 is used.
1785     .P
1786 nigel 63 .in 0
1787 nigel 91 Last updated: 08 June 2006
1788 nigel 63 .br
1789 nigel 87 Copyright (c) 1997-2006 University of Cambridge.

ViewVC Help
Powered by ViewVC 1.1.12