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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12