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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5