ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 49 - (hide annotations) (download)
Sat Feb 24 21:39:33 2007 UTC (8 years, 1 month ago) by nigel
File size: 83844 byte(s)
Load pcre-3.3 into code/trunk.

1 nigel 41 .TH PCRE 3
2     .SH NAME
3     pcre - Perl-compatible regular expressions.
5     .B #include <pcre.h>
6     .PP
7     .SM
8     .br
9     .B pcre *pcre_compile(const char *\fIpattern\fR, int \fIoptions\fR,
10     .ti +5n
11     .B const char **\fIerrptr\fR, int *\fIerroffset\fR,
12     .ti +5n
13     .B const unsigned char *\fItableptr\fR);
14     .PP
15     .br
16     .B pcre_extra *pcre_study(const pcre *\fIcode\fR, int \fIoptions\fR,
17     .ti +5n
18     .B const char **\fIerrptr\fR);
19     .PP
20     .br
21     .B int pcre_exec(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"
22     .ti +5n
23     .B "const char *\fIsubject\fR," int \fIlength\fR, int \fIstartoffset\fR,
24     .ti +5n
25     .B int \fIoptions\fR, int *\fIovector\fR, int \fIovecsize\fR);
26     .PP
27     .br
28     .B int pcre_copy_substring(const char *\fIsubject\fR, int *\fIovector\fR,
29     .ti +5n
30     .B int \fIstringcount\fR, int \fIstringnumber\fR, char *\fIbuffer\fR,
31     .ti +5n
32     .B int \fIbuffersize\fR);
33     .PP
34     .br
35     .B int pcre_get_substring(const char *\fIsubject\fR, int *\fIovector\fR,
36     .ti +5n
37     .B int \fIstringcount\fR, int \fIstringnumber\fR,
38     .ti +5n
39     .B const char **\fIstringptr\fR);
40     .PP
41     .br
42     .B int pcre_get_substring_list(const char *\fIsubject\fR,
43     .ti +5n
44     .B int *\fIovector\fR, int \fIstringcount\fR, "const char ***\fIlistptr\fR);"
45     .PP
46     .br
47 nigel 49 .B void pcre_free_substring(const char *\fIstringptr\fR);
48     .PP
49     .br
50     .B void pcre_free_substring_list(const char **\fIstringptr\fR);
51     .PP
52     .br
53 nigel 41 .B const unsigned char *pcre_maketables(void);
54     .PP
55     .br
56 nigel 43 .B int pcre_fullinfo(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"
57     .ti +5n
58     .B int \fIwhat\fR, void *\fIwhere\fR);
59     .PP
60     .br
61 nigel 41 .B int pcre_info(const pcre *\fIcode\fR, int *\fIoptptr\fR, int
62     .B *\fIfirstcharptr\fR);
63     .PP
64     .br
65     .B char *pcre_version(void);
66     .PP
67     .br
68     .B void *(*pcre_malloc)(size_t);
69     .PP
70     .br
71     .B void (*pcre_free)(void *);
76     The PCRE library is a set of functions that implement regular expression
77     pattern matching using the same syntax and semantics as Perl 5, with just a few
78 nigel 43 differences (see below). The current implementation corresponds to Perl 5.005,
79 nigel 49 with some additional features from later versions. This includes some
80     experimental, incomplete support for UTF-8 encoded strings. Details of exactly
81     what is and what is not supported are given below.
82 nigel 41
83     PCRE has its own native API, which is described in this document. There is also
84 nigel 43 a set of wrapper functions that correspond to the POSIX regular expression API.
85     These are described in the \fBpcreposix\fR documentation.
86 nigel 41
87     The native API function prototypes are defined in the header file \fBpcre.h\fR,
88     and on Unix systems the library itself is called \fBlibpcre.a\fR, so can be
89     accessed by adding \fB-lpcre\fR to the command for linking an application which
90 nigel 43 calls it. The header file defines the macros PCRE_MAJOR and PCRE_MINOR to
91     contain the major and minor release numbers for the library. Applications can
92     use these to include support for different releases.
93 nigel 41
94     The functions \fBpcre_compile()\fR, \fBpcre_study()\fR, and \fBpcre_exec()\fR
95 nigel 49 are used for compiling and matching regular expressions.
97     The functions \fBpcre_copy_substring()\fR, \fBpcre_get_substring()\fR, and
98 nigel 41 \fBpcre_get_substring_list()\fR are convenience functions for extracting
99 nigel 49 captured substrings from a matched subject string; \fBpcre_free_substring()\fR
100     and \fBpcre_free_substring_list()\fR are also provided, to free the memory used
101     for extracted strings.
102 nigel 41
103 nigel 49 The function \fBpcre_maketables()\fR is used (optionally) to build a set of
104     character tables in the current locale for passing to \fBpcre_compile()\fR.
106 nigel 43 The function \fBpcre_fullinfo()\fR is used to find out information about a
107     compiled pattern; \fBpcre_info()\fR is an obsolete version which returns only
108     some of the available information, but is retained for backwards compatibility.
109     The function \fBpcre_version()\fR returns a pointer to a string containing the
110     version of PCRE and its date of release.
111 nigel 41
112     The global variables \fBpcre_malloc\fR and \fBpcre_free\fR initially contain
113     the entry points of the standard \fBmalloc()\fR and \fBfree()\fR functions
114     respectively. PCRE calls the memory management functions via these variables,
115     so a calling program can replace them if it wishes to intercept the calls. This
116     should be done before calling any PCRE functions.
120     The PCRE functions can be used in multi-threading applications, with the
121     proviso that the memory management functions pointed to by \fBpcre_malloc\fR
122     and \fBpcre_free\fR are shared by all threads.
124     The compiled form of a regular expression is not altered during matching, so
125     the same compiled pattern can safely be used by several threads at once.
129     The function \fBpcre_compile()\fR is called to compile a pattern into an
130     internal form. The pattern is a C string terminated by a binary zero, and
131     is passed in the argument \fIpattern\fR. A pointer to a single block of memory
132     that is obtained via \fBpcre_malloc\fR is returned. This contains the
133     compiled code and related data. The \fBpcre\fR type is defined for this for
134     convenience, but in fact \fBpcre\fR is just a typedef for \fBvoid\fR, since the
135     contents of the block are not externally defined. It is up to the caller to
136     free the memory when it is no longer required.
137     .PP
138     The size of a compiled pattern is roughly proportional to the length of the
139     pattern string, except that each character class (other than those containing
140     just a single character, negated or not) requires 33 bytes, and repeat
141     quantifiers with a minimum greater than one or a bounded maximum cause the
142     relevant portions of the compiled pattern to be replicated.
143     .PP
144     The \fIoptions\fR argument contains independent bits that affect the
145     compilation. It should be zero if no options are required. Some of the options,
146     in particular, those that are compatible with Perl, can also be set and unset
147     from within the pattern (see the detailed description of regular expressions
148     below). For these options, the contents of the \fIoptions\fR argument specifies
149     their initial settings at the start of compilation and execution. The
150     PCRE_ANCHORED option can be set at the time of matching as well as at compile
151     time.
152     .PP
153     If \fIerrptr\fR is NULL, \fBpcre_compile()\fR returns NULL immediately.
154     Otherwise, if compilation of a pattern fails, \fBpcre_compile()\fR returns
155     NULL, and sets the variable pointed to by \fIerrptr\fR to point to a textual
156     error message. The offset from the start of the pattern to the character where
157     the error was discovered is placed in the variable pointed to by
158     \fIerroffset\fR, which must not be NULL. If it is, an immediate error is given.
159     .PP
160     If the final argument, \fItableptr\fR, is NULL, PCRE uses a default set of
161     character tables which are built when it is compiled, using the default C
162     locale. Otherwise, \fItableptr\fR must be the result of a call to
163     \fBpcre_maketables()\fR. See the section on locale support below.
164     .PP
165     The following option bits are defined in the header file:
169     If this bit is set, the pattern is forced to be "anchored", that is, it is
170     constrained to match only at the start of the string which is being searched
171     (the "subject string"). This effect can also be achieved by appropriate
172     constructs in the pattern itself, which is the only way to do it in Perl.
176     If this bit is set, letters in the pattern match both upper and lower case
177     letters. It is equivalent to Perl's /i option.
181     If this bit is set, a dollar metacharacter in the pattern matches only at the
182     end of the subject string. Without this option, a dollar also matches
183     immediately before the final character if it is a newline (but not before any
184     other newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
185     set. There is no equivalent to this option in Perl.
189     If this bit is set, a dot metacharater in the pattern matches all characters,
190     including newlines. Without it, newlines are excluded. This option is
191     equivalent to Perl's /s option. A negative class such as [^a] always matches a
192     newline character, independent of the setting of this option.
196     If this bit is set, whitespace data characters in the pattern are totally
197     ignored except when escaped or inside a character class, and characters between
198     an unescaped # outside a character class and the next newline character,
199     inclusive, are also ignored. This is equivalent to Perl's /x option, and makes
200     it possible to include comments inside complicated patterns. Note, however,
201     that this applies only to data characters. Whitespace characters may never
202     appear within special character sequences in a pattern, for example within the
203     sequence (?( which introduces a conditional subpattern.
205     PCRE_EXTRA
207 nigel 43 This option was invented in order to turn on additional functionality of PCRE
208     that is incompatible with Perl, but it is currently of very little use. When
209     set, any backslash in a pattern that is followed by a letter that has no
210 nigel 41 special meaning causes an error, thus reserving these combinations for future
211     expansion. By default, as in Perl, a backslash followed by a letter with no
212     special meaning is treated as a literal. There are at present no other features
213 nigel 43 controlled by this option. It can also be set by a (?X) option setting within a
214     pattern.
215 nigel 41
218     By default, PCRE treats the subject string as consisting of a single "line" of
219     characters (even if it actually contains several newlines). The "start of line"
220     metacharacter (^) matches only at the start of the string, while the "end of
221     line" metacharacter ($) matches only at the end of the string, or before a
222     terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
223     Perl.
225     When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
226     match immediately following or immediately before any newline in the subject
227     string, respectively, as well as at the very start and end. This is equivalent
228     to Perl's /m option. If there are no "\\n" characters in a subject string, or
229     no occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no
230     effect.
234     This option inverts the "greediness" of the quantifiers so that they are not
235     greedy by default, but become greedy if followed by "?". It is not compatible
236     with Perl. It can also be set by a (?U) option setting within the pattern.
238 nigel 49 PCRE_UTF8
239 nigel 41
240 nigel 49 This option causes PCRE to regard both the pattern and the subject as strings
241     of UTF-8 characters instead of just byte strings. However, it is available only
242     if PCRE has been built to include UTF-8 support. If not, the use of this option
243     provokes an error. Support for UTF-8 is new, experimental, and incomplete.
244     Details of exactly what it entails are given below.
248     When a pattern is going to be used several times, it is worth spending more
249     time analyzing it in order to speed up the time taken for matching. The
250     function \fBpcre_study()\fR takes a pointer to a compiled pattern as its first
251     argument, and returns a pointer to a \fBpcre_extra\fR block (another \fBvoid\fR
252     typedef) containing additional information about the pattern; this can be
253     passed to \fBpcre_exec()\fR. If no additional information is available, NULL
254     is returned.
256     The second argument contains option bits. At present, no options are defined
257     for \fBpcre_study()\fR, and this argument should always be zero.
259     The third argument for \fBpcre_study()\fR is a pointer to an error message. If
260     studying succeeds (even if no data is returned), the variable it points to is
261     set to NULL. Otherwise it points to a textual error message.
263     At present, studying a pattern is useful only for non-anchored patterns that do
264     not have a single fixed starting character. A bitmap of possible starting
265     characters is created.
269     PCRE handles caseless matching, and determines whether characters are letters,
270     digits, or whatever, by reference to a set of tables. The library contains a
271     default set of tables which is created in the default C locale when PCRE is
272     compiled. This is used when the final argument of \fBpcre_compile()\fR is NULL,
273     and is sufficient for many applications.
275     An alternative set of tables can, however, be supplied. Such tables are built
276     by calling the \fBpcre_maketables()\fR function, which has no arguments, in the
277     relevant locale. The result can then be passed to \fBpcre_compile()\fR as often
278     as necessary. For example, to build and use tables that are appropriate for the
279     French locale (where accented characters with codes greater than 128 are
280     treated as letters), the following code could be used:
282     setlocale(LC_CTYPE, "fr");
283     tables = pcre_maketables();
284     re = pcre_compile(..., tables);
286     The tables are built in memory that is obtained via \fBpcre_malloc\fR. The
287     pointer that is passed to \fBpcre_compile\fR is saved with the compiled
288     pattern, and the same tables are used via this pointer by \fBpcre_study()\fR
289     and \fBpcre_exec()\fR. Thus for any single pattern, compilation, studying and
290     matching all happen in the same locale, but different patterns can be compiled
291     in different locales. It is the caller's responsibility to ensure that the
292     memory containing the tables remains available for as long as it is needed.
296 nigel 43 The \fBpcre_fullinfo()\fR function returns information about a compiled
297     pattern. It replaces the obsolete \fBpcre_info()\fR function, which is
298     nevertheless retained for backwards compability (and is documented below).
299 nigel 41
300 nigel 43 The first argument for \fBpcre_fullinfo()\fR is a pointer to the compiled
301     pattern. The second argument is the result of \fBpcre_study()\fR, or NULL if
302     the pattern was not studied. The third argument specifies which piece of
303     information is required, while the fourth argument is a pointer to a variable
304     to receive the data. The yield of the function is zero for success, or one of
305     the following negative numbers:
307 nigel 41 PCRE_ERROR_NULL the argument \fIcode\fR was NULL
308 nigel 43 the argument \fIwhere\fR was NULL
309 nigel 41 PCRE_ERROR_BADMAGIC the "magic number" was not found
310 nigel 43 PCRE_ERROR_BADOPTION the value of \fIwhat\fR was invalid
311 nigel 41
312 nigel 43 The possible values for the third argument are defined in \fBpcre.h\fR, and are
313     as follows:
317     Return a copy of the options with which the pattern was compiled. The fourth
318     argument should point to au \fBunsigned long int\fR variable. These option bits
319 nigel 41 are those specified in the call to \fBpcre_compile()\fR, modified by any
320     top-level option settings within the pattern itself, and with the PCRE_ANCHORED
321 nigel 43 bit forcibly set if the form of the pattern implies that it can match only at
322     the start of a subject string.
323 nigel 41
324 nigel 43 PCRE_INFO_SIZE
325 nigel 41
326 nigel 43 Return the size of the compiled pattern, that is, the value that was passed as
327     the argument to \fBpcre_malloc()\fR when PCRE was getting memory in which to
328     place the compiled data. The fourth argument should point to a \fBsize_t\fR
329     variable.
333     Return the number of capturing subpatterns in the pattern. The fourth argument
334     should point to an \fbint\fR variable.
338     Return the number of the highest back reference in the pattern. The fourth
339     argument should point to an \fBint\fR variable. Zero is returned if there are
340     no back references.
344     Return information about the first character of any matched string, for a
345     non-anchored pattern. If there is a fixed first character, e.g. from a pattern
346 nigel 47 such as (cat|cow|coyote), it is returned in the integer pointed to by
347 nigel 43 \fIwhere\fR. Otherwise, if either
349 nigel 41 (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
350     starts with "^", or
352     (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
353     (if it were set, the pattern would be anchored),
355 nigel 47 -1 is returned, indicating that the pattern matches only at the start of a
356     subject string or after any "\\n" within the string. Otherwise -2 is returned.
357     For anchored patterns, -2 is returned.
358 nigel 41
360 nigel 41
361 nigel 43 If the pattern was studied, and this resulted in the construction of a 256-bit
362     table indicating a fixed set of characters for the first character in any
363     matching string, a pointer to the table is returned. Otherwise NULL is
364     returned. The fourth argument should point to an \fBunsigned char *\fR
365     variable.
369     For a non-anchored pattern, return the value of the rightmost literal character
370     which must exist in any matched string, other than at its start. The fourth
371     argument should point to an \fBint\fR variable. If there is no such character,
372     or if the pattern is anchored, -1 is returned. For example, for the pattern
373     /a\\d+z\\d+/ the returned value is 'z'.
375     The \fBpcre_info()\fR function is now obsolete because its interface is too
376     restrictive to return all the available data about a compiled pattern. New
377     programs should use \fBpcre_fullinfo()\fR instead. The yield of
378     \fBpcre_info()\fR is the number of capturing subpatterns, or one of the
379     following negative numbers:
381     PCRE_ERROR_NULL the argument \fIcode\fR was NULL
382     PCRE_ERROR_BADMAGIC the "magic number" was not found
384     If the \fIoptptr\fR argument is not NULL, a copy of the options with which the
385     pattern was compiled is placed in the integer it points to (see
386     PCRE_INFO_OPTIONS above).
388     If the pattern is not anchored and the \fIfirstcharptr\fR argument is not NULL,
389     it is used to pass back information about the first character of any matched
390     string (see PCRE_INFO_FIRSTCHAR above).
394     The function \fBpcre_exec()\fR is called to match a subject string against a
395     pre-compiled pattern, which is passed in the \fIcode\fR argument. If the
396     pattern has been studied, the result of the study should be passed in the
397     \fIextra\fR argument. Otherwise this must be NULL.
399     The PCRE_ANCHORED option can be passed in the \fIoptions\fR argument, whose
400     unused bits must be zero. However, if a pattern was compiled with
401     PCRE_ANCHORED, or turned out to be anchored by virtue of its contents, it
402     cannot be made unachored at matching time.
404     There are also three further options that can be set only at matching time:
408     The first character of the string is not the beginning of a line, so the
409     circumflex metacharacter should not match before it. Setting this without
410     PCRE_MULTILINE (at compile time) causes circumflex never to match.
414     The end of the string is not the end of a line, so the dollar metacharacter
415     should not match it nor (except in multiline mode) a newline immediately before
416     it. Setting this without PCRE_MULTILINE (at compile time) causes dollar never
417     to match.
421     An empty string is not considered to be a valid match if this option is set. If
422     there are alternatives in the pattern, they are tried. If all the alternatives
423     match the empty string, the entire match fails. For example, if the pattern
425     a?b?
427     is applied to a string not beginning with "a" or "b", it matches the empty
428     string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
429     valid, so PCRE searches further into the string for occurrences of "a" or "b".
431     Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a special case
432     of a pattern match of the empty string within its \fBsplit()\fR function, and
433     when using the /g modifier. It is possible to emulate Perl's behaviour after
434     matching a null string by first trying the match again at the same offset with
435     PCRE_NOTEMPTY set, and then if that fails by advancing the starting offset (see
436     below) and trying an ordinary match again.
438     The subject string is passed as a pointer in \fIsubject\fR, a length in
439     \fIlength\fR, and a starting offset in \fIstartoffset\fR. Unlike the pattern
440     string, it may contain binary zero characters. When the starting offset is
441     zero, the search for a match starts at the beginning of the subject, and this
442     is by far the most common case.
444     A non-zero starting offset is useful when searching for another match in the
445     same subject by calling \fBpcre_exec()\fR again after a previous success.
446     Setting \fIstartoffset\fR differs from just passing over a shortened string and
447     setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
448     lookbehind. For example, consider the pattern
450     \\Biss\\B
452     which finds occurrences of "iss" in the middle of words. (\\B matches only if
453     the current position in the subject is not a word boundary.) When applied to
454     the string "Mississipi" the first call to \fBpcre_exec()\fR finds the first
455     occurrence. If \fBpcre_exec()\fR is called again with just the remainder of the
456     subject, namely "issipi", it does not match, because \\B is always false at the
457     start of the subject, which is deemed to be a word boundary. However, if
458     \fBpcre_exec()\fR is passed the entire string again, but with \fIstartoffset\fR
459     set to 4, it finds the second occurrence of "iss" because it is able to look
460     behind the starting point to discover that it is preceded by a letter.
462     If a non-zero starting offset is passed when the pattern is anchored, one
463     attempt to match at the given offset is tried. This can only succeed if the
464     pattern does not require the match to be at the start of the subject.
466     In general, a pattern matches a certain portion of the subject, and in
467     addition, further substrings from the subject may be picked out by parts of the
468     pattern. Following the usage in Jeffrey Friedl's book, this is called
469     "capturing" in what follows, and the phrase "capturing subpattern" is used for
470     a fragment of a pattern that picks out a substring. PCRE supports several other
471     kinds of parenthesized subpattern that do not cause substrings to be captured.
473     Captured substrings are returned to the caller via a vector of integer offsets
474     whose address is passed in \fIovector\fR. The number of elements in the vector
475     is passed in \fIovecsize\fR. The first two-thirds of the vector is used to pass
476     back captured substrings, each substring using a pair of integers. The
477     remaining third of the vector is used as workspace by \fBpcre_exec()\fR while
478     matching capturing subpatterns, and is not available for passing back
479     information. The length passed in \fIovecsize\fR should always be a multiple of
480     three. If it is not, it is rounded down.
482     When a match has been successful, information about captured substrings is
483     returned in pairs of integers, starting at the beginning of \fIovector\fR, and
484     continuing up to two-thirds of its length at the most. The first element of a
485     pair is set to the offset of the first character in a substring, and the second
486     is set to the offset of the first character after the end of a substring. The
487     first pair, \fIovector[0]\fR and \fIovector[1]\fR, identify the portion of the
488     subject string matched by the entire pattern. The next pair is used for the
489     first capturing subpattern, and so on. The value returned by \fBpcre_exec()\fR
490     is the number of pairs that have been set. If there are no capturing
491     subpatterns, the return value from a successful match is 1, indicating that
492     just the first pair of offsets has been set.
494     Some convenience functions are provided for extracting the captured substrings
495     as separate strings. These are described in the following section.
497     It is possible for an capturing subpattern number \fIn+1\fR to match some
498     part of the subject when subpattern \fIn\fR has not been used at all. For
499     example, if the string "abc" is matched against the pattern (a|(z))(bc)
500     subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
501     values corresponding to the unused subpattern are set to -1.
503     If a capturing subpattern is matched repeatedly, it is the last portion of the
504     string that it matched that gets returned.
506     If the vector is too small to hold all the captured substrings, it is used as
507     far as possible (up to two-thirds of its length), and the function returns a
508     value of zero. In particular, if the substring offsets are not of interest,
509     \fBpcre_exec()\fR may be called with \fIovector\fR passed as NULL and
510     \fIovecsize\fR as zero. However, if the pattern contains back references and
511     the \fIovector\fR isn't big enough to remember the related substrings, PCRE has
512     to get additional memory for use during matching. Thus it is usually advisable
513     to supply an \fIovector\fR.
515     Note that \fBpcre_info()\fR can be used to find out how many capturing
516     subpatterns there are in a compiled pattern. The smallest size for
517     \fIovector\fR that will allow for \fIn\fR captured substrings in addition to
518     the offsets of the substring matched by the whole pattern is (\fIn\fR+1)*3.
520     If \fBpcre_exec()\fR fails, it returns a negative number. The following are
521     defined in the header file:
525     The subject string did not match the pattern.
527     PCRE_ERROR_NULL (-2)
529     Either \fIcode\fR or \fIsubject\fR was passed as NULL, or \fIovector\fR was
530     NULL and \fIovecsize\fR was not zero.
534     An unrecognized bit was set in the \fIoptions\fR argument.
538     PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
539     the case when it is passed a junk pointer. This is the error it gives when the
540     magic number isn't present.
544     While running the pattern match, an unknown item was encountered in the
545     compiled pattern. This error could be caused by a bug in PCRE or by overwriting
546     of the compiled pattern.
550     If a pattern contains back references, but the \fIovector\fR that is passed to
551     \fBpcre_exec()\fR is not big enough to remember the referenced substrings, PCRE
552     gets a block of memory at the start of matching to use for this purpose. If the
553     call via \fBpcre_malloc()\fR fails, this error is given. The memory is freed at
554     the end of matching.
558     Captured substrings can be accessed directly by using the offsets returned by
559     \fBpcre_exec()\fR in \fIovector\fR. For convenience, the functions
560     \fBpcre_copy_substring()\fR, \fBpcre_get_substring()\fR, and
561     \fBpcre_get_substring_list()\fR are provided for extracting captured substrings
562     as new, separate, zero-terminated strings. A substring that contains a binary
563     zero is correctly extracted and has a further zero added on the end, but the
564     result does not, of course, function as a C string.
566     The first three arguments are the same for all three functions: \fIsubject\fR
567     is the subject string which has just been successfully matched, \fIovector\fR
568     is a pointer to the vector of integer offsets that was passed to
569     \fBpcre_exec()\fR, and \fIstringcount\fR is the number of substrings that
570     were captured by the match, including the substring that matched the entire
571     regular expression. This is the value returned by \fBpcre_exec\fR if it
572     is greater than zero. If \fBpcre_exec()\fR returned zero, indicating that it
573 nigel 47 ran out of space in \fIovector\fR, the value passed as \fIstringcount\fR should
574     be the size of the vector divided by three.
575 nigel 41
576     The functions \fBpcre_copy_substring()\fR and \fBpcre_get_substring()\fR
577     extract a single substring, whose number is given as \fIstringnumber\fR. A
578     value of zero extracts the substring that matched the entire pattern, while
579     higher values extract the captured substrings. For \fBpcre_copy_substring()\fR,
580     the string is placed in \fIbuffer\fR, whose length is given by
581 nigel 49 \fIbuffersize\fR, while for \fBpcre_get_substring()\fR a new block of memory is
582 nigel 41 obtained via \fBpcre_malloc\fR, and its address is returned via
583     \fIstringptr\fR. The yield of the function is the length of the string, not
584     including the terminating zero, or one of
588     The buffer was too small for \fBpcre_copy_substring()\fR, or the attempt to get
589     memory failed for \fBpcre_get_substring()\fR.
593     There is no substring whose number is \fIstringnumber\fR.
595     The \fBpcre_get_substring_list()\fR function extracts all available substrings
596     and builds a list of pointers to them. All this is done in a single block of
597     memory which is obtained via \fBpcre_malloc\fR. The address of the memory block
598     is returned via \fIlistptr\fR, which is also the start of the list of string
599     pointers. The end of the list is marked by a NULL pointer. The yield of the
600     function is zero if all went well, or
604     if the attempt to get the memory block failed.
606     When any of these functions encounter a substring that is unset, which can
607     happen when capturing subpattern number \fIn+1\fR matches some part of the
608     subject, but subpattern \fIn\fR has not been used at all, they return an empty
609     string. This can be distinguished from a genuine zero-length substring by
610     inspecting the appropriate offset in \fIovector\fR, which is negative for unset
611     substrings.
613 nigel 49 The two convenience functions \fBpcre_free_substring()\fR and
614     \fBpcre_free_substring_list()\fR can be used to free the memory returned by
615     a previous call of \fBpcre_get_substring()\fR or
616     \fBpcre_get_substring_list()\fR, respectively. They do nothing more than call
617     the function pointed to by \fBpcre_free\fR, which of course could be called
618     directly from a C program. However, PCRE is used in some situations where it is
619     linked via a special interface to another programming language which cannot use
620     \fBpcre_free\fR directly; it is for these cases that the functions are
621     provided.
622 nigel 41
625     There are some size limitations in PCRE but it is hoped that they will never in
626     practice be relevant.
627     The maximum length of a compiled pattern is 65539 (sic) bytes.
628     All values in repeating quantifiers must be less than 65536.
629     The maximum number of capturing subpatterns is 99.
630     The maximum number of all parenthesized subpatterns, including capturing
631     subpatterns, assertions, and other types of subpattern, is 200.
633     The maximum length of a subject string is the largest positive number that an
634     integer variable can hold. However, PCRE uses recursion to handle subpatterns
635     and indefinite repetition. This means that the available stack space may limit
636     the size of a subject string that can be processed by certain patterns.
640     The differences described here are with respect to Perl 5.005.
642     1. By default, a whitespace character is any character that the C library
643     function \fBisspace()\fR recognizes, though it is possible to compile PCRE with
644     alternative character type tables. Normally \fBisspace()\fR matches space,
645     formfeed, newline, carriage return, horizontal tab, and vertical tab. Perl 5
646     no longer includes vertical tab in its set of whitespace characters. The \\v
647     escape that was in the Perl documentation for a long time was never in fact
648     recognized. However, the character itself was treated as whitespace at least
649     up to 5.002. In 5.004 and 5.005 it does not match \\s.
651     2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl permits
652     them, but they do not mean what you might think. For example, (?!a){3} does
653     not assert that the next three characters are not "a". It just asserts that the
654     next character is not "a" three times.
656     3. Capturing subpatterns that occur inside negative lookahead assertions are
657     counted, but their entries in the offsets vector are never set. Perl sets its
658     numerical variables from any such patterns that are matched before the
659     assertion fails to match something (thereby succeeding), but only if the
660     negative lookahead assertion contains just one branch.
662     4. Though binary zero characters are supported in the subject string, they are
663     not allowed in a pattern string because it is passed as a normal C string,
664     terminated by zero. The escape sequence "\\0" can be used in the pattern to
665     represent a binary zero.
667     5. The following Perl escape sequences are not supported: \\l, \\u, \\L, \\U,
668     \\E, \\Q. In fact these are implemented by Perl's general string-handling and
669     are not part of its pattern matching engine.
671     6. The Perl \\G assertion is not supported as it is not relevant to single
672     pattern matches.
674 nigel 43 7. Fairly obviously, PCRE does not support the (?{code}) and (?p{code})
675     constructions. However, there is some experimental support for recursive
676     patterns using the non-Perl item (?R).
677 nigel 41
678     8. There are at the time of writing some oddities in Perl 5.005_02 concerned
679     with the settings of captured strings when part of a pattern is repeated. For
680     example, matching "aba" against the pattern /^(a(b)?)+$/ sets $2 to the value
681     "b", but matching "aabbaa" against /^(aa(bb)?)+$/ leaves $2 unset. However, if
682 nigel 47 the pattern is changed to /^(aa(b(b))?)+$/ then $2 (and $3) are set.
683 nigel 41
684     In Perl 5.004 $2 is set in both cases, and that is also true of PCRE. If in the
685     future Perl changes to a consistent state that is different, PCRE may change to
686     follow.
688     9. Another as yet unresolved discrepancy is that in Perl 5.005_02 the pattern
689     /^(a)?(?(1)a|b)+$/ matches the string "a", whereas in PCRE it does not.
690     However, in both Perl and PCRE /^(a)?a/ matched against "a" leaves $1 unset.
692     10. PCRE provides some extensions to the Perl regular expression facilities:
694     (a) Although lookbehind assertions must match fixed length strings, each
695     alternative branch of a lookbehind assertion can match a different length of
696     string. Perl 5.005 requires them all to have the same length.
698     (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ meta-
699     character matches only at the very end of the string.
701     (c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
702     meaning is faulted.
704     (d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
705     inverted, that is, by default they are not greedy, but if followed by a
706     question mark they are.
708     (e) PCRE_ANCHORED can be used to force a pattern to be tried only at the start
709     of the subject.
711     (f) The PCRE_NOTBOL, PCRE_NOTEOL, and PCRE_NOTEMPTY options for
712     \fBpcre_exec()\fR have no Perl equivalents.
714 nigel 43 (g) The (?R) construct allows for recursive pattern matching (Perl 5.6 can do
715     this using the (?p{code}) construct, which PCRE cannot of course support.)
716 nigel 41
717 nigel 43
719     The syntax and semantics of the regular expressions supported by PCRE are
720     described below. Regular expressions are also described in the Perl
721     documentation and in a number of other books, some of which have copious
722     examples. Jeffrey Friedl's "Mastering Regular Expressions", published by
723 nigel 49 O'Reilly (ISBN 1-56592-257), covers them in great detail.
724 nigel 41
725 nigel 49 The description here is intended as reference documentation. The basic
726     operation of PCRE is on strings of bytes. However, there is the beginnings of
727     some support for UTF-8 character strings. To use this support you must
728     configure PCRE to include it, and then call \fBpcre_compile()\fR with the
729     PCRE_UTF8 option. How this affects the pattern matching is described in the
730     final section of this document.
732 nigel 41 A regular expression is a pattern that is matched against a subject string from
733     left to right. Most characters stand for themselves in a pattern, and match the
734     corresponding characters in the subject. As a trivial example, the pattern
736     The quick brown fox
738     matches a portion of a subject string that is identical to itself. The power of
739     regular expressions comes from the ability to include alternatives and
740     repetitions in the pattern. These are encoded in the pattern by the use of
741     \fImeta-characters\fR, which do not stand for themselves but instead are
742     interpreted in some special way.
744     There are two different sets of meta-characters: those that are recognized
745     anywhere in the pattern except within square brackets, and those that are
746     recognized in square brackets. Outside square brackets, the meta-characters are
747     as follows:
749     \\ general escape character with several uses
750     ^ assert start of subject (or line, in multiline mode)
751     $ assert end of subject (or line, in multiline mode)
752     . match any character except newline (by default)
753     [ start character class definition
754     | start of alternative branch
755     ( start subpattern
756     ) end subpattern
757     ? extends the meaning of (
758     also 0 or 1 quantifier
759     also quantifier minimizer
760     * 0 or more quantifier
761     + 1 or more quantifier
762     { start min/max quantifier
764     Part of a pattern that is in square brackets is called a "character class". In
765     a character class the only meta-characters are:
767     \\ general escape character
768     ^ negate the class, but only if the first character
769     - indicates character range
770     ] terminates the character class
772     The following sections describe the use of each of the meta-characters.
776     The backslash character has several uses. Firstly, if it is followed by a
777     non-alphameric character, it takes away any special meaning that character may
778     have. This use of backslash as an escape character applies both inside and
779     outside character classes.
781     For example, if you want to match a "*" character, you write "\\*" in the
782     pattern. This applies whether or not the following character would otherwise be
783     interpreted as a meta-character, so it is always safe to precede a
784     non-alphameric with "\\" to specify that it stands for itself. In particular,
785     if you want to match a backslash, you write "\\\\".
787     If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
788     pattern (other than in a character class) and characters between a "#" outside
789     a character class and the next newline character are ignored. An escaping
790     backslash can be used to include a whitespace or "#" character as part of the
791     pattern.
793     A second use of backslash provides a way of encoding non-printing characters
794     in patterns in a visible manner. There is no restriction on the appearance of
795     non-printing characters, apart from the binary zero that terminates a pattern,
796     but when a pattern is being prepared by text editing, it is usually easier to
797     use one of the following escape sequences than the binary character it
798     represents:
800     \\a alarm, that is, the BEL character (hex 07)
801     \\cx "control-x", where x is any character
802     \\e escape (hex 1B)
803     \\f formfeed (hex 0C)
804     \\n newline (hex 0A)
805     \\r carriage return (hex 0D)
806     \\t tab (hex 09)
807     \\xhh character with hex code hh
808     \\ddd character with octal code ddd, or backreference
810     The precise effect of "\\cx" is as follows: if "x" is a lower case letter, it
811     is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
812     Thus "\\cz" becomes hex 1A, but "\\c{" becomes hex 3B, while "\\c;" becomes hex
813     7B.
815     After "\\x", up to two hexadecimal digits are read (letters can be in upper or
816     lower case).
818     After "\\0" up to two further octal digits are read. In both cases, if there
819     are fewer than two digits, just those that are present are used. Thus the
820     sequence "\\0\\x\\07" specifies two binary zeros followed by a BEL character.
821     Make sure you supply two digits after the initial zero if the character that
822     follows is itself an octal digit.
824     The handling of a backslash followed by a digit other than 0 is complicated.
825     Outside a character class, PCRE reads it and any following digits as a decimal
826     number. If the number is less than 10, or if there have been at least that many
827     previous capturing left parentheses in the expression, the entire sequence is
828     taken as a \fIback reference\fR. A description of how this works is given
829     later, following the discussion of parenthesized subpatterns.
831     Inside a character class, or if the decimal number is greater than 9 and there
832     have not been that many capturing subpatterns, PCRE re-reads up to three octal
833     digits following the backslash, and generates a single byte from the least
834     significant 8 bits of the value. Any subsequent digits stand for themselves.
835     For example:
837     \\040 is another way of writing a space
838     \\40 is the same, provided there are fewer than 40
839     previous capturing subpatterns
840     \\7 is always a back reference
841     \\11 might be a back reference, or another way of
842     writing a tab
843     \\011 is always a tab
844     \\0113 is a tab followed by the character "3"
845     \\113 is the character with octal code 113 (since there
846     can be no more than 99 back references)
847     \\377 is a byte consisting entirely of 1 bits
848     \\81 is either a back reference, or a binary zero
849     followed by the two characters "8" and "1"
851     Note that octal values of 100 or greater must not be introduced by a leading
852     zero, because no more than three octal digits are ever read.
854     All the sequences that define a single byte value can be used both inside and
855     outside character classes. In addition, inside a character class, the sequence
856     "\\b" is interpreted as the backspace character (hex 08). Outside a character
857     class it has a different meaning (see below).
859     The third use of backslash is for specifying generic character types:
861     \\d any decimal digit
862     \\D any character that is not a decimal digit
863     \\s any whitespace character
864     \\S any character that is not a whitespace character
865     \\w any "word" character
866     \\W any "non-word" character
868     Each pair of escape sequences partitions the complete set of characters into
869     two disjoint sets. Any given character matches one, and only one, of each pair.
871     A "word" character is any letter or digit or the underscore character, that is,
872     any character which can be part of a Perl "word". The definition of letters and
873     digits is controlled by PCRE's character tables, and may vary if locale-
874     specific matching is taking place (see "Locale support" above). For example, in
875     the "fr" (French) locale, some character codes greater than 128 are used for
876     accented letters, and these are matched by \\w.
878     These character type sequences can appear both inside and outside character
879     classes. They each match one character of the appropriate type. If the current
880     matching point is at the end of the subject string, all of them fail, since
881     there is no character to match.
883     The fourth use of backslash is for certain simple assertions. An assertion
884     specifies a condition that has to be met at a particular point in a match,
885     without consuming any characters from the subject string. The use of
886     subpatterns for more complicated assertions is described below. The backslashed
887     assertions are
889     \\b word boundary
890     \\B not a word boundary
891     \\A start of subject (independent of multiline mode)
892     \\Z end of subject or newline at end (independent of multiline mode)
893     \\z end of subject (independent of multiline mode)
895     These assertions may not appear in character classes (but note that "\\b" has a
896     different meaning, namely the backspace character, inside a character class).
898     A word boundary is a position in the subject string where the current character
899     and the previous character do not both match \\w or \\W (i.e. one matches
900     \\w and the other matches \\W), or the start or end of the string if the
901     first or last character matches \\w, respectively.
903     The \\A, \\Z, and \\z assertions differ from the traditional circumflex and
904     dollar (described below) in that they only ever match at the very start and end
905     of the subject string, whatever options are set. They are not affected by the
906     PCRE_NOTBOL or PCRE_NOTEOL options. If the \fIstartoffset\fR argument of
907     \fBpcre_exec()\fR is non-zero, \\A can never match. The difference between \\Z
908     and \\z is that \\Z matches before a newline that is the last character of the
909     string as well as at the end of the string, whereas \\z matches only at the
910     end.
914     Outside a character class, in the default matching mode, the circumflex
915     character is an assertion which is true only if the current matching point is
916     at the start of the subject string. If the \fIstartoffset\fR argument of
917     \fBpcre_exec()\fR is non-zero, circumflex can never match. Inside a character
918     class, circumflex has an entirely different meaning (see below).
920     Circumflex need not be the first character of the pattern if a number of
921     alternatives are involved, but it should be the first thing in each alternative
922     in which it appears if the pattern is ever to match that branch. If all
923     possible alternatives start with a circumflex, that is, if the pattern is
924     constrained to match only at the start of the subject, it is said to be an
925     "anchored" pattern. (There are also other constructs that can cause a pattern
926     to be anchored.)
928     A dollar character is an assertion which is true only if the current matching
929     point is at the end of the subject string, or immediately before a newline
930     character that is the last character in the string (by default). Dollar need
931     not be the last character of the pattern if a number of alternatives are
932     involved, but it should be the last item in any branch in which it appears.
933     Dollar has no special meaning in a character class.
935     The meaning of dollar can be changed so that it matches only at the very end of
936     the string, by setting the PCRE_DOLLAR_ENDONLY option at compile or matching
937     time. This does not affect the \\Z assertion.
939     The meanings of the circumflex and dollar characters are changed if the
940     PCRE_MULTILINE option is set. When this is the case, they match immediately
941     after and immediately before an internal "\\n" character, respectively, in
942     addition to matching at the start and end of the subject string. For example,
943     the pattern /^abc$/ matches the subject string "def\\nabc" in multiline mode,
944     but not otherwise. Consequently, patterns that are anchored in single line mode
945     because all branches start with "^" are not anchored in multiline mode, and a
946     match for circumflex is possible when the \fIstartoffset\fR argument of
947     \fBpcre_exec()\fR is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if
948     PCRE_MULTILINE is set.
950     Note that the sequences \\A, \\Z, and \\z can be used to match the start and
951     end of the subject in both modes, and if all branches of a pattern start with
952     \\A is it always anchored, whether PCRE_MULTILINE is set or not.
956     Outside a character class, a dot in the pattern matches any one character in
957     the subject, including a non-printing character, but not (by default) newline.
958 nigel 47 If the PCRE_DOTALL option is set, dots match newlines as well. The handling of
959     dot is entirely independent of the handling of circumflex and dollar, the only
960     relationship being that they both involve newline characters. Dot has no
961     special meaning in a character class.
962 nigel 41
965     An opening square bracket introduces a character class, terminated by a closing
966     square bracket. A closing square bracket on its own is not special. If a
967     closing square bracket is required as a member of the class, it should be the
968     first data character in the class (after an initial circumflex, if present) or
969     escaped with a backslash.
971     A character class matches a single character in the subject; the character must
972     be in the set of characters defined by the class, unless the first character in
973     the class is a circumflex, in which case the subject character must not be in
974     the set defined by the class. If a circumflex is actually required as a member
975     of the class, ensure it is not the first character, or escape it with a
976     backslash.
978     For example, the character class [aeiou] matches any lower case vowel, while
979     [^aeiou] matches any character that is not a lower case vowel. Note that a
980     circumflex is just a convenient notation for specifying the characters which
981     are in the class by enumerating those that are not. It is not an assertion: it
982     still consumes a character from the subject string, and fails if the current
983     pointer is at the end of the string.
985     When caseless matching is set, any letters in a class represent both their
986     upper case and lower case versions, so for example, a caseless [aeiou] matches
987     "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
988     caseful version would.
990     The newline character is never treated in any special way in character classes,
991     whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE options is. A class
992     such as [^a] will always match a newline.
994     The minus (hyphen) character can be used to specify a range of characters in a
995     character class. For example, [d-m] matches any letter between d and m,
996     inclusive. If a minus character is required in a class, it must be escaped with
997     a backslash or appear in a position where it cannot be interpreted as
998     indicating a range, typically as the first or last character in the class.
1000     It is not possible to have the literal character "]" as the end character of a
1001     range. A pattern such as [W-]46] is interpreted as a class of two characters
1002     ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
1003     "-46]". However, if the "]" is escaped with a backslash it is interpreted as
1004     the end of range, so [W-\\]46] is interpreted as a single class containing a
1005     range followed by two separate characters. The octal or hexadecimal
1006     representation of "]" can also be used to end a range.
1008     Ranges operate in ASCII collating sequence. They can also be used for
1009     characters specified numerically, for example [\\000-\\037]. If a range that
1010     includes letters is used when caseless matching is set, it matches the letters
1011     in either case. For example, [W-c] is equivalent to [][\\^_`wxyzabc], matched
1012     caselessly, and if character tables for the "fr" locale are in use,
1013     [\\xc8-\\xcb] matches accented E characters in both cases.
1015     The character types \\d, \\D, \\s, \\S, \\w, and \\W may also appear in a
1016     character class, and add the characters that they match to the class. For
1017     example, [\\dABCDEF] matches any hexadecimal digit. A circumflex can
1018     conveniently be used with the upper case character types to specify a more
1019     restricted set of characters than the matching lower case type. For example,
1020     the class [^\\W_] matches any letter or digit, but not underscore.
1022     All non-alphameric characters other than \\, -, ^ (at the start) and the
1023     terminating ] are non-special in character classes, but it does no harm if they
1024     are escaped.
1028     Perl 5.6 (not yet released at the time of writing) is going to support the
1029     POSIX notation for character classes, which uses names enclosed by [: and :]
1030     within the enclosing square brackets. PCRE supports this notation. For example,
1032     [01[:alpha:]%]
1034     matches "0", "1", any alphabetic character, or "%". The supported class names
1035     are
1037     alnum letters and digits
1038     alpha letters
1039     ascii character codes 0 - 127
1040     cntrl control characters
1041     digit decimal digits (same as \\d)
1042     graph printing characters, excluding space
1043     lower lower case letters
1044     print printing characters, including space
1045     punct printing characters, excluding letters and digits
1046     space white space (same as \\s)
1047     upper upper case letters
1048     word "word" characters (same as \\w)
1049     xdigit hexadecimal digits
1051     The names "ascii" and "word" are Perl extensions. Another Perl extension is
1052     negation, which is indicated by a ^ character after the colon. For example,
1054     [12[:^digit:]]
1056     matches "1", "2", or any non-digit. PCRE (and Perl) also recogize the POSIX
1057     syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
1058     supported, and an error is given if they are encountered.
1061 nigel 41 .SH VERTICAL BAR
1062     Vertical bar characters are used to separate alternative patterns. For example,
1063     the pattern
1065     gilbert|sullivan
1067     matches either "gilbert" or "sullivan". Any number of alternatives may appear,
1068     and an empty alternative is permitted (matching the empty string).
1069     The matching process tries each alternative in turn, from left to right,
1070     and the first one that succeeds is used. If the alternatives are within a
1071     subpattern (defined below), "succeeds" means matching the rest of the main
1072     pattern as well as the alternative in the subpattern.
1077     can be changed from within the pattern by a sequence of Perl option letters
1078     enclosed between "(?" and ")". The option letters are
1080     i for PCRE_CASELESS
1081     m for PCRE_MULTILINE
1082     s for PCRE_DOTALL
1083     x for PCRE_EXTENDED
1085     For example, (?im) sets caseless, multiline matching. It is also possible to
1086     unset these options by preceding the letter with a hyphen, and a combined
1087     setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
1088     PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
1089     permitted. If a letter appears both before and after the hyphen, the option is
1090     unset.
1092     The scope of these option changes depends on where in the pattern the setting
1093     occurs. For settings that are outside any subpattern (defined below), the
1094     effect is the same as if the options were set or unset at the start of
1095     matching. The following patterns all behave in exactly the same way:
1097     (?i)abc
1098     a(?i)bc
1099     ab(?i)c
1100     abc(?i)
1102     which in turn is the same as compiling the pattern abc with PCRE_CASELESS set.
1103     In other words, such "top level" settings apply to the whole pattern (unless
1104     there are other changes inside subpatterns). If there is more than one setting
1105     of the same option at top level, the rightmost setting is used.
1107     If an option change occurs inside a subpattern, the effect is different. This
1108     is a change of behaviour in Perl 5.005. An option change inside a subpattern
1109     affects only that part of the subpattern that follows it, so
1111     (a(?i)b)c
1113     matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
1114     By this means, options can be made to have different settings in different
1115     parts of the pattern. Any changes made in one alternative do carry on
1116     into subsequent branches within the same subpattern. For example,
1118     (a(?i)b|c)
1120     matches "ab", "aB", "c", and "C", even though when matching "C" the first
1121     branch is abandoned before the option setting. This is because the effects of
1122     option settings happen at compile time. There would be some very weird
1123     behaviour otherwise.
1125     The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changed in the
1126     same way as the Perl-compatible options by using the characters U and X
1127     respectively. The (?X) flag setting is special in that it must always occur
1128     earlier in the pattern than any of the additional features it turns on, even
1129     when it is at top level. It is best put at the start.
1133     Subpatterns are delimited by parentheses (round brackets), which can be nested.
1134     Marking part of a pattern as a subpattern does two things:
1136     1. It localizes a set of alternatives. For example, the pattern
1138     cat(aract|erpillar|)
1140     matches one of the words "cat", "cataract", or "caterpillar". Without the
1141     parentheses, it would match "cataract", "erpillar" or the empty string.
1143     2. It sets up the subpattern as a capturing subpattern (as defined above).
1144     When the whole pattern matches, that portion of the subject string that matched
1145     the subpattern is passed back to the caller via the \fIovector\fR argument of
1146     \fBpcre_exec()\fR. Opening parentheses are counted from left to right (starting
1147     from 1) to obtain the numbers of the capturing subpatterns.
1149     For example, if the string "the red king" is matched against the pattern
1151     the ((red|white) (king|queen))
1153     the captured substrings are "red king", "red", and "king", and are numbered 1,
1154     2, and 3.
1156     The fact that plain parentheses fulfil two functions is not always helpful.
1157     There are often times when a grouping subpattern is required without a
1158     capturing requirement. If an opening parenthesis is followed by "?:", the
1159     subpattern does not do any capturing, and is not counted when computing the
1160     number of any subsequent capturing subpatterns. For example, if the string "the
1161     white queen" is matched against the pattern
1163     the ((?:red|white) (king|queen))
1165     the captured substrings are "white queen" and "queen", and are numbered 1 and
1166     2. The maximum number of captured substrings is 99, and the maximum number of
1167     all subpatterns, both capturing and non-capturing, is 200.
1169     As a convenient shorthand, if any option settings are required at the start of
1170     a non-capturing subpattern, the option letters may appear between the "?" and
1171     the ":". Thus the two patterns
1173     (?i:saturday|sunday)
1174     (?:(?i)saturday|sunday)
1176     match exactly the same set of strings. Because alternative branches are tried
1177     from left to right, and options are not reset until the end of the subpattern
1178     is reached, an option setting in one branch does affect subsequent branches, so
1179     the above patterns match "SUNDAY" as well as "Saturday".
1183     Repetition is specified by quantifiers, which can follow any of the following
1184     items:
1186     a single character, possibly escaped
1187     the . metacharacter
1188     a character class
1189     a back reference (see next section)
1190     a parenthesized subpattern (unless it is an assertion - see below)
1192     The general repetition quantifier specifies a minimum and maximum number of
1193     permitted matches, by giving the two numbers in curly brackets (braces),
1194     separated by a comma. The numbers must be less than 65536, and the first must
1195     be less than or equal to the second. For example:
1197     z{2,4}
1199     matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
1200     character. If the second number is omitted, but the comma is present, there is
1201     no upper limit; if the second number and the comma are both omitted, the
1202     quantifier specifies an exact number of required matches. Thus
1204     [aeiou]{3,}
1206     matches at least 3 successive vowels, but may match many more, while
1208     \\d{8}
1210     matches exactly 8 digits. An opening curly bracket that appears in a position
1211     where a quantifier is not allowed, or one that does not match the syntax of a
1212     quantifier, is taken as a literal character. For example, {,6} is not a
1213     quantifier, but a literal string of four characters.
1215     The quantifier {0} is permitted, causing the expression to behave as if the
1216     previous item and the quantifier were not present.
1218     For convenience (and historical compatibility) the three most common
1219     quantifiers have single-character abbreviations:
1221     * is equivalent to {0,}
1222     + is equivalent to {1,}
1223     ? is equivalent to {0,1}
1225     It is possible to construct infinite loops by following a subpattern that can
1226     match no characters with a quantifier that has no upper limit, for example:
1228     (a?)*
1230     Earlier versions of Perl and PCRE used to give an error at compile time for
1231     such patterns. However, because there are cases where this can be useful, such
1232     patterns are now accepted, but if any repetition of the subpattern does in fact
1233     match no characters, the loop is forcibly broken.
1235     By default, the quantifiers are "greedy", that is, they match as much as
1236     possible (up to the maximum number of permitted times), without causing the
1237     rest of the pattern to fail. The classic example of where this gives problems
1238     is in trying to match comments in C programs. These appear between the
1239     sequences /* and */ and within the sequence, individual * and / characters may
1240     appear. An attempt to match C comments by applying the pattern
1242     /\\*.*\\*/
1244     to the string
1246     /* first command */ not comment /* second comment */
1248     fails, because it matches the entire string due to the greediness of the .*
1249     item.
1251 nigel 47 However, if a quantifier is followed by a question mark, it ceases to be
1252 nigel 41 greedy, and instead matches the minimum number of times possible, so the
1253     pattern
1255     /\\*.*?\\*/
1257     does the right thing with the C comments. The meaning of the various
1258     quantifiers is not otherwise changed, just the preferred number of matches.
1259     Do not confuse this use of question mark with its use as a quantifier in its
1260     own right. Because it has two uses, it can sometimes appear doubled, as in
1262     \\d??\\d
1264     which matches one digit by preference, but can match two if that is the only
1265     way the rest of the pattern matches.
1267 nigel 47 If the PCRE_UNGREEDY option is set (an option which is not available in Perl),
1268     the quantifiers are not greedy by default, but individual ones can be made
1269 nigel 41 greedy by following them with a question mark. In other words, it inverts the
1270     default behaviour.
1272     When a parenthesized subpattern is quantified with a minimum repeat count that
1273     is greater than 1 or with a limited maximum, more store is required for the
1274     compiled pattern, in proportion to the size of the minimum or maximum.
1276     If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
1277 nigel 47 to Perl's /s) is set, thus allowing the . to match newlines, the pattern is
1278     implicitly anchored, because whatever follows will be tried against every
1279 nigel 41 character position in the subject string, so there is no point in retrying the
1280     overall match at any position after the first. PCRE treats such a pattern as
1281     though it were preceded by \\A. In cases where it is known that the subject
1282     string contains no newlines, it is worth setting PCRE_DOTALL when the pattern
1283     begins with .* in order to obtain this optimization, or alternatively using ^
1284     to indicate anchoring explicitly.
1286     When a capturing subpattern is repeated, the value captured is the substring
1287     that matched the final iteration. For example, after
1289     (tweedle[dume]{3}\\s*)+
1291     has matched "tweedledum tweedledee" the value of the captured substring is
1292     "tweedledee". However, if there are nested capturing subpatterns, the
1293     corresponding captured values may have been set in previous iterations. For
1294     example, after
1296     /(a|(b))+/
1298     matches "aba" the value of the second captured substring is "b".
1302     Outside a character class, a backslash followed by a digit greater than 0 (and
1303     possibly further digits) is a back reference to a capturing subpattern earlier
1304     (i.e. to its left) in the pattern, provided there have been that many previous
1305     capturing left parentheses.
1307     However, if the decimal number following the backslash is less than 10, it is
1308     always taken as a back reference, and causes an error only if there are not
1309     that many capturing left parentheses in the entire pattern. In other words, the
1310     parentheses that are referenced need not be to the left of the reference for
1311     numbers less than 10. See the section entitled "Backslash" above for further
1312     details of the handling of digits following a backslash.
1314     A back reference matches whatever actually matched the capturing subpattern in
1315     the current subject string, rather than anything matching the subpattern
1316     itself. So the pattern
1318     (sens|respons)e and \\1ibility
1320     matches "sense and sensibility" and "response and responsibility", but not
1321     "sense and responsibility". If caseful matching is in force at the time of the
1322 nigel 47 back reference, the case of letters is relevant. For example,
1323 nigel 41
1324     ((?i)rah)\\s+\\1
1326     matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
1327     capturing subpattern is matched caselessly.
1329     There may be more than one back reference to the same subpattern. If a
1330 nigel 47 subpattern has not actually been used in a particular match, any back
1331 nigel 41 references to it always fail. For example, the pattern
1333     (a|(bc))\\2
1335     always fails if it starts to match "a" rather than "bc". Because there may be
1336     up to 99 back references, all digits following the backslash are taken
1337     as part of a potential back reference number. If the pattern continues with a
1338 nigel 47 digit character, some delimiter must be used to terminate the back reference.
1339     If the PCRE_EXTENDED option is set, this can be whitespace. Otherwise an empty
1340     comment can be used.
1341 nigel 41
1342     A back reference that occurs inside the parentheses to which it refers fails
1343     when the subpattern is first used, so, for example, (a\\1) never matches.
1344     However, such references can be useful inside repeated subpatterns. For
1345     example, the pattern
1347     (a|b\\1)+
1349 nigel 49 matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of
1350 nigel 41 the subpattern, the back reference matches the character string corresponding
1351     to the previous iteration. In order for this to work, the pattern must be such
1352     that the first iteration does not need to match the back reference. This can be
1353     done using alternation, as in the example above, or by a quantifier with a
1354     minimum of zero.
1358     An assertion is a test on the characters following or preceding the current
1359     matching point that does not actually consume any characters. The simple
1360     assertions coded as \\b, \\B, \\A, \\Z, \\z, ^ and $ are described above. More
1361     complicated assertions are coded as subpatterns. There are two kinds: those
1362     that look ahead of the current position in the subject string, and those that
1363     look behind it.
1365     An assertion subpattern is matched in the normal way, except that it does not
1366     cause the current matching position to be changed. Lookahead assertions start
1367     with (?= for positive assertions and (?! for negative assertions. For example,
1369     \\w+(?=;)
1371     matches a word followed by a semicolon, but does not include the semicolon in
1372     the match, and
1374     foo(?!bar)
1376     matches any occurrence of "foo" that is not followed by "bar". Note that the
1377     apparently similar pattern
1379     (?!foo)bar
1381     does not find an occurrence of "bar" that is preceded by something other than
1382     "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
1383     (?!foo) is always true when the next three characters are "bar". A
1384     lookbehind assertion is needed to achieve this effect.
1386     Lookbehind assertions start with (?<= for positive assertions and (?<! for
1387     negative assertions. For example,
1389     (?<!foo)bar
1391     does find an occurrence of "bar" that is not preceded by "foo". The contents of
1392     a lookbehind assertion are restricted such that all the strings it matches must
1393     have a fixed length. However, if there are several alternatives, they do not
1394     all have to have the same fixed length. Thus
1396     (?<=bullock|donkey)
1398     is permitted, but
1400     (?<!dogs?|cats?)
1402     causes an error at compile time. Branches that match different length strings
1403     are permitted only at the top level of a lookbehind assertion. This is an
1404     extension compared with Perl 5.005, which requires all branches to match the
1405     same length of string. An assertion such as
1407     (?<=ab(c|de))
1409     is not permitted, because its single top-level branch can match two different
1410     lengths, but it is acceptable if rewritten to use two top-level branches:
1412     (?<=abc|abde)
1414     The implementation of lookbehind assertions is, for each alternative, to
1415     temporarily move the current position back by the fixed width and then try to
1416     match. If there are insufficient characters before the current position, the
1417     match is deemed to fail. Lookbehinds in conjunction with once-only subpatterns
1418     can be particularly useful for matching at the ends of strings; an example is
1419     given at the end of the section on once-only subpatterns.
1421     Several assertions (of any sort) may occur in succession. For example,
1423     (?<=\\d{3})(?<!999)foo
1425     matches "foo" preceded by three digits that are not "999". Notice that each of
1426     the assertions is applied independently at the same point in the subject
1427     string. First there is a check that the previous three characters are all
1428 nigel 47 digits, and then there is a check that the same three characters are not "999".
1429 nigel 41 This pattern does \fInot\fR match "foo" preceded by six characters, the first
1430     of which are digits and the last three of which are not "999". For example, it
1431     doesn't match "123abcfoo". A pattern to do that is
1433     (?<=\\d{3}...)(?<!999)foo
1435     This time the first assertion looks at the preceding six characters, checking
1436     that the first three are digits, and then the second assertion checks that the
1437     preceding three characters are not "999".
1439     Assertions can be nested in any combination. For example,
1441     (?<=(?<!foo)bar)baz
1443     matches an occurrence of "baz" that is preceded by "bar" which in turn is not
1444     preceded by "foo", while
1446     (?<=\\d{3}(?!999)...)foo
1448     is another pattern which matches "foo" preceded by three digits and any three
1449     characters that are not "999".
1451     Assertion subpatterns are not capturing subpatterns, and may not be repeated,
1452     because it makes no sense to assert the same thing several times. If any kind
1453     of assertion contains capturing subpatterns within it, these are counted for
1454     the purposes of numbering the capturing subpatterns in the whole pattern.
1455     However, substring capturing is carried out only for positive assertions,
1456     because it does not make sense for negative assertions.
1458     Assertions count towards the maximum of 200 parenthesized subpatterns.
1462     With both maximizing and minimizing repetition, failure of what follows
1463     normally causes the repeated item to be re-evaluated to see if a different
1464     number of repeats allows the rest of the pattern to match. Sometimes it is
1465     useful to prevent this, either to change the nature of the match, or to cause
1466     it fail earlier than it otherwise might, when the author of the pattern knows
1467     there is no point in carrying on.
1469     Consider, for example, the pattern \\d+foo when applied to the subject line
1471     123456bar
1473     After matching all 6 digits and then failing to match "foo", the normal
1474     action of the matcher is to try again with only 5 digits matching the \\d+
1475     item, and then with 4, and so on, before ultimately failing. Once-only
1476     subpatterns provide the means for specifying that once a portion of the pattern
1477     has matched, it is not to be re-evaluated in this way, so the matcher would
1478     give up immediately on failing to match "foo" the first time. The notation is
1479     another kind of special parenthesis, starting with (?> as in this example:
1481     (?>\\d+)bar
1483     This kind of parenthesis "locks up" the part of the pattern it contains once
1484     it has matched, and a failure further into the pattern is prevented from
1485     backtracking into it. Backtracking past it to previous items, however, works as
1486     normal.
1488     An alternative description is that a subpattern of this type matches the string
1489     of characters that an identical standalone pattern would match, if anchored at
1490     the current point in the subject string.
1492     Once-only subpatterns are not capturing subpatterns. Simple cases such as the
1493     above example can be thought of as a maximizing repeat that must swallow
1494     everything it can. So, while both \\d+ and \\d+? are prepared to adjust the
1495     number of digits they match in order to make the rest of the pattern match,
1496     (?>\\d+) can only match an entire sequence of digits.
1498     This construction can of course contain arbitrarily complicated subpatterns,
1499     and it can be nested.
1501     Once-only subpatterns can be used in conjunction with lookbehind assertions to
1502     specify efficient matching at the end of the subject string. Consider a simple
1503     pattern such as
1505     abcd$
1507 nigel 43 when applied to a long string which does not match. Because matching proceeds
1508     from left to right, PCRE will look for each "a" in the subject and then see if
1509     what follows matches the rest of the pattern. If the pattern is specified as
1510 nigel 41
1511     ^.*abcd$
1513 nigel 47 the initial .* matches the entire string at first, but when this fails (because
1514     there is no following "a"), it backtracks to match all but the last character,
1515     then all but the last two characters, and so on. Once again the search for "a"
1516     covers the entire string, from right to left, so we are no better off. However,
1517     if the pattern is written as
1518 nigel 41
1519     ^(?>.*)(?<=abcd)
1521 nigel 47 there can be no backtracking for the .* item; it can match only the entire
1522 nigel 41 string. The subsequent lookbehind assertion does a single test on the last four
1523     characters. If it fails, the match fails immediately. For long strings, this
1524     approach makes a significant difference to the processing time.
1526 nigel 43 When a pattern contains an unlimited repeat inside a subpattern that can itself
1527     be repeated an unlimited number of times, the use of a once-only subpattern is
1528     the only way to avoid some failing matches taking a very long time indeed.
1529     The pattern
1530 nigel 41
1531 nigel 43 (\\D+|<\\d+>)*[!?]
1533     matches an unlimited number of substrings that either consist of non-digits, or
1534     digits enclosed in <>, followed by either ! or ?. When it matches, it runs
1535     quickly. However, if it is applied to
1537     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1539     it takes a long time before reporting failure. This is because the string can
1540     be divided between the two repeats in a large number of ways, and all have to
1541     be tried. (The example used [!?] rather than a single character at the end,
1542     because both PCRE and Perl have an optimization that allows for fast failure
1543     when a single character is used. They remember the last single character that
1544     is required for a match, and fail early if it is not present in the string.)
1545     If the pattern is changed to
1547     ((?>\\D+)|<\\d+>)*[!?]
1549     sequences of non-digits cannot be broken, and failure happens quickly.
1553     It is possible to cause the matching process to obey a subpattern
1554     conditionally or to choose between two alternative subpatterns, depending on
1555     the result of an assertion, or whether a previous capturing subpattern matched
1556     or not. The two possible forms of conditional subpattern are
1558     (?(condition)yes-pattern)
1559     (?(condition)yes-pattern|no-pattern)
1561     If the condition is satisfied, the yes-pattern is used; otherwise the
1562     no-pattern (if present) is used. If there are more than two alternatives in the
1563     subpattern, a compile-time error occurs.
1565     There are two kinds of condition. If the text between the parentheses consists
1566 nigel 47 of a sequence of digits, the condition is satisfied if the capturing subpattern
1567     of that number has previously matched. Consider the following pattern, which
1568     contains non-significant white space to make it more readable (assume the
1569     PCRE_EXTENDED option) and to divide it into three parts for ease of discussion:
1570 nigel 41
1571     ( \\( )? [^()]+ (?(1) \\) )
1573     The first part matches an optional opening parenthesis, and if that
1574     character is present, sets it as the first captured substring. The second part
1575     matches one or more characters that are not parentheses. The third part is a
1576     conditional subpattern that tests whether the first set of parentheses matched
1577     or not. If they did, that is, if subject started with an opening parenthesis,
1578     the condition is true, and so the yes-pattern is executed and a closing
1579     parenthesis is required. Otherwise, since no-pattern is not present, the
1580     subpattern matches nothing. In other words, this pattern matches a sequence of
1581     non-parentheses, optionally enclosed in parentheses.
1583     If the condition is not a sequence of digits, it must be an assertion. This may
1584     be a positive or negative lookahead or lookbehind assertion. Consider this
1585     pattern, again containing non-significant white space, and with the two
1586     alternatives on the second line:
1588     (?(?=[^a-z]*[a-z])
1589     \\d{2}-[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )
1591     The condition is a positive lookahead assertion that matches an optional
1592     sequence of non-letters followed by a letter. In other words, it tests for the
1593     presence of at least one letter in the subject. If a letter is found, the
1594     subject is matched against the first alternative; otherwise it is matched
1595     against the second. This pattern matches strings in one of the two forms
1596     dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
1599     .SH COMMENTS
1600     The sequence (?# marks the start of a comment which continues up to the next
1601     closing parenthesis. Nested parentheses are not permitted. The characters
1602     that make up a comment play no part in the pattern matching at all.
1604     If the PCRE_EXTENDED option is set, an unescaped # character outside a
1605     character class introduces a comment that continues up to the next newline
1606     character in the pattern.
1610     Consider the problem of matching a string in parentheses, allowing for
1611     unlimited nested parentheses. Without the use of recursion, the best that can
1612     be done is to use a pattern that matches up to some fixed depth of nesting. It
1613     is not possible to handle an arbitrary nesting depth. Perl 5.6 has provided an
1614     experimental facility that allows regular expressions to recurse (amongst other
1615     things). It does this by interpolating Perl code in the expression at run time,
1616     and the code can refer to the expression itself. A Perl pattern to solve the
1617     parentheses problem can be created like this:
1619     $re = qr{\\( (?: (?>[^()]+) | (?p{$re}) )* \\)}x;
1621     The (?p{...}) item interpolates Perl code at run time, and in this case refers
1622     recursively to the pattern in which it appears. Obviously, PCRE cannot support
1623     the interpolation of Perl code. Instead, the special item (?R) is provided for
1624     the specific case of recursion. This PCRE pattern solves the parentheses
1625     problem (assume the PCRE_EXTENDED option is set so that white space is
1626     ignored):
1628     \\( ( (?>[^()]+) | (?R) )* \\)
1630     First it matches an opening parenthesis. Then it matches any number of
1631     substrings which can either be a sequence of non-parentheses, or a recursive
1632     match of the pattern itself (i.e. a correctly parenthesized substring). Finally
1633     there is a closing parenthesis.
1635     This particular example pattern contains nested unlimited repeats, and so the
1636     use of a once-only subpattern for matching strings of non-parentheses is
1637     important when applying the pattern to strings that do not match. For example,
1638     when it is applied to
1640     (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
1642     it yields "no match" quickly. However, if a once-only subpattern is not used,
1643     the match runs for a very long time indeed because there are so many different
1644     ways the + and * repeats can carve up the subject, and all have to be tested
1645     before failure can be reported.
1647     The values set for any capturing subpatterns are those from the outermost level
1648     of the recursion at which the subpattern value is set. If the pattern above is
1649     matched against
1651     (ab(cd)ef)
1653     the value for the capturing parentheses is "ef", which is the last value taken
1654     on at the top level. If additional parentheses are added, giving
1656     \\( ( ( (?>[^()]+) | (?R) )* ) \\)
1657     ^ ^
1658     ^ ^
1659 nigel 47 the string they capture is "ab(cd)ef", the contents of the top level
1660 nigel 43 parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE
1661     has to obtain extra memory to store data during a recursion, which it does by
1662     using \fBpcre_malloc\fR, freeing it via \fBpcre_free\fR afterwards. If no
1663     memory can be obtained, it saves data for the first 15 capturing parentheses
1664     only, as there is no way to give an out-of-memory error from within a
1665     recursion.
1668 nigel 41 .SH PERFORMANCE
1669     Certain items that may appear in patterns are more efficient than others. It is
1670     more efficient to use a character class like [aeiou] than a set of alternatives
1671     such as (a|e|i|o|u). In general, the simplest construction that provides the
1672     required behaviour is usually the most efficient. Jeffrey Friedl's book
1673     contains a lot of discussion about optimizing regular expressions for efficient
1674     performance.
1676     When a pattern begins with .* and the PCRE_DOTALL option is set, the pattern is
1677     implicitly anchored by PCRE, since it can match only at the start of a subject
1678     string. However, if PCRE_DOTALL is not set, PCRE cannot make this optimization,
1679     because the . metacharacter does not then match a newline, and if the subject
1680     string contains newlines, the pattern may match from the character immediately
1681     following one of them instead of from the very start. For example, the pattern
1683     (.*) second
1685     matches the subject "first\\nand second" (where \\n stands for a newline
1686     character) with the first captured substring being "and". In order to do this,
1687     PCRE has to retry the match starting after every newline in the subject.
1689     If you are using such a pattern with subject strings that do not contain
1690     newlines, the best performance is obtained by setting PCRE_DOTALL, or starting
1691     the pattern with ^.* to indicate explicit anchoring. That saves PCRE from
1692     having to scan along the subject looking for a newline to restart at.
1694     Beware of patterns that contain nested indefinite repeats. These can take a
1695     long time to run when applied to a string that does not match. Consider the
1696     pattern fragment
1698     (a+)*
1700     This can match "aaaa" in 33 different ways, and this number increases very
1701     rapidly as the string gets longer. (The * repeat can match 0, 1, 2, 3, or 4
1702     times, and for each of those cases other than 0, the + repeats can match
1703     different numbers of times.) When the remainder of the pattern is such that the
1704     entire match is going to fail, PCRE has in principle to try every possible
1705     variation, and this can take an extremely long time.
1707     An optimization catches some of the more simple cases such as
1709     (a+)*b
1711     where a literal character follows. Before embarking on the standard matching
1712     procedure, PCRE checks that there is a "b" later in the subject string, and if
1713     there is not, it fails the match immediately. However, when there is no
1714     following literal this optimization cannot be used. You can see the difference
1715     by comparing the behaviour of
1717     (a+)*\\d
1719     with the pattern above. The former gives a failure almost instantly when
1720     applied to a whole line of "a" characters, whereas the latter takes an
1721     appreciable time with strings longer than about 20 characters.
1723 nigel 49
1724     .SH UTF-8 SUPPORT
1725     Starting at release 3.3, PCRE has some support for character strings encoded
1726     in the UTF-8 format. This is incomplete, and is regarded as experimental. In
1727     order to use it, you must configure PCRE to include UTF-8 support in the code,
1728     and, in addition, you must call \fBpcre_compile()\fR with the PCRE_UTF8 option
1729     flag. When you do this, both the pattern and any subject strings that are
1730     matched against it are treated as UTF-8 strings instead of just strings of
1731     bytes, but only in the cases that are mentioned below.
1733     If you compile PCRE with UTF-8 support, but do not use it at run time, the
1734     library will be a bit bigger, but the additional run time overhead is limited
1735     to testing the PCRE_UTF8 flag in several places, so should not be very large.
1737     PCRE assumes that the strings it is given contain valid UTF-8 codes. It does
1738     not diagnose invalid UTF-8 strings. If you pass invalid UTF-8 strings to PCRE,
1739     the results are undefined.
1741     Running with PCRE_UTF8 set causes these changes in the way PCRE works:
1743     1. In a pattern, the escape sequence \\x{...}, where the contents of the braces
1744     is a string of hexadecimal digits, is interpreted as a UTF-8 character whose
1745     code number is the given hexadecimal number, for example: \\x{1234}. This
1746     inserts from one to six literal bytes into the pattern, using the UTF-8
1747     encoding. If a non-hexadecimal digit appears between the braces, the item is
1748     not recognized.
1750     2. The original hexadecimal escape sequence, \\xhh, generates a two-byte UTF-8
1751     character if its value is greater than 127.
1753     3. Repeat quantifiers are NOT correctly handled if they follow a multibyte
1754     character. For example, \\x{100}* and \\xc3+ do not work. If you want to
1755     repeat such characters, you must enclose them in non-capturing parentheses,
1756     for example (?:\\x{100}), at present.
1758     4. The dot metacharacter matches one UTF-8 character instead of a single byte.
1760     5. Unlike literal UTF-8 characters, the dot metacharacter followed by a
1761     repeat quantifier does operate correctly on UTF-8 characters instead of
1762     single bytes.
1764     4. Although the \\x{...} escape is permitted in a character class, characters
1765     whose values are greater than 255 cannot be included in a class.
1767     5. A class is matched against a UTF-8 character instead of just a single byte,
1768     but it can match only characters whose values are less than 256. Characters
1769     with greater values always fail to match a class.
1771     6. Repeated classes work correctly on multiple characters.
1773     7. Classes containing just a single character whose value is greater than 127
1774     (but less than 256), for example, [\\x80] or [^\\x{93}], do not work because
1775     these are optimized into single byte matches. In the first case, of course,
1776     the class brackets are just redundant.
1778     8. Lookbehind assertions move backwards in the subject by a fixed number of
1779     characters instead of a fixed number of bytes. Simple cases have been tested
1780     to work correctly, but there may be hidden gotchas herein.
1782     9. The character types such as \\d and \\w do not work correctly with UTF-8
1783     characters. They continue to test a single byte.
1785     10. Anything not explicitly mentioned here continues to work in bytes rather
1786     than in characters.
1788     The following UTF-8 features of Perl 5.6 are not implemented:
1790     1. The escape sequence \\C to match a single byte.
1792     2. The use of Unicode tables and properties and escapes \\p, \\P, and \\X.
1794 nigel 41 .SH AUTHOR
1795     Philip Hazel <ph10@cam.ac.uk>
1796     .br
1797     University Computing Service,
1798     .br
1799     New Museums Site,
1800     .br
1801     Cambridge CB2 3QG, England.
1802     .br
1803     Phone: +44 1223 334714
1805 nigel 49 Last updated: 28 August 2000,
1806 nigel 41 .br
1807 nigel 49 the 250th anniversary of the death of J.S. Bach.
1808     .br
1809 nigel 43 Copyright (c) 1997-2000 University of Cambridge.

ViewVC Help
Powered by ViewVC 1.1.12