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