/[pcre]/code/trunk/pcre.3
ViewVC logotype

Contents of /code/trunk/pcre.3

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (show annotations) (download)
Sat Feb 24 21:38:45 2007 UTC (7 years, 1 month ago) by nigel
File size: 57044 byte(s)
Load pcre-2.01 into code/trunk.

1 .TH PCRE 3
2 .SH NAME
3 pcre - Perl-compatible regular expressions.
4 .SH SYNOPSIS
5 .B #include <pcre.h>
6 .PP
7 .SM
8 .br
9 .B pcre *pcre_compile(const char *\fIpattern\fR, int \fIoptions\fR,
10 .ti +5n
11 .B const char **\fIerrptr\fR, int *\fIerroffset\fR,
12 .ti +5n
13 .B const unsigned char *\fItableptr\fR);
14 .PP
15 .br
16 .B const unsigned char *pcre_maketables(void);
17 .PP
18 .br
19 .B pcre_extra *pcre_study(const pcre *\fIcode\fR, int \fIoptions\fR,
20 .ti +5n
21 .B const char **\fIerrptr\fR);
22 .PP
23 .br
24 .B int pcre_exec(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"
25 .ti +5n
26 .B "const char *\fIsubject\fR," int \fIlength\fR, int \fIoptions\fR,
27 .ti +5n
28 .B int *\fIovector\fR, int \fIovecsize\fR);
29 .PP
30 .br
31 .B int pcre_info(const pcre *\fIcode\fR, int *\fIoptptr\fR, int
32 .B *\fIfirstcharptr\fR);
33 .PP
34 .br
35 .B char *pcre_version(void);
36 .PP
37 .br
38 .B void *(*pcre_malloc)(size_t);
39 .PP
40 .br
41 .B void (*pcre_free)(void *);
42
43
44
45 .SH DESCRIPTION
46 The PCRE library is a set of functions that implement regular expression
47 pattern matching using the same syntax and semantics as Perl 5, with just a few
48 differences (see below). The current implementation corresponds to Perl 5.005.
49
50 PCRE has its own native API, which is described in this man page. There is also
51 a set of wrapper functions that correspond to the POSIX API. See
52 \fBpcreposix (3)\fR.
53
54 The three functions \fBpcre_compile()\fR, \fBpcre_study()\fR, and
55 \fBpcre_exec()\fR are used for compiling and matching regular expressions. The
56 function \fBpcre_maketables()\fR is used (optionally) to build a set of
57 character tables in the current locale for passing to \fBpcre_compile()\fR.
58
59 The function \fBpcre_info()\fR is used to find out information about a compiled
60 pattern, while the function \fBpcre_version()\fR returns a pointer to a string
61 containing the version of PCRE and its date of release.
62
63 The global variables \fBpcre_malloc\fR and \fBpcre_free\fR initially contain
64 the entry points of the standard \fBmalloc()\fR and \fBfree()\fR functions
65 respectively. PCRE calls the memory management functions via these variables,
66 so a calling program can replace them if it wishes to intercept the calls. This
67 should be done before calling any PCRE functions.
68
69
70 .SH MULTI-THREADING
71 The PCRE functions can be used in multi-threading applications, with the
72 proviso that the memory management functions pointed to by \fBpcre_malloc\fR
73 and \fBpcre_free\fR are shared by all threads.
74
75 The compiled form of a regular expression is not altered during matching, so
76 the same compiled pattern can safely be used by several threads at once.
77
78
79 .SH COMPILING A PATTERN
80 The function \fBpcre_compile()\fR is called to compile a pattern into an
81 internal form. The pattern is a C string terminated by a binary zero, and
82 is passed in the argument \fIpattern\fR. A pointer to a single block of memory
83 that is obtained via \fBpcre_malloc\fR is returned. This contains the
84 compiled code and related data. The \fBpcre\fR type is defined for this for
85 convenience, but in fact \fBpcre\fR is just a typedef for \fBvoid\fR, since the
86 contents of the block are not externally defined. It is up to the caller to
87 free the memory when it is no longer required.
88 .PP
89 The size of a compiled pattern is roughly proportional to the length of the
90 pattern string, except that each character class (other than those containing
91 just a single character, negated or not) requires 33 bytes, and repeat
92 quantifiers with a minimum greater than one or a bounded maximum cause the
93 relevant portions of the compiled pattern to be replicated.
94 .PP
95 The \fIoptions\fR argument contains independent bits that affect the
96 compilation. It should be zero if no options are required. Some of the options,
97 in particular, those that are compatible with Perl, can also be set and unset
98 from within the pattern (see the detailed description of regular expressions
99 below). For these options, the contents of the \fIoptions\fR argument specifies
100 their initial settings at the start of compilation and execution. The
101 PCRE_ANCHORED option can be set at the time of matching as well as at compile
102 time.
103 .PP
104 If \fIerrptr\fR is NULL, \fBpcre_compile()\fR returns NULL immediately.
105 Otherwise, if compilation of a pattern fails, \fBpcre_compile()\fR returns
106 NULL, and sets the variable pointed to by \fIerrptr\fR to point to a textual
107 error message. The offset from the start of the pattern to the character where
108 the error was discovered is placed in the variable pointed to by
109 \fIerroffset\fR, which must not be NULL. If it is, an immediate error is given.
110 .PP
111 If the final argument, \fItableptr\fR, is NULL, PCRE uses a default set of
112 character tables which are built when it is compiled, using the default C
113 locale. Otherwise, \fItableptr\fR must be the result of a call to
114 \fBpcre_maketables()\fR. See the section on locale support below.
115 .PP
116 The following option bits are defined in the header file:
117
118 PCRE_ANCHORED
119
120 If this bit is set, the pattern is forced to be "anchored", that is, it is
121 constrained to match only at the start of the string which is being searched
122 (the "subject string"). This effect can also be achieved by appropriate
123 constructs in the pattern itself, which is the only way to do it in Perl.
124
125 PCRE_CASELESS
126
127 If this bit is set, letters in the pattern match both upper and lower case
128 letters. It is equivalent to Perl's /i option.
129
130 PCRE_DOLLAR_ENDONLY
131
132 If this bit is set, a dollar metacharacter in the pattern matches only at the
133 end of the subject string. Without this option, a dollar also matches
134 immediately before the final character if it is a newline (but not before any
135 other newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
136 set. There is no equivalent to this option in Perl.
137
138 PCRE_DOTALL
139
140 If this bit is set, a dot metacharater in the pattern matches all characters,
141 including newlines. Without it, newlines are excluded. This option is
142 equivalent to Perl's /s option. A negative class such as [^a] always matches a
143 newline character, independent of the setting of this option.
144
145 PCRE_EXTENDED
146
147 If this bit is set, whitespace data characters in the pattern are totally
148 ignored except when escaped or inside a character class, and characters between
149 an unescaped # outside a character class and the next newline character,
150 inclusive, are also ignored. This is equivalent to Perl's /x option, and makes
151 it possible to include comments inside complicated patterns. Note, however,
152 that this applies only to data characters. Whitespace characters may never
153 appear within special character sequences in a pattern, for example within the
154 sequence (?( which introduces a conditional subpattern.
155
156 PCRE_EXTRA
157
158 This option turns on additional functionality of PCRE that is incompatible with
159 Perl. Any backslash in a pattern that is followed by a letter that has no
160 special meaning causes an error, thus reserving these combinations for future
161 expansion. By default, as in Perl, a backslash followed by a letter with no
162 special meaning is treated as a literal. There are at present no other features
163 controlled by this option.
164
165 PCRE_MULTILINE
166
167 By default, PCRE treats the subject string as consisting of a single "line" of
168 characters (even if it actually contains several newlines). The "start of line"
169 metacharacter (^) matches only at the start of the string, while the "end of
170 line" metacharacter ($) matches only at the end of the string, or before a
171 terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
172 Perl.
173
174 When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
175 match immediately following or immediately before any newline in the subject
176 string, respectively, as well as at the very start and end. This is equivalent
177 to Perl's /m option. If there are no "\\n" characters in a subject string, or
178 no occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no
179 effect.
180
181 PCRE_UNGREEDY
182
183 This option inverts the "greediness" of the quantifiers so that they are not
184 greedy by default, but become greedy if followed by "?". It is not compatible
185 with Perl. It can also be set by a (?U) option setting within the pattern.
186
187
188 .SH STUDYING A PATTERN
189 When a pattern is going to be used several times, it is worth spending more
190 time analyzing it in order to speed up the time taken for matching. The
191 function \fBpcre_study()\fR takes a pointer to a compiled pattern as its first
192 argument, and returns a pointer to a \fBpcre_extra\fR block (another \fBvoid\fR
193 typedef) containing additional information about the pattern; this can be
194 passed to \fBpcre_exec()\fR. If no additional information is available, NULL
195 is returned.
196
197 The second argument contains option bits. At present, no options are defined
198 for \fBpcre_study()\fR, and this argument should always be zero.
199
200 The third argument for \fBpcre_study()\fR is a pointer to an error message. If
201 studying succeeds (even if no data is returned), the variable it points to is
202 set to NULL. Otherwise it points to a textual error message.
203
204 At present, studying a pattern is useful only for non-anchored patterns that do
205 not have a single fixed starting character. A bitmap of possible starting
206 characters is created.
207
208
209 .SH LOCALE SUPPORT
210 PCRE handles caseless matching, and determines whether characters are letters,
211 digits, or whatever, by reference to a set of tables. The library contains a
212 default set of tables which is created in the default C locale when PCRE is
213 compiled. This is used when the final argument of \fBpcre_compile()\fR is NULL,
214 and is sufficient for many applications.
215
216 An alternative set of tables can, however, be supplied. Such tables are built
217 by calling the \fBpcre_maketables()\fR function, which has no arguments, in the
218 relevant locale. The result can then be passed to \fBpcre_compile()\ as often
219 as necessary. For example, to build and use tables that are appropriate for the
220 French locale (where accented characters with codes greater than 128 are
221 treated as letters), the following code could be used:
222
223 setlocale(LC_CTYPE, "fr");
224 tables = pcre_maketables();
225 re = pcre_compile(..., tables);
226
227 The tables are built in memory that is obtained via \fBpcre_malloc\fR. The
228 pointer that is passed to \fBpcre_compile\fR is saved with the compiled
229 pattern, and the same tables are used via this pointer by \fBpcre_study()\fR
230 and \fBpcre_match()\fR. Thus for any single pattern, compilation, studying and
231 matching all happen in the same locale, but different patterns can be compiled
232 in different locales. It is the caller's responsibility to ensure that the
233 memory containing the tables remains available for as long as it is needed.
234
235
236 .SH MATCHING A PATTERN
237 The function \fBpcre_exec()\fR is called to match a subject string against a
238 pre-compiled pattern, which is passed in the \fIcode\fR argument. If the
239 pattern has been studied, the result of the study should be passed in the
240 \fIextra\fR argument. Otherwise this must be NULL.
241
242 The subject string is passed as a pointer in \fIsubject\fR and a length in
243 \fIlength\fR. Unlike the pattern string, it may contain binary zero characters.
244
245 The PCRE_ANCHORED option can be passed in the \fIoptions\fR argument, whose
246 unused bits must be zero. However, if a pattern was compiled with
247 PCRE_ANCHORED, or turned out to be anchored by virtue of its contents, it
248 cannot be made unachored at matching time.
249
250 There are also two further options that can be set only at matching time:
251
252 PCRE_NOTBOL
253
254 The first character of the string is not the beginning of a line, so the
255 circumflex metacharacter should not match before it. Setting this without
256 PCRE_MULTILINE (at compile time) causes circumflex never to match.
257
258 PCRE_NOTEOL
259
260 The end of the string is not the end of a line, so the dollar metacharacter
261 should not match it nor (except in multiline mode) a newline immediately before
262 it. Setting this without PCRE_MULTILINE (at compile time) causes dollar never
263 to match.
264
265 In general, a pattern matches a certain portion of the subject, and in
266 addition, further substrings from the subject may be picked out by parts of the
267 pattern. Following the usage in Jeffrey Friedl's book, this is called
268 "capturing" in what follows, and the phrase "capturing subpattern" is used for
269 a fragment of a pattern that picks out a substring. PCRE supports several other
270 kinds of parenthesized subpattern that do not cause substrings to be captured.
271
272 Captured substrings are returned to the caller via a vector of integer offsets
273 whose address is passed in \fIovector\fR. The number of elements in the vector
274 is passed in \fIovecsize\fR. The first two-thirds of the vector is used to pass
275 back captured substrings, each substring using a pair of integers. The
276 remaining third of the vector is used as workspace by \fBpcre_exec()\fR while
277 matching capturing subpatterns, and is not available for passing back
278 information. The length passed in \fIovecsize\fR should always be a multiple of
279 three. If it is not, it is rounded down.
280
281 When a match has been successful, information about captured substrings is
282 returned in pairs of integers, starting at the beginning of \fIovector\fR, and
283 continuing up to two-thirds of its length at the most. The first element of a
284 pair is set to the offset of the first character in a substring, and the second
285 is set to the offset of the first character after the end of a substring. The
286 first pair, \fIovector[0]\fR and \fIovector[1]\fR, identify the portion of the
287 subject string matched by the entire pattern. The next pair is used for the
288 first capturing subpattern, and so on. The value returned by \fBpcre_exec()\fR
289 is the number of pairs that have been set. If there are no capturing
290 subpatterns, the return value from a successful match is 1, indicating that
291 just the first pair of offsets has been set.
292
293 It is possible for an capturing subpattern number \fIn+1\fR to match some
294 part of the subject when subpattern \fIn\fR has not been used at all. For
295 example, if the string "abc" is matched against the pattern (a|(z))(bc)
296 subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
297 values corresponding to the unused subpattern are set to -1.
298
299 If a capturing subpattern is matched repeatedly, it is the last portion of the
300 string that it matched that gets returned.
301
302 If the vector is too small to hold all the captured substrings, it is used as
303 far as possible (up to two-thirds of its length), and the function returns a
304 value of zero. In particular, if the substring offsets are not of interest,
305 \fBpcre_exec()\fR may be called with \fIovector\fR passed as NULL and
306 \fIovecsize\fR as zero. However, if the pattern contains back references and
307 the \fIovector\fR isn't big enough to remember the related substrings, PCRE has
308 to get additional memory for use during matching. Thus it is usually advisable
309 to supply an \fIovector\fR.
310
311 Note that \fBpcre_info()\fR can be used to find out how many capturing
312 subpatterns there are in a compiled pattern. The smallest size for
313 \fIovector\fR that will allow for \fIn\fR captured substrings in addition to
314 the offsets of the substring matched by the whole pattern is (\fIn\fR+1)*3.
315
316 If \fBpcre_exec()\fR fails, it returns a negative number. The following are
317 defined in the header file:
318
319 PCRE_ERROR_NOMATCH (-1)
320
321 The subject string did not match the pattern.
322
323 PCRE_ERROR_NULL (-2)
324
325 Either \fIcode\fR or \fIsubject\fR was passed as NULL, or \fIovector\fR was
326 NULL and \fIovecsize\fR was not zero.
327
328 PCRE_ERROR_BADOPTION (-3)
329
330 An unrecognized bit was set in the \fIoptions\fR argument.
331
332 PCRE_ERROR_BADMAGIC (-4)
333
334 PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
335 the case when it is passed a junk pointer. This is the error it gives when the
336 magic number isn't present.
337
338 PCRE_ERROR_UNKNOWN_NODE (-5)
339
340 While running the pattern match, an unknown item was encountered in the
341 compiled pattern. This error could be caused by a bug in PCRE or by overwriting
342 of the compiled pattern.
343
344 PCRE_ERROR_NOMEMORY (-6)
345
346 If a pattern contains back references, but the \fIovector\fR that is passed to
347 \fBpcre_exec()\fR is not big enough to remember the referenced substrings, PCRE
348 gets a block of memory at the start of matching to use for this purpose. If the
349 call via \fBpcre_malloc()\fR fails, this error is given. The memory is freed at
350 the end of matching.
351
352
353 .SH INFORMATION ABOUT A PATTERN
354 The \fBpcre_info()\fR function returns information about a compiled pattern.
355 Its yield is the number of capturing subpatterns, or one of the following
356 negative numbers:
357
358 PCRE_ERROR_NULL the argument \fIcode\fR was NULL
359 PCRE_ERROR_BADMAGIC the "magic number" was not found
360
361 If the \fIoptptr\fR argument is not NULL, a copy of the options with which the
362 pattern was compiled is placed in the integer it points to.
363
364 If the \fIfirstcharptr\fR argument is not NULL, is is used to pass back
365 information about the first character of any matched string. If there is a
366 fixed first character, e.g. from a pattern such as (cat|cow|coyote), then it is
367 returned in the integer pointed to by \fIfirstcharptr\fR. Otherwise, if the
368 pattern was compiled with the PCRE_MULTILINE option, and every branch started
369 with "^", then -1 is returned, indicating that the pattern will match at the
370 start of a subject string or after any "\\n" within the string. Otherwise -2 is
371 returned.
372
373
374 .SH LIMITATIONS
375 There are some size limitations in PCRE but it is hoped that they will never in
376 practice be relevant.
377 The maximum length of a compiled pattern is 65539 (sic) bytes.
378 All values in repeating quantifiers must be less than 65536.
379 The maximum number of capturing subpatterns is 99.
380 The maximum number of all parenthesized subpatterns, including capturing
381 subpatterns, assertions, and other types of subpattern, is 200.
382
383 The maximum length of a subject string is the largest positive number that an
384 integer variable can hold. However, PCRE uses recursion to handle subpatterns
385 and indefinite repetition. This means that the available stack space may limit
386 the size of a subject string that can be processed by certain patterns.
387
388
389 .SH DIFFERENCES FROM PERL
390 The differences described here are with respect to Perl 5.005.
391
392 1. By default, a whitespace character is any character that the C library
393 function \fBisspace()\fR recognizes, though it is possible to compile PCRE with
394 alternative character type tables. Normally \fBisspace()\fR matches space,
395 formfeed, newline, carriage return, horizontal tab, and vertical tab. Perl 5
396 no longer includes vertical tab in its set of whitespace characters. The \\v
397 escape that was in the Perl documentation for a long time was never in fact
398 recognized. However, the character itself was treated as whitespace at least
399 up to 5.002. In 5.004 and 5.005 it does not match \\s.
400
401 2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl permits
402 them, but they do not mean what you might think. For example, (?!a){3} does
403 not assert that the next three characters are not "a". It just asserts that the
404 next character is not "a" three times.
405
406 3. Capturing subpatterns that occur inside negative lookahead assertions are
407 counted, but their entries in the offsets vector are never set. Perl sets its
408 numerical variables from any such patterns that are matched before the
409 assertion fails to match something (thereby succeeding), but only if the
410 negative lookahead assertion contains just one branch.
411
412 4. Though binary zero characters are supported in the subject string, they are
413 not allowed in a pattern string because it is passed as a normal C string,
414 terminated by zero. The escape sequence "\\0" can be used in the pattern to
415 represent a binary zero.
416
417 5. The following Perl escape sequences are not supported: \\l, \\u, \\L, \\U,
418 \\E, \\Q. In fact these are implemented by Perl's general string-handling and
419 are not part of its pattern matching engine.
420
421 6. The Perl \\G assertion is not supported as it is not relevant to single
422 pattern matches.
423
424 7. Fairly obviously, PCRE does not support the (?{code}) construction.
425
426 8. There are at the time of writing some oddities in Perl 5.005_02 concerned
427 with the settings of captured strings when part of a pattern is repeated. For
428 example, matching "aba" against the pattern /^(a(b)?)+$/ sets $2 to the value
429 "b", but matching "aabbaa" against /^(aa(bb)?)+$/ leaves $2 unset. However, if
430 the pattern is changed to /^(aa(b(b))?)+$/ then $2 (and $3) get set.
431
432 In Perl 5.004 $2 is set in both cases, and that is also true of PCRE. If in the
433 future Perl changes to a consistent state that is different, PCRE may change to
434 follow.
435
436 9. Another as yet unresolved discrepancy is that in Perl 5.005_02 the pattern
437 /^(a)?(?(1)a|b)+$/ matches the string "a", whereas in PCRE it does not.
438 However, in both Perl and PCRE /^(a)?a/ matched against "a" leaves $1 unset.
439
440 10. PCRE provides some extensions to the Perl regular expression facilities:
441
442 (a) Although lookbehind assertions must match fixed length strings, each
443 alternative branch of a lookbehind assertion can match a different length of
444 string. Perl 5.005 requires them all to have the same length.
445
446 (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ meta-
447 character matches only at the very end of the string.
448
449 (c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
450 meaning is faulted.
451
452 (d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
453 inverted, that is, by default they are not greedy, but if followed by a
454 question mark they are.
455
456
457 .SH REGULAR EXPRESSION DETAILS
458 The syntax and semantics of the regular expressions supported by PCRE are
459 described below. Regular expressions are also described in the Perl
460 documentation and in a number of other books, some of which have copious
461 examples. Jeffrey Friedl's "Mastering Regular Expressions", published by
462 O'Reilly (ISBN 1-56592-257-3), covers them in great detail. The description
463 here is intended as reference documentation.
464
465 A regular expression is a pattern that is matched against a subject string from
466 left to right. Most characters stand for themselves in a pattern, and match the
467 corresponding characters in the subject. As a trivial example, the pattern
468
469 The quick brown fox
470
471 matches a portion of a subject string that is identical to itself. The power of
472 regular expressions comes from the ability to include alternatives and
473 repetitions in the pattern. These are encoded in the pattern by the use of
474 \fImeta-characters\fR, which do not stand for themselves but instead are
475 interpreted in some special way.
476
477 There are two different sets of meta-characters: those that are recognized
478 anywhere in the pattern except within square brackets, and those that are
479 recognized in square brackets. Outside square brackets, the meta-characters are
480 as follows:
481
482 \\ general escape character with several uses
483 ^ assert start of subject (or line, in multiline mode)
484 $ assert end of subject (or line, in multiline mode)
485 . match any character except newline (by default)
486 [ start character class definition
487 | start of alternative branch
488 ( start subpattern
489 ) end subpattern
490 ? extends the meaning of (
491 also 0 or 1 quantifier
492 also quantifier minimizer
493 * 0 or more quantifier
494 + 1 or more quantifier
495 { start min/max quantifier
496
497 Part of a pattern that is in square brackets is called a "character class". In
498 a character class the only meta-characters are:
499
500 \\ general escape character
501 ^ negate the class, but only if the first character
502 - indicates character range
503 ] terminates the character class
504
505 The following sections describe the use of each of the meta-characters.
506
507
508 .SH BACKSLASH
509 The backslash character has several uses. Firstly, if it is followed by a
510 non-alphameric character, it takes away any special meaning that character may
511 have. This use of backslash as an escape character applies both inside and
512 outside character classes.
513
514 For example, if you want to match a "*" character, you write "\\*" in the
515 pattern. This applies whether or not the following character would otherwise be
516 interpreted as a meta-character, so it is always safe to precede a
517 non-alphameric with "\\" to specify that it stands for itself. In particular,
518 if you want to match a backslash, you write "\\\\".
519
520 If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
521 pattern (other than in a character class) and characters between a "#" outside
522 a character class and the next newline character are ignored. An escaping
523 backslash can be used to include a whitespace or "#" character as part of the
524 pattern.
525
526 A second use of backslash provides a way of encoding non-printing characters
527 in patterns in a visible manner. There is no restriction on the appearance of
528 non-printing characters, apart from the binary zero that terminates a pattern,
529 but when a pattern is being prepared by text editing, it is usually easier to
530 use one of the following escape sequences than the binary character it
531 represents:
532
533 \\a alarm, that is, the BEL character (hex 07)
534 \\cx "control-x", where x is any character
535 \\e escape (hex 1B)
536 \\f formfeed (hex 0C)
537 \\n newline (hex 0A)
538 \\r carriage return (hex 0D)
539 \\t tab (hex 09)
540 \\xhh character with hex code hh
541 \\ddd character with octal code ddd, or backreference
542
543 The precise effect of "\\cx" is as follows: if "x" is a lower case letter, it
544 is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
545 Thus "\\cz" becomes hex 1A, but "\\c{" becomes hex 3B, while "\\c;" becomes hex
546 7B.
547
548 After "\\x", up to two hexadecimal digits are read (letters can be in upper or
549 lower case).
550
551 After "\\0" up to two further octal digits are read. In both cases, if there
552 are fewer than two digits, just those that are present are used. Thus the
553 sequence "\\0\\x\\07" specifies two binary zeros followed by a BEL character.
554 Make sure you supply two digits after the initial zero if the character that
555 follows is itself an octal digit.
556
557 The handling of a backslash followed by a digit other than 0 is complicated.
558 Outside a character class, PCRE reads it and any following digits as a decimal
559 number. If the number is less than 10, or if there have been at least that many
560 previous capturing left parentheses in the expression, the entire sequence is
561 taken as a \fIback reference\fR. A description of how this works is given
562 later, following the discussion of parenthesized subpatterns.
563
564 Inside a character class, or if the decimal number is greater than 9 and there
565 have not been that many capturing subpatterns, PCRE re-reads up to three octal
566 digits following the backslash, and generates a single byte from the least
567 significant 8 bits of the value. Any subsequent digits stand for themselves.
568 For example:
569
570 \\040 is another way of writing a space
571 \\40 is the same, provided there are fewer than 40
572 previous capturing subpatterns
573 \\7 is always a back reference
574 \\11 might be a back reference, or another way of
575 writing a tab
576 \\011 is always a tab
577 \\0113 is a tab followed by the character "3"
578 \\113 is the character with octal code 113 (since there
579 can be no more than 99 back references)
580 \\377 is a byte consisting entirely of 1 bits
581 \\81 is either a back reference, or a binary zero
582 followed by the two characters "8" and "1"
583
584 Note that octal values of 100 or greater must not be introduced by a leading
585 zero, because no more than three octal digits are ever read.
586
587 All the sequences that define a single byte value can be used both inside and
588 outside character classes. In addition, inside a character class, the sequence
589 "\\b" is interpreted as the backspace character (hex 08). Outside a character
590 class it has a different meaning (see below).
591
592 The third use of backslash is for specifying generic character types:
593
594 \\d any decimal digit
595 \\D any character that is not a decimal digit
596 \\s any whitespace character
597 \\S any character that is not a whitespace character
598 \\w any "word" character
599 \\W any "non-word" character
600
601 Each pair of escape sequences partitions the complete set of characters into
602 two disjoint sets. Any given character matches one, and only one, of each pair.
603
604 A "word" character is any letter or digit or the underscore character, that is,
605 any character which can be part of a Perl "word". The definition of letters and
606 digits is controlled by PCRE's character tables, and may vary if locale-
607 specific matching is taking place (see "Locale support" above). For example, in
608 the "fr" (French) locale, some character codes greater than 128 are used for
609 accented letters, and these are matched by \\w.
610
611 These character type sequences can appear both inside and outside character
612 classes. They each match one character of the appropriate type. If the current
613 matching point is at the end of the subject string, all of them fail, since
614 there is no character to match.
615
616 The fourth use of backslash is for certain simple assertions. An assertion
617 specifies a condition that has to be met at a particular point in a match,
618 without consuming any characters from the subject string. The use of
619 subpatterns for more complicated assertions is described below. The backslashed
620 assertions are
621
622 \\b word boundary
623 \\B not a word boundary
624 \\A start of subject (independent of multiline mode)
625 \\Z end of subject or newline at end (independent of multiline mode)
626 \\z end of subject (independent of multiline mode)
627
628 These assertions may not appear in character classes (but note that "\\b" has a
629 different meaning, namely the backspace character, inside a character class).
630
631 A word boundary is a position in the subject string where the current character
632 and the previous character do not both match \\w or \\W (i.e. one matches
633 \\w and the other matches \\W), or the start or end of the string if the
634 first or last character matches \\w, respectively.
635
636 The \\A, \\Z, and \\z assertions differ from the traditional circumflex and
637 dollar (described below) in that they only ever match at the very start and end
638 of the subject string, whatever options are set. They are not affected by the
639 PCRE_NOTBOL or PCRE_NOTEOL options. The difference between \\Z and \\z is that
640 \\Z matches before a newline that is the last character of the string as well
641 as at the end of the string, whereas \\z matches only at the end.
642
643
644 .SH CIRCUMFLEX AND DOLLAR
645 Outside a character class, in the default matching mode, the circumflex
646 character is an assertion which is true only if the current matching point is
647 at the start of the subject string. Inside a character class, circumflex has an
648 entirely different meaning (see below).
649
650 Circumflex need not be the first character of the pattern if a number of
651 alternatives are involved, but it should be the first thing in each alternative
652 in which it appears if the pattern is ever to match that branch. If all
653 possible alternatives start with a circumflex, that is, if the pattern is
654 constrained to match only at the start of the subject, it is said to be an
655 "anchored" pattern. (There are also other constructs that can cause a pattern
656 to be anchored.)
657
658 A dollar character is an assertion which is true only if the current matching
659 point is at the end of the subject string, or immediately before a newline
660 character that is the last character in the string (by default). Dollar need
661 not be the last character of the pattern if a number of alternatives are
662 involved, but it should be the last item in any branch in which it appears.
663 Dollar has no special meaning in a character class.
664
665 The meaning of dollar can be changed so that it matches only at the very end of
666 the string, by setting the PCRE_DOLLAR_ENDONLY option at compile or matching
667 time. This does not affect the \\Z assertion.
668
669 The meanings of the circumflex and dollar characters are changed if the
670 PCRE_MULTILINE option is set. When this is the case, they match immediately
671 after and immediately before an internal "\\n" character, respectively, in
672 addition to matching at the start and end of the subject string. For example,
673 the pattern /^abc$/ matches the subject string "def\\nabc" in multiline mode,
674 but not otherwise. Consequently, patterns that are anchored in single line mode
675 because all branches start with "^" are not anchored in multiline mode. The
676 PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
677
678 Note that the sequences \\A, \\Z, and \\z can be used to match the start and
679 end of the subject in both modes, and if all branches of a pattern start with
680 \\A is it always anchored, whether PCRE_MULTILINE is set or not.
681
682
683 .SH FULL STOP (PERIOD, DOT)
684 Outside a character class, a dot in the pattern matches any one character in
685 the subject, including a non-printing character, but not (by default) newline.
686 If the PCRE_DOTALL option is set, then dots match newlines as well. The
687 handling of dot is entirely independent of the handling of circumflex and
688 dollar, the only relationship being that they both involve newline characters.
689 Dot has no special meaning in a character class.
690
691
692 .SH SQUARE BRACKETS
693 An opening square bracket introduces a character class, terminated by a closing
694 square bracket. A closing square bracket on its own is not special. If a
695 closing square bracket is required as a member of the class, it should be the
696 first data character in the class (after an initial circumflex, if present) or
697 escaped with a backslash.
698
699 A character class matches a single character in the subject; the character must
700 be in the set of characters defined by the class, unless the first character in
701 the class is a circumflex, in which case the subject character must not be in
702 the set defined by the class. If a circumflex is actually required as a member
703 of the class, ensure it is not the first character, or escape it with a
704 backslash.
705
706 For example, the character class [aeiou] matches any lower case vowel, while
707 [^aeiou] matches any character that is not a lower case vowel. Note that a
708 circumflex is just a convenient notation for specifying the characters which
709 are in the class by enumerating those that are not. It is not an assertion: it
710 still consumes a character from the subject string, and fails if the current
711 pointer is at the end of the string.
712
713 When caseless matching is set, any letters in a class represent both their
714 upper case and lower case versions, so for example, a caseless [aeiou] matches
715 "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
716 caseful version would.
717
718 The newline character is never treated in any special way in character classes,
719 whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE options is. A class
720 such as [^a] will always match a newline.
721
722 The minus (hyphen) character can be used to specify a range of characters in a
723 character class. For example, [d-m] matches any letter between d and m,
724 inclusive. If a minus character is required in a class, it must be escaped with
725 a backslash or appear in a position where it cannot be interpreted as
726 indicating a range, typically as the first or last character in the class. It
727 is not possible to have the character "]" as the end character of a range,
728 since a sequence such as [w-] is interpreted as a class of two characters. The
729 octal or hexadecimal representation of "]" can, however, be used to end a
730 range.
731
732 Ranges operate in ASCII collating sequence. They can also be used for
733 characters specified numerically, for example [\\000-\\037]. If a range that
734 includes letters is used when caseless matching is set, it matches the letters
735 in either case. For example, [W-c] is equivalent to [][\\^_`wxyzabc], matched
736 caselessly, and if character tables for the "fr" locale are in use,
737 [\\xc8-\\xcb] matches accented E characters in both cases.
738
739 The character types \\d, \\D, \\s, \\S, \\w, and \\W may also appear in a
740 character class, and add the characters that they match to the class. For
741 example, [\\dABCDEF] matches any hexadecimal digit. A circumflex can
742 conveniently be used with the upper case character types to specify a more
743 restricted set of characters than the matching lower case type. For example,
744 the class [^\\W_] matches any letter or digit, but not underscore.
745
746 All non-alphameric characters other than \\, -, ^ (at the start) and the
747 terminating ] are non-special in character classes, but it does no harm if they
748 are escaped.
749
750
751 .SH VERTICAL BAR
752 Vertical bar characters are used to separate alternative patterns. For example,
753 the pattern
754
755 gilbert|sullivan
756
757 matches either "gilbert" or "sullivan". Any number of alternatives may appear,
758 and an empty alternative is permitted (matching the empty string).
759 The matching process tries each alternative in turn, from left to right,
760 and the first one that succeeds is used. If the alternatives are within a
761 subpattern (defined below), "succeeds" means matching the rest of the main
762 pattern as well as the alternative in the subpattern.
763
764
765 .SH INTERNAL OPTION SETTING
766 The settings of PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and PCRE_EXTENDED
767 can be changed from within the pattern by a sequence of Perl option letters
768 enclosed between "(?" and ")". The option letters are
769
770 i for PCRE_CASELESS
771 m for PCRE_MULTILINE
772 s for PCRE_DOTALL
773 x for PCRE_EXTENDED
774
775 For example, (?im) sets caseless, multiline matching. It is also possible to
776 unset these options by preceding the letter with a hyphen, and a combined
777 setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
778 PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
779 permitted. If a letter appears both before and after the hyphen, the option is
780 unset.
781
782 The scope of these option changes depends on where in the pattern the setting
783 occurs. For settings that are outside any subpattern (defined below), the
784 effect is the same as if the options were set or unset at the start of
785 matching. The following patterns all behave in exactly the same way:
786
787 (?i)abc
788 a(?i)bc
789 ab(?i)c
790 abc(?i)
791
792 which in turn is the same as compiling the pattern abc with PCRE_CASELESS set.
793 In other words, such "top level" settings apply to the whole pattern (unless
794 there are other changes inside subpatterns). If there is more than one setting
795 of the same option at top level, the rightmost setting is used.
796
797 If an option change occurs inside a subpattern, the effect is different. This
798 is a change of behaviour in Perl 5.005. An option change inside a subpattern
799 affects only that part of the subpattern that follows it, so
800
801 (a(?i)b)c
802
803 matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
804 By this means, options can be made to have different settings in different
805 parts of the pattern. Any changes made in one alternative do carry on
806 into subsequent branches within the same subpattern. For example,
807
808 (a(?i)b|c)
809
810 matches "ab", "aB", "c", and "C", even though when matching "C" the first
811 branch is abandoned before the option setting. This is because the effects of
812 option settings happen at compile time. There would be some very weird
813 behaviour otherwise.
814
815 The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changed in the
816 same way as the Perl-compatible options by using the characters U and X
817 respectively. The (?X) flag setting is special in that it must always occur
818 earlier in the pattern than any of the additional features it turns on, even
819 when it is at top level. It is best put at the start.
820
821
822 .SH SUBPATTERNS
823 Subpatterns are delimited by parentheses (round brackets), which can be nested.
824 Marking part of a pattern as a subpattern does two things:
825
826 1. It localizes a set of alternatives. For example, the pattern
827
828 cat(aract|erpillar|)
829
830 matches one of the words "cat", "cataract", or "caterpillar". Without the
831 parentheses, it would match "cataract", "erpillar" or the empty string.
832
833 2. It sets up the subpattern as a capturing subpattern (as defined above).
834 When the whole pattern matches, that portion of the subject string that matched
835 the subpattern is passed back to the caller via the \fIovector\fR argument of
836 \fBpcre_exec()\fR. Opening parentheses are counted from left to right (starting
837 from 1) to obtain the numbers of the capturing subpatterns.
838
839 For example, if the string "the red king" is matched against the pattern
840
841 the ((red|white) (king|queen))
842
843 the captured substrings are "red king", "red", and "king", and are numbered 1,
844 2, and 3.
845
846 The fact that plain parentheses fulfil two functions is not always helpful.
847 There are often times when a grouping subpattern is required without a
848 capturing requirement. If an opening parenthesis is followed by "?:", the
849 subpattern does not do any capturing, and is not counted when computing the
850 number of any subsequent capturing subpatterns. For example, if the string "the
851 white queen" is matched against the pattern
852
853 the ((?:red|white) (king|queen))
854
855 the captured substrings are "white queen" and "queen", and are numbered 1 and
856 2. The maximum number of captured substrings is 99, and the maximum number of
857 all subpatterns, both capturing and non-capturing, is 200.
858
859 As a convenient shorthand, if any option settings are required at the start of
860 a non-capturing subpattern, the option letters may appear between the "?" and
861 the ":". Thus the two patterns
862
863 (?i:saturday|sunday)
864 (?:(?i)saturday|sunday)
865
866 match exactly the same set of strings. Because alternative branches are tried
867 from left to right, and options are not reset until the end of the subpattern
868 is reached, an option setting in one branch does affect subsequent branches, so
869 the above patterns match "SUNDAY" as well as "Saturday".
870
871
872 .SH REPETITION
873 Repetition is specified by quantifiers, which can follow any of the following
874 items:
875
876 a single character, possibly escaped
877 the . metacharacter
878 a character class
879 a back reference (see next section)
880 a parenthesized subpattern (unless it is an assertion - see below)
881
882 The general repetition quantifier specifies a minimum and maximum number of
883 permitted matches, by giving the two numbers in curly brackets (braces),
884 separated by a comma. The numbers must be less than 65536, and the first must
885 be less than or equal to the second. For example:
886
887 z{2,4}
888
889 matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
890 character. If the second number is omitted, but the comma is present, there is
891 no upper limit; if the second number and the comma are both omitted, the
892 quantifier specifies an exact number of required matches. Thus
893
894 [aeiou]{3,}
895
896 matches at least 3 successive vowels, but may match many more, while
897
898 \\d{8}
899
900 matches exactly 8 digits. An opening curly bracket that appears in a position
901 where a quantifier is not allowed, or one that does not match the syntax of a
902 quantifier, is taken as a literal character. For example, {,6} is not a
903 quantifier, but a literal string of four characters.
904
905 The quantifier {0} is permitted, causing the expression to behave as if the
906 previous item and the quantifier were not present.
907
908 For convenience (and historical compatibility) the three most common
909 quantifiers have single-character abbreviations:
910
911 * is equivalent to {0,}
912 + is equivalent to {1,}
913 ? is equivalent to {0,1}
914
915 It is possible to construct infinite loops by following a subpattern that can
916 match no characters with a quantifier that has no upper limit, for example:
917
918 (a?)*
919
920 Earlier versions of Perl and PCRE used to give an error at compile time for
921 such patterns. However, because there are cases where this can be useful, such
922 patterns are now accepted, but if any repetition of the subpattern does in fact
923 match no characters, the loop is forcibly broken.
924
925 By default, the quantifiers are "greedy", that is, they match as much as
926 possible (up to the maximum number of permitted times), without causing the
927 rest of the pattern to fail. The classic example of where this gives problems
928 is in trying to match comments in C programs. These appear between the
929 sequences /* and */ and within the sequence, individual * and / characters may
930 appear. An attempt to match C comments by applying the pattern
931
932 /\\*.*\\*/
933
934 to the string
935
936 /* first command */ not comment /* second comment */
937
938 fails, because it matches the entire string due to the greediness of the .*
939 item.
940
941 However, if a quantifier is followed by a question mark, then it ceases to be
942 greedy, and instead matches the minimum number of times possible, so the
943 pattern
944
945 /\\*.*?\\*/
946
947 does the right thing with the C comments. The meaning of the various
948 quantifiers is not otherwise changed, just the preferred number of matches.
949 Do not confuse this use of question mark with its use as a quantifier in its
950 own right. Because it has two uses, it can sometimes appear doubled, as in
951
952 \\d??\\d
953
954 which matches one digit by preference, but can match two if that is the only
955 way the rest of the pattern matches.
956
957 If the PCRE_UNGREEDY option is set (an option which is not available in Perl)
958 then the quantifiers are not greedy by default, but individual ones can be made
959 greedy by following them with a question mark. In other words, it inverts the
960 default behaviour.
961
962 When a parenthesized subpattern is quantified with a minimum repeat count that
963 is greater than 1 or with a limited maximum, more store is required for the
964 compiled pattern, in proportion to the size of the minimum or maximum.
965
966 If a pattern starts with .* then it is implicitly anchored, since whatever
967 follows will be tried against every character position in the subject string.
968 PCRE treats this as though it were preceded by \\A.
969
970 When a capturing subpattern is repeated, the value captured is the substring
971 that matched the final iteration. For example, after
972
973 (tweedle[dume]{3}\\s*)+
974
975 has matched "tweedledum tweedledee" the value of the captured substring is
976 "tweedledee". However, if there are nested capturing subpatterns, the
977 corresponding captured values may have been set in previous iterations. For
978 example, after
979
980 /(a|(b))+/
981
982 matches "aba" the value of the second captured substring is "b".
983
984
985 .SH BACK REFERENCES
986 Outside a character class, a backslash followed by a digit greater than 0 (and
987 possibly further digits) is a back reference to a capturing subpattern earlier
988 (i.e. to its left) in the pattern, provided there have been that many previous
989 capturing left parentheses.
990
991 However, if the decimal number following the backslash is less than 10, it is
992 always taken as a back reference, and causes an error only if there are not
993 that many capturing left parentheses in the entire pattern. In other words, the
994 parentheses that are referenced need not be to the left of the reference for
995 numbers less than 10. See the section entitled "Backslash" above for further
996 details of the handling of digits following a backslash.
997
998 A back reference matches whatever actually matched the capturing subpattern in
999 the current subject string, rather than anything matching the subpattern
1000 itself. So the pattern
1001
1002 (sens|respons)e and \\1ibility
1003
1004 matches "sense and sensibility" and "response and responsibility", but not
1005 "sense and responsibility". If caseful matching is in force at the time of the
1006 back reference, then the case of letters is relevant. For example,
1007
1008 ((?i)rah)\\s+\\1
1009
1010 matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
1011 capturing subpattern is matched caselessly.
1012
1013 There may be more than one back reference to the same subpattern. If a
1014 subpattern has not actually been used in a particular match, then any back
1015 references to it always fail. For example, the pattern
1016
1017 (a|(bc))\\2
1018
1019 always fails if it starts to match "a" rather than "bc". Because there may be
1020 up to 99 back references, all digits following the backslash are taken
1021 as part of a potential back reference number. If the pattern continues with a
1022 digit character, then some delimiter must be used to terminate the back
1023 reference. If the PCRE_EXTENDED option is set, this can be whitespace.
1024 Otherwise an empty comment can be used.
1025
1026 A back reference that occurs inside the parentheses to which it refers fails
1027 when the subpattern is first used, so, for example, (a\\1) never matches.
1028 However, such references can be useful inside repeated subpatterns. For
1029 example, the pattern
1030
1031 (a|b\\1)+
1032
1033 matches any number of "a"s and also "aba", "ababaa" etc. At each iteration of
1034 the subpattern, the back reference matches the character string corresponding
1035 to the previous iteration. In order for this to work, the pattern must be such
1036 that the first iteration does not need to match the back reference. This can be
1037 done using alternation, as in the example above, or by a quantifier with a
1038 minimum of zero.
1039
1040
1041 .SH ASSERTIONS
1042 An assertion is a test on the characters following or preceding the current
1043 matching point that does not actually consume any characters. The simple
1044 assertions coded as \\b, \\B, \\A, \\Z, \\z, ^ and $ are described above. More
1045 complicated assertions are coded as subpatterns. There are two kinds: those
1046 that look ahead of the current position in the subject string, and those that
1047 look behind it.
1048
1049 An assertion subpattern is matched in the normal way, except that it does not
1050 cause the current matching position to be changed. Lookahead assertions start
1051 with (?= for positive assertions and (?! for negative assertions. For example,
1052
1053 \\w+(?=;)
1054
1055 matches a word followed by a semicolon, but does not include the semicolon in
1056 the match, and
1057
1058 foo(?!bar)
1059
1060 matches any occurrence of "foo" that is not followed by "bar". Note that the
1061 apparently similar pattern
1062
1063 (?!foo)bar
1064
1065 does not find an occurrence of "bar" that is preceded by something other than
1066 "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
1067 (?!foo) is always true when the next three characters are "bar". A
1068 lookbehind assertion is needed to achieve this effect.
1069
1070 Lookbehind assertions start with (?<= for positive assertions and (?<! for
1071 negative assertions. For example,
1072
1073 (?<!foo)bar
1074
1075 does find an occurrence of "bar" that is not preceded by "foo". The contents of
1076 a lookbehind assertion are restricted such that all the strings it matches must
1077 have a fixed length. However, if there are several alternatives, they do not
1078 all have to have the same fixed length. Thus
1079
1080 (?<=bullock|donkey)
1081
1082 is permitted, but
1083
1084 (?<!dogs?|cats?)
1085
1086 causes an error at compile time. Branches that match different length strings
1087 are permitted only at the top level of a lookbehind assertion. This is an
1088 extension compared with Perl 5.005, which requires all branches to match the
1089 same length of string. An assertion such as
1090
1091 (?<=ab(c|de))
1092
1093 is not permitted, because its single branch can match two different lengths,
1094 but it is acceptable if rewritten to use two branches:
1095
1096 (?<=abc|abde)
1097
1098 The implementation of lookbehind assertions is, for each alternative, to
1099 temporarily move the current position back by the fixed width and then try to
1100 match. If there are insufficient characters before the current position, the
1101 match is deemed to fail.
1102
1103 Assertions can be nested in any combination. For example,
1104
1105 (?<=(?<!foo)bar)baz
1106
1107 matches an occurrence of "baz" that is preceded by "bar" which in turn is not
1108 preceded by "foo".
1109
1110 Assertion subpatterns are not capturing subpatterns, and may not be repeated,
1111 because it makes no sense to assert the same thing several times. If an
1112 assertion contains capturing subpatterns within it, these are always counted
1113 for the purposes of numbering the capturing subpatterns in the whole pattern.
1114 Substring capturing is carried out for positive assertions, but it does not
1115 make sense for negative assertions.
1116
1117 Assertions count towards the maximum of 200 parenthesized subpatterns.
1118
1119
1120 .SH ONCE-ONLY SUBPATTERNS
1121 With both maximizing and minimizing repetition, failure of what follows
1122 normally causes the repeated item to be re-evaluated to see if a different
1123 number of repeats allows the rest of the pattern to match. Sometimes it is
1124 useful to prevent this, either to change the nature of the match, or to cause
1125 it fail earlier than it otherwise might, when the author of the pattern knows
1126 there is no point in carrying on.
1127
1128 Consider, for example, the pattern \\d+foo when applied to the subject line
1129
1130 123456bar
1131
1132 After matching all 6 digits and then failing to match "foo", the normal
1133 action of the matcher is to try again with only 5 digits matching the \\d+
1134 item, and then with 4, and so on, before ultimately failing. Once-only
1135 subpatterns provide the means for specifying that once a portion of the pattern
1136 has matched, it is not to be re-evaluated in this way, so the matcher would
1137 give up immediately on failing to match "foo" the first time. The notation is
1138 another kind of special parenthesis, starting with (?> as in this example:
1139
1140 (?>\\d+)bar
1141
1142 This kind of parenthesis "locks up" the part of the pattern it contains once
1143 it has matched, and a failure further into the pattern is prevented from
1144 backtracking into it. Backtracking past it to previous items, however, works as
1145 normal.
1146
1147 An alternative description is that a subpattern of this type matches the string
1148 of characters that an identical standalone pattern would match, if anchored at
1149 the current point in the subject string.
1150
1151 Once-only subpatterns are not capturing subpatterns. Simple cases such as the
1152 above example can be though of as a maximizing repeat that must swallow
1153 everything it can. So, while both \\d+ and \\d+? are prepared to adjust the
1154 number of digits they match in order to make the rest of the pattern match,
1155 (?>\\d+) can only match an entire sequence of digits.
1156
1157 This construction can of course contain arbitrarily complicated subpatterns,
1158 and it can be nested.
1159
1160
1161 .SH CONDITIONAL SUBPATTERNS
1162 It is possible to cause the matching process to obey a subpattern
1163 conditionally or to choose between two alternative subpatterns, depending on
1164 the result of an assertion, or whether a previous capturing subpattern matched
1165 or not. The two possible forms of conditional subpattern are
1166
1167 (?(condition)yes-pattern)
1168 (?(condition)yes-pattern|no-pattern)
1169
1170 If the condition is satisfied, the yes-pattern is used; otherwise the
1171 no-pattern (if present) is used. If there are more than two alternatives in the
1172 subpattern, a compile-time error occurs.
1173
1174 There are two kinds of condition. If the text between the parentheses consists
1175 of a sequence of digits, then the condition is satisfied if the capturing
1176 subpattern of that number has previously matched. Consider the following
1177 pattern, which contains non-significant white space to make it more readable
1178 (assume the PCRE_EXTENDED option) and to divide it into three parts for ease
1179 of discussion:
1180
1181 ( \\( )? [^()]+ (?(1) \\) )
1182
1183 The first part matches an optional opening parenthesis, and if that
1184 character is present, sets it as the first captured substring. The second part
1185 matches one or more characters that are not parentheses. The third part is a
1186 conditional subpattern that tests whether the first set of parentheses matched
1187 or not. If they did, that is, if subject started with an opening parenthesis,
1188 the condition is true, and so the yes-pattern is executed and a closing
1189 parenthesis is required. Otherwise, since no-pattern is not present, the
1190 subpattern matches nothing. In other words, this pattern matches a sequence of
1191 non-parentheses, optionally enclosed in parentheses.
1192
1193 If the condition is not a sequence of digits, it must be an assertion. This may
1194 be a positive or negative lookahead or lookbehind assertion. Consider this
1195 pattern, again containing non-significant white space, and with the two
1196 alternatives on the second line:
1197
1198 (?(?=[^a-z]*[a-z])
1199 \\d{2}[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )
1200
1201 The condition is a positive lookahead assertion that matches an optional
1202 sequence of non-letters followed by a letter. In other words, it tests for the
1203 presence of at least one letter in the subject. If a letter is found, the
1204 subject is matched against the first alternative; otherwise it is matched
1205 against the second. This pattern matches strings in one of the two forms
1206 dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
1207
1208
1209 .SH COMMENTS
1210 The sequence (?# marks the start of a comment which continues up to the next
1211 closing parenthesis. Nested parentheses are not permitted. The characters
1212 that make up a comment play no part in the pattern matching at all.
1213
1214 If the PCRE_EXTENDED option is set, an unescaped # character outside a
1215 character class introduces a comment that continues up to the next newline
1216 character in the pattern.
1217
1218
1219 .SH PERFORMANCE
1220 Certain items that may appear in patterns are more efficient than others. It is
1221 more efficient to use a character class like [aeiou] than a set of alternatives
1222 such as (a|e|i|o|u). In general, the simplest construction that provides the
1223 required behaviour is usually the most efficient. Jeffrey Friedl's book
1224 contains a lot of discussion about optimizing regular expressions for efficient
1225 performance.
1226
1227
1228 .SH AUTHOR
1229 Philip Hazel <ph10@cam.ac.uk>
1230 .br
1231 University Computing Service,
1232 .br
1233 New Museums Site,
1234 .br
1235 Cambridge CB2 3QG, England.
1236 .br
1237 Phone: +44 1223 334714
1238
1239 Copyright (c) 1998 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12