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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65 - (hide annotations) (download)
Sat Feb 24 21:40:08 2007 UTC (7 years, 6 months ago) by nigel
File size: 43658 byte(s)
Load pcre-4.1 into code/trunk.

1 nigel 63 .TH PCRE 3
2     .SH NAME
3     PCRE - Perl-compatible regular expressions
4     .SH SYNOPSIS OF PCRE API
5     .rs
6     .sp
7     .B #include <pcre.h>
8     .PP
9     .SM
10     .br
11     .B pcre *pcre_compile(const char *\fIpattern\fR, int \fIoptions\fR,
12     .ti +5n
13     .B const char **\fIerrptr\fR, int *\fIerroffset\fR,
14     .ti +5n
15     .B const unsigned char *\fItableptr\fR);
16     .PP
17     .br
18     .B pcre_extra *pcre_study(const pcre *\fIcode\fR, int \fIoptions\fR,
19     .ti +5n
20     .B const char **\fIerrptr\fR);
21     .PP
22     .br
23     .B int pcre_exec(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"
24     .ti +5n
25     .B "const char *\fIsubject\fR," int \fIlength\fR, int \fIstartoffset\fR,
26     .ti +5n
27     .B int \fIoptions\fR, int *\fIovector\fR, int \fIovecsize\fR);
28     .PP
29     .br
30     .B int pcre_copy_named_substring(const pcre *\fIcode\fR,
31     .ti +5n
32     .B const char *\fIsubject\fR, int *\fIovector\fR,
33     .ti +5n
34     .B int \fIstringcount\fR, const char *\fIstringname\fR,
35     .ti +5n
36     .B char *\fIbuffer\fR, int \fIbuffersize\fR);
37     .PP
38     .br
39     .B int pcre_copy_substring(const char *\fIsubject\fR, int *\fIovector\fR,
40     .ti +5n
41     .B int \fIstringcount\fR, int \fIstringnumber\fR, char *\fIbuffer\fR,
42     .ti +5n
43     .B int \fIbuffersize\fR);
44     .PP
45     .br
46     .B int pcre_get_named_substring(const pcre *\fIcode\fR,
47     .ti +5n
48     .B const char *\fIsubject\fR, int *\fIovector\fR,
49     .ti +5n
50     .B int \fIstringcount\fR, const char *\fIstringname\fR,
51     .ti +5n
52     .B const char **\fIstringptr\fR);
53     .PP
54     .br
55     .B int pcre_get_stringnumber(const pcre *\fIcode\fR,
56     .ti +5n
57     .B const char *\fIname\fR);
58     .PP
59     .br
60     .B int pcre_get_substring(const char *\fIsubject\fR, int *\fIovector\fR,
61     .ti +5n
62     .B int \fIstringcount\fR, int \fIstringnumber\fR,
63     .ti +5n
64     .B const char **\fIstringptr\fR);
65     .PP
66     .br
67     .B int pcre_get_substring_list(const char *\fIsubject\fR,
68     .ti +5n
69     .B int *\fIovector\fR, int \fIstringcount\fR, "const char ***\fIlistptr\fR);"
70     .PP
71     .br
72     .B void pcre_free_substring(const char *\fIstringptr\fR);
73     .PP
74     .br
75     .B void pcre_free_substring_list(const char **\fIstringptr\fR);
76     .PP
77     .br
78     .B const unsigned char *pcre_maketables(void);
79     .PP
80     .br
81     .B int pcre_fullinfo(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"
82     .ti +5n
83     .B int \fIwhat\fR, void *\fIwhere\fR);
84     .PP
85     .br
86     .B int pcre_info(const pcre *\fIcode\fR, int *\fIoptptr\fR, int
87     .B *\fIfirstcharptr\fR);
88     .PP
89     .br
90     .B int pcre_config(int \fIwhat\fR, void *\fIwhere\fR);
91     .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     .B int (*pcre_callout)(pcre_callout_block *);
103    
104     .SH PCRE API
105     .rs
106     .sp
107     PCRE has its own native API, which is described in this document. There is also
108     a set of wrapper functions that correspond to the POSIX regular expression API.
109     These are described in the \fBpcreposix\fR documentation.
110    
111     The native API function prototypes are defined in the header file \fBpcre.h\fR,
112     and on Unix systems the library itself is called \fBlibpcre.a\fR, so can be
113     accessed by adding \fB-lpcre\fR to the command for linking an application which
114     calls it. The header file defines the macros PCRE_MAJOR and PCRE_MINOR to
115     contain the major and minor release numbers for the library. Applications can
116     use these to include support for different releases.
117    
118     The functions \fBpcre_compile()\fR, \fBpcre_study()\fR, and \fBpcre_exec()\fR
119     are used for compiling and matching regular expressions. A sample program that
120     demonstrates the simplest way of using them is given in the file
121     \fIpcredemo.c\fR. The \fBpcresample\fR documentation describes how to run it.
122    
123     There are convenience functions for extracting captured substrings from a
124     matched subject string. They are:
125    
126     \fBpcre_copy_substring()\fR
127     \fBpcre_copy_named_substring()\fR
128     \fBpcre_get_substring()\fR
129     \fBpcre_get_named_substring()\fR
130     \fBpcre_get_substring_list()\fR
131    
132     \fBpcre_free_substring()\fR and \fBpcre_free_substring_list()\fR are also
133     provided, to free the memory used for extracted strings.
134    
135     The function \fBpcre_maketables()\fR is used (optionally) to build a set of
136     character tables in the current locale for passing to \fBpcre_compile()\fR.
137    
138     The function \fBpcre_fullinfo()\fR is used to find out information about a
139     compiled pattern; \fBpcre_info()\fR is an obsolete version which returns only
140     some of the available information, but is retained for backwards compatibility.
141     The function \fBpcre_version()\fR returns a pointer to a string containing the
142     version of PCRE and its date of release.
143    
144     The global variables \fBpcre_malloc\fR and \fBpcre_free\fR initially contain
145     the entry points of the standard \fBmalloc()\fR and \fBfree()\fR functions
146     respectively. PCRE calls the memory management functions via these variables,
147     so a calling program can replace them if it wishes to intercept the calls. This
148     should be done before calling any PCRE functions.
149    
150     The global variable \fBpcre_callout\fR initially contains NULL. It can be set
151     by the caller to a "callout" function, which PCRE will then call at specified
152     points during a matching operation. Details are given in the \fBpcrecallout\fR
153     documentation.
154    
155     .SH MULTITHREADING
156     .rs
157     .sp
158     The PCRE functions can be used in multi-threading applications, with the
159     proviso that the memory management functions pointed to by \fBpcre_malloc\fR
160     and \fBpcre_free\fR, and the callout function pointed to by \fBpcre_callout\fR,
161     are shared by all threads.
162    
163     The compiled form of a regular expression is not altered during matching, so
164     the same compiled pattern can safely be used by several threads at once.
165    
166     .SH CHECKING BUILD-TIME OPTIONS
167     .rs
168     .sp
169     .B int pcre_config(int \fIwhat\fR, void *\fIwhere\fR);
170     .PP
171     The function \fBpcre_config()\fR makes it possible for a PCRE client to
172     discover which optional features have been compiled into the PCRE library. The
173     .\" HREF
174     \fBpcrebuild\fR
175     .\"
176     documentation has more details about these optional features.
177    
178     The first argument for \fBpcre_config()\fR is an integer, specifying which
179     information is required; the second argument is a pointer to a variable into
180     which the information is placed. The following information is available:
181    
182     PCRE_CONFIG_UTF8
183    
184     The output is an integer that is set to one if UTF-8 support is available;
185     otherwise it is set to zero.
186    
187     PCRE_CONFIG_NEWLINE
188    
189     The output is an integer that is set to the value of the code that is used for
190     the newline character. It is either linefeed (10) or carriage return (13), and
191     should normally be the standard character for your operating system.
192    
193     PCRE_CONFIG_LINK_SIZE
194    
195     The output is an integer that contains the number of bytes used for internal
196     linkage in compiled regular expressions. The value is 2, 3, or 4. Larger values
197     allow larger regular expressions to be compiled, at the expense of slower
198     matching. The default value of 2 is sufficient for all but the most massive
199     patterns, since it allows the compiled pattern to be up to 64K in size.
200    
201     PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
202    
203     The output is an integer that contains the threshold above which the POSIX
204     interface uses \fBmalloc()\fR for output vectors. Further details are given in
205     the \fBpcreposix\fR documentation.
206    
207     PCRE_CONFIG_MATCH_LIMIT
208    
209     The output is an integer that gives the default limit for the number of
210     internal matching function calls in a \fBpcre_exec()\fR execution. Further
211     details are given with \fBpcre_exec()\fR below.
212    
213     .SH COMPILING A PATTERN
214     .rs
215     .sp
216     .B pcre *pcre_compile(const char *\fIpattern\fR, int \fIoptions\fR,
217     .ti +5n
218     .B const char **\fIerrptr\fR, int *\fIerroffset\fR,
219     .ti +5n
220     .B const unsigned char *\fItableptr\fR);
221     .PP
222    
223     The function \fBpcre_compile()\fR is called to compile a pattern into an
224     internal form. The pattern is a C string terminated by a binary zero, and
225     is passed in the argument \fIpattern\fR. A pointer to a single block of memory
226     that is obtained via \fBpcre_malloc\fR is returned. This contains the compiled
227     code and related data. The \fBpcre\fR type is defined for the returned block;
228     this is a typedef for a structure whose contents are not externally defined. It
229     is up to the caller to free the memory when it is no longer required.
230    
231     Although the compiled code of a PCRE regex is relocatable, that is, it does not
232     depend on memory location, the complete \fBpcre\fR data block is not
233     fully relocatable, because it contains a copy of the \fItableptr\fR argument,
234     which is an address (see below).
235    
236     The \fIoptions\fR argument contains independent bits that affect the
237     compilation. It should be zero if no options are required. Some of the options,
238     in particular, those that are compatible with Perl, can also be set and unset
239     from within the pattern (see the detailed description of regular expressions
240     in the \fBpcrepattern\fR documentation). For these options, the contents of the
241     \fIoptions\fR argument specifies their initial settings at the start of
242     compilation and execution. The PCRE_ANCHORED option can be set at the time of
243     matching as well as at compile time.
244    
245     If \fIerrptr\fR is NULL, \fBpcre_compile()\fR returns NULL immediately.
246     Otherwise, if compilation of a pattern fails, \fBpcre_compile()\fR returns
247     NULL, and sets the variable pointed to by \fIerrptr\fR to point to a textual
248     error message. The offset from the start of the pattern to the character where
249     the error was discovered is placed in the variable pointed to by
250     \fIerroffset\fR, which must not be NULL. If it is, an immediate error is given.
251    
252     If the final argument, \fItableptr\fR, is NULL, PCRE uses a default set of
253     character tables which are built when it is compiled, using the default C
254     locale. Otherwise, \fItableptr\fR must be the result of a call to
255     \fBpcre_maketables()\fR. See the section on locale support below.
256    
257     This code fragment shows a typical straightforward call to \fBpcre_compile()\fR:
258    
259     pcre *re;
260     const char *error;
261     int erroffset;
262     re = pcre_compile(
263     "^A.*Z", /* the pattern */
264     0, /* default options */
265     &error, /* for error message */
266     &erroffset, /* for error offset */
267     NULL); /* use default character tables */
268    
269     The following option bits are defined:
270    
271     PCRE_ANCHORED
272    
273     If this bit is set, the pattern is forced to be "anchored", that is, it is
274     constrained to match only at the first matching point in the string which is
275     being searched (the "subject string"). This effect can also be achieved by
276     appropriate constructs in the pattern itself, which is the only way to do it in
277     Perl.
278    
279     PCRE_CASELESS
280    
281     If this bit is set, letters in the pattern match both upper and lower case
282     letters. It is equivalent to Perl's /i option, and it can be changed within a
283     pattern by a (?i) option setting.
284    
285     PCRE_DOLLAR_ENDONLY
286    
287     If this bit is set, a dollar metacharacter in the pattern matches only at the
288     end of the subject string. Without this option, a dollar also matches
289     immediately before the final character if it is a newline (but not before any
290     other newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
291     set. There is no equivalent to this option in Perl, and no way to set it within
292     a pattern.
293    
294     PCRE_DOTALL
295    
296     If this bit is set, a dot metacharater in the pattern matches all characters,
297     including newlines. Without it, newlines are excluded. This option is
298     equivalent to Perl's /s option, and it can be changed within a pattern by a
299     (?s) option setting. A negative class such as [^a] always matches a newline
300     character, independent of the setting of this option.
301    
302     PCRE_EXTENDED
303    
304     If this bit is set, whitespace data characters in the pattern are totally
305     ignored except when escaped or inside a character class. Whitespace does not
306     include the VT character (code 11). In addition, characters between an
307     unescaped # outside a character class and the next newline character,
308     inclusive, are also ignored. This is equivalent to Perl's /x option, and it can
309     be changed within a pattern by a (?x) option setting.
310    
311     This option makes it possible to include comments inside complicated patterns.
312     Note, however, that this applies only to data characters. Whitespace characters
313     may never appear within special character sequences in a pattern, for example
314     within the sequence (?( which introduces a conditional subpattern.
315    
316     PCRE_EXTRA
317    
318     This option was invented in order to turn on additional functionality of PCRE
319     that is incompatible with Perl, but it is currently of very little use. When
320     set, any backslash in a pattern that is followed by a letter that has no
321     special meaning causes an error, thus reserving these combinations for future
322     expansion. By default, as in Perl, a backslash followed by a letter with no
323     special meaning is treated as a literal. There are at present no other features
324     controlled by this option. It can also be set by a (?X) option setting within a
325     pattern.
326    
327     PCRE_MULTILINE
328    
329     By default, PCRE treats the subject string as consisting of a single "line" of
330     characters (even if it actually contains several newlines). The "start of line"
331     metacharacter (^) matches only at the start of the string, while the "end of
332     line" metacharacter ($) matches only at the end of the string, or before a
333     terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
334     Perl.
335    
336     When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
337     match immediately following or immediately before any newline in the subject
338     string, respectively, as well as at the very start and end. This is equivalent
339     to Perl's /m option, and it can be changed within a pattern by a (?m) option
340     setting. If there are no "\\n" characters in a subject string, or no
341     occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no effect.
342    
343     PCRE_NO_AUTO_CAPTURE
344    
345     If this option is set, it disables the use of numbered capturing parentheses in
346     the pattern. Any opening parenthesis that is not followed by ? behaves as if it
347     were followed by ?: but named parentheses can still be used for capturing (and
348     they acquire numbers in the usual way). There is no equivalent of this option
349     in Perl.
350    
351     PCRE_UNGREEDY
352    
353     This option inverts the "greediness" of the quantifiers so that they are not
354     greedy by default, but become greedy if followed by "?". It is not compatible
355     with Perl. It can also be set by a (?U) option setting within the pattern.
356    
357     PCRE_UTF8
358    
359     This option causes PCRE to regard both the pattern and the subject as strings
360     of UTF-8 characters instead of single-byte character strings. However, it is
361     available only if PCRE has been built to include UTF-8 support. If not, the use
362     of this option provokes an error. Details of how this option changes the
363     behaviour of PCRE are given in the
364     .\" HTML <a href="pcre.html#utf8support">
365     .\" </a>
366     section on UTF-8 support
367     .\"
368     in the main
369     .\" HREF
370     \fBpcre\fR
371     .\"
372     page.
373    
374     .SH STUDYING A PATTERN
375     .rs
376     .sp
377     .B pcre_extra *pcre_study(const pcre *\fIcode\fR, int \fIoptions\fR,
378     .ti +5n
379     .B const char **\fIerrptr\fR);
380     .PP
381     When a pattern is going to be used several times, it is worth spending more
382     time analyzing it in order to speed up the time taken for matching. The
383     function \fBpcre_study()\fR takes a pointer to a compiled pattern as its first
384     argument. If studing the pattern produces additional information that will help
385     speed up matching, \fBpcre_study()\fR returns a pointer to a \fBpcre_extra\fR
386     block, in which the \fIstudy_data\fR field points to the results of the study.
387    
388     The returned value from a \fBpcre_study()\fR can be passed directly to
389     \fBpcre_exec()\fR. However, the \fBpcre_extra\fR block also contains other
390     fields that can be set by the caller before the block is passed; these are
391     described below. If studying the pattern does not produce any additional
392     information, \fBpcre_study()\fR returns NULL. In that circumstance, if the
393     calling program wants to pass some of the other fields to \fBpcre_exec()\fR, it
394     must set up its own \fBpcre_extra\fR block.
395    
396     The second argument contains option bits. At present, no options are defined
397     for \fBpcre_study()\fR, and this argument should always be zero.
398    
399     The third argument for \fBpcre_study()\fR is a pointer for an error message. If
400     studying succeeds (even if no data is returned), the variable it points to is
401     set to NULL. Otherwise it points to a textual error message. You should
402     therefore test the error pointer for NULL after calling \fBpcre_study()\fR, to
403     be sure that it has run successfully.
404    
405     This is a typical call to \fBpcre_study\fR():
406    
407     pcre_extra *pe;
408     pe = pcre_study(
409     re, /* result of pcre_compile() */
410     0, /* no options exist */
411     &error); /* set to NULL or points to a message */
412    
413     At present, studying a pattern is useful only for non-anchored patterns that do
414     not have a single fixed starting character. A bitmap of possible starting
415     characters is created.
416    
417     .\" HTML <a name="localesupport"></a>
418     .SH LOCALE SUPPORT
419     .rs
420     .sp
421     PCRE handles caseless matching, and determines whether characters are letters,
422     digits, or whatever, by reference to a set of tables. When running in UTF-8
423     mode, this applies only to characters with codes less than 256. The library
424     contains a default set of tables that is created in the default C locale when
425     PCRE is compiled. This is used when the final argument of \fBpcre_compile()\fR
426     is NULL, and is sufficient for many applications.
427    
428     An alternative set of tables can, however, be supplied. Such tables are built
429     by calling the \fBpcre_maketables()\fR function, which has no arguments, in the
430     relevant locale. The result can then be passed to \fBpcre_compile()\fR as often
431     as necessary. For example, to build and use tables that are appropriate for the
432     French locale (where accented characters with codes greater than 128 are
433     treated as letters), the following code could be used:
434    
435     setlocale(LC_CTYPE, "fr");
436     tables = pcre_maketables();
437     re = pcre_compile(..., tables);
438    
439     The tables are built in memory that is obtained via \fBpcre_malloc\fR. The
440     pointer that is passed to \fBpcre_compile\fR is saved with the compiled
441     pattern, and the same tables are used via this pointer by \fBpcre_study()\fR
442     and \fBpcre_exec()\fR. Thus, for any single pattern, compilation, studying and
443     matching all happen in the same locale, but different patterns can be compiled
444     in different locales. It is the caller's responsibility to ensure that the
445     memory containing the tables remains available for as long as it is needed.
446    
447     .SH INFORMATION ABOUT A PATTERN
448     .rs
449     .sp
450     .B int pcre_fullinfo(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"
451     .ti +5n
452     .B int \fIwhat\fR, void *\fIwhere\fR);
453     .PP
454     The \fBpcre_fullinfo()\fR function returns information about a compiled
455     pattern. It replaces the obsolete \fBpcre_info()\fR function, which is
456     nevertheless retained for backwards compability (and is documented below).
457    
458     The first argument for \fBpcre_fullinfo()\fR is a pointer to the compiled
459     pattern. The second argument is the result of \fBpcre_study()\fR, or NULL if
460     the pattern was not studied. The third argument specifies which piece of
461     information is required, and the fourth argument is a pointer to a variable
462     to receive the data. The yield of the function is zero for success, or one of
463     the following negative numbers:
464    
465     PCRE_ERROR_NULL the argument \fIcode\fR was NULL
466     the argument \fIwhere\fR was NULL
467     PCRE_ERROR_BADMAGIC the "magic number" was not found
468     PCRE_ERROR_BADOPTION the value of \fIwhat\fR was invalid
469    
470     Here is a typical call of \fBpcre_fullinfo()\fR, to obtain the length of the
471     compiled pattern:
472    
473     int rc;
474     unsigned long int length;
475     rc = pcre_fullinfo(
476     re, /* result of pcre_compile() */
477     pe, /* result of pcre_study(), or NULL */
478     PCRE_INFO_SIZE, /* what is required */
479     &length); /* where to put the data */
480    
481     The possible values for the third argument are defined in \fBpcre.h\fR, and are
482     as follows:
483    
484     PCRE_INFO_BACKREFMAX
485    
486     Return the number of the highest back reference in the pattern. The fourth
487     argument should point to an \fBint\fR variable. Zero is returned if there are
488     no back references.
489    
490     PCRE_INFO_CAPTURECOUNT
491    
492     Return the number of capturing subpatterns in the pattern. The fourth argument
493     should point to an \fbint\fR variable.
494    
495     PCRE_INFO_FIRSTBYTE
496    
497     Return information about the first byte of any matched string, for a
498     non-anchored pattern. (This option used to be called PCRE_INFO_FIRSTCHAR; the
499     old name is still recognized for backwards compatibility.)
500    
501     If there is a fixed first byte, e.g. from a pattern such as (cat|cow|coyote),
502     it is returned in the integer pointed to by \fIwhere\fR. Otherwise, if either
503    
504     (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
505     starts with "^", or
506    
507     (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
508     (if it were set, the pattern would be anchored),
509    
510     -1 is returned, indicating that the pattern matches only at the start of a
511     subject string or after any newline within the string. Otherwise -2 is
512     returned. For anchored patterns, -2 is returned.
513    
514     PCRE_INFO_FIRSTTABLE
515    
516     If the pattern was studied, and this resulted in the construction of a 256-bit
517     table indicating a fixed set of bytes for the first byte in any matching
518     string, a pointer to the table is returned. Otherwise NULL is returned. The
519     fourth argument should point to an \fBunsigned char *\fR variable.
520    
521     PCRE_INFO_LASTLITERAL
522    
523 nigel 65 Return the value of the rightmost literal byte that must exist in any matched
524     string, other than at its start, if such a byte has been recorded. The fourth
525     argument should point to an \fBint\fR variable. If there is no such byte, -1 is
526     returned. For anchored patterns, a last literal byte is recorded only if it
527     follows something of variable length. For example, for the pattern
528     /^a\\d+z\\d+/ the returned value is "z", but for /^a\\dz\\d/ the returned value
529     is -1.
530 nigel 63
531     PCRE_INFO_NAMECOUNT
532     PCRE_INFO_NAMEENTRYSIZE
533     PCRE_INFO_NAMETABLE
534    
535     PCRE supports the use of named as well as numbered capturing parentheses. The
536     names are just an additional way of identifying the parentheses, which still
537     acquire a number. A caller that wants to extract data from a named subpattern
538     must convert the name to a number in order to access the correct pointers in
539     the output vector (described with \fBpcre_exec()\fR below). In order to do
540     this, it must first use these three values to obtain the name-to-number mapping
541     table for the pattern.
542    
543     The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT gives
544     the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size of each
545     entry; both of these return an \fBint\fR value. The entry size depends on the
546     length of the longest name. PCRE_INFO_NAMETABLE returns a pointer to the first
547     entry of the table (a pointer to \fBchar\fR). The first two bytes of each entry
548     are the number of the capturing parenthesis, most significant byte first. The
549     rest of the entry is the corresponding name, zero terminated. The names are in
550     alphabetical order. For example, consider the following pattern (assume
551     PCRE_EXTENDED is set, so white space - including newlines - is ignored):
552    
553     (?P<date> (?P<year>(\\d\\d)?\\d\\d) -
554     (?P<month>\\d\\d) - (?P<day>\\d\\d) )
555    
556     There are four named subpatterns, so the table has four entries, and each entry
557     in the table is eight bytes long. The table is as follows, with non-printing
558     bytes shows in hex, and undefined bytes shown as ??:
559    
560     00 01 d a t e 00 ??
561     00 05 d a y 00 ?? ??
562     00 04 m o n t h 00
563     00 02 y e a r 00 ??
564    
565     When writing code to extract data from named subpatterns, remember that the
566     length of each entry may be different for each compiled pattern.
567    
568     PCRE_INFO_OPTIONS
569    
570     Return a copy of the options with which the pattern was compiled. The fourth
571     argument should point to an \fBunsigned long int\fR variable. These option bits
572     are those specified in the call to \fBpcre_compile()\fR, modified by any
573     top-level option settings within the pattern itself.
574    
575     A pattern is automatically anchored by PCRE if all of its top-level
576     alternatives begin with one of the following:
577    
578     ^ unless PCRE_MULTILINE is set
579     \\A always
580     \\G always
581     .* if PCRE_DOTALL is set and there are no back
582     references to the subpattern in which .* appears
583    
584     For such patterns, the PCRE_ANCHORED bit is set in the options returned by
585     \fBpcre_fullinfo()\fR.
586    
587     PCRE_INFO_SIZE
588    
589     Return the size of the compiled pattern, that is, the value that was passed as
590     the argument to \fBpcre_malloc()\fR when PCRE was getting memory in which to
591     place the compiled data. The fourth argument should point to a \fBsize_t\fR
592     variable.
593    
594     PCRE_INFO_STUDYSIZE
595    
596     Returns the size of the data block pointed to by the \fIstudy_data\fR field in
597     a \fBpcre_extra\fR block. That is, it is the value that was passed to
598     \fBpcre_malloc()\fR when PCRE was getting memory into which to place the data
599     created by \fBpcre_study()\fR. The fourth argument should point to a
600     \fBsize_t\fR variable.
601    
602     .SH OBSOLETE INFO FUNCTION
603     .rs
604     .sp
605     .B int pcre_info(const pcre *\fIcode\fR, int *\fIoptptr\fR, int
606     .B *\fIfirstcharptr\fR);
607     .PP
608     The \fBpcre_info()\fR function is now obsolete because its interface is too
609     restrictive to return all the available data about a compiled pattern. New
610     programs should use \fBpcre_fullinfo()\fR instead. The yield of
611     \fBpcre_info()\fR is the number of capturing subpatterns, or one of the
612     following negative numbers:
613    
614     PCRE_ERROR_NULL the argument \fIcode\fR was NULL
615     PCRE_ERROR_BADMAGIC the "magic number" was not found
616    
617     If the \fIoptptr\fR argument is not NULL, a copy of the options with which the
618     pattern was compiled is placed in the integer it points to (see
619     PCRE_INFO_OPTIONS above).
620    
621     If the pattern is not anchored and the \fIfirstcharptr\fR argument is not NULL,
622     it is used to pass back information about the first character of any matched
623     string (see PCRE_INFO_FIRSTBYTE above).
624    
625     .SH MATCHING A PATTERN
626     .rs
627     .sp
628     .B int pcre_exec(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"
629     .ti +5n
630     .B "const char *\fIsubject\fR," int \fIlength\fR, int \fIstartoffset\fR,
631     .ti +5n
632     .B int \fIoptions\fR, int *\fIovector\fR, int \fIovecsize\fR);
633     .PP
634     The function \fBpcre_exec()\fR is called to match a subject string against a
635     pre-compiled pattern, which is passed in the \fIcode\fR argument. If the
636     pattern has been studied, the result of the study should be passed in the
637     \fIextra\fR argument.
638    
639     Here is an example of a simple call to \fBpcre_exec()\fR:
640    
641     int rc;
642     int ovector[30];
643     rc = pcre_exec(
644     re, /* result of pcre_compile() */
645     NULL, /* we didn't study the pattern */
646     "some string", /* the subject string */
647     11, /* the length of the subject string */
648     0, /* start at offset 0 in the subject */
649     0, /* default options */
650     ovector, /* vector for substring information */
651     30); /* number of elements in the vector */
652    
653     If the \fIextra\fR argument is not NULL, it must point to a \fBpcre_extra\fR
654     data block. The \fBpcre_study()\fR function returns such a block (when it
655     doesn't return NULL), but you can also create one for yourself, and pass
656     additional information in it. The fields in the block are as follows:
657    
658     unsigned long int \fIflags\fR;
659     void *\fIstudy_data\fR;
660     unsigned long int \fImatch_limit\fR;
661     void *\fIcallout_data\fR;
662    
663     The \fIflags\fR field is a bitmap that specifies which of the other fields
664     are set. The flag bits are:
665    
666     PCRE_EXTRA_STUDY_DATA
667     PCRE_EXTRA_MATCH_LIMIT
668     PCRE_EXTRA_CALLOUT_DATA
669    
670     Other flag bits should be set to zero. The \fIstudy_data\fR field is set in the
671     \fBpcre_extra\fR block that is returned by \fBpcre_study()\fR, together with
672     the appropriate flag bit. You should not set this yourself, but you can add to
673     the block by setting the other fields.
674    
675     The \fImatch_limit\fR field provides a means of preventing PCRE from using up a
676     vast amount of resources when running patterns that are not going to match,
677     but which have a very large number of possibilities in their search trees. The
678     classic example is the use of nested unlimited repeats. Internally, PCRE uses a
679     function called \fBmatch()\fR which it calls repeatedly (sometimes
680     recursively). The limit is imposed on the number of times this function is
681     called during a match, which has the effect of limiting the amount of recursion
682     and backtracking that can take place. For patterns that are not anchored, the
683     count starts from zero for each position in the subject string.
684    
685     The default limit for the library can be set when PCRE is built; the default
686     default is 10 million, which handles all but the most extreme cases. You can
687     reduce the default by suppling \fBpcre_exec()\fR with a \fRpcre_extra\fR block
688     in which \fImatch_limit\fR is set to a smaller value, and
689     PCRE_EXTRA_MATCH_LIMIT is set in the \fIflags\fR field. If the limit is
690     exceeded, \fBpcre_exec()\fR returns PCRE_ERROR_MATCHLIMIT.
691    
692     The \fIpcre_callout\fR field is used in conjunction with the "callout" feature,
693     which is described in the \fBpcrecallout\fR documentation.
694    
695     The PCRE_ANCHORED option can be passed in the \fIoptions\fR argument, whose
696     unused bits must be zero. This limits \fBpcre_exec()\fR to matching at the
697     first matching position. However, if a pattern was compiled with PCRE_ANCHORED,
698     or turned out to be anchored by virtue of its contents, it cannot be made
699     unachored at matching time.
700    
701     There are also three further options that can be set only at matching time:
702    
703     PCRE_NOTBOL
704    
705     The first character of the string is not the beginning of a line, so the
706     circumflex metacharacter should not match before it. Setting this without
707     PCRE_MULTILINE (at compile time) causes circumflex never to match.
708    
709     PCRE_NOTEOL
710    
711     The end of the string is not the end of a line, so the dollar metacharacter
712     should not match it nor (except in multiline mode) a newline immediately before
713     it. Setting this without PCRE_MULTILINE (at compile time) causes dollar never
714     to match.
715    
716     PCRE_NOTEMPTY
717    
718     An empty string is not considered to be a valid match if this option is set. If
719     there are alternatives in the pattern, they are tried. If all the alternatives
720     match the empty string, the entire match fails. For example, if the pattern
721    
722     a?b?
723    
724     is applied to a string not beginning with "a" or "b", it matches the empty
725     string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
726     valid, so PCRE searches further into the string for occurrences of "a" or "b".
727    
728     Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
729     of a pattern match of the empty string within its \fBsplit()\fR function, and
730     when using the /g modifier. It is possible to emulate Perl's behaviour after
731     matching a null string by first trying the match again at the same offset with
732     PCRE_NOTEMPTY set, and then if that fails by advancing the starting offset (see
733     below) and trying an ordinary match again.
734    
735     The subject string is passed to \fBpcre_exec()\fR as a pointer in
736     \fIsubject\fR, a length in \fIlength\fR, and a starting offset in
737     \fIstartoffset\fR. Unlike the pattern string, the subject may contain binary
738     zero bytes. When the starting offset is zero, the search for a match starts at
739     the beginning of the subject, and this is by far the most common case.
740    
741     If the pattern was compiled with the PCRE_UTF8 option, the subject must be a
742     sequence of bytes that is a valid UTF-8 string. If an invalid UTF-8 string is
743     passed, PCRE's behaviour is not defined.
744    
745     A non-zero starting offset is useful when searching for another match in the
746     same subject by calling \fBpcre_exec()\fR again after a previous success.
747     Setting \fIstartoffset\fR differs from just passing over a shortened string and
748     setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
749     lookbehind. For example, consider the pattern
750    
751     \\Biss\\B
752    
753     which finds occurrences of "iss" in the middle of words. (\\B matches only if
754     the current position in the subject is not a word boundary.) When applied to
755     the string "Mississipi" the first call to \fBpcre_exec()\fR finds the first
756     occurrence. If \fBpcre_exec()\fR is called again with just the remainder of the
757     subject, namely "issipi", it does not match, because \\B is always false at the
758     start of the subject, which is deemed to be a word boundary. However, if
759     \fBpcre_exec()\fR is passed the entire string again, but with \fIstartoffset\fR
760     set to 4, it finds the second occurrence of "iss" because it is able to look
761     behind the starting point to discover that it is preceded by a letter.
762    
763     If a non-zero starting offset is passed when the pattern is anchored, one
764     attempt to match at the given offset is tried. This can only succeed if the
765     pattern does not require the match to be at the start of the subject.
766    
767     In general, a pattern matches a certain portion of the subject, and in
768     addition, further substrings from the subject may be picked out by parts of the
769     pattern. Following the usage in Jeffrey Friedl's book, this is called
770     "capturing" in what follows, and the phrase "capturing subpattern" is used for
771     a fragment of a pattern that picks out a substring. PCRE supports several other
772     kinds of parenthesized subpattern that do not cause substrings to be captured.
773    
774     Captured substrings are returned to the caller via a vector of integer offsets
775     whose address is passed in \fIovector\fR. The number of elements in the vector
776     is passed in \fIovecsize\fR. The first two-thirds of the vector is used to pass
777     back captured substrings, each substring using a pair of integers. The
778     remaining third of the vector is used as workspace by \fBpcre_exec()\fR while
779     matching capturing subpatterns, and is not available for passing back
780     information. The length passed in \fIovecsize\fR should always be a multiple of
781     three. If it is not, it is rounded down.
782    
783     When a match has been successful, information about captured substrings is
784     returned in pairs of integers, starting at the beginning of \fIovector\fR, and
785     continuing up to two-thirds of its length at the most. The first element of a
786     pair is set to the offset of the first character in a substring, and the second
787     is set to the offset of the first character after the end of a substring. The
788     first pair, \fIovector[0]\fR and \fIovector[1]\fR, identify the portion of the
789     subject string matched by the entire pattern. The next pair is used for the
790     first capturing subpattern, and so on. The value returned by \fBpcre_exec()\fR
791     is the number of pairs that have been set. If there are no capturing
792     subpatterns, the return value from a successful match is 1, indicating that
793     just the first pair of offsets has been set.
794    
795     Some convenience functions are provided for extracting the captured substrings
796     as separate strings. These are described in the following section.
797    
798     It is possible for an capturing subpattern number \fIn+1\fR to match some
799     part of the subject when subpattern \fIn\fR has not been used at all. For
800     example, if the string "abc" is matched against the pattern (a|(z))(bc)
801     subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
802     values corresponding to the unused subpattern are set to -1.
803    
804     If a capturing subpattern is matched repeatedly, it is the last portion of the
805     string that it matched that gets returned.
806    
807     If the vector is too small to hold all the captured substrings, it is used as
808     far as possible (up to two-thirds of its length), and the function returns a
809     value of zero. In particular, if the substring offsets are not of interest,
810     \fBpcre_exec()\fR may be called with \fIovector\fR passed as NULL and
811     \fIovecsize\fR as zero. However, if the pattern contains back references and
812     the \fIovector\fR isn't big enough to remember the related substrings, PCRE has
813     to get additional memory for use during matching. Thus it is usually advisable
814     to supply an \fIovector\fR.
815    
816     Note that \fBpcre_info()\fR can be used to find out how many capturing
817     subpatterns there are in a compiled pattern. The smallest size for
818     \fIovector\fR that will allow for \fIn\fR captured substrings, in addition to
819     the offsets of the substring matched by the whole pattern, is (\fIn\fR+1)*3.
820    
821     If \fBpcre_exec()\fR fails, it returns a negative number. The following are
822     defined in the header file:
823    
824     PCRE_ERROR_NOMATCH (-1)
825    
826     The subject string did not match the pattern.
827    
828     PCRE_ERROR_NULL (-2)
829    
830     Either \fIcode\fR or \fIsubject\fR was passed as NULL, or \fIovector\fR was
831     NULL and \fIovecsize\fR was not zero.
832    
833     PCRE_ERROR_BADOPTION (-3)
834    
835     An unrecognized bit was set in the \fIoptions\fR argument.
836    
837     PCRE_ERROR_BADMAGIC (-4)
838    
839     PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
840     the case when it is passed a junk pointer. This is the error it gives when the
841     magic number isn't present.
842    
843     PCRE_ERROR_UNKNOWN_NODE (-5)
844    
845     While running the pattern match, an unknown item was encountered in the
846     compiled pattern. This error could be caused by a bug in PCRE or by overwriting
847     of the compiled pattern.
848    
849     PCRE_ERROR_NOMEMORY (-6)
850    
851     If a pattern contains back references, but the \fIovector\fR that is passed to
852     \fBpcre_exec()\fR is not big enough to remember the referenced substrings, PCRE
853     gets a block of memory at the start of matching to use for this purpose. If the
854     call via \fBpcre_malloc()\fR fails, this error is given. The memory is freed at
855     the end of matching.
856    
857     PCRE_ERROR_NOSUBSTRING (-7)
858    
859     This error is used by the \fBpcre_copy_substring()\fR,
860     \fBpcre_get_substring()\fR, and \fBpcre_get_substring_list()\fR functions (see
861     below). It is never returned by \fBpcre_exec()\fR.
862    
863     PCRE_ERROR_MATCHLIMIT (-8)
864    
865     The recursion and backtracking limit, as specified by the \fImatch_limit\fR
866     field in a \fBpcre_extra\fR structure (or defaulted) was reached. See the
867     description above.
868    
869     PCRE_ERROR_CALLOUT (-9)
870    
871     This error is never generated by \fBpcre_exec()\fR itself. It is provided for
872     use by callout functions that want to yield a distinctive error code. See the
873     \fBpcrecallout\fR documentation for details.
874    
875     .SH EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
876     .rs
877     .sp
878     .B int pcre_copy_substring(const char *\fIsubject\fR, int *\fIovector\fR,
879     .ti +5n
880     .B int \fIstringcount\fR, int \fIstringnumber\fR, char *\fIbuffer\fR,
881     .ti +5n
882     .B int \fIbuffersize\fR);
883     .PP
884     .br
885     .B int pcre_get_substring(const char *\fIsubject\fR, int *\fIovector\fR,
886     .ti +5n
887     .B int \fIstringcount\fR, int \fIstringnumber\fR,
888     .ti +5n
889     .B const char **\fIstringptr\fR);
890     .PP
891     .br
892     .B int pcre_get_substring_list(const char *\fIsubject\fR,
893     .ti +5n
894     .B int *\fIovector\fR, int \fIstringcount\fR, "const char ***\fIlistptr\fR);"
895     .PP
896     Captured substrings can be accessed directly by using the offsets returned by
897     \fBpcre_exec()\fR in \fIovector\fR. For convenience, the functions
898     \fBpcre_copy_substring()\fR, \fBpcre_get_substring()\fR, and
899     \fBpcre_get_substring_list()\fR are provided for extracting captured substrings
900     as new, separate, zero-terminated strings. These functions identify substrings
901     by number. The next section describes functions for extracting named
902     substrings. A substring that contains a binary zero is correctly extracted and
903     has a further zero added on the end, but the result is not, of course,
904     a C string.
905    
906     The first three arguments are the same for all three of these functions:
907     \fIsubject\fR is the subject string which has just been successfully matched,
908     \fIovector\fR is a pointer to the vector of integer offsets that was passed to
909     \fBpcre_exec()\fR, and \fIstringcount\fR is the number of substrings that were
910     captured by the match, including the substring that matched the entire regular
911     expression. This is the value returned by \fBpcre_exec\fR if it is greater than
912     zero. If \fBpcre_exec()\fR returned zero, indicating that it ran out of space
913     in \fIovector\fR, the value passed as \fIstringcount\fR should be the size of
914     the vector divided by three.
915    
916     The functions \fBpcre_copy_substring()\fR and \fBpcre_get_substring()\fR
917     extract a single substring, whose number is given as \fIstringnumber\fR. A
918     value of zero extracts the substring that matched the entire pattern, while
919     higher values extract the captured substrings. For \fBpcre_copy_substring()\fR,
920     the string is placed in \fIbuffer\fR, whose length is given by
921     \fIbuffersize\fR, while for \fBpcre_get_substring()\fR a new block of memory is
922     obtained via \fBpcre_malloc\fR, and its address is returned via
923     \fIstringptr\fR. The yield of the function is the length of the string, not
924     including the terminating zero, or one of
925    
926     PCRE_ERROR_NOMEMORY (-6)
927    
928     The buffer was too small for \fBpcre_copy_substring()\fR, or the attempt to get
929     memory failed for \fBpcre_get_substring()\fR.
930    
931     PCRE_ERROR_NOSUBSTRING (-7)
932    
933     There is no substring whose number is \fIstringnumber\fR.
934    
935     The \fBpcre_get_substring_list()\fR function extracts all available substrings
936     and builds a list of pointers to them. All this is done in a single block of
937     memory which is obtained via \fBpcre_malloc\fR. The address of the memory block
938     is returned via \fIlistptr\fR, which is also the start of the list of string
939     pointers. The end of the list is marked by a NULL pointer. The yield of the
940     function is zero if all went well, or
941    
942     PCRE_ERROR_NOMEMORY (-6)
943    
944     if the attempt to get the memory block failed.
945    
946     When any of these functions encounter a substring that is unset, which can
947     happen when capturing subpattern number \fIn+1\fR matches some part of the
948     subject, but subpattern \fIn\fR has not been used at all, they return an empty
949     string. This can be distinguished from a genuine zero-length substring by
950     inspecting the appropriate offset in \fIovector\fR, which is negative for unset
951     substrings.
952    
953     The two convenience functions \fBpcre_free_substring()\fR and
954     \fBpcre_free_substring_list()\fR can be used to free the memory returned by
955     a previous call of \fBpcre_get_substring()\fR or
956     \fBpcre_get_substring_list()\fR, respectively. They do nothing more than call
957     the function pointed to by \fBpcre_free\fR, which of course could be called
958     directly from a C program. However, PCRE is used in some situations where it is
959     linked via a special interface to another programming language which cannot use
960     \fBpcre_free\fR directly; it is for these cases that the functions are
961     provided.
962    
963     .SH EXTRACTING CAPTURED SUBSTRINGS BY NAME
964     .rs
965     .sp
966     .B int pcre_copy_named_substring(const pcre *\fIcode\fR,
967     .ti +5n
968     .B const char *\fIsubject\fR, int *\fIovector\fR,
969     .ti +5n
970     .B int \fIstringcount\fR, const char *\fIstringname\fR,
971     .ti +5n
972     .B char *\fIbuffer\fR, int \fIbuffersize\fR);
973     .PP
974     .br
975     .B int pcre_get_stringnumber(const pcre *\fIcode\fR,
976     .ti +5n
977     .B const char *\fIname\fR);
978     .PP
979     .br
980     .B int pcre_get_named_substring(const pcre *\fIcode\fR,
981     .ti +5n
982     .B const char *\fIsubject\fR, int *\fIovector\fR,
983     .ti +5n
984     .B int \fIstringcount\fR, const char *\fIstringname\fR,
985     .ti +5n
986     .B const char **\fIstringptr\fR);
987     .PP
988     To extract a substring by name, you first have to find associated number. This
989     can be done by calling \fBpcre_get_stringnumber()\fR. The first argument is the
990     compiled pattern, and the second is the name. For example, for this pattern
991    
992     ab(?<xxx>\\d+)...
993    
994     the number of the subpattern called "xxx" is 1. Given the number, you can then
995     extract the substring directly, or use one of the functions described in the
996     previous section. For convenience, there are also two functions that do the
997     whole job.
998    
999     Most of the arguments of \fIpcre_copy_named_substring()\fR and
1000     \fIpcre_get_named_substring()\fR are the same as those for the functions that
1001     extract by number, and so are not re-described here. There are just two
1002     differences.
1003    
1004     First, instead of a substring number, a substring name is given. Second, there
1005     is an extra argument, given at the start, which is a pointer to the compiled
1006     pattern. This is needed in order to gain access to the name-to-number
1007     translation table.
1008    
1009     These functions call \fBpcre_get_stringnumber()\fR, and if it succeeds, they
1010     then call \fIpcre_copy_substring()\fR or \fIpcre_get_substring()\fR, as
1011     appropriate.
1012    
1013     .in 0
1014     Last updated: 03 February 2003
1015     .br
1016     Copyright (c) 1997-2003 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12