/[pcre]/code/trunk/doc/pcre.html
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show annotations) (download) (as text)
Sat Feb 24 21:39:17 2007 UTC (7 years, 2 months ago) by nigel
File MIME type: text/html
File size: 76198 byte(s)
Load pcre-2.08a into code/trunk.

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12