ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 37 - (hide annotations) (download) (as text)
Sat Feb 24 21:39:09 2007 UTC (8 years, 2 months ago) by nigel
File MIME type: text/html
File size: 75961 byte(s)
Load pcre-2.07 into code/trunk.

1 nigel 37 <HTML>
2     <HEAD>
3     <TITLE>pcre specification</TITLE>
4     </HEAD>
5     <body bgcolor="#FFFFFF" text="#00005A">
6     <H1>pcre specification</H1>
7     This HTML document has been generated automatically from the original man page.
8     If there is any nonsense in it, please consult the man page in case the
9     conversion went wrong.
10     <UL>
11     <LI><A NAME="TOC1" HREF="#SEC1">NAME</A>
24     <LI><A NAME="TOC14" HREF="#SEC14">BACKSLASH</A>
36     <LI><A NAME="TOC26" HREF="#SEC26">COMMENTS</A>
38     <LI><A NAME="TOC28" HREF="#SEC28">AUTHOR</A>
39     </UL>
40     <LI><A NAME="SEC1" HREF="#TOC1">NAME</A>
41     <P>
42     pcre - Perl-compatible regular expressions.
43     </P>
45     <P>
46     <B>#include &#60;pcre.h&#62;</B>
47     </P>
48     <P>
49     <B>pcre *pcre_compile(const char *<I>pattern</I>, int <I>options</I>,</B>
50     <B>const char **<I>errptr</I>, int *<I>erroffset</I>,</B>
51     <B>const unsigned char *<I>tableptr</I>);</B>
52     </P>
53     <P>
54     <B>pcre_extra *pcre_study(const pcre *<I>code</I>, int <I>options</I>,</B>
55     <B>const char **<I>errptr</I>);</B>
56     </P>
57     <P>
58     <B>int pcre_exec(const pcre *<I>code</I>, const pcre_extra *<I>extra</I>,</B>
59     <B>const char *<I>subject</I>, int <I>length</I>, int <I>startoffset</I>,</B>
60     <B>int <I>options</I>, int *<I>ovector</I>, int <I>ovecsize</I>);</B>
61     </P>
62     <P>
63     <B>int pcre_copy_substring(const char *<I>subject</I>, int *<I>ovector</I>,</B>
64     <B>int <I>stringcount</I>, int <I>stringnumber</I>, char *<I>buffer</I>,</B>
65     <B>int <I>buffersize</I>);</B>
66     </P>
67     <P>
68     <B>int pcre_get_substring(const char *<I>subject</I>, int *<I>ovector</I>,</B>
69     <B>int <I>stringcount</I>, int <I>stringnumber</I>,</B>
70     <B>const char **<I>stringptr</I>);</B>
71     </P>
72     <P>
73     <B>int pcre_get_substring_list(const char *<I>subject</I>,</B>
74     <B>int *<I>ovector</I>, int <I>stringcount</I>, const char ***<I>listptr</I>);</B>
75     </P>
76     <P>
77     <B>const unsigned char *pcre_maketables(void);</B>
78     </P>
79     <P>
80     <B>int pcre_info(const pcre *<I>code</I>, int *<I>optptr</I>, int</B>
81     <B>*<I>firstcharptr</I>);</B>
82     </P>
83     <P>
84     <B>char *pcre_version(void);</B>
85     </P>
86     <P>
87     <B>void *(*pcre_malloc)(size_t);</B>
88     </P>
89     <P>
90     <B>void (*pcre_free)(void *);</B>
91     </P>
93     <P>
94     The PCRE library is a set of functions that implement regular expression
95     pattern matching using the same syntax and semantics as Perl 5, with just a few
96     differences (see below). The current implementation corresponds to Perl 5.005.
97     </P>
98     <P>
99     PCRE has its own native API, which is described in this document. There is also
100     a set of wrapper functions that correspond to the POSIX API. These are
101     described in the <B>pcreposix</B> documentation.
102     </P>
103     <P>
104     The native API function prototypes are defined in the header file <B>pcre.h</B>,
105     and on Unix systems the library itself is called <B>libpcre.a</B>, so can be
106     accessed by adding <B>-lpcre</B> to the command for linking an application which
107     calls it.
108     </P>
109     <P>
110     The functions <B>pcre_compile()</B>, <B>pcre_study()</B>, and <B>pcre_exec()</B>
111     are used for compiling and matching regular expressions, while
112     <B>pcre_copy_substring()</B>, <B>pcre_get_substring()</B>, and
113     <B>pcre_get_substring_list()</B> are convenience functions for extracting
114     captured substrings from a matched subject string. The function
115     <B>pcre_maketables()</B> is used (optionally) to build a set of character tables
116     in the current locale for passing to <B>pcre_compile()</B>.
117     </P>
118     <P>
119     The function <B>pcre_info()</B> is used to find out information about a compiled
120     pattern, while the function <B>pcre_version()</B> returns a pointer to a string
121     containing the version of PCRE and its date of release.
122     </P>
123     <P>
124     The global variables <B>pcre_malloc</B> and <B>pcre_free</B> initially contain
125     the entry points of the standard <B>malloc()</B> and <B>free()</B> functions
126     respectively. PCRE calls the memory management functions via these variables,
127     so a calling program can replace them if it wishes to intercept the calls. This
128     should be done before calling any PCRE functions.
129     </P>
131     <P>
132     The PCRE functions can be used in multi-threading applications, with the
133     proviso that the memory management functions pointed to by <B>pcre_malloc</B>
134     and <B>pcre_free</B> are shared by all threads.
135     </P>
136     <P>
137     The compiled form of a regular expression is not altered during matching, so
138     the same compiled pattern can safely be used by several threads at once.
139     </P>
141     <P>
142     The function <B>pcre_compile()</B> is called to compile a pattern into an
143     internal form. The pattern is a C string terminated by a binary zero, and
144     is passed in the argument <I>pattern</I>. A pointer to a single block of memory
145     that is obtained via <B>pcre_malloc</B> is returned. This contains the
146     compiled code and related data. The <B>pcre</B> type is defined for this for
147     convenience, but in fact <B>pcre</B> is just a typedef for <B>void</B>, since the
148     contents of the block are not externally defined. It is up to the caller to
149     free the memory when it is no longer required.
150     </P>
151     <P>
152     The size of a compiled pattern is roughly proportional to the length of the
153     pattern string, except that each character class (other than those containing
154     just a single character, negated or not) requires 33 bytes, and repeat
155     quantifiers with a minimum greater than one or a bounded maximum cause the
156     relevant portions of the compiled pattern to be replicated.
157     </P>
158     <P>
159     The <I>options</I> argument contains independent bits that affect the
160     compilation. It should be zero if no options are required. Some of the options,
161     in particular, those that are compatible with Perl, can also be set and unset
162     from within the pattern (see the detailed description of regular expressions
163     below). For these options, the contents of the <I>options</I> argument specifies
164     their initial settings at the start of compilation and execution. The
165     PCRE_ANCHORED option can be set at the time of matching as well as at compile
166     time.
167     </P>
168     <P>
169     If <I>errptr</I> is NULL, <B>pcre_compile()</B> returns NULL immediately.
170     Otherwise, if compilation of a pattern fails, <B>pcre_compile()</B> returns
171     NULL, and sets the variable pointed to by <I>errptr</I> to point to a textual
172     error message. The offset from the start of the pattern to the character where
173     the error was discovered is placed in the variable pointed to by
174     <I>erroffset</I>, which must not be NULL. If it is, an immediate error is given.
175     </P>
176     <P>
177     If the final argument, <I>tableptr</I>, is NULL, PCRE uses a default set of
178     character tables which are built when it is compiled, using the default C
179     locale. Otherwise, <I>tableptr</I> must be the result of a call to
180     <B>pcre_maketables()</B>. See the section on locale support below.
181     </P>
182     <P>
183     The following option bits are defined in the header file:
184     </P>
185     <P>
186     <PRE>
188     </PRE>
189     </P>
190     <P>
191     If this bit is set, the pattern is forced to be "anchored", that is, it is
192     constrained to match only at the start of the string which is being searched
193     (the "subject string"). This effect can also be achieved by appropriate
194     constructs in the pattern itself, which is the only way to do it in Perl.
195     </P>
196     <P>
197     <PRE>
199     </PRE>
200     </P>
201     <P>
202     If this bit is set, letters in the pattern match both upper and lower case
203     letters. It is equivalent to Perl's /i option.
204     </P>
205     <P>
206     <PRE>
208     </PRE>
209     </P>
210     <P>
211     If this bit is set, a dollar metacharacter in the pattern matches only at the
212     end of the subject string. Without this option, a dollar also matches
213     immediately before the final character if it is a newline (but not before any
214     other newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
215     set. There is no equivalent to this option in Perl.
216     </P>
217     <P>
218     <PRE>
220     </PRE>
221     </P>
222     <P>
223     If this bit is set, a dot metacharater in the pattern matches all characters,
224     including newlines. Without it, newlines are excluded. This option is
225     equivalent to Perl's /s option. A negative class such as [^a] always matches a
226     newline character, independent of the setting of this option.
227     </P>
228     <P>
229     <PRE>
231     </PRE>
232     </P>
233     <P>
234     If this bit is set, whitespace data characters in the pattern are totally
235     ignored except when escaped or inside a character class, and characters between
236     an unescaped # outside a character class and the next newline character,
237     inclusive, are also ignored. This is equivalent to Perl's /x option, and makes
238     it possible to include comments inside complicated patterns. Note, however,
239     that this applies only to data characters. Whitespace characters may never
240     appear within special character sequences in a pattern, for example within the
241     sequence (?( which introduces a conditional subpattern.
242     </P>
243     <P>
244     <PRE>
245     PCRE_EXTRA
246     </PRE>
247     </P>
248     <P>
249     This option turns on additional functionality of PCRE that is incompatible with
250     Perl. Any backslash in a pattern that is followed by a letter that has no
251     special meaning causes an error, thus reserving these combinations for future
252     expansion. By default, as in Perl, a backslash followed by a letter with no
253     special meaning is treated as a literal. There are at present no other features
254     controlled by this option.
255     </P>
256     <P>
257     <PRE>
259     </PRE>
260     </P>
261     <P>
262     By default, PCRE treats the subject string as consisting of a single "line" of
263     characters (even if it actually contains several newlines). The "start of line"
264     metacharacter (^) matches only at the start of the string, while the "end of
265     line" metacharacter ($) matches only at the end of the string, or before a
266     terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
267     Perl.
268     </P>
269     <P>
270     When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
271     match immediately following or immediately before any newline in the subject
272     string, respectively, as well as at the very start and end. This is equivalent
273     to Perl's /m option. If there are no "\n" characters in a subject string, or
274     no occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no
275     effect.
276     </P>
277     <P>
278     <PRE>
280     </PRE>
281     </P>
282     <P>
283     This option inverts the "greediness" of the quantifiers so that they are not
284     greedy by default, but become greedy if followed by "?". It is not compatible
285     with Perl. It can also be set by a (?U) option setting within the pattern.
286     </P>
288     <P>
289     When a pattern is going to be used several times, it is worth spending more
290     time analyzing it in order to speed up the time taken for matching. The
291     function <B>pcre_study()</B> takes a pointer to a compiled pattern as its first
292     argument, and returns a pointer to a <B>pcre_extra</B> block (another <B>void</B>
293     typedef) containing additional information about the pattern; this can be
294     passed to <B>pcre_exec()</B>. If no additional information is available, NULL
295     is returned.
296     </P>
297     <P>
298     The second argument contains option bits. At present, no options are defined
299     for <B>pcre_study()</B>, and this argument should always be zero.
300     </P>
301     <P>
302     The third argument for <B>pcre_study()</B> is a pointer to an error message. If
303     studying succeeds (even if no data is returned), the variable it points to is
304     set to NULL. Otherwise it points to a textual error message.
305     </P>
306     <P>
307     At present, studying a pattern is useful only for non-anchored patterns that do
308     not have a single fixed starting character. A bitmap of possible starting
309     characters is created.
310     </P>
312     <P>
313     PCRE handles caseless matching, and determines whether characters are letters,
314     digits, or whatever, by reference to a set of tables. The library contains a
315     default set of tables which is created in the default C locale when PCRE is
316     compiled. This is used when the final argument of <B>pcre_compile()</B> is NULL,
317     and is sufficient for many applications.
318     </P>
319     <P>
320     An alternative set of tables can, however, be supplied. Such tables are built
321     by calling the <B>pcre_maketables()</B> function, which has no arguments, in the
322     relevant locale. The result can then be passed to <B>pcre_compile()</B> as often
323     as necessary. For example, to build and use tables that are appropriate for the
324     French locale (where accented characters with codes greater than 128 are
325     treated as letters), the following code could be used:
326     </P>
327     <P>
328     <PRE>
329     setlocale(LC_CTYPE, "fr");
330     tables = pcre_maketables();
331     re = pcre_compile(..., tables);
332     </PRE>
333     </P>
334     <P>
335     The tables are built in memory that is obtained via <B>pcre_malloc</B>. The
336     pointer that is passed to <B>pcre_compile</B> is saved with the compiled
337     pattern, and the same tables are used via this pointer by <B>pcre_study()</B>
338     and <B>pcre_exec()</B>. Thus for any single pattern, compilation, studying and
339     matching all happen in the same locale, but different patterns can be compiled
340     in different locales. It is the caller's responsibility to ensure that the
341     memory containing the tables remains available for as long as it is needed.
342     </P>
344     <P>
345     The <B>pcre_info()</B> function returns information about a compiled pattern.
346     Its yield is the number of capturing subpatterns, or one of the following
347     negative numbers:
348     </P>
349     <P>
350     <PRE>
351     PCRE_ERROR_NULL the argument <I>code</I> was NULL
352     PCRE_ERROR_BADMAGIC the "magic number" was not found
353     </PRE>
354     </P>
355     <P>
356     If the <I>optptr</I> argument is not NULL, a copy of the options with which the
357     pattern was compiled is placed in the integer it points to. These option bits
358     are those specified in the call to <B>pcre_compile()</B>, modified by any
359     top-level option settings within the pattern itself, and with the PCRE_ANCHORED
360     bit set if the form of the pattern implies that it can match only at the start
361     of a subject string.
362     </P>
363     <P>
364     If the pattern is not anchored and the <I>firstcharptr</I> argument is not NULL,
365     it is used to pass back information about the first character of any matched
366     string. If there is a fixed first character, e.g. from a pattern such as
367     (cat|cow|coyote), then it is returned in the integer pointed to by
368     <I>firstcharptr</I>. Otherwise, if either
369     </P>
370     <P>
371     (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
372     starts with "^", or
373     </P>
374     <P>
375     (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
376     (if it were set, the pattern would be anchored),
377     </P>
378     <P>
379     then -1 is returned, indicating that the pattern matches only at the
380     start of a subject string or after any "\n" within the string. Otherwise -2 is
381     returned.
382     </P>
384     <P>
385     The function <B>pcre_exec()</B> is called to match a subject string against a
386     pre-compiled pattern, which is passed in the <I>code</I> argument. If the
387     pattern has been studied, the result of the study should be passed in the
388     <I>extra</I> argument. Otherwise this must be NULL.
389     </P>
390     <P>
391     The PCRE_ANCHORED option can be passed in the <I>options</I> argument, whose
392     unused bits must be zero. However, if a pattern was compiled with
393     PCRE_ANCHORED, or turned out to be anchored by virtue of its contents, it
394     cannot be made unachored at matching time.
395     </P>
396     <P>
397     There are also three further options that can be set only at matching time:
398     </P>
399     <P>
400     <PRE>
402     </PRE>
403     </P>
404     <P>
405     The first character of the string is not the beginning of a line, so the
406     circumflex metacharacter should not match before it. Setting this without
407     PCRE_MULTILINE (at compile time) causes circumflex never to match.
408     </P>
409     <P>
410     <PRE>
412     </PRE>
413     </P>
414     <P>
415     The end of the string is not the end of a line, so the dollar metacharacter
416     should not match it nor (except in multiline mode) a newline immediately before
417     it. Setting this without PCRE_MULTILINE (at compile time) causes dollar never
418     to match.
419     </P>
420     <P>
421     <PRE>
423     </PRE>
424     </P>
425     <P>
426     An empty string is not considered to be a valid match if this option is set. If
427     there are alternatives in the pattern, they are tried. If all the alternatives
428     match the empty string, the entire match fails. For example, if the pattern
429     </P>
430     <P>
431     <PRE>
432     a?b?
433     </PRE>
434     </P>
435     <P>
436     is applied to a string not beginning with "a" or "b", it matches the empty
437     string at the start of the subject. With PCRE_NOTEMPTY set, this match is not
438     valid, so PCRE searches further into the string for occurrences of "a" or "b".
439     Perl has no direct equivalent of this option, but it makes a special case of
440     a pattern match of the empty string within its <B>split()</B> function. Using
441     PCRE_NOTEMPTY it is possible to emulate this behaviour.
442     </P>
443     <P>
444     The subject string is passed as a pointer in <I>subject</I>, a length in
445     <I>length</I>, and a starting offset in <I>startoffset</I>. Unlike the pattern
446     string, it may contain binary zero characters. When the starting offset is
447     zero, the search for a match starts at the beginning of the subject, and this
448     is by far the most common case.
449     </P>
450     <P>
451     A non-zero starting offset is useful when searching for another match in the
452     same subject by calling <B>pcre_exec()</B> again after a previous success.
453     Setting <I>startoffset</I> differs from just passing over a shortened string and
454     setting PCRE_NOTBOL in the case of a pattern that begins with any kind of
455     lookbehind. For example, consider the pattern
456     </P>
457     <P>
458     <PRE>
459     \Biss\B
460     </PRE>
461     </P>
462     <P>
463     which finds occurrences of "iss" in the middle of words. (\B matches only if
464     the current position in the subject is not a word boundary.) When applied to
465     the string "Mississipi" the first call to <B>pcre_exec()</B> finds the first
466     occurrence. If <B>pcre_exec()</B> is called again with just the remainder of the
467     subject, namely "issipi", it does not match, because \B is always false at the
468     start of the subject, which is deemed to be a word boundary. However, if
469     <B>pcre_exec()</B> is passed the entire string again, but with <I>startoffset</I>
470     set to 4, it finds the second occurrence of "iss" because it is able to look
471     behind the starting point to discover that it is preceded by a letter.
472     </P>
473     <P>
474     If a non-zero starting offset is passed when the pattern is anchored, one
475     attempt to match at the given offset is tried. This can only succeed if the
476     pattern does not require the match to be at the start of the subject.
477     </P>
478     <P>
479     In general, a pattern matches a certain portion of the subject, and in
480     addition, further substrings from the subject may be picked out by parts of the
481     pattern. Following the usage in Jeffrey Friedl's book, this is called
482     "capturing" in what follows, and the phrase "capturing subpattern" is used for
483     a fragment of a pattern that picks out a substring. PCRE supports several other
484     kinds of parenthesized subpattern that do not cause substrings to be captured.
485     </P>
486     <P>
487     Captured substrings are returned to the caller via a vector of integer offsets
488     whose address is passed in <I>ovector</I>. The number of elements in the vector
489     is passed in <I>ovecsize</I>. The first two-thirds of the vector is used to pass
490     back captured substrings, each substring using a pair of integers. The
491     remaining third of the vector is used as workspace by <B>pcre_exec()</B> while
492     matching capturing subpatterns, and is not available for passing back
493     information. The length passed in <I>ovecsize</I> should always be a multiple of
494     three. If it is not, it is rounded down.
495     </P>
496     <P>
497     When a match has been successful, information about captured substrings is
498     returned in pairs of integers, starting at the beginning of <I>ovector</I>, and
499     continuing up to two-thirds of its length at the most. The first element of a
500     pair is set to the offset of the first character in a substring, and the second
501     is set to the offset of the first character after the end of a substring. The
502     first pair, <I>ovector[0]</I> and <I>ovector[1]</I>, identify the portion of the
503     subject string matched by the entire pattern. The next pair is used for the
504     first capturing subpattern, and so on. The value returned by <B>pcre_exec()</B>
505     is the number of pairs that have been set. If there are no capturing
506     subpatterns, the return value from a successful match is 1, indicating that
507     just the first pair of offsets has been set.
508     </P>
509     <P>
510     Some convenience functions are provided for extracting the captured substrings
511     as separate strings. These are described in the following section.
512     </P>
513     <P>
514     It is possible for an capturing subpattern number <I>n+1</I> to match some
515     part of the subject when subpattern <I>n</I> has not been used at all. For
516     example, if the string "abc" is matched against the pattern (a|(z))(bc)
517     subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
518     values corresponding to the unused subpattern are set to -1.
519     </P>
520     <P>
521     If a capturing subpattern is matched repeatedly, it is the last portion of the
522     string that it matched that gets returned.
523     </P>
524     <P>
525     If the vector is too small to hold all the captured substrings, it is used as
526     far as possible (up to two-thirds of its length), and the function returns a
527     value of zero. In particular, if the substring offsets are not of interest,
528     <B>pcre_exec()</B> may be called with <I>ovector</I> passed as NULL and
529     <I>ovecsize</I> as zero. However, if the pattern contains back references and
530     the <I>ovector</I> isn't big enough to remember the related substrings, PCRE has
531     to get additional memory for use during matching. Thus it is usually advisable
532     to supply an <I>ovector</I>.
533     </P>
534     <P>
535     Note that <B>pcre_info()</B> can be used to find out how many capturing
536     subpatterns there are in a compiled pattern. The smallest size for
537     <I>ovector</I> that will allow for <I>n</I> captured substrings in addition to
538     the offsets of the substring matched by the whole pattern is (<I>n</I>+1)*3.
539     </P>
540     <P>
541     If <B>pcre_exec()</B> fails, it returns a negative number. The following are
542     defined in the header file:
543     </P>
544     <P>
545     <PRE>
547     </PRE>
548     </P>
549     <P>
550     The subject string did not match the pattern.
551     </P>
552     <P>
553     <PRE>
554     PCRE_ERROR_NULL (-2)
555     </PRE>
556     </P>
557     <P>
558     Either <I>code</I> or <I>subject</I> was passed as NULL, or <I>ovector</I> was
559     NULL and <I>ovecsize</I> was not zero.
560     </P>
561     <P>
562     <PRE>
564     </PRE>
565     </P>
566     <P>
567     An unrecognized bit was set in the <I>options</I> argument.
568     </P>
569     <P>
570     <PRE>
572     </PRE>
573     </P>
574     <P>
575     PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
576     the case when it is passed a junk pointer. This is the error it gives when the
577     magic number isn't present.
578     </P>
579     <P>
580     <PRE>
582     </PRE>
583     </P>
584     <P>
585     While running the pattern match, an unknown item was encountered in the
586     compiled pattern. This error could be caused by a bug in PCRE or by overwriting
587     of the compiled pattern.
588     </P>
589     <P>
590     <PRE>
592     </PRE>
593     </P>
594     <P>
595     If a pattern contains back references, but the <I>ovector</I> that is passed to
596     <B>pcre_exec()</B> is not big enough to remember the referenced substrings, PCRE
597     gets a block of memory at the start of matching to use for this purpose. If the
598     call via <B>pcre_malloc()</B> fails, this error is given. The memory is freed at
599     the end of matching.
600     </P>
602     <P>
603     Captured substrings can be accessed directly by using the offsets returned by
604     <B>pcre_exec()</B> in <I>ovector</I>. For convenience, the functions
605     <B>pcre_copy_substring()</B>, <B>pcre_get_substring()</B>, and
606     <B>pcre_get_substring_list()</B> are provided for extracting captured substrings
607     as new, separate, zero-terminated strings. A substring that contains a binary
608     zero is correctly extracted and has a further zero added on the end, but the
609     result does not, of course, function as a C string.
610     </P>
611     <P>
612     The first three arguments are the same for all three functions: <I>subject</I>
613     is the subject string which has just been successfully matched, <I>ovector</I>
614     is a pointer to the vector of integer offsets that was passed to
615     <B>pcre_exec()</B>, and <I>stringcount</I> is the number of substrings that
616     were captured by the match, including the substring that matched the entire
617     regular expression. This is the value returned by <B>pcre_exec</B> if it
618     is greater than zero. If <B>pcre_exec()</B> returned zero, indicating that it
619     ran out of space in <I>ovector</I>, then the value passed as
620     <I>stringcount</I> should be the size of the vector divided by three.
621     </P>
622     <P>
623     The functions <B>pcre_copy_substring()</B> and <B>pcre_get_substring()</B>
624     extract a single substring, whose number is given as <I>stringnumber</I>. A
625     value of zero extracts the substring that matched the entire pattern, while
626     higher values extract the captured substrings. For <B>pcre_copy_substring()</B>,
627     the string is placed in <I>buffer</I>, whose length is given by
628     <I>buffersize</I>, while for <B>pcre_get_substring()</B> a new block of store is
629     obtained via <B>pcre_malloc</B>, and its address is returned via
630     <I>stringptr</I>. The yield of the function is the length of the string, not
631     including the terminating zero, or one of
632     </P>
633     <P>
634     <PRE>
636     </PRE>
637     </P>
638     <P>
639     The buffer was too small for <B>pcre_copy_substring()</B>, or the attempt to get
640     memory failed for <B>pcre_get_substring()</B>.
641     </P>
642     <P>
643     <PRE>
645     </PRE>
646     </P>
647     <P>
648     There is no substring whose number is <I>stringnumber</I>.
649     </P>
650     <P>
651     The <B>pcre_get_substring_list()</B> function extracts all available substrings
652     and builds a list of pointers to them. All this is done in a single block of
653     memory which is obtained via <B>pcre_malloc</B>. The address of the memory block
654     is returned via <I>listptr</I>, which is also the start of the list of string
655     pointers. The end of the list is marked by a NULL pointer. The yield of the
656     function is zero if all went well, or
657     </P>
658     <P>
659     <PRE>
661     </PRE>
662     </P>
663     <P>
664     if the attempt to get the memory block failed.
665     </P>
666     <P>
667     When any of these functions encounter a substring that is unset, which can
668     happen when capturing subpattern number <I>n+1</I> matches some part of the
669     subject, but subpattern <I>n</I> has not been used at all, they return an empty
670     string. This can be distinguished from a genuine zero-length substring by
671     inspecting the appropriate offset in <I>ovector</I>, which is negative for unset
672     substrings.
673     </P>
675     <P>
676     There are some size limitations in PCRE but it is hoped that they will never in
677     practice be relevant.
678     The maximum length of a compiled pattern is 65539 (sic) bytes.
679     All values in repeating quantifiers must be less than 65536.
680     The maximum number of capturing subpatterns is 99.
681     The maximum number of all parenthesized subpatterns, including capturing
682     subpatterns, assertions, and other types of subpattern, is 200.
683     </P>
684     <P>
685     The maximum length of a subject string is the largest positive number that an
686     integer variable can hold. However, PCRE uses recursion to handle subpatterns
687     and indefinite repetition. This means that the available stack space may limit
688     the size of a subject string that can be processed by certain patterns.
689     </P>
691     <P>
692     The differences described here are with respect to Perl 5.005.
693     </P>
694     <P>
695     1. By default, a whitespace character is any character that the C library
696     function <B>isspace()</B> recognizes, though it is possible to compile PCRE with
697     alternative character type tables. Normally <B>isspace()</B> matches space,
698     formfeed, newline, carriage return, horizontal tab, and vertical tab. Perl 5
699     no longer includes vertical tab in its set of whitespace characters. The \v
700     escape that was in the Perl documentation for a long time was never in fact
701     recognized. However, the character itself was treated as whitespace at least
702     up to 5.002. In 5.004 and 5.005 it does not match \s.
703     </P>
704     <P>
705     2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl permits
706     them, but they do not mean what you might think. For example, (?!a){3} does
707     not assert that the next three characters are not "a". It just asserts that the
708     next character is not "a" three times.
709     </P>
710     <P>
711     3. Capturing subpatterns that occur inside negative lookahead assertions are
712     counted, but their entries in the offsets vector are never set. Perl sets its
713     numerical variables from any such patterns that are matched before the
714     assertion fails to match something (thereby succeeding), but only if the
715     negative lookahead assertion contains just one branch.
716     </P>
717     <P>
718     4. Though binary zero characters are supported in the subject string, they are
719     not allowed in a pattern string because it is passed as a normal C string,
720     terminated by zero. The escape sequence "\0" can be used in the pattern to
721     represent a binary zero.
722     </P>
723     <P>
724     5. The following Perl escape sequences are not supported: \l, \u, \L, \U,
725     \E, \Q. In fact these are implemented by Perl's general string-handling and
726     are not part of its pattern matching engine.
727     </P>
728     <P>
729     6. The Perl \G assertion is not supported as it is not relevant to single
730     pattern matches.
731     </P>
732     <P>
733     7. Fairly obviously, PCRE does not support the (?{code}) construction.
734     </P>
735     <P>
736     8. There are at the time of writing some oddities in Perl 5.005_02 concerned
737     with the settings of captured strings when part of a pattern is repeated. For
738     example, matching "aba" against the pattern /^(a(b)?)+$/ sets $2 to the value
739     "b", but matching "aabbaa" against /^(aa(bb)?)+$/ leaves $2 unset. However, if
740     the pattern is changed to /^(aa(b(b))?)+$/ then $2 (and $3) get set.
741     </P>
742     <P>
743     In Perl 5.004 $2 is set in both cases, and that is also true of PCRE. If in the
744     future Perl changes to a consistent state that is different, PCRE may change to
745     follow.
746     </P>
747     <P>
748     9. Another as yet unresolved discrepancy is that in Perl 5.005_02 the pattern
749     /^(a)?(?(1)a|b)+$/ matches the string "a", whereas in PCRE it does not.
750     However, in both Perl and PCRE /^(a)?a/ matched against "a" leaves $1 unset.
751     </P>
752     <P>
753     10. PCRE provides some extensions to the Perl regular expression facilities:
754     </P>
755     <P>
756     (a) Although lookbehind assertions must match fixed length strings, each
757     alternative branch of a lookbehind assertion can match a different length of
758     string. Perl 5.005 requires them all to have the same length.
759     </P>
760     <P>
761     (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ meta-
762     character matches only at the very end of the string.
763     </P>
764     <P>
765     (c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
766     meaning is faulted.
767     </P>
768     <P>
769     (d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
770     inverted, that is, by default they are not greedy, but if followed by a
771     question mark they are.
772     </P>
773     <P>
774     (e) PCRE_ANCHORED can be used to force a pattern to be tried only at the start
775     of the subject.
776     </P>
777     <P>
778     (f) The PCRE_NOTBOL, PCRE_NOTEOL, and PCRE_NOTEMPTY options for
779     <B>pcre_exec()</B> have no Perl equivalents.
780     </P>
782     <P>
783     The syntax and semantics of the regular expressions supported by PCRE are
784     described below. Regular expressions are also described in the Perl
785     documentation and in a number of other books, some of which have copious
786     examples. Jeffrey Friedl's "Mastering Regular Expressions", published by
787     O'Reilly (ISBN 1-56592-257-3), covers them in great detail. The description
788     here is intended as reference documentation.
789     </P>
790     <P>
791     A regular expression is a pattern that is matched against a subject string from
792     left to right. Most characters stand for themselves in a pattern, and match the
793     corresponding characters in the subject. As a trivial example, the pattern
794     </P>
795     <P>
796     <PRE>
797     The quick brown fox
798     </PRE>
799     </P>
800     <P>
801     matches a portion of a subject string that is identical to itself. The power of
802     regular expressions comes from the ability to include alternatives and
803     repetitions in the pattern. These are encoded in the pattern by the use of
804     <I>meta-characters</I>, which do not stand for themselves but instead are
805     interpreted in some special way.
806     </P>
807     <P>
808     There are two different sets of meta-characters: those that are recognized
809     anywhere in the pattern except within square brackets, and those that are
810     recognized in square brackets. Outside square brackets, the meta-characters are
811     as follows:
812     </P>
813     <P>
814     <PRE>
815     \ general escape character with several uses
816     ^ assert start of subject (or line, in multiline mode)
817     $ assert end of subject (or line, in multiline mode)
818     . match any character except newline (by default)
819     [ start character class definition
820     | start of alternative branch
821     ( start subpattern
822     ) end subpattern
823     ? extends the meaning of (
824     also 0 or 1 quantifier
825     also quantifier minimizer
826     * 0 or more quantifier
827     + 1 or more quantifier
828     { start min/max quantifier
829     </PRE>
830     </P>
831     <P>
832     Part of a pattern that is in square brackets is called a "character class". In
833     a character class the only meta-characters are:
834     </P>
835     <P>
836     <PRE>
837     \ general escape character
838     ^ negate the class, but only if the first character
839     - indicates character range
840     ] terminates the character class
841     </PRE>
842     </P>
843     <P>
844     The following sections describe the use of each of the meta-characters.
845     </P>
846     <LI><A NAME="SEC14" HREF="#TOC1">BACKSLASH</A>
847     <P>
848     The backslash character has several uses. Firstly, if it is followed by a
849     non-alphameric character, it takes away any special meaning that character may
850     have. This use of backslash as an escape character applies both inside and
851     outside character classes.
852     </P>
853     <P>
854     For example, if you want to match a "*" character, you write "\*" in the
855     pattern. This applies whether or not the following character would otherwise be
856     interpreted as a meta-character, so it is always safe to precede a
857     non-alphameric with "\" to specify that it stands for itself. In particular,
858     if you want to match a backslash, you write "\\".
859     </P>
860     <P>
861     If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
862     pattern (other than in a character class) and characters between a "#" outside
863     a character class and the next newline character are ignored. An escaping
864     backslash can be used to include a whitespace or "#" character as part of the
865     pattern.
866     </P>
867     <P>
868     A second use of backslash provides a way of encoding non-printing characters
869     in patterns in a visible manner. There is no restriction on the appearance of
870     non-printing characters, apart from the binary zero that terminates a pattern,
871     but when a pattern is being prepared by text editing, it is usually easier to
872     use one of the following escape sequences than the binary character it
873     represents:
874     </P>
875     <P>
876     <PRE>
877     \a alarm, that is, the BEL character (hex 07)
878     \cx "control-x", where x is any character
879     \e escape (hex 1B)
880     \f formfeed (hex 0C)
881     \n newline (hex 0A)
882     \r carriage return (hex 0D)
883     \t tab (hex 09)
884     \xhh character with hex code hh
885     \ddd character with octal code ddd, or backreference
886     </PRE>
887     </P>
888     <P>
889     The precise effect of "\cx" is as follows: if "x" is a lower case letter, it
890     is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
891     Thus "\cz" becomes hex 1A, but "\c{" becomes hex 3B, while "\c;" becomes hex
892     7B.
893     </P>
894     <P>
895     After "\x", up to two hexadecimal digits are read (letters can be in upper or
896     lower case).
897     </P>
898     <P>
899     After "\0" up to two further octal digits are read. In both cases, if there
900     are fewer than two digits, just those that are present are used. Thus the
901     sequence "\0\x\07" specifies two binary zeros followed by a BEL character.
902     Make sure you supply two digits after the initial zero if the character that
903     follows is itself an octal digit.
904     </P>
905     <P>
906     The handling of a backslash followed by a digit other than 0 is complicated.
907     Outside a character class, PCRE reads it and any following digits as a decimal
908     number. If the number is less than 10, or if there have been at least that many
909     previous capturing left parentheses in the expression, the entire sequence is
910     taken as a <I>back reference</I>. A description of how this works is given
911     later, following the discussion of parenthesized subpatterns.
912     </P>
913     <P>
914     Inside a character class, or if the decimal number is greater than 9 and there
915     have not been that many capturing subpatterns, PCRE re-reads up to three octal
916     digits following the backslash, and generates a single byte from the least
917     significant 8 bits of the value. Any subsequent digits stand for themselves.
918     For example:
919     </P>
920     <P>
921     <PRE>
922     \040 is another way of writing a space
923     \40 is the same, provided there are fewer than 40
924     previous capturing subpatterns
925     \7 is always a back reference
926     \11 might be a back reference, or another way of
927     writing a tab
928     \011 is always a tab
929     \0113 is a tab followed by the character "3"
930     \113 is the character with octal code 113 (since there
931     can be no more than 99 back references)
932     \377 is a byte consisting entirely of 1 bits
933     \81 is either a back reference, or a binary zero
934     followed by the two characters "8" and "1"
935     </PRE>
936     </P>
937     <P>
938     Note that octal values of 100 or greater must not be introduced by a leading
939     zero, because no more than three octal digits are ever read.
940     </P>
941     <P>
942     All the sequences that define a single byte value can be used both inside and
943     outside character classes. In addition, inside a character class, the sequence
944     "\b" is interpreted as the backspace character (hex 08). Outside a character
945     class it has a different meaning (see below).
946     </P>
947     <P>
948     The third use of backslash is for specifying generic character types:
949     </P>
950     <P>
951     <PRE>
952     \d any decimal digit
953     \D any character that is not a decimal digit
954     \s any whitespace character
955     \S any character that is not a whitespace character
956     \w any "word" character
957     \W any "non-word" character
958     </PRE>
959     </P>
960     <P>
961     Each pair of escape sequences partitions the complete set of characters into
962     two disjoint sets. Any given character matches one, and only one, of each pair.
963     </P>
964     <P>
965     A "word" character is any letter or digit or the underscore character, that is,
966     any character which can be part of a Perl "word". The definition of letters and
967     digits is controlled by PCRE's character tables, and may vary if locale-
968     specific matching is taking place (see "Locale support" above). For example, in
969     the "fr" (French) locale, some character codes greater than 128 are used for
970     accented letters, and these are matched by \w.
971     </P>
972     <P>
973     These character type sequences can appear both inside and outside character
974     classes. They each match one character of the appropriate type. If the current
975     matching point is at the end of the subject string, all of them fail, since
976     there is no character to match.
977     </P>
978     <P>
979     The fourth use of backslash is for certain simple assertions. An assertion
980     specifies a condition that has to be met at a particular point in a match,
981     without consuming any characters from the subject string. The use of
982     subpatterns for more complicated assertions is described below. The backslashed
983     assertions are
984     </P>
985     <P>
986     <PRE>
987     \b word boundary
988     \B not a word boundary
989     \A start of subject (independent of multiline mode)
990     \Z end of subject or newline at end (independent of multiline mode)
991     \z end of subject (independent of multiline mode)
992     </PRE>
993     </P>
994     <P>
995     These assertions may not appear in character classes (but note that "\b" has a
996     different meaning, namely the backspace character, inside a character class).
997     </P>
998     <P>
999     A word boundary is a position in the subject string where the current character
1000     and the previous character do not both match \w or \W (i.e. one matches
1001     \w and the other matches \W), or the start or end of the string if the
1002     first or last character matches \w, respectively.
1003     </P>
1004     <P>
1005     The \A, \Z, and \z assertions differ from the traditional circumflex and
1006     dollar (described below) in that they only ever match at the very start and end
1007     of the subject string, whatever options are set. They are not affected by the
1008     PCRE_NOTBOL or PCRE_NOTEOL options. If the <I>startoffset</I> argument of
1009     <B>pcre_exec()</B> is non-zero, \A can never match. The difference between \Z
1010     and \z is that \Z matches before a newline that is the last character of the
1011     string as well as at the end of the string, whereas \z matches only at the
1012     end.
1013     </P>
1015     <P>
1016     Outside a character class, in the default matching mode, the circumflex
1017     character is an assertion which is true only if the current matching point is
1018     at the start of the subject string. If the <I>startoffset</I> argument of
1019     <B>pcre_exec()</B> is non-zero, circumflex can never match. Inside a character
1020     class, circumflex has an entirely different meaning (see below).
1021     </P>
1022     <P>
1023     Circumflex need not be the first character of the pattern if a number of
1024     alternatives are involved, but it should be the first thing in each alternative
1025     in which it appears if the pattern is ever to match that branch. If all
1026     possible alternatives start with a circumflex, that is, if the pattern is
1027     constrained to match only at the start of the subject, it is said to be an
1028     "anchored" pattern. (There are also other constructs that can cause a pattern
1029     to be anchored.)
1030     </P>
1031     <P>
1032     A dollar character is an assertion which is true only if the current matching
1033     point is at the end of the subject string, or immediately before a newline
1034     character that is the last character in the string (by default). Dollar need
1035     not be the last character of the pattern if a number of alternatives are
1036     involved, but it should be the last item in any branch in which it appears.
1037     Dollar has no special meaning in a character class.
1038     </P>
1039     <P>
1040     The meaning of dollar can be changed so that it matches only at the very end of
1041     the string, by setting the PCRE_DOLLAR_ENDONLY option at compile or matching
1042     time. This does not affect the \Z assertion.
1043     </P>
1044     <P>
1045     The meanings of the circumflex and dollar characters are changed if the
1046     PCRE_MULTILINE option is set. When this is the case, they match immediately
1047     after and immediately before an internal "\n" character, respectively, in
1048     addition to matching at the start and end of the subject string. For example,
1049     the pattern /^abc$/ matches the subject string "def\nabc" in multiline mode,
1050     but not otherwise. Consequently, patterns that are anchored in single line mode
1051     because all branches start with "^" are not anchored in multiline mode, and a
1052     match for circumflex is possible when the <I>startoffset</I> argument of
1053     <B>pcre_exec()</B> is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if
1054     PCRE_MULTILINE is set.
1055     </P>
1056     <P>
1057     Note that the sequences \A, \Z, and \z can be used to match the start and
1058     end of the subject in both modes, and if all branches of a pattern start with
1059     \A is it always anchored, whether PCRE_MULTILINE is set or not.
1060     </P>
1062     <P>
1063     Outside a character class, a dot in the pattern matches any one character in
1064     the subject, including a non-printing character, but not (by default) newline.
1065     If the PCRE_DOTALL option is set, then dots match newlines as well. The
1066     handling of dot is entirely independent of the handling of circumflex and
1067     dollar, the only relationship being that they both involve newline characters.
1068     Dot has no special meaning in a character class.
1069     </P>
1071     <P>
1072     An opening square bracket introduces a character class, terminated by a closing
1073     square bracket. A closing square bracket on its own is not special. If a
1074     closing square bracket is required as a member of the class, it should be the
1075     first data character in the class (after an initial circumflex, if present) or
1076     escaped with a backslash.
1077     </P>
1078     <P>
1079     A character class matches a single character in the subject; the character must
1080     be in the set of characters defined by the class, unless the first character in
1081     the class is a circumflex, in which case the subject character must not be in
1082     the set defined by the class. If a circumflex is actually required as a member
1083     of the class, ensure it is not the first character, or escape it with a
1084     backslash.
1085     </P>
1086     <P>
1087     For example, the character class [aeiou] matches any lower case vowel, while
1088     [^aeiou] matches any character that is not a lower case vowel. Note that a
1089     circumflex is just a convenient notation for specifying the characters which
1090     are in the class by enumerating those that are not. It is not an assertion: it
1091     still consumes a character from the subject string, and fails if the current
1092     pointer is at the end of the string.
1093     </P>
1094     <P>
1095     When caseless matching is set, any letters in a class represent both their
1096     upper case and lower case versions, so for example, a caseless [aeiou] matches
1097     "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
1098     caseful version would.
1099     </P>
1100     <P>
1101     The newline character is never treated in any special way in character classes,
1102     whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE options is. A class
1103     such as [^a] will always match a newline.
1104     </P>
1105     <P>
1106     The minus (hyphen) character can be used to specify a range of characters in a
1107     character class. For example, [d-m] matches any letter between d and m,
1108     inclusive. If a minus character is required in a class, it must be escaped with
1109     a backslash or appear in a position where it cannot be interpreted as
1110     indicating a range, typically as the first or last character in the class.
1111     </P>
1112     <P>
1113     It is not possible to have the literal character "]" as the end character of a
1114     range. A pattern such as [W-]46] is interpreted as a class of two characters
1115     ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
1116     "-46]". However, if the "]" is escaped with a backslash it is interpreted as
1117     the end of range, so [W-\]46] is interpreted as a single class containing a
1118     range followed by two separate characters. The octal or hexadecimal
1119     representation of "]" can also be used to end a range.
1120     </P>
1121     <P>
1122     Ranges operate in ASCII collating sequence. They can also be used for
1123     characters specified numerically, for example [\000-\037]. If a range that
1124     includes letters is used when caseless matching is set, it matches the letters
1125     in either case. For example, [W-c] is equivalent to [][\^_`wxyzabc], matched
1126     caselessly, and if character tables for the "fr" locale are in use,
1127     [\xc8-\xcb] matches accented E characters in both cases.
1128     </P>
1129     <P>
1130     The character types \d, \D, \s, \S, \w, and \W may also appear in a
1131     character class, and add the characters that they match to the class. For
1132     example, [\dABCDEF] matches any hexadecimal digit. A circumflex can
1133     conveniently be used with the upper case character types to specify a more
1134     restricted set of characters than the matching lower case type. For example,
1135     the class [^\W_] matches any letter or digit, but not underscore.
1136     </P>
1137     <P>
1138     All non-alphameric characters other than \, -, ^ (at the start) and the
1139     terminating ] are non-special in character classes, but it does no harm if they
1140     are escaped.
1141     </P>
1142     <LI><A NAME="SEC18" HREF="#TOC1">VERTICAL BAR</A>
1143     <P>
1144     Vertical bar characters are used to separate alternative patterns. For example,
1145     the pattern
1146     </P>
1147     <P>
1148     <PRE>
1149     gilbert|sullivan
1150     </PRE>
1151     </P>
1152     <P>
1153     matches either "gilbert" or "sullivan". Any number of alternatives may appear,
1154     and an empty alternative is permitted (matching the empty string).
1155     The matching process tries each alternative in turn, from left to right,
1156     and the first one that succeeds is used. If the alternatives are within a
1157     subpattern (defined below), "succeeds" means matching the rest of the main
1158     pattern as well as the alternative in the subpattern.
1159     </P>
1161     <P>
1163     can be changed from within the pattern by a sequence of Perl option letters
1164     enclosed between "(?" and ")". The option letters are
1165     </P>
1166     <P>
1167     <PRE>
1168     i for PCRE_CASELESS
1169     m for PCRE_MULTILINE
1170     s for PCRE_DOTALL
1171     x for PCRE_EXTENDED
1172     </PRE>
1173     </P>
1174     <P>
1175     For example, (?im) sets caseless, multiline matching. It is also possible to
1176     unset these options by preceding the letter with a hyphen, and a combined
1177     setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
1178     PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
1179     permitted. If a letter appears both before and after the hyphen, the option is
1180     unset.
1181     </P>
1182     <P>
1183     The scope of these option changes depends on where in the pattern the setting
1184     occurs. For settings that are outside any subpattern (defined below), the
1185     effect is the same as if the options were set or unset at the start of
1186     matching. The following patterns all behave in exactly the same way:
1187     </P>
1188     <P>
1189     <PRE>
1190     (?i)abc
1191     a(?i)bc
1192     ab(?i)c
1193     abc(?i)
1194     </PRE>
1195     </P>
1196     <P>
1197     which in turn is the same as compiling the pattern abc with PCRE_CASELESS set.
1198     In other words, such "top level" settings apply to the whole pattern (unless
1199     there are other changes inside subpatterns). If there is more than one setting
1200     of the same option at top level, the rightmost setting is used.
1201     </P>
1202     <P>
1203     If an option change occurs inside a subpattern, the effect is different. This
1204     is a change of behaviour in Perl 5.005. An option change inside a subpattern
1205     affects only that part of the subpattern that follows it, so
1206     </P>
1207     <P>
1208     <PRE>
1209     (a(?i)b)c
1210     </PRE>
1211     </P>
1212     <P>
1213     matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
1214     By this means, options can be made to have different settings in different
1215     parts of the pattern. Any changes made in one alternative do carry on
1216     into subsequent branches within the same subpattern. For example,
1217     </P>
1218     <P>
1219     <PRE>
1220     (a(?i)b|c)
1221     </PRE>
1222     </P>
1223     <P>
1224     matches "ab", "aB", "c", and "C", even though when matching "C" the first
1225     branch is abandoned before the option setting. This is because the effects of
1226     option settings happen at compile time. There would be some very weird
1227     behaviour otherwise.
1228     </P>
1229     <P>
1230     The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changed in the
1231     same way as the Perl-compatible options by using the characters U and X
1232     respectively. The (?X) flag setting is special in that it must always occur
1233     earlier in the pattern than any of the additional features it turns on, even
1234     when it is at top level. It is best put at the start.
1235     </P>
1237     <P>
1238     Subpatterns are delimited by parentheses (round brackets), which can be nested.
1239     Marking part of a pattern as a subpattern does two things:
1240     </P>
1241     <P>
1242     1. It localizes a set of alternatives. For example, the pattern
1243     </P>
1244     <P>
1245     <PRE>
1246     cat(aract|erpillar|)
1247     </PRE>
1248     </P>
1249     <P>
1250     matches one of the words "cat", "cataract", or "caterpillar". Without the
1251     parentheses, it would match "cataract", "erpillar" or the empty string.
1252     </P>
1253     <P>
1254     2. It sets up the subpattern as a capturing subpattern (as defined above).
1255     When the whole pattern matches, that portion of the subject string that matched
1256     the subpattern is passed back to the caller via the <I>ovector</I> argument of
1257     <B>pcre_exec()</B>. Opening parentheses are counted from left to right (starting
1258     from 1) to obtain the numbers of the capturing subpatterns.
1259     </P>
1260     <P>
1261     For example, if the string "the red king" is matched against the pattern
1262     </P>
1263     <P>
1264     <PRE>
1265     the ((red|white) (king|queen))
1266     </PRE>
1267     </P>
1268     <P>
1269     the captured substrings are "red king", "red", and "king", and are numbered 1,
1270     2, and 3.
1271     </P>
1272     <P>
1273     The fact that plain parentheses fulfil two functions is not always helpful.
1274     There are often times when a grouping subpattern is required without a
1275     capturing requirement. If an opening parenthesis is followed by "?:", the
1276     subpattern does not do any capturing, and is not counted when computing the
1277     number of any subsequent capturing subpatterns. For example, if the string "the
1278     white queen" is matched against the pattern
1279     </P>
1280     <P>
1281     <PRE>
1282     the ((?:red|white) (king|queen))
1283     </PRE>
1284     </P>
1285     <P>
1286     the captured substrings are "white queen" and "queen", and are numbered 1 and
1287     2. The maximum number of captured substrings is 99, and the maximum number of
1288     all subpatterns, both capturing and non-capturing, is 200.
1289     </P>
1290     <P>
1291     As a convenient shorthand, if any option settings are required at the start of
1292     a non-capturing subpattern, the option letters may appear between the "?" and
1293     the ":". Thus the two patterns
1294     </P>
1295     <P>
1296     <PRE>
1297     (?i:saturday|sunday)
1298     (?:(?i)saturday|sunday)
1299     </PRE>
1300     </P>
1301     <P>
1302     match exactly the same set of strings. Because alternative branches are tried
1303     from left to right, and options are not reset until the end of the subpattern
1304     is reached, an option setting in one branch does affect subsequent branches, so
1305     the above patterns match "SUNDAY" as well as "Saturday".
1306     </P>
1307     <LI><A NAME="SEC21" HREF="#TOC1">REPETITION</A>
1308     <P>
1309     Repetition is specified by quantifiers, which can follow any of the following
1310     items:
1311     </P>
1312     <P>
1313     <PRE>
1314     a single character, possibly escaped
1315     the . metacharacter
1316     a character class
1317     a back reference (see next section)
1318     a parenthesized subpattern (unless it is an assertion - see below)
1319     </PRE>
1320     </P>
1321     <P>
1322     The general repetition quantifier specifies a minimum and maximum number of
1323     permitted matches, by giving the two numbers in curly brackets (braces),
1324     separated by a comma. The numbers must be less than 65536, and the first must
1325     be less than or equal to the second. For example:
1326     </P>
1327     <P>
1328     <PRE>
1329     z{2,4}
1330     </PRE>
1331     </P>
1332     <P>
1333     matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
1334     character. If the second number is omitted, but the comma is present, there is
1335     no upper limit; if the second number and the comma are both omitted, the
1336     quantifier specifies an exact number of required matches. Thus
1337     </P>
1338     <P>
1339     <PRE>
1340     [aeiou]{3,}
1341     </PRE>
1342     </P>
1343     <P>
1344     matches at least 3 successive vowels, but may match many more, while
1345     </P>
1346     <P>
1347     <PRE>
1348     \d{8}
1349     </PRE>
1350     </P>
1351     <P>
1352     matches exactly 8 digits. An opening curly bracket that appears in a position
1353     where a quantifier is not allowed, or one that does not match the syntax of a
1354     quantifier, is taken as a literal character. For example, {,6} is not a
1355     quantifier, but a literal string of four characters.
1356     </P>
1357     <P>
1358     The quantifier {0} is permitted, causing the expression to behave as if the
1359     previous item and the quantifier were not present.
1360     </P>
1361     <P>
1362     For convenience (and historical compatibility) the three most common
1363     quantifiers have single-character abbreviations:
1364     </P>
1365     <P>
1366     <PRE>
1367     * is equivalent to {0,}
1368     + is equivalent to {1,}
1369     ? is equivalent to {0,1}
1370     </PRE>
1371     </P>
1372     <P>
1373     It is possible to construct infinite loops by following a subpattern that can
1374     match no characters with a quantifier that has no upper limit, for example:
1375     </P>
1376     <P>
1377     <PRE>
1378     (a?)*
1379     </PRE>
1380     </P>
1381     <P>
1382     Earlier versions of Perl and PCRE used to give an error at compile time for
1383     such patterns. However, because there are cases where this can be useful, such
1384     patterns are now accepted, but if any repetition of the subpattern does in fact
1385     match no characters, the loop is forcibly broken.
1386     </P>
1387     <P>
1388     By default, the quantifiers are "greedy", that is, they match as much as
1389     possible (up to the maximum number of permitted times), without causing the
1390     rest of the pattern to fail. The classic example of where this gives problems
1391     is in trying to match comments in C programs. These appear between the
1392     sequences /* and */ and within the sequence, individual * and / characters may
1393     appear. An attempt to match C comments by applying the pattern
1394     </P>
1395     <P>
1396     <PRE>
1397     /\*.*\*/
1398     </PRE>
1399     </P>
1400     <P>
1401     to the string
1402     </P>
1403     <P>
1404     <PRE>
1405     /* first command */ not comment /* second comment */
1406     </PRE>
1407     </P>
1408     <P>
1409     fails, because it matches the entire string due to the greediness of the .*
1410     item.
1411     </P>
1412     <P>
1413     However, if a quantifier is followed by a question mark, then it ceases to be
1414     greedy, and instead matches the minimum number of times possible, so the
1415     pattern
1416     </P>
1417     <P>
1418     <PRE>
1419     /\*.*?\*/
1420     </PRE>
1421     </P>
1422     <P>
1423     does the right thing with the C comments. The meaning of the various
1424     quantifiers is not otherwise changed, just the preferred number of matches.
1425     Do not confuse this use of question mark with its use as a quantifier in its
1426     own right. Because it has two uses, it can sometimes appear doubled, as in
1427     </P>
1428     <P>
1429     <PRE>
1430     \d??\d
1431     </PRE>
1432     </P>
1433     <P>
1434     which matches one digit by preference, but can match two if that is the only
1435     way the rest of the pattern matches.
1436     </P>
1437     <P>
1438     If the PCRE_UNGREEDY option is set (an option which is not available in Perl)
1439     then the quantifiers are not greedy by default, but individual ones can be made
1440     greedy by following them with a question mark. In other words, it inverts the
1441     default behaviour.
1442     </P>
1443     <P>
1444     When a parenthesized subpattern is quantified with a minimum repeat count that
1445     is greater than 1 or with a limited maximum, more store is required for the
1446     compiled pattern, in proportion to the size of the minimum or maximum.
1447     </P>
1448     <P>
1449     If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
1450     to Perl's /s) is set, thus allowing the . to match newlines, then the pattern
1451     is implicitly anchored, because whatever follows will be tried against every
1452     character position in the subject string, so there is no point in retrying the
1453     overall match at any position after the first. PCRE treats such a pattern as
1454     though it were preceded by \A. In cases where it is known that the subject
1455     string contains no newlines, it is worth setting PCRE_DOTALL when the pattern
1456     begins with .* in order to obtain this optimization, or alternatively using ^
1457     to indicate anchoring explicitly.
1458     </P>
1459     <P>
1460     When a capturing subpattern is repeated, the value captured is the substring
1461     that matched the final iteration. For example, after
1462     </P>
1463     <P>
1464     <PRE>
1465     (tweedle[dume]{3}\s*)+
1466     </PRE>
1467     </P>
1468     <P>
1469     has matched "tweedledum tweedledee" the value of the captured substring is
1470     "tweedledee". However, if there are nested capturing subpatterns, the
1471     corresponding captured values may have been set in previous iterations. For
1472     example, after
1473     </P>
1474     <P>
1475     <PRE>
1476     /(a|(b))+/
1477     </PRE>
1478     </P>
1479     <P>
1480     matches "aba" the value of the second captured substring is "b".
1481     </P>
1483     <P>
1484     Outside a character class, a backslash followed by a digit greater than 0 (and
1485     possibly further digits) is a back reference to a capturing subpattern earlier
1486     (i.e. to its left) in the pattern, provided there have been that many previous
1487     capturing left parentheses.
1488     </P>
1489     <P>
1490     However, if the decimal number following the backslash is less than 10, it is
1491     always taken as a back reference, and causes an error only if there are not
1492     that many capturing left parentheses in the entire pattern. In other words, the
1493     parentheses that are referenced need not be to the left of the reference for
1494     numbers less than 10. See the section entitled "Backslash" above for further
1495     details of the handling of digits following a backslash.
1496     </P>
1497     <P>
1498     A back reference matches whatever actually matched the capturing subpattern in
1499     the current subject string, rather than anything matching the subpattern
1500     itself. So the pattern
1501     </P>
1502     <P>
1503     <PRE>
1504     (sens|respons)e and \1ibility
1505     </PRE>
1506     </P>
1507     <P>
1508     matches "sense and sensibility" and "response and responsibility", but not
1509     "sense and responsibility". If caseful matching is in force at the time of the
1510     back reference, then the case of letters is relevant. For example,
1511     </P>
1512     <P>
1513     <PRE>
1514     ((?i)rah)\s+\1
1515     </PRE>
1516     </P>
1517     <P>
1518     matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
1519     capturing subpattern is matched caselessly.
1520     </P>
1521     <P>
1522     There may be more than one back reference to the same subpattern. If a
1523     subpattern has not actually been used in a particular match, then any back
1524     references to it always fail. For example, the pattern
1525     </P>
1526     <P>
1527     <PRE>
1528     (a|(bc))\2
1529     </PRE>
1530     </P>
1531     <P>
1532     always fails if it starts to match "a" rather than "bc". Because there may be
1533     up to 99 back references, all digits following the backslash are taken
1534     as part of a potential back reference number. If the pattern continues with a
1535     digit character, then some delimiter must be used to terminate the back
1536     reference. If the PCRE_EXTENDED option is set, this can be whitespace.
1537     Otherwise an empty comment can be used.
1538     </P>
1539     <P>
1540     A back reference that occurs inside the parentheses to which it refers fails
1541     when the subpattern is first used, so, for example, (a\1) never matches.
1542     However, such references can be useful inside repeated subpatterns. For
1543     example, the pattern
1544     </P>
1545     <P>
1546     <PRE>
1547     (a|b\1)+
1548     </PRE>
1549     </P>
1550     <P>
1551     matches any number of "a"s and also "aba", "ababaa" etc. At each iteration of
1552     the subpattern, the back reference matches the character string corresponding
1553     to the previous iteration. In order for this to work, the pattern must be such
1554     that the first iteration does not need to match the back reference. This can be
1555     done using alternation, as in the example above, or by a quantifier with a
1556     minimum of zero.
1557     </P>
1558     <LI><A NAME="SEC23" HREF="#TOC1">ASSERTIONS</A>
1559     <P>
1560     An assertion is a test on the characters following or preceding the current
1561     matching point that does not actually consume any characters. The simple
1562     assertions coded as \b, \B, \A, \Z, \z, ^ and $ are described above. More
1563     complicated assertions are coded as subpatterns. There are two kinds: those
1564     that look ahead of the current position in the subject string, and those that
1565     look behind it.
1566     </P>
1567     <P>
1568     An assertion subpattern is matched in the normal way, except that it does not
1569     cause the current matching position to be changed. Lookahead assertions start
1570     with (?= for positive assertions and (?! for negative assertions. For example,
1571     </P>
1572     <P>
1573     <PRE>
1574     \w+(?=;)
1575     </PRE>
1576     </P>
1577     <P>
1578     matches a word followed by a semicolon, but does not include the semicolon in
1579     the match, and
1580     </P>
1581     <P>
1582     <PRE>
1583     foo(?!bar)
1584     </PRE>
1585     </P>
1586     <P>
1587     matches any occurrence of "foo" that is not followed by "bar". Note that the
1588     apparently similar pattern
1589     </P>
1590     <P>
1591     <PRE>
1592     (?!foo)bar
1593     </PRE>
1594     </P>
1595     <P>
1596     does not find an occurrence of "bar" that is preceded by something other than
1597     "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
1598     (?!foo) is always true when the next three characters are "bar". A
1599     lookbehind assertion is needed to achieve this effect.
1600     </P>
1601     <P>
1602     Lookbehind assertions start with (?&#60;= for positive assertions and (?&#60;! for
1603     negative assertions. For example,
1604     </P>
1605     <P>
1606     <PRE>
1607     (?&#60;!foo)bar
1608     </PRE>
1609     </P>
1610     <P>
1611     does find an occurrence of "bar" that is not preceded by "foo". The contents of
1612     a lookbehind assertion are restricted such that all the strings it matches must
1613     have a fixed length. However, if there are several alternatives, they do not
1614     all have to have the same fixed length. Thus
1615     </P>
1616     <P>
1617     <PRE>
1618     (?&#60;=bullock|donkey)
1619     </PRE>
1620     </P>
1621     <P>
1622     is permitted, but
1623     </P>
1624     <P>
1625     <PRE>
1626     (?&#60;!dogs?|cats?)
1627     </PRE>
1628     </P>
1629     <P>
1630     causes an error at compile time. Branches that match different length strings
1631     are permitted only at the top level of a lookbehind assertion. This is an
1632     extension compared with Perl 5.005, which requires all branches to match the
1633     same length of string. An assertion such as
1634     </P>
1635     <P>
1636     <PRE>
1637     (?&#60;=ab(c|de))
1638     </PRE>
1639     </P>
1640     <P>
1641     is not permitted, because its single top-level branch can match two different
1642     lengths, but it is acceptable if rewritten to use two top-level branches:
1643     </P>
1644     <P>
1645     <PRE>
1646     (?&#60;=abc|abde)
1647     </PRE>
1648     </P>
1649     <P>
1650     The implementation of lookbehind assertions is, for each alternative, to
1651     temporarily move the current position back by the fixed width and then try to
1652     match. If there are insufficient characters before the current position, the
1653     match is deemed to fail. Lookbehinds in conjunction with once-only subpatterns
1654     can be particularly useful for matching at the ends of strings; an example is
1655     given at the end of the section on once-only subpatterns.
1656     </P>
1657     <P>
1658     Several assertions (of any sort) may occur in succession. For example,
1659     </P>
1660     <P>
1661     <PRE>
1662     (?&#60;=\d{3})(?&#60;!999)foo
1663     </PRE>
1664     </P>
1665     <P>
1666     matches "foo" preceded by three digits that are not "999". Notice that each of
1667     the assertions is applied independently at the same point in the subject
1668     string. First there is a check that the previous three characters are all
1669     digits, then there is a check that the same three characters are not "999".
1670     This pattern does <I>not</I> match "foo" preceded by six characters, the first
1671     of which are digits and the last three of which are not "999". For example, it
1672     doesn't match "123abcfoo". A pattern to do that is
1673     </P>
1674     <P>
1675     <PRE>
1676     (?&#60;=\d{3}...)(?&#60;!999)foo
1677     </PRE>
1678     </P>
1679     <P>
1680     This time the first assertion looks at the preceding six characters, checking
1681     that the first three are digits, and then the second assertion checks that the
1682     preceding three characters are not "999".
1683     </P>
1684     <P>
1685     Assertions can be nested in any combination. For example,
1686     </P>
1687     <P>
1688     <PRE>
1689     (?&#60;=(?&#60;!foo)bar)baz
1690     </PRE>
1691     </P>
1692     <P>
1693     matches an occurrence of "baz" that is preceded by "bar" which in turn is not
1694     preceded by "foo", while
1695     </P>
1696     <P>
1697     <PRE>
1698     (?&#60;=\d{3}(?!999)...)foo
1699     </PRE>
1700     </P>
1701     <P>
1702     is another pattern which matches "foo" preceded by three digits and any three
1703     characters that are not "999".
1704     </P>
1705     <P>
1706     Assertion subpatterns are not capturing subpatterns, and may not be repeated,
1707     because it makes no sense to assert the same thing several times. If any kind
1708     of assertion contains capturing subpatterns within it, these are counted for
1709     the purposes of numbering the capturing subpatterns in the whole pattern.
1710     However, substring capturing is carried out only for positive assertions,
1711     because it does not make sense for negative assertions.
1712     </P>
1713     <P>
1714     Assertions count towards the maximum of 200 parenthesized subpatterns.
1715     </P>
1717     <P>
1718     With both maximizing and minimizing repetition, failure of what follows
1719     normally causes the repeated item to be re-evaluated to see if a different
1720     number of repeats allows the rest of the pattern to match. Sometimes it is
1721     useful to prevent this, either to change the nature of the match, or to cause
1722     it fail earlier than it otherwise might, when the author of the pattern knows
1723     there is no point in carrying on.
1724     </P>
1725     <P>
1726     Consider, for example, the pattern \d+foo when applied to the subject line
1727     </P>
1728     <P>
1729     <PRE>
1730     123456bar
1731     </PRE>
1732     </P>
1733     <P>
1734     After matching all 6 digits and then failing to match "foo", the normal
1735     action of the matcher is to try again with only 5 digits matching the \d+
1736     item, and then with 4, and so on, before ultimately failing. Once-only
1737     subpatterns provide the means for specifying that once a portion of the pattern
1738     has matched, it is not to be re-evaluated in this way, so the matcher would
1739     give up immediately on failing to match "foo" the first time. The notation is
1740     another kind of special parenthesis, starting with (?&#62; as in this example:
1741     </P>
1742     <P>
1743     <PRE>
1744     (?&#62;\d+)bar
1745     </PRE>
1746     </P>
1747     <P>
1748     This kind of parenthesis "locks up" the part of the pattern it contains once
1749     it has matched, and a failure further into the pattern is prevented from
1750     backtracking into it. Backtracking past it to previous items, however, works as
1751     normal.
1752     </P>
1753     <P>
1754     An alternative description is that a subpattern of this type matches the string
1755     of characters that an identical standalone pattern would match, if anchored at
1756     the current point in the subject string.
1757     </P>
1758     <P>
1759     Once-only subpatterns are not capturing subpatterns. Simple cases such as the
1760     above example can be thought of as a maximizing repeat that must swallow
1761     everything it can. So, while both \d+ and \d+? are prepared to adjust the
1762     number of digits they match in order to make the rest of the pattern match,
1763     (?&#62;\d+) can only match an entire sequence of digits.
1764     </P>
1765     <P>
1766     This construction can of course contain arbitrarily complicated subpatterns,
1767     and it can be nested.
1768     </P>
1769     <P>
1770     Once-only subpatterns can be used in conjunction with lookbehind assertions to
1771     specify efficient matching at the end of the subject string. Consider a simple
1772     pattern such as
1773     </P>
1774     <P>
1775     <PRE>
1776     abcd$
1777     </PRE>
1778     </P>
1779     <P>
1780     when applied to a long string which does not match it. Because matching
1781     proceeds from left to right, PCRE will look for each "a" in the subject and
1782     then see if what follows matches the rest of the pattern. If the pattern is
1783     specified as
1784     </P>
1785     <P>
1786     <PRE>
1787     ^.*abcd$
1788     </PRE>
1789     </P>
1790     <P>
1791     then the initial .* matches the entire string at first, but when this fails, it
1792     backtracks to match all but the last character, then all but the last two
1793     characters, and so on. Once again the search for "a" covers the entire string,
1794     from right to left, so we are no better off. However, if the pattern is written
1795     as
1796     </P>
1797     <P>
1798     <PRE>
1799     ^(?&#62;.*)(?&#60;=abcd)
1800     </PRE>
1801     </P>
1802     <P>
1803     then there can be no backtracking for the .* item; it can match only the entire
1804     string. The subsequent lookbehind assertion does a single test on the last four
1805     characters. If it fails, the match fails immediately. For long strings, this
1806     approach makes a significant difference to the processing time.
1807     </P>
1809     <P>
1810     It is possible to cause the matching process to obey a subpattern
1811     conditionally or to choose between two alternative subpatterns, depending on
1812     the result of an assertion, or whether a previous capturing subpattern matched
1813     or not. The two possible forms of conditional subpattern are
1814     </P>
1815     <P>
1816     <PRE>
1817     (?(condition)yes-pattern)
1818     (?(condition)yes-pattern|no-pattern)
1819     </PRE>
1820     </P>
1821     <P>
1822     If the condition is satisfied, the yes-pattern is used; otherwise the
1823     no-pattern (if present) is used. If there are more than two alternatives in the
1824     subpattern, a compile-time error occurs.
1825     </P>
1826     <P>
1827     There are two kinds of condition. If the text between the parentheses consists
1828     of a sequence of digits, then the condition is satisfied if the capturing
1829     subpattern of that number has previously matched. Consider the following
1830     pattern, which contains non-significant white space to make it more readable
1831     (assume the PCRE_EXTENDED option) and to divide it into three parts for ease
1832     of discussion:
1833     </P>
1834     <P>
1835     <PRE>
1836     ( \( )? [^()]+ (?(1) \) )
1837     </PRE>
1838     </P>
1839     <P>
1840     The first part matches an optional opening parenthesis, and if that
1841     character is present, sets it as the first captured substring. The second part
1842     matches one or more characters that are not parentheses. The third part is a
1843     conditional subpattern that tests whether the first set of parentheses matched
1844     or not. If they did, that is, if subject started with an opening parenthesis,
1845     the condition is true, and so the yes-pattern is executed and a closing
1846     parenthesis is required. Otherwise, since no-pattern is not present, the
1847     subpattern matches nothing. In other words, this pattern matches a sequence of
1848     non-parentheses, optionally enclosed in parentheses.
1849     </P>
1850     <P>
1851     If the condition is not a sequence of digits, it must be an assertion. This may
1852     be a positive or negative lookahead or lookbehind assertion. Consider this
1853     pattern, again containing non-significant white space, and with the two
1854     alternatives on the second line:
1855     </P>
1856     <P>
1857     <PRE>
1858     (?(?=[^a-z]*[a-z])
1859     \d{2}[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
1860     </PRE>
1861     </P>
1862     <P>
1863     The condition is a positive lookahead assertion that matches an optional
1864     sequence of non-letters followed by a letter. In other words, it tests for the
1865     presence of at least one letter in the subject. If a letter is found, the
1866     subject is matched against the first alternative; otherwise it is matched
1867     against the second. This pattern matches strings in one of the two forms
1868     dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
1869     </P>
1870     <LI><A NAME="SEC26" HREF="#TOC1">COMMENTS</A>
1871     <P>
1872     The sequence (?# marks the start of a comment which continues up to the next
1873     closing parenthesis. Nested parentheses are not permitted. The characters
1874     that make up a comment play no part in the pattern matching at all.
1875     </P>
1876     <P>
1877     If the PCRE_EXTENDED option is set, an unescaped # character outside a
1878     character class introduces a comment that continues up to the next newline
1879     character in the pattern.
1880     </P>
1882     <P>
1883     Certain items that may appear in patterns are more efficient than others. It is
1884     more efficient to use a character class like [aeiou] than a set of alternatives
1885     such as (a|e|i|o|u). In general, the simplest construction that provides the
1886     required behaviour is usually the most efficient. Jeffrey Friedl's book
1887     contains a lot of discussion about optimizing regular expressions for efficient
1888     performance.
1889     </P>
1890     <P>
1891     When a pattern begins with .* and the PCRE_DOTALL option is set, the pattern is
1892     implicitly anchored by PCRE, since it can match only at the start of a subject
1893     string. However, if PCRE_DOTALL is not set, PCRE cannot make this optimization,
1894     because the . metacharacter does not then match a newline, and if the subject
1895     string contains newlines, the pattern may match from the character immediately
1896     following one of them instead of from the very start. For example, the pattern
1897     </P>
1898     <P>
1899     <PRE>
1900     (.*) second
1901     </PRE>
1902     </P>
1903     <P>
1904     matches the subject "first\nand second" (where \n stands for a newline
1905     character) with the first captured substring being "and". In order to do this,
1906     PCRE has to retry the match starting after every newline in the subject.
1907     </P>
1908     <P>
1909     If you are using such a pattern with subject strings that do not contain
1910     newlines, the best performance is obtained by setting PCRE_DOTALL, or starting
1911     the pattern with ^.* to indicate explicit anchoring. That saves PCRE from
1912     having to scan along the subject looking for a newline to restart at.
1913     </P>
1914     <P>
1915     Beware of patterns that contain nested indefinite repeats. These can take a
1916     long time to run when applied to a string that does not match. Consider the
1917     pattern fragment
1918     </P>
1919     <P>
1920     <PRE>
1921     (a+)*
1922     </PRE>
1923     </P>
1924     <P>
1925     This can match "aaaa" in 33 different ways, and this number increases very
1926     rapidly as the string gets longer. (The * repeat can match 0, 1, 2, 3, or 4
1927     times, and for each of those cases other than 0, the + repeats can match
1928     different numbers of times.) When the remainder of the pattern is such that the
1929     entire match is going to fail, PCRE has in principle to try every possible
1930     variation, and this can take an extremely long time.
1931     </P>
1932     <P>
1933     An optimization catches some of the more simple cases such as
1934     </P>
1935     <P>
1936     <PRE>
1937     (a+)*b
1938     </PRE>
1939     </P>
1940     <P>
1941     where a literal character follows. Before embarking on the standard matching
1942     procedure, PCRE checks that there is a "b" later in the subject string, and if
1943     there is not, it fails the match immediately. However, when there is no
1944     following literal this optimization cannot be used. You can see the difference
1945     by comparing the behaviour of
1946     </P>
1947     <P>
1948     <PRE>
1949     (a+)*\d
1950     </PRE>
1951     </P>
1952     <P>
1953     with the pattern above. The former gives a failure almost instantly when
1954     applied to a whole line of "a" characters, whereas the latter takes an
1955     appreciable time with strings longer than about 20 characters.
1956     </P>
1957     <LI><A NAME="SEC28" HREF="#TOC1">AUTHOR</A>
1958     <P>
1959     Philip Hazel &#60;ph10@cam.ac.uk&#62;
1960     <BR>
1961     University Computing Service,
1962     <BR>
1963     New Museums Site,
1964     <BR>
1965     Cambridge CB2 3QG, England.
1966     <BR>
1967     Phone: +44 1223 334714
1968     </P>
1969     <P>
1970     Last updated: 29 July 1999
1971     <BR>
1972     Copyright (c) 1997-1999 University of Cambridge.

ViewVC Help
Powered by ViewVC 1.1.12