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