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

Contents of /code/trunk/doc/pcrepattern.3

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (show annotations) (download)
Sat Feb 24 21:41:42 2007 UTC (7 years, 8 months ago) by nigel
File size: 74510 byte(s)
Load pcre-7.0 into code/trunk.

1 .TH PCREPATTERN 3
2 .SH NAME
3 PCRE - Perl-compatible regular expressions
4 .SH "PCRE REGULAR EXPRESSION DETAILS"
5 .rs
6 .sp
7 The syntax and semantics of the regular expressions supported by PCRE are
8 described below. Regular expressions are also described in the Perl
9 documentation and in a number of books, some of which have copious examples.
10 Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly, covers
11 regular expressions in great detail. This description of PCRE's regular
12 expressions is intended as reference material.
13 .P
14 The original operation of PCRE was on strings of one-byte characters. However,
15 there is now also support for UTF-8 character strings. To use this, you must
16 build PCRE to include UTF-8 support, and then call \fBpcre_compile()\fP with
17 the PCRE_UTF8 option. How this affects pattern matching is mentioned in several
18 places below. There is also a summary of UTF-8 features in the
19 .\" HTML <a href="pcre.html#utf8support">
20 .\" </a>
21 section on UTF-8 support
22 .\"
23 in the main
24 .\" HREF
25 \fBpcre\fP
26 .\"
27 page.
28 .P
29 The remainder of this document discusses the patterns that are supported by
30 PCRE when its main matching function, \fBpcre_exec()\fP, is used.
31 From release 6.0, PCRE offers a second matching function,
32 \fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not
33 Perl-compatible. The advantages and disadvantages of the alternative function,
34 and how it differs from the normal function, are discussed in the
35 .\" HREF
36 \fBpcrematching\fP
37 .\"
38 page.
39 .
40 .
41 .SH "CHARACTERS AND METACHARACTERS"
42 .rs
43 .sp
44 A regular expression is a pattern that is matched against a subject string from
45 left to right. Most characters stand for themselves in a pattern, and match the
46 corresponding characters in the subject. As a trivial example, the pattern
47 .sp
48 The quick brown fox
49 .sp
50 matches a portion of a subject string that is identical to itself. When
51 caseless matching is specified (the PCRE_CASELESS option), letters are matched
52 independently of case. In UTF-8 mode, PCRE always understands the concept of
53 case for characters whose values are less than 128, so caseless matching is
54 always possible. For characters with higher values, the concept of case is
55 supported if PCRE is compiled with Unicode property support, but not otherwise.
56 If you want to use caseless matching for characters 128 and above, you must
57 ensure that PCRE is compiled with Unicode property support as well as with
58 UTF-8 support.
59 .P
60 The power of regular expressions comes from the ability to include alternatives
61 and repetitions in the pattern. These are encoded in the pattern by the use of
62 \fImetacharacters\fP, which do not stand for themselves but instead are
63 interpreted in some special way.
64 .P
65 There are two different sets of metacharacters: those that are recognized
66 anywhere in the pattern except within square brackets, and those that are
67 recognized within square brackets. Outside square brackets, the metacharacters
68 are as follows:
69 .sp
70 \e general escape character with several uses
71 ^ assert start of string (or line, in multiline mode)
72 $ assert end of string (or line, in multiline mode)
73 . match any character except newline (by default)
74 [ start character class definition
75 | start of alternative branch
76 ( start subpattern
77 ) end subpattern
78 ? extends the meaning of (
79 also 0 or 1 quantifier
80 also quantifier minimizer
81 * 0 or more quantifier
82 + 1 or more quantifier
83 also "possessive quantifier"
84 { start min/max quantifier
85 .sp
86 Part of a pattern that is in square brackets is called a "character class". In
87 a character class the only metacharacters are:
88 .sp
89 \e general escape character
90 ^ negate the class, but only if the first character
91 - indicates character range
92 .\" JOIN
93 [ POSIX character class (only if followed by POSIX
94 syntax)
95 ] terminates the character class
96 .sp
97 The following sections describe the use of each of the metacharacters.
98 .
99 .
100 .SH BACKSLASH
101 .rs
102 .sp
103 The backslash character has several uses. Firstly, if it is followed by a
104 non-alphanumeric character, it takes away any special meaning that character
105 may have. This use of backslash as an escape character applies both inside and
106 outside character classes.
107 .P
108 For example, if you want to match a * character, you write \e* in the pattern.
109 This escaping action applies whether or not the following character would
110 otherwise be interpreted as a metacharacter, so it is always safe to precede a
111 non-alphanumeric with backslash to specify that it stands for itself. In
112 particular, if you want to match a backslash, you write \e\e.
113 .P
114 If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
115 pattern (other than in a character class) and characters between a # outside
116 a character class and the next newline are ignored. An escaping backslash can
117 be used to include a whitespace or # character as part of the pattern.
118 .P
119 If you want to remove the special meaning from a sequence of characters, you
120 can do so by putting them between \eQ and \eE. This is different from Perl in
121 that $ and @ are handled as literals in \eQ...\eE sequences in PCRE, whereas in
122 Perl, $ and @ cause variable interpolation. Note the following examples:
123 .sp
124 Pattern PCRE matches Perl matches
125 .sp
126 .\" JOIN
127 \eQabc$xyz\eE abc$xyz abc followed by the
128 contents of $xyz
129 \eQabc\e$xyz\eE abc\e$xyz abc\e$xyz
130 \eQabc\eE\e$\eQxyz\eE abc$xyz abc$xyz
131 .sp
132 The \eQ...\eE sequence is recognized both inside and outside character classes.
133 .
134 .
135 .\" HTML <a name="digitsafterbackslash"></a>
136 .SS "Non-printing characters"
137 .rs
138 .sp
139 A second use of backslash provides a way of encoding non-printing characters
140 in patterns in a visible manner. There is no restriction on the appearance of
141 non-printing characters, apart from the binary zero that terminates a pattern,
142 but when a pattern is being prepared by text editing, it is usually easier to
143 use one of the following escape sequences than the binary character it
144 represents:
145 .sp
146 \ea alarm, that is, the BEL character (hex 07)
147 \ecx "control-x", where x is any character
148 \ee escape (hex 1B)
149 \ef formfeed (hex 0C)
150 \en newline (hex 0A)
151 \er carriage return (hex 0D)
152 \et tab (hex 09)
153 \eddd character with octal code ddd, or backreference
154 \exhh character with hex code hh
155 \ex{hhh..} character with hex code hhh..
156 .sp
157 The precise effect of \ecx is as follows: if x is a lower case letter, it
158 is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
159 Thus \ecz becomes hex 1A, but \ec{ becomes hex 3B, while \ec; becomes hex
160 7B.
161 .P
162 After \ex, from zero to two hexadecimal digits are read (letters can be in
163 upper or lower case). Any number of hexadecimal digits may appear between \ex{
164 and }, but the value of the character code must be less than 256 in non-UTF-8
165 mode, and less than 2**31 in UTF-8 mode (that is, the maximum hexadecimal value
166 is 7FFFFFFF). If characters other than hexadecimal digits appear between \ex{
167 and }, or if there is no terminating }, this form of escape is not recognized.
168 Instead, the initial \ex will be interpreted as a basic hexadecimal escape,
169 with no following digits, giving a character whose value is zero.
170 .P
171 Characters whose value is less than 256 can be defined by either of the two
172 syntaxes for \ex. There is no difference in the way they are handled. For
173 example, \exdc is exactly the same as \ex{dc}.
174 .P
175 After \e0 up to two further octal digits are read. If there are fewer than two
176 digits, just those that are present are used. Thus the sequence \e0\ex\e07
177 specifies two binary zeros followed by a BEL character (code value 7). Make
178 sure you supply two digits after the initial zero if the pattern character that
179 follows is itself an octal digit.
180 .P
181 The handling of a backslash followed by a digit other than 0 is complicated.
182 Outside a character class, PCRE reads it and any following digits as a decimal
183 number. If the number is less than 10, or if there have been at least that many
184 previous capturing left parentheses in the expression, the entire sequence is
185 taken as a \fIback reference\fP. A description of how this works is given
186 .\" HTML <a href="#backreferences">
187 .\" </a>
188 later,
189 .\"
190 following the discussion of
191 .\" HTML <a href="#subpattern">
192 .\" </a>
193 parenthesized subpatterns.
194 .\"
195 .P
196 Inside a character class, or if the decimal number is greater than 9 and there
197 have not been that many capturing subpatterns, PCRE re-reads up to three octal
198 digits following the backslash, and uses them to generate a data character. Any
199 subsequent digits stand for themselves. In non-UTF-8 mode, the value of a
200 character specified in octal must be less than \e400. In UTF-8 mode, values up
201 to \e777 are permitted. For example:
202 .sp
203 \e040 is another way of writing a space
204 .\" JOIN
205 \e40 is the same, provided there are fewer than 40
206 previous capturing subpatterns
207 \e7 is always a back reference
208 .\" JOIN
209 \e11 might be a back reference, or another way of
210 writing a tab
211 \e011 is always a tab
212 \e0113 is a tab followed by the character "3"
213 .\" JOIN
214 \e113 might be a back reference, otherwise the
215 character with octal code 113
216 .\" JOIN
217 \e377 might be a back reference, otherwise
218 the byte consisting entirely of 1 bits
219 .\" JOIN
220 \e81 is either a back reference, or a binary zero
221 followed by the two characters "8" and "1"
222 .sp
223 Note that octal values of 100 or greater must not be introduced by a leading
224 zero, because no more than three octal digits are ever read.
225 .P
226 All the sequences that define a single character value can be used both inside
227 and outside character classes. In addition, inside a character class, the
228 sequence \eb is interpreted as the backspace character (hex 08), and the
229 sequences \eR and \eX are interpreted as the characters "R" and "X",
230 respectively. Outside a character class, these sequences have different
231 meanings
232 .\" HTML <a href="#uniextseq">
233 .\" </a>
234 (see below).
235 .\"
236 .
237 .
238 .SS "Absolute and relative back references"
239 .rs
240 .sp
241 The sequence \eg followed by a positive or negative number, optionally enclosed
242 in braces, is an absolute or relative back reference. Back references are
243 discussed
244 .\" HTML <a href="#backreferences">
245 .\" </a>
246 later,
247 .\"
248 following the discussion of
249 .\" HTML <a href="#subpattern">
250 .\" </a>
251 parenthesized subpatterns.
252 .\"
253 .
254 .
255 .SS "Generic character types"
256 .rs
257 .sp
258 Another use of backslash is for specifying generic character types. The
259 following are always recognized:
260 .sp
261 \ed any decimal digit
262 \eD any character that is not a decimal digit
263 \es any whitespace character
264 \eS any character that is not a whitespace character
265 \ew any "word" character
266 \eW any "non-word" character
267 .sp
268 Each pair of escape sequences partitions the complete set of characters into
269 two disjoint sets. Any given character matches one, and only one, of each pair.
270 .P
271 These character type sequences can appear both inside and outside character
272 classes. They each match one character of the appropriate type. If the current
273 matching point is at the end of the subject string, all of them fail, since
274 there is no character to match.
275 .P
276 For compatibility with Perl, \es does not match the VT character (code 11).
277 This makes it different from the the POSIX "space" class. The \es characters
278 are HT (9), LF (10), FF (12), CR (13), and space (32). (If "use locale;" is
279 included in a Perl script, \es may match the VT character. In PCRE, it never
280 does.)
281 .P
282 A "word" character is an underscore or any character less than 256 that is a
283 letter or digit. The definition of letters and digits is controlled by PCRE's
284 low-valued character tables, and may vary if locale-specific matching is taking
285 place (see
286 .\" HTML <a href="pcreapi.html#localesupport">
287 .\" </a>
288 "Locale support"
289 .\"
290 in the
291 .\" HREF
292 \fBpcreapi\fP
293 .\"
294 page). For example, in the "fr_FR" (French) locale, some character codes
295 greater than 128 are used for accented letters, and these are matched by \ew.
296 .P
297 In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or
298 \ew, and always match \eD, \eS, and \eW. This is true even when Unicode
299 character property support is available. The use of locales with Unicode is
300 discouraged.
301 .
302 .
303 .SS "Newline sequences"
304 .rs
305 .sp
306 Outside a character class, the escape sequence \eR matches any Unicode newline
307 sequence. This is an extension to Perl. In non-UTF-8 mode \eR is equivalent to
308 the following:
309 .sp
310 (?>\er\en|\en|\ex0b|\ef|\er|\ex85)
311 .sp
312 This is an example of an "atomic group", details of which are given
313 .\" HTML <a href="#atomicgroup">
314 .\" </a>
315 below.
316 .\"
317 This particular group matches either the two-character sequence CR followed by
318 LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
319 U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next
320 line, U+0085). The two-character sequence is treated as a single unit that
321 cannot be split.
322 .P
323 In UTF-8 mode, two additional characters whose codepoints are greater than 255
324 are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
325 Unicode character property support is not needed for these characters to be
326 recognized.
327 .P
328 Inside a character class, \eR matches the letter "R".
329 .
330 .
331 .\" HTML <a name="uniextseq"></a>
332 .SS Unicode character properties
333 .rs
334 .sp
335 When PCRE is built with Unicode character property support, three additional
336 escape sequences to match character properties are available when UTF-8 mode
337 is selected. They are:
338 .sp
339 \ep{\fIxx\fP} a character with the \fIxx\fP property
340 \eP{\fIxx\fP} a character without the \fIxx\fP property
341 \eX an extended Unicode sequence
342 .sp
343 The property names represented by \fIxx\fP above are limited to the Unicode
344 script names, the general category properties, and "Any", which matches any
345 character (including newline). Other properties such as "InMusicalSymbols" are
346 not currently supported by PCRE. Note that \eP{Any} does not match any
347 characters, so always causes a match failure.
348 .P
349 Sets of Unicode characters are defined as belonging to certain scripts. A
350 character from one of these sets can be matched using a script name. For
351 example:
352 .sp
353 \ep{Greek}
354 \eP{Han}
355 .sp
356 Those that are not part of an identified script are lumped together as
357 "Common". The current list of scripts is:
358 .P
359 Arabic,
360 Armenian,
361 Balinese,
362 Bengali,
363 Bopomofo,
364 Braille,
365 Buginese,
366 Buhid,
367 Canadian_Aboriginal,
368 Cherokee,
369 Common,
370 Coptic,
371 Cuneiform,
372 Cypriot,
373 Cyrillic,
374 Deseret,
375 Devanagari,
376 Ethiopic,
377 Georgian,
378 Glagolitic,
379 Gothic,
380 Greek,
381 Gujarati,
382 Gurmukhi,
383 Han,
384 Hangul,
385 Hanunoo,
386 Hebrew,
387 Hiragana,
388 Inherited,
389 Kannada,
390 Katakana,
391 Kharoshthi,
392 Khmer,
393 Lao,
394 Latin,
395 Limbu,
396 Linear_B,
397 Malayalam,
398 Mongolian,
399 Myanmar,
400 New_Tai_Lue,
401 Nko,
402 Ogham,
403 Old_Italic,
404 Old_Persian,
405 Oriya,
406 Osmanya,
407 Phags_Pa,
408 Phoenician,
409 Runic,
410 Shavian,
411 Sinhala,
412 Syloti_Nagri,
413 Syriac,
414 Tagalog,
415 Tagbanwa,
416 Tai_Le,
417 Tamil,
418 Telugu,
419 Thaana,
420 Thai,
421 Tibetan,
422 Tifinagh,
423 Ugaritic,
424 Yi.
425 .P
426 Each character has exactly one general category property, specified by a
427 two-letter abbreviation. For compatibility with Perl, negation can be specified
428 by including a circumflex between the opening brace and the property name. For
429 example, \ep{^Lu} is the same as \eP{Lu}.
430 .P
431 If only one letter is specified with \ep or \eP, it includes all the general
432 category properties that start with that letter. In this case, in the absence
433 of negation, the curly brackets in the escape sequence are optional; these two
434 examples have the same effect:
435 .sp
436 \ep{L}
437 \epL
438 .sp
439 The following general category property codes are supported:
440 .sp
441 C Other
442 Cc Control
443 Cf Format
444 Cn Unassigned
445 Co Private use
446 Cs Surrogate
447 .sp
448 L Letter
449 Ll Lower case letter
450 Lm Modifier letter
451 Lo Other letter
452 Lt Title case letter
453 Lu Upper case letter
454 .sp
455 M Mark
456 Mc Spacing mark
457 Me Enclosing mark
458 Mn Non-spacing mark
459 .sp
460 N Number
461 Nd Decimal number
462 Nl Letter number
463 No Other number
464 .sp
465 P Punctuation
466 Pc Connector punctuation
467 Pd Dash punctuation
468 Pe Close punctuation
469 Pf Final punctuation
470 Pi Initial punctuation
471 Po Other punctuation
472 Ps Open punctuation
473 .sp
474 S Symbol
475 Sc Currency symbol
476 Sk Modifier symbol
477 Sm Mathematical symbol
478 So Other symbol
479 .sp
480 Z Separator
481 Zl Line separator
482 Zp Paragraph separator
483 Zs Space separator
484 .sp
485 The special property L& is also supported: it matches a character that has
486 the Lu, Ll, or Lt property, in other words, a letter that is not classified as
487 a modifier or "other".
488 .P
489 The long synonyms for these properties that Perl supports (such as \ep{Letter})
490 are not supported by PCRE, nor is it permitted to prefix any of these
491 properties with "Is".
492 .P
493 No character that is in the Unicode table has the Cn (unassigned) property.
494 Instead, this property is assumed for any code point that is not in the
495 Unicode table.
496 .P
497 Specifying caseless matching does not affect these escape sequences. For
498 example, \ep{Lu} always matches only upper case letters.
499 .P
500 The \eX escape matches any number of Unicode characters that form an extended
501 Unicode sequence. \eX is equivalent to
502 .sp
503 (?>\ePM\epM*)
504 .sp
505 That is, it matches a character without the "mark" property, followed by zero
506 or more characters with the "mark" property, and treats the sequence as an
507 atomic group
508 .\" HTML <a href="#atomicgroup">
509 .\" </a>
510 (see below).
511 .\"
512 Characters with the "mark" property are typically accents that affect the
513 preceding character.
514 .P
515 Matching characters by Unicode property is not fast, because PCRE has to search
516 a structure that contains data for over fifteen thousand characters. That is
517 why the traditional escape sequences such as \ed and \ew do not use Unicode
518 properties in PCRE.
519 .
520 .
521 .\" HTML <a name="smallassertions"></a>
522 .SS "Simple assertions"
523 .rs
524 .sp
525 The final use of backslash is for certain simple assertions. An assertion
526 specifies a condition that has to be met at a particular point in a match,
527 without consuming any characters from the subject string. The use of
528 subpatterns for more complicated assertions is described
529 .\" HTML <a href="#bigassertions">
530 .\" </a>
531 below.
532 .\"
533 The backslashed assertions are:
534 .sp
535 \eb matches at a word boundary
536 \eB matches when not at a word boundary
537 \eA matches at the start of the subject
538 \eZ matches at the end of the subject
539 also matches before a newline at the end of the subject
540 \ez matches only at the end of the subject
541 \eG matches at the first matching position in the subject
542 .sp
543 These assertions may not appear in character classes (but note that \eb has a
544 different meaning, namely the backspace character, inside a character class).
545 .P
546 A word boundary is a position in the subject string where the current character
547 and the previous character do not both match \ew or \eW (i.e. one matches
548 \ew and the other matches \eW), or the start or end of the string if the
549 first or last character matches \ew, respectively.
550 .P
551 The \eA, \eZ, and \ez assertions differ from the traditional circumflex and
552 dollar (described in the next section) in that they only ever match at the very
553 start and end of the subject string, whatever options are set. Thus, they are
554 independent of multiline mode. These three assertions are not affected by the
555 PCRE_NOTBOL or PCRE_NOTEOL options, which affect only the behaviour of the
556 circumflex and dollar metacharacters. However, if the \fIstartoffset\fP
557 argument of \fBpcre_exec()\fP is non-zero, indicating that matching is to start
558 at a point other than the beginning of the subject, \eA can never match. The
559 difference between \eZ and \ez is that \eZ matches before a newline at the end
560 of the string as well as at the very end, whereas \ez matches only at the end.
561 .P
562 The \eG assertion is true only when the current matching position is at the
563 start point of the match, as specified by the \fIstartoffset\fP argument of
564 \fBpcre_exec()\fP. It differs from \eA when the value of \fIstartoffset\fP is
565 non-zero. By calling \fBpcre_exec()\fP multiple times with appropriate
566 arguments, you can mimic Perl's /g option, and it is in this kind of
567 implementation where \eG can be useful.
568 .P
569 Note, however, that PCRE's interpretation of \eG, as the start of the current
570 match, is subtly different from Perl's, which defines it as the end of the
571 previous match. In Perl, these can be different when the previously matched
572 string was empty. Because PCRE does just one match at a time, it cannot
573 reproduce this behaviour.
574 .P
575 If all the alternatives of a pattern begin with \eG, the expression is anchored
576 to the starting match position, and the "anchored" flag is set in the compiled
577 regular expression.
578 .
579 .
580 .SH "CIRCUMFLEX AND DOLLAR"
581 .rs
582 .sp
583 Outside a character class, in the default matching mode, the circumflex
584 character is an assertion that is true only if the current matching point is
585 at the start of the subject string. If the \fIstartoffset\fP argument of
586 \fBpcre_exec()\fP is non-zero, circumflex can never match if the PCRE_MULTILINE
587 option is unset. Inside a character class, circumflex has an entirely different
588 meaning
589 .\" HTML <a href="#characterclass">
590 .\" </a>
591 (see below).
592 .\"
593 .P
594 Circumflex need not be the first character of the pattern if a number of
595 alternatives are involved, but it should be the first thing in each alternative
596 in which it appears if the pattern is ever to match that branch. If all
597 possible alternatives start with a circumflex, that is, if the pattern is
598 constrained to match only at the start of the subject, it is said to be an
599 "anchored" pattern. (There are also other constructs that can cause a pattern
600 to be anchored.)
601 .P
602 A dollar character is an assertion that is true only if the current matching
603 point is at the end of the subject string, or immediately before a newline
604 at the end of the string (by default). Dollar need not be the last character of
605 the pattern if a number of alternatives are involved, but it should be the last
606 item in any branch in which it appears. Dollar has no special meaning in a
607 character class.
608 .P
609 The meaning of dollar can be changed so that it matches only at the very end of
610 the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This
611 does not affect the \eZ assertion.
612 .P
613 The meanings of the circumflex and dollar characters are changed if the
614 PCRE_MULTILINE option is set. When this is the case, a circumflex matches
615 immediately after internal newlines as well as at the start of the subject
616 string. It does not match after a newline that ends the string. A dollar
617 matches before any newlines in the string, as well as at the very end, when
618 PCRE_MULTILINE is set. When newline is specified as the two-character
619 sequence CRLF, isolated CR and LF characters do not indicate newlines.
620 .P
621 For example, the pattern /^abc$/ matches the subject string "def\enabc" (where
622 \en represents a newline) in multiline mode, but not otherwise. Consequently,
623 patterns that are anchored in single line mode because all branches start with
624 ^ are not anchored in multiline mode, and a match for circumflex is possible
625 when the \fIstartoffset\fP argument of \fBpcre_exec()\fP is non-zero. The
626 PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
627 .P
628 Note that the sequences \eA, \eZ, and \ez can be used to match the start and
629 end of the subject in both modes, and if all branches of a pattern start with
630 \eA it is always anchored, whether or not PCRE_MULTILINE is set.
631 .
632 .
633 .SH "FULL STOP (PERIOD, DOT)"
634 .rs
635 .sp
636 Outside a character class, a dot in the pattern matches any one character in
637 the subject string except (by default) a character that signifies the end of a
638 line. In UTF-8 mode, the matched character may be more than one byte long.
639 .P
640 When a line ending is defined as a single character, dot never matches that
641 character; when the two-character sequence CRLF is used, dot does not match CR
642 if it is immediately followed by LF, but otherwise it matches all characters
643 (including isolated CRs and LFs). When any Unicode line endings are being
644 recognized, dot does not match CR or LF or any of the other line ending
645 characters.
646 .P
647 The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL
648 option is set, a dot matches any one character, without exception. If the
649 two-character sequence CRLF is present in the subject string, it takes two dots
650 to match it.
651 .P
652 The handling of dot is entirely independent of the handling of circumflex and
653 dollar, the only relationship being that they both involve newlines. Dot has no
654 special meaning in a character class.
655 .
656 .
657 .SH "MATCHING A SINGLE BYTE"
658 .rs
659 .sp
660 Outside a character class, the escape sequence \eC matches any one byte, both
661 in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending
662 characters. The feature is provided in Perl in order to match individual bytes
663 in UTF-8 mode. Because it breaks up UTF-8 characters into individual bytes,
664 what remains in the string may be a malformed UTF-8 string. For this reason,
665 the \eC escape sequence is best avoided.
666 .P
667 PCRE does not allow \eC to appear in lookbehind assertions
668 .\" HTML <a href="#lookbehind">
669 .\" </a>
670 (described below),
671 .\"
672 because in UTF-8 mode this would make it impossible to calculate the length of
673 the lookbehind.
674 .
675 .
676 .\" HTML <a name="characterclass"></a>
677 .SH "SQUARE BRACKETS AND CHARACTER CLASSES"
678 .rs
679 .sp
680 An opening square bracket introduces a character class, terminated by a closing
681 square bracket. A closing square bracket on its own is not special. If a
682 closing square bracket is required as a member of the class, it should be the
683 first data character in the class (after an initial circumflex, if present) or
684 escaped with a backslash.
685 .P
686 A character class matches a single character in the subject. In UTF-8 mode, the
687 character may occupy more than one byte. A matched character must be in the set
688 of characters defined by the class, unless the first character in the class
689 definition is a circumflex, in which case the subject character must not be in
690 the set defined by the class. If a circumflex is actually required as a member
691 of the class, ensure it is not the first character, or escape it with a
692 backslash.
693 .P
694 For example, the character class [aeiou] matches any lower case vowel, while
695 [^aeiou] matches any character that is not a lower case vowel. Note that a
696 circumflex is just a convenient notation for specifying the characters that
697 are in the class by enumerating those that are not. A class that starts with a
698 circumflex is not an assertion: it still consumes a character from the subject
699 string, and therefore it fails if the current pointer is at the end of the
700 string.
701 .P
702 In UTF-8 mode, characters with values greater than 255 can be included in a
703 class as a literal string of bytes, or by using the \ex{ escaping mechanism.
704 .P
705 When caseless matching is set, any letters in a class represent both their
706 upper case and lower case versions, so for example, a caseless [aeiou] matches
707 "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
708 caseful version would. In UTF-8 mode, PCRE always understands the concept of
709 case for characters whose values are less than 128, so caseless matching is
710 always possible. For characters with higher values, the concept of case is
711 supported if PCRE is compiled with Unicode property support, but not otherwise.
712 If you want to use caseless matching for characters 128 and above, you must
713 ensure that PCRE is compiled with Unicode property support as well as with
714 UTF-8 support.
715 .P
716 Characters that might indicate line breaks are never treated in any special way
717 when matching character classes, whatever line-ending sequence is in use, and
718 whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class
719 such as [^a] always matches one of these characters.
720 .P
721 The minus (hyphen) character can be used to specify a range of characters in a
722 character class. For example, [d-m] matches any letter between d and m,
723 inclusive. If a minus character is required in a class, it must be escaped with
724 a backslash or appear in a position where it cannot be interpreted as
725 indicating a range, typically as the first or last character in the class.
726 .P
727 It is not possible to have the literal character "]" as the end character of a
728 range. A pattern such as [W-]46] is interpreted as a class of two characters
729 ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
730 "-46]". However, if the "]" is escaped with a backslash it is interpreted as
731 the end of range, so [W-\e]46] is interpreted as a class containing a range
732 followed by two other characters. The octal or hexadecimal representation of
733 "]" can also be used to end a range.
734 .P
735 Ranges operate in the collating sequence of character values. They can also be
736 used for characters specified numerically, for example [\e000-\e037]. In UTF-8
737 mode, ranges can include characters whose values are greater than 255, for
738 example [\ex{100}-\ex{2ff}].
739 .P
740 If a range that includes letters is used when caseless matching is set, it
741 matches the letters in either case. For example, [W-c] is equivalent to
742 [][\e\e^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character
743 tables for the "fr_FR" locale are in use, [\exc8-\excb] matches accented E
744 characters in both cases. In UTF-8 mode, PCRE supports the concept of case for
745 characters with values greater than 128 only when it is compiled with Unicode
746 property support.
747 .P
748 The character types \ed, \eD, \ep, \eP, \es, \eS, \ew, and \eW may also appear
749 in a character class, and add the characters that they match to the class. For
750 example, [\edABCDEF] matches any hexadecimal digit. A circumflex can
751 conveniently be used with the upper case character types to specify a more
752 restricted set of characters than the matching lower case type. For example,
753 the class [^\eW_] matches any letter or digit, but not underscore.
754 .P
755 The only metacharacters that are recognized in character classes are backslash,
756 hyphen (only where it can be interpreted as specifying a range), circumflex
757 (only at the start), opening square bracket (only when it can be interpreted as
758 introducing a POSIX class name - see the next section), and the terminating
759 closing square bracket. However, escaping other non-alphanumeric characters
760 does no harm.
761 .
762 .
763 .SH "POSIX CHARACTER CLASSES"
764 .rs
765 .sp
766 Perl supports the POSIX notation for character classes. This uses names
767 enclosed by [: and :] within the enclosing square brackets. PCRE also supports
768 this notation. For example,
769 .sp
770 [01[:alpha:]%]
771 .sp
772 matches "0", "1", any alphabetic character, or "%". The supported class names
773 are
774 .sp
775 alnum letters and digits
776 alpha letters
777 ascii character codes 0 - 127
778 blank space or tab only
779 cntrl control characters
780 digit decimal digits (same as \ed)
781 graph printing characters, excluding space
782 lower lower case letters
783 print printing characters, including space
784 punct printing characters, excluding letters and digits
785 space white space (not quite the same as \es)
786 upper upper case letters
787 word "word" characters (same as \ew)
788 xdigit hexadecimal digits
789 .sp
790 The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
791 space (32). Notice that this list includes the VT character (code 11). This
792 makes "space" different to \es, which does not include VT (for Perl
793 compatibility).
794 .P
795 The name "word" is a Perl extension, and "blank" is a GNU extension from Perl
796 5.8. Another Perl extension is negation, which is indicated by a ^ character
797 after the colon. For example,
798 .sp
799 [12[:^digit:]]
800 .sp
801 matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX
802 syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
803 supported, and an error is given if they are encountered.
804 .P
805 In UTF-8 mode, characters with values greater than 128 do not match any of
806 the POSIX character classes.
807 .
808 .
809 .SH "VERTICAL BAR"
810 .rs
811 .sp
812 Vertical bar characters are used to separate alternative patterns. For example,
813 the pattern
814 .sp
815 gilbert|sullivan
816 .sp
817 matches either "gilbert" or "sullivan". Any number of alternatives may appear,
818 and an empty alternative is permitted (matching the empty string). The matching
819 process tries each alternative in turn, from left to right, and the first one
820 that succeeds is used. If the alternatives are within a subpattern
821 .\" HTML <a href="#subpattern">
822 .\" </a>
823 (defined below),
824 .\"
825 "succeeds" means matching the rest of the main pattern as well as the
826 alternative in the subpattern.
827 .
828 .
829 .SH "INTERNAL OPTION SETTING"
830 .rs
831 .sp
832 The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
833 PCRE_EXTENDED options can be changed from within the pattern by a sequence of
834 Perl option letters enclosed between "(?" and ")". The option letters are
835 .sp
836 i for PCRE_CASELESS
837 m for PCRE_MULTILINE
838 s for PCRE_DOTALL
839 x for PCRE_EXTENDED
840 .sp
841 For example, (?im) sets caseless, multiline matching. It is also possible to
842 unset these options by preceding the letter with a hyphen, and a combined
843 setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
844 PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
845 permitted. If a letter appears both before and after the hyphen, the option is
846 unset.
847 .P
848 When an option change occurs at top level (that is, not inside subpattern
849 parentheses), the change applies to the remainder of the pattern that follows.
850 If the change is placed right at the start of a pattern, PCRE extracts it into
851 the global options (and it will therefore show up in data extracted by the
852 \fBpcre_fullinfo()\fP function).
853 .P
854 An option change within a subpattern (see below for a description of
855 subpatterns) affects only that part of the current pattern that follows it, so
856 .sp
857 (a(?i)b)c
858 .sp
859 matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
860 By this means, options can be made to have different settings in different
861 parts of the pattern. Any changes made in one alternative do carry on
862 into subsequent branches within the same subpattern. For example,
863 .sp
864 (a(?i)b|c)
865 .sp
866 matches "ab", "aB", "c", and "C", even though when matching "C" the first
867 branch is abandoned before the option setting. This is because the effects of
868 option settings happen at compile time. There would be some very weird
869 behaviour otherwise.
870 .P
871 The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
872 changed in the same way as the Perl-compatible options by using the characters
873 J, U and X respectively.
874 .
875 .
876 .\" HTML <a name="subpattern"></a>
877 .SH SUBPATTERNS
878 .rs
879 .sp
880 Subpatterns are delimited by parentheses (round brackets), which can be nested.
881 Turning part of a pattern into a subpattern does two things:
882 .sp
883 1. It localizes a set of alternatives. For example, the pattern
884 .sp
885 cat(aract|erpillar|)
886 .sp
887 matches one of the words "cat", "cataract", or "caterpillar". Without the
888 parentheses, it would match "cataract", "erpillar" or an empty string.
889 .sp
890 2. It sets up the subpattern as a capturing subpattern. This means that, when
891 the whole pattern matches, that portion of the subject string that matched the
892 subpattern is passed back to the caller via the \fIovector\fP argument of
893 \fBpcre_exec()\fP. Opening parentheses are counted from left to right (starting
894 from 1) to obtain numbers for the capturing subpatterns.
895 .P
896 For example, if the string "the red king" is matched against the pattern
897 .sp
898 the ((red|white) (king|queen))
899 .sp
900 the captured substrings are "red king", "red", and "king", and are numbered 1,
901 2, and 3, respectively.
902 .P
903 The fact that plain parentheses fulfil two functions is not always helpful.
904 There are often times when a grouping subpattern is required without a
905 capturing requirement. If an opening parenthesis is followed by a question mark
906 and a colon, the subpattern does not do any capturing, and is not counted when
907 computing the number of any subsequent capturing subpatterns. For example, if
908 the string "the white queen" is matched against the pattern
909 .sp
910 the ((?:red|white) (king|queen))
911 .sp
912 the captured substrings are "white queen" and "queen", and are numbered 1 and
913 2. The maximum number of capturing subpatterns is 65535.
914 .P
915 As a convenient shorthand, if any option settings are required at the start of
916 a non-capturing subpattern, the option letters may appear between the "?" and
917 the ":". Thus the two patterns
918 .sp
919 (?i:saturday|sunday)
920 (?:(?i)saturday|sunday)
921 .sp
922 match exactly the same set of strings. Because alternative branches are tried
923 from left to right, and options are not reset until the end of the subpattern
924 is reached, an option setting in one branch does affect subsequent branches, so
925 the above patterns match "SUNDAY" as well as "Saturday".
926 .
927 .
928 .SH "NAMED SUBPATTERNS"
929 .rs
930 .sp
931 Identifying capturing parentheses by number is simple, but it can be very hard
932 to keep track of the numbers in complicated regular expressions. Furthermore,
933 if an expression is modified, the numbers may change. To help with this
934 difficulty, PCRE supports the naming of subpatterns. This feature was not
935 added to Perl until release 5.10. Python had the feature earlier, and PCRE
936 introduced it at release 4.0, using the Python syntax. PCRE now supports both
937 the Perl and the Python syntax.
938 .P
939 In PCRE, a subpattern can be named in one of three ways: (?<name>...) or
940 (?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing
941 parentheses from other parts of the pattern, such as
942 .\" HTML <a href="#backreferences">
943 .\" </a>
944 backreferences,
945 .\"
946 .\" HTML <a href="#recursion">
947 .\" </a>
948 recursion,
949 .\"
950 and
951 .\" HTML <a href="#conditions">
952 .\" </a>
953 conditions,
954 .\"
955 can be made by name as well as by number.
956 .P
957 Names consist of up to 32 alphanumeric characters and underscores. Named
958 capturing parentheses are still allocated numbers as well as names, exactly as
959 if the names were not present. The PCRE API provides function calls for
960 extracting the name-to-number translation table from a compiled pattern. There
961 is also a convenience function for extracting a captured substring by name.
962 .P
963 By default, a name must be unique within a pattern, but it is possible to relax
964 this constraint by setting the PCRE_DUPNAMES option at compile time. This can
965 be useful for patterns where only one instance of the named parentheses can
966 match. Suppose you want to match the name of a weekday, either as a 3-letter
967 abbreviation or as the full name, and in both cases you want to extract the
968 abbreviation. This pattern (ignoring the line breaks) does the job:
969 .sp
970 (?<DN>Mon|Fri|Sun)(?:day)?|
971 (?<DN>Tue)(?:sday)?|
972 (?<DN>Wed)(?:nesday)?|
973 (?<DN>Thu)(?:rsday)?|
974 (?<DN>Sat)(?:urday)?
975 .sp
976 There are five capturing substrings, but only one is ever set after a match.
977 The convenience function for extracting the data by name returns the substring
978 for the first (and in this example, the only) subpattern of that name that
979 matched. This saves searching to find which numbered subpattern it was. If you
980 make a reference to a non-unique named subpattern from elsewhere in the
981 pattern, the one that corresponds to the lowest number is used. For further
982 details of the interfaces for handling named subpatterns, see the
983 .\" HREF
984 \fBpcreapi\fP
985 .\"
986 documentation.
987 .
988 .
989 .SH REPETITION
990 .rs
991 .sp
992 Repetition is specified by quantifiers, which can follow any of the following
993 items:
994 .sp
995 a literal data character
996 the dot metacharacter
997 the \eC escape sequence
998 the \eX escape sequence (in UTF-8 mode with Unicode properties)
999 the \eR escape sequence
1000 an escape such as \ed that matches a single character
1001 a character class
1002 a back reference (see next section)
1003 a parenthesized subpattern (unless it is an assertion)
1004 .sp
1005 The general repetition quantifier specifies a minimum and maximum number of
1006 permitted matches, by giving the two numbers in curly brackets (braces),
1007 separated by a comma. The numbers must be less than 65536, and the first must
1008 be less than or equal to the second. For example:
1009 .sp
1010 z{2,4}
1011 .sp
1012 matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
1013 character. If the second number is omitted, but the comma is present, there is
1014 no upper limit; if the second number and the comma are both omitted, the
1015 quantifier specifies an exact number of required matches. Thus
1016 .sp
1017 [aeiou]{3,}
1018 .sp
1019 matches at least 3 successive vowels, but may match many more, while
1020 .sp
1021 \ed{8}
1022 .sp
1023 matches exactly 8 digits. An opening curly bracket that appears in a position
1024 where a quantifier is not allowed, or one that does not match the syntax of a
1025 quantifier, is taken as a literal character. For example, {,6} is not a
1026 quantifier, but a literal string of four characters.
1027 .P
1028 In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to individual
1029 bytes. Thus, for example, \ex{100}{2} matches two UTF-8 characters, each of
1030 which is represented by a two-byte sequence. Similarly, when Unicode property
1031 support is available, \eX{3} matches three Unicode extended sequences, each of
1032 which may be several bytes long (and they may be of different lengths).
1033 .P
1034 The quantifier {0} is permitted, causing the expression to behave as if the
1035 previous item and the quantifier were not present.
1036 .P
1037 For convenience, the three most common quantifiers have single-character
1038 abbreviations:
1039 .sp
1040 * is equivalent to {0,}
1041 + is equivalent to {1,}
1042 ? is equivalent to {0,1}
1043 .sp
1044 It is possible to construct infinite loops by following a subpattern that can
1045 match no characters with a quantifier that has no upper limit, for example:
1046 .sp
1047 (a?)*
1048 .sp
1049 Earlier versions of Perl and PCRE used to give an error at compile time for
1050 such patterns. However, because there are cases where this can be useful, such
1051 patterns are now accepted, but if any repetition of the subpattern does in fact
1052 match no characters, the loop is forcibly broken.
1053 .P
1054 By default, the quantifiers are "greedy", that is, they match as much as
1055 possible (up to the maximum number of permitted times), without causing the
1056 rest of the pattern to fail. The classic example of where this gives problems
1057 is in trying to match comments in C programs. These appear between /* and */
1058 and within the comment, individual * and / characters may appear. An attempt to
1059 match C comments by applying the pattern
1060 .sp
1061 /\e*.*\e*/
1062 .sp
1063 to the string
1064 .sp
1065 /* first comment */ not comment /* second comment */
1066 .sp
1067 fails, because it matches the entire string owing to the greediness of the .*
1068 item.
1069 .P
1070 However, if a quantifier is followed by a question mark, it ceases to be
1071 greedy, and instead matches the minimum number of times possible, so the
1072 pattern
1073 .sp
1074 /\e*.*?\e*/
1075 .sp
1076 does the right thing with the C comments. The meaning of the various
1077 quantifiers is not otherwise changed, just the preferred number of matches.
1078 Do not confuse this use of question mark with its use as a quantifier in its
1079 own right. Because it has two uses, it can sometimes appear doubled, as in
1080 .sp
1081 \ed??\ed
1082 .sp
1083 which matches one digit by preference, but can match two if that is the only
1084 way the rest of the pattern matches.
1085 .P
1086 If the PCRE_UNGREEDY option is set (an option that is not available in Perl),
1087 the quantifiers are not greedy by default, but individual ones can be made
1088 greedy by following them with a question mark. In other words, it inverts the
1089 default behaviour.
1090 .P
1091 When a parenthesized subpattern is quantified with a minimum repeat count that
1092 is greater than 1 or with a limited maximum, more memory is required for the
1093 compiled pattern, in proportion to the size of the minimum or maximum.
1094 .P
1095 If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
1096 to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is
1097 implicitly anchored, because whatever follows will be tried against every
1098 character position in the subject string, so there is no point in retrying the
1099 overall match at any position after the first. PCRE normally treats such a
1100 pattern as though it were preceded by \eA.
1101 .P
1102 In cases where it is known that the subject string contains no newlines, it is
1103 worth setting PCRE_DOTALL in order to obtain this optimization, or
1104 alternatively using ^ to indicate anchoring explicitly.
1105 .P
1106 However, there is one situation where the optimization cannot be used. When .*
1107 is inside capturing parentheses that are the subject of a backreference
1108 elsewhere in the pattern, a match at the start may fail where a later one
1109 succeeds. Consider, for example:
1110 .sp
1111 (.*)abc\e1
1112 .sp
1113 If the subject is "xyz123abc123" the match point is the fourth character. For
1114 this reason, such a pattern is not implicitly anchored.
1115 .P
1116 When a capturing subpattern is repeated, the value captured is the substring
1117 that matched the final iteration. For example, after
1118 .sp
1119 (tweedle[dume]{3}\es*)+
1120 .sp
1121 has matched "tweedledum tweedledee" the value of the captured substring is
1122 "tweedledee". However, if there are nested capturing subpatterns, the
1123 corresponding captured values may have been set in previous iterations. For
1124 example, after
1125 .sp
1126 /(a|(b))+/
1127 .sp
1128 matches "aba" the value of the second captured substring is "b".
1129 .
1130 .
1131 .\" HTML <a name="atomicgroup"></a>
1132 .SH "ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS"
1133 .rs
1134 .sp
1135 With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1136 repetition, failure of what follows normally causes the repeated item to be
1137 re-evaluated to see if a different number of repeats allows the rest of the
1138 pattern to match. Sometimes it is useful to prevent this, either to change the
1139 nature of the match, or to cause it fail earlier than it otherwise might, when
1140 the author of the pattern knows there is no point in carrying on.
1141 .P
1142 Consider, for example, the pattern \ed+foo when applied to the subject line
1143 .sp
1144 123456bar
1145 .sp
1146 After matching all 6 digits and then failing to match "foo", the normal
1147 action of the matcher is to try again with only 5 digits matching the \ed+
1148 item, and then with 4, and so on, before ultimately failing. "Atomic grouping"
1149 (a term taken from Jeffrey Friedl's book) provides the means for specifying
1150 that once a subpattern has matched, it is not to be re-evaluated in this way.
1151 .P
1152 If we use atomic grouping for the previous example, the matcher gives up
1153 immediately on failing to match "foo" the first time. The notation is a kind of
1154 special parenthesis, starting with (?> as in this example:
1155 .sp
1156 (?>\ed+)foo
1157 .sp
1158 This kind of parenthesis "locks up" the part of the pattern it contains once
1159 it has matched, and a failure further into the pattern is prevented from
1160 backtracking into it. Backtracking past it to previous items, however, works as
1161 normal.
1162 .P
1163 An alternative description is that a subpattern of this type matches the string
1164 of characters that an identical standalone pattern would match, if anchored at
1165 the current point in the subject string.
1166 .P
1167 Atomic grouping subpatterns are not capturing subpatterns. Simple cases such as
1168 the above example can be thought of as a maximizing repeat that must swallow
1169 everything it can. So, while both \ed+ and \ed+? are prepared to adjust the
1170 number of digits they match in order to make the rest of the pattern match,
1171 (?>\ed+) can only match an entire sequence of digits.
1172 .P
1173 Atomic groups in general can of course contain arbitrarily complicated
1174 subpatterns, and can be nested. However, when the subpattern for an atomic
1175 group is just a single repeated item, as in the example above, a simpler
1176 notation, called a "possessive quantifier" can be used. This consists of an
1177 additional + character following a quantifier. Using this notation, the
1178 previous example can be rewritten as
1179 .sp
1180 \ed++foo
1181 .sp
1182 Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1183 option is ignored. They are a convenient notation for the simpler forms of
1184 atomic group. However, there is no difference in the meaning of a possessive
1185 quantifier and the equivalent atomic group, though there may be a performance
1186 difference; possessive quantifiers should be slightly faster.
1187 .P
1188 The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
1189 Jeffrey Friedl originated the idea (and the name) in the first edition of his
1190 book. Mike McCloskey liked it, so implemented it when he built Sun's Java
1191 package, and PCRE copied it from there. It ultimately found its way into Perl
1192 at release 5.10.
1193 .P
1194 PCRE has an optimization that automatically "possessifies" certain simple
1195 pattern constructs. For example, the sequence A+B is treated as A++B because
1196 there is no point in backtracking into a sequence of A's when B must follow.
1197 .P
1198 When a pattern contains an unlimited repeat inside a subpattern that can itself
1199 be repeated an unlimited number of times, the use of an atomic group is the
1200 only way to avoid some failing matches taking a very long time indeed. The
1201 pattern
1202 .sp
1203 (\eD+|<\ed+>)*[!?]
1204 .sp
1205 matches an unlimited number of substrings that either consist of non-digits, or
1206 digits enclosed in <>, followed by either ! or ?. When it matches, it runs
1207 quickly. However, if it is applied to
1208 .sp
1209 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1210 .sp
1211 it takes a long time before reporting failure. This is because the string can
1212 be divided between the internal \eD+ repeat and the external * repeat in a
1213 large number of ways, and all have to be tried. (The example uses [!?] rather
1214 than a single character at the end, because both PCRE and Perl have an
1215 optimization that allows for fast failure when a single character is used. They
1216 remember the last single character that is required for a match, and fail early
1217 if it is not present in the string.) If the pattern is changed so that it uses
1218 an atomic group, like this:
1219 .sp
1220 ((?>\eD+)|<\ed+>)*[!?]
1221 .sp
1222 sequences of non-digits cannot be broken, and failure happens quickly.
1223 .
1224 .
1225 .\" HTML <a name="backreferences"></a>
1226 .SH "BACK REFERENCES"
1227 .rs
1228 .sp
1229 Outside a character class, a backslash followed by a digit greater than 0 (and
1230 possibly further digits) is a back reference to a capturing subpattern earlier
1231 (that is, to its left) in the pattern, provided there have been that many
1232 previous capturing left parentheses.
1233 .P
1234 However, if the decimal number following the backslash is less than 10, it is
1235 always taken as a back reference, and causes an error only if there are not
1236 that many capturing left parentheses in the entire pattern. In other words, the
1237 parentheses that are referenced need not be to the left of the reference for
1238 numbers less than 10. A "forward back reference" of this type can make sense
1239 when a repetition is involved and the subpattern to the right has participated
1240 in an earlier iteration.
1241 .P
1242 It is not possible to have a numerical "forward back reference" to a subpattern
1243 whose number is 10 or more using this syntax because a sequence such as \e50 is
1244 interpreted as a character defined in octal. See the subsection entitled
1245 "Non-printing characters"
1246 .\" HTML <a href="#digitsafterbackslash">
1247 .\" </a>
1248 above
1249 .\"
1250 for further details of the handling of digits following a backslash. There is
1251 no such problem when named parentheses are used. A back reference to any
1252 subpattern is possible using named parentheses (see below).
1253 .P
1254 Another way of avoiding the ambiguity inherent in the use of digits following a
1255 backslash is to use the \eg escape sequence, which is a feature introduced in
1256 Perl 5.10. This escape must be followed by a positive or a negative number,
1257 optionally enclosed in braces. These examples are all identical:
1258 .sp
1259 (ring), \e1
1260 (ring), \eg1
1261 (ring), \eg{1}
1262 .sp
1263 A positive number specifies an absolute reference without the ambiguity that is
1264 present in the older syntax. It is also useful when literal digits follow the
1265 reference. A negative number is a relative reference. Consider this example:
1266 .sp
1267 (abc(def)ghi)\eg{-1}
1268 .sp
1269 The sequence \eg{-1} is a reference to the most recently started capturing
1270 subpattern before \eg, that is, is it equivalent to \e2. Similarly, \eg{-2}
1271 would be equivalent to \e1. The use of relative references can be helpful in
1272 long patterns, and also in patterns that are created by joining together
1273 fragments that contain references within themselves.
1274 .P
1275 A back reference matches whatever actually matched the capturing subpattern in
1276 the current subject string, rather than anything matching the subpattern
1277 itself (see
1278 .\" HTML <a href="#subpatternsassubroutines">
1279 .\" </a>
1280 "Subpatterns as subroutines"
1281 .\"
1282 below for a way of doing that). So the pattern
1283 .sp
1284 (sens|respons)e and \e1ibility
1285 .sp
1286 matches "sense and sensibility" and "response and responsibility", but not
1287 "sense and responsibility". If caseful matching is in force at the time of the
1288 back reference, the case of letters is relevant. For example,
1289 .sp
1290 ((?i)rah)\es+\e1
1291 .sp
1292 matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
1293 capturing subpattern is matched caselessly.
1294 .P
1295 Back references to named subpatterns use the Perl syntax \ek<name> or \ek'name'
1296 or the Python syntax (?P=name). We could rewrite the above example in either of
1297 the following ways:
1298 .sp
1299 (?<p1>(?i)rah)\es+\ek<p1>
1300 (?P<p1>(?i)rah)\es+(?P=p1)
1301 .sp
1302 A subpattern that is referenced by name may appear in the pattern before or
1303 after the reference.
1304 .P
1305 There may be more than one back reference to the same subpattern. If a
1306 subpattern has not actually been used in a particular match, any back
1307 references to it always fail. For example, the pattern
1308 .sp
1309 (a|(bc))\e2
1310 .sp
1311 always fails if it starts to match "a" rather than "bc". Because there may be
1312 many capturing parentheses in a pattern, all digits following the backslash are
1313 taken as part of a potential back reference number. If the pattern continues
1314 with a digit character, some delimiter must be used to terminate the back
1315 reference. If the PCRE_EXTENDED option is set, this can be whitespace.
1316 Otherwise an empty comment (see
1317 .\" HTML <a href="#comments">
1318 .\" </a>
1319 "Comments"
1320 .\"
1321 below) can be used.
1322 .P
1323 A back reference that occurs inside the parentheses to which it refers fails
1324 when the subpattern is first used, so, for example, (a\e1) never matches.
1325 However, such references can be useful inside repeated subpatterns. For
1326 example, the pattern
1327 .sp
1328 (a|b\e1)+
1329 .sp
1330 matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of
1331 the subpattern, the back reference matches the character string corresponding
1332 to the previous iteration. In order for this to work, the pattern must be such
1333 that the first iteration does not need to match the back reference. This can be
1334 done using alternation, as in the example above, or by a quantifier with a
1335 minimum of zero.
1336 .
1337 .
1338 .\" HTML <a name="bigassertions"></a>
1339 .SH ASSERTIONS
1340 .rs
1341 .sp
1342 An assertion is a test on the characters following or preceding the current
1343 matching point that does not actually consume any characters. The simple
1344 assertions coded as \eb, \eB, \eA, \eG, \eZ, \ez, ^ and $ are described
1345 .\" HTML <a href="#smallassertions">
1346 .\" </a>
1347 above.
1348 .\"
1349 .P
1350 More complicated assertions are coded as subpatterns. There are two kinds:
1351 those that look ahead of the current position in the subject string, and those
1352 that look behind it. An assertion subpattern is matched in the normal way,
1353 except that it does not cause the current matching position to be changed.
1354 .P
1355 Assertion subpatterns are not capturing subpatterns, and may not be repeated,
1356 because it makes no sense to assert the same thing several times. If any kind
1357 of assertion contains capturing subpatterns within it, these are counted for
1358 the purposes of numbering the capturing subpatterns in the whole pattern.
1359 However, substring capturing is carried out only for positive assertions,
1360 because it does not make sense for negative assertions.
1361 .
1362 .
1363 .SS "Lookahead assertions"
1364 .rs
1365 .sp
1366 Lookahead assertions start with (?= for positive assertions and (?! for
1367 negative assertions. For example,
1368 .sp
1369 \ew+(?=;)
1370 .sp
1371 matches a word followed by a semicolon, but does not include the semicolon in
1372 the match, and
1373 .sp
1374 foo(?!bar)
1375 .sp
1376 matches any occurrence of "foo" that is not followed by "bar". Note that the
1377 apparently similar pattern
1378 .sp
1379 (?!foo)bar
1380 .sp
1381 does not find an occurrence of "bar" that is preceded by something other than
1382 "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
1383 (?!foo) is always true when the next three characters are "bar". A
1384 lookbehind assertion is needed to achieve the other effect.
1385 .P
1386 If you want to force a matching failure at some point in a pattern, the most
1387 convenient way to do it is with (?!) because an empty string always matches, so
1388 an assertion that requires there not to be an empty string must always fail.
1389 .
1390 .
1391 .\" HTML <a name="lookbehind"></a>
1392 .SS "Lookbehind assertions"
1393 .rs
1394 .sp
1395 Lookbehind assertions start with (?<= for positive assertions and (?<! for
1396 negative assertions. For example,
1397 .sp
1398 (?<!foo)bar
1399 .sp
1400 does find an occurrence of "bar" that is not preceded by "foo". The contents of
1401 a lookbehind assertion are restricted such that all the strings it matches must
1402 have a fixed length. However, if there are several top-level alternatives, they
1403 do not all have to have the same fixed length. Thus
1404 .sp
1405 (?<=bullock|donkey)
1406 .sp
1407 is permitted, but
1408 .sp
1409 (?<!dogs?|cats?)
1410 .sp
1411 causes an error at compile time. Branches that match different length strings
1412 are permitted only at the top level of a lookbehind assertion. This is an
1413 extension compared with Perl (at least for 5.8), which requires all branches to
1414 match the same length of string. An assertion such as
1415 .sp
1416 (?<=ab(c|de))
1417 .sp
1418 is not permitted, because its single top-level branch can match two different
1419 lengths, but it is acceptable if rewritten to use two top-level branches:
1420 .sp
1421 (?<=abc|abde)
1422 .sp
1423 The implementation of lookbehind assertions is, for each alternative, to
1424 temporarily move the current position back by the fixed length and then try to
1425 match. If there are insufficient characters before the current position, the
1426 assertion fails.
1427 .P
1428 PCRE does not allow the \eC escape (which matches a single byte in UTF-8 mode)
1429 to appear in lookbehind assertions, because it makes it impossible to calculate
1430 the length of the lookbehind. The \eX and \eR escapes, which can match
1431 different numbers of bytes, are also not permitted.
1432 .P
1433 Possessive quantifiers can be used in conjunction with lookbehind assertions to
1434 specify efficient matching at the end of the subject string. Consider a simple
1435 pattern such as
1436 .sp
1437 abcd$
1438 .sp
1439 when applied to a long string that does not match. Because matching proceeds
1440 from left to right, PCRE will look for each "a" in the subject and then see if
1441 what follows matches the rest of the pattern. If the pattern is specified as
1442 .sp
1443 ^.*abcd$
1444 .sp
1445 the initial .* matches the entire string at first, but when this fails (because
1446 there is no following "a"), it backtracks to match all but the last character,
1447 then all but the last two characters, and so on. Once again the search for "a"
1448 covers the entire string, from right to left, so we are no better off. However,
1449 if the pattern is written as
1450 .sp
1451 ^.*+(?<=abcd)
1452 .sp
1453 there can be no backtracking for the .*+ item; it can match only the entire
1454 string. The subsequent lookbehind assertion does a single test on the last four
1455 characters. If it fails, the match fails immediately. For long strings, this
1456 approach makes a significant difference to the processing time.
1457 .
1458 .
1459 .SS "Using multiple assertions"
1460 .rs
1461 .sp
1462 Several assertions (of any sort) may occur in succession. For example,
1463 .sp
1464 (?<=\ed{3})(?<!999)foo
1465 .sp
1466 matches "foo" preceded by three digits that are not "999". Notice that each of
1467 the assertions is applied independently at the same point in the subject
1468 string. First there is a check that the previous three characters are all
1469 digits, and then there is a check that the same three characters are not "999".
1470 This pattern does \fInot\fP match "foo" preceded by six characters, the first
1471 of which are digits and the last three of which are not "999". For example, it
1472 doesn't match "123abcfoo". A pattern to do that is
1473 .sp
1474 (?<=\ed{3}...)(?<!999)foo
1475 .sp
1476 This time the first assertion looks at the preceding six characters, checking
1477 that the first three are digits, and then the second assertion checks that the
1478 preceding three characters are not "999".
1479 .P
1480 Assertions can be nested in any combination. For example,
1481 .sp
1482 (?<=(?<!foo)bar)baz
1483 .sp
1484 matches an occurrence of "baz" that is preceded by "bar" which in turn is not
1485 preceded by "foo", while
1486 .sp
1487 (?<=\ed{3}(?!999)...)foo
1488 .sp
1489 is another pattern that matches "foo" preceded by three digits and any three
1490 characters that are not "999".
1491 .
1492 .
1493 .\" HTML <a name="conditions"></a>
1494 .SH "CONDITIONAL SUBPATTERNS"
1495 .rs
1496 .sp
1497 It is possible to cause the matching process to obey a subpattern
1498 conditionally or to choose between two alternative subpatterns, depending on
1499 the result of an assertion, or whether a previous capturing subpattern matched
1500 or not. The two possible forms of conditional subpattern are
1501 .sp
1502 (?(condition)yes-pattern)
1503 (?(condition)yes-pattern|no-pattern)
1504 .sp
1505 If the condition is satisfied, the yes-pattern is used; otherwise the
1506 no-pattern (if present) is used. If there are more than two alternatives in the
1507 subpattern, a compile-time error occurs.
1508 .P
1509 There are four kinds of condition: references to subpatterns, references to
1510 recursion, a pseudo-condition called DEFINE, and assertions.
1511 .
1512 .SS "Checking for a used subpattern by number"
1513 .rs
1514 .sp
1515 If the text between the parentheses consists of a sequence of digits, the
1516 condition is true if the capturing subpattern of that number has previously
1517 matched.
1518 .P
1519 Consider the following pattern, which contains non-significant white space to
1520 make it more readable (assume the PCRE_EXTENDED option) and to divide it into
1521 three parts for ease of discussion:
1522 .sp
1523 ( \e( )? [^()]+ (?(1) \e) )
1524 .sp
1525 The first part matches an optional opening parenthesis, and if that
1526 character is present, sets it as the first captured substring. The second part
1527 matches one or more characters that are not parentheses. The third part is a
1528 conditional subpattern that tests whether the first set of parentheses matched
1529 or not. If they did, that is, if subject started with an opening parenthesis,
1530 the condition is true, and so the yes-pattern is executed and a closing
1531 parenthesis is required. Otherwise, since no-pattern is not present, the
1532 subpattern matches nothing. In other words, this pattern matches a sequence of
1533 non-parentheses, optionally enclosed in parentheses.
1534 .
1535 .SS "Checking for a used subpattern by name"
1536 .rs
1537 .sp
1538 Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a used
1539 subpattern by name. For compatibility with earlier versions of PCRE, which had
1540 this facility before Perl, the syntax (?(name)...) is also recognized. However,
1541 there is a possible ambiguity with this syntax, because subpattern names may
1542 consist entirely of digits. PCRE looks first for a named subpattern; if it
1543 cannot find one and the name consists entirely of digits, PCRE looks for a
1544 subpattern of that number, which must be greater than zero. Using subpattern
1545 names that consist entirely of digits is not recommended.
1546 .P
1547 Rewriting the above example to use a named subpattern gives this:
1548 .sp
1549 (?<OPEN> \e( )? [^()]+ (?(<OPEN>) \e) )
1550 .sp
1551 .
1552 .SS "Checking for pattern recursion"
1553 .rs
1554 .sp
1555 If the condition is the string (R), and there is no subpattern with the name R,
1556 the condition is true if a recursive call to the whole pattern or any
1557 subpattern has been made. If digits or a name preceded by ampersand follow the
1558 letter R, for example:
1559 .sp
1560 (?(R3)...) or (?(R&name)...)
1561 .sp
1562 the condition is true if the most recent recursion is into the subpattern whose
1563 number or name is given. This condition does not check the entire recursion
1564 stack.
1565 .P
1566 At "top level", all these recursion test conditions are false. Recursive
1567 patterns are described below.
1568 .
1569 .SS "Defining subpatterns for use by reference only"
1570 .rs
1571 .sp
1572 If the condition is the string (DEFINE), and there is no subpattern with the
1573 name DEFINE, the condition is always false. In this case, there may be only one
1574 alternative in the subpattern. It is always skipped if control reaches this
1575 point in the pattern; the idea of DEFINE is that it can be used to define
1576 "subroutines" that can be referenced from elsewhere. (The use of "subroutines"
1577 is described below.) For example, a pattern to match an IPv4 address could be
1578 written like this (ignore whitespace and line breaks):
1579 .sp
1580 (?(DEFINE) (?<byte> 2[0-4]\ed | 25[0-5] | 1\ed\ed | [1-9]?\ed) )
1581 \eb (?&byte) (\e.(?&byte)){3} \eb
1582 .sp
1583 The first part of the pattern is a DEFINE group inside which a another group
1584 named "byte" is defined. This matches an individual component of an IPv4
1585 address (a number less than 256). When matching takes place, this part of the
1586 pattern is skipped because DEFINE acts like a false condition.
1587 .P
1588 The rest of the pattern uses references to the named group to match the four
1589 dot-separated components of an IPv4 address, insisting on a word boundary at
1590 each end.
1591 .
1592 .SS "Assertion conditions"
1593 .rs
1594 .sp
1595 If the condition is not in any of the above formats, it must be an assertion.
1596 This may be a positive or negative lookahead or lookbehind assertion. Consider
1597 this pattern, again containing non-significant white space, and with the two
1598 alternatives on the second line:
1599 .sp
1600 (?(?=[^a-z]*[a-z])
1601 \ed{2}-[a-z]{3}-\ed{2} | \ed{2}-\ed{2}-\ed{2} )
1602 .sp
1603 The condition is a positive lookahead assertion that matches an optional
1604 sequence of non-letters followed by a letter. In other words, it tests for the
1605 presence of at least one letter in the subject. If a letter is found, the
1606 subject is matched against the first alternative; otherwise it is matched
1607 against the second. This pattern matches strings in one of the two forms
1608 dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
1609 .
1610 .
1611 .\" HTML <a name="comments"></a>
1612 .SH COMMENTS
1613 .rs
1614 .sp
1615 The sequence (?# marks the start of a comment that continues up to the next
1616 closing parenthesis. Nested parentheses are not permitted. The characters
1617 that make up a comment play no part in the pattern matching at all.
1618 .P
1619 If the PCRE_EXTENDED option is set, an unescaped # character outside a
1620 character class introduces a comment that continues to immediately after the
1621 next newline in the pattern.
1622 .
1623 .
1624 .\" HTML <a name="recursion"></a>
1625 .SH "RECURSIVE PATTERNS"
1626 .rs
1627 .sp
1628 Consider the problem of matching a string in parentheses, allowing for
1629 unlimited nested parentheses. Without the use of recursion, the best that can
1630 be done is to use a pattern that matches up to some fixed depth of nesting. It
1631 is not possible to handle an arbitrary nesting depth.
1632 .P
1633 For some time, Perl has provided a facility that allows regular expressions to
1634 recurse (amongst other things). It does this by interpolating Perl code in the
1635 expression at run time, and the code can refer to the expression itself. A Perl
1636 pattern using code interpolation to solve the parentheses problem can be
1637 created like this:
1638 .sp
1639 $re = qr{\e( (?: (?>[^()]+) | (?p{$re}) )* \e)}x;
1640 .sp
1641 The (?p{...}) item interpolates Perl code at run time, and in this case refers
1642 recursively to the pattern in which it appears.
1643 .P
1644 Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
1645 supports special syntax for recursion of the entire pattern, and also for
1646 individual subpattern recursion. After its introduction in PCRE and Python,
1647 this kind of recursion was introduced into Perl at release 5.10.
1648 .P
1649 A special item that consists of (? followed by a number greater than zero and a
1650 closing parenthesis is a recursive call of the subpattern of the given number,
1651 provided that it occurs inside that subpattern. (If not, it is a "subroutine"
1652 call, which is described in the next section.) The special item (?R) or (?0) is
1653 a recursive call of the entire regular expression.
1654 .P
1655 In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
1656 treated as an atomic group. That is, once it has matched some of the subject
1657 string, it is never re-entered, even if it contains untried alternatives and
1658 there is a subsequent matching failure.
1659 .P
1660 This PCRE pattern solves the nested parentheses problem (assume the
1661 PCRE_EXTENDED option is set so that white space is ignored):
1662 .sp
1663 \e( ( (?>[^()]+) | (?R) )* \e)
1664 .sp
1665 First it matches an opening parenthesis. Then it matches any number of
1666 substrings which can either be a sequence of non-parentheses, or a recursive
1667 match of the pattern itself (that is, a correctly parenthesized substring).
1668 Finally there is a closing parenthesis.
1669 .P
1670 If this were part of a larger pattern, you would not want to recurse the entire
1671 pattern, so instead you could use this:
1672 .sp
1673 ( \e( ( (?>[^()]+) | (?1) )* \e) )
1674 .sp
1675 We have put the pattern into parentheses, and caused the recursion to refer to
1676 them instead of the whole pattern. In a larger pattern, keeping track of
1677 parenthesis numbers can be tricky. It may be more convenient to use named
1678 parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier
1679 syntax (?P>name) is also supported. We could rewrite the above example as
1680 follows:
1681 .sp
1682 (?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) )
1683 .sp
1684 If there is more than one subpattern with the same name, the earliest one is
1685 used. This particular example pattern contains nested unlimited repeats, and so
1686 the use of atomic grouping for matching strings of non-parentheses is important
1687 when applying the pattern to strings that do not match. For example, when this
1688 pattern is applied to
1689 .sp
1690 (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
1691 .sp
1692 it yields "no match" quickly. However, if atomic grouping is not used,
1693 the match runs for a very long time indeed because there are so many different
1694 ways the + and * repeats can carve up the subject, and all have to be tested
1695 before failure can be reported.
1696 .P
1697 At the end of a match, the values set for any capturing subpatterns are those
1698 from the outermost level of the recursion at which the subpattern value is set.
1699 If you want to obtain intermediate values, a callout function can be used (see
1700 below and the
1701 .\" HREF
1702 \fBpcrecallout\fP
1703 .\"
1704 documentation). If the pattern above is matched against
1705 .sp
1706 (ab(cd)ef)
1707 .sp
1708 the value for the capturing parentheses is "ef", which is the last value taken
1709 on at the top level. If additional parentheses are added, giving
1710 .sp
1711 \e( ( ( (?>[^()]+) | (?R) )* ) \e)
1712 ^ ^
1713 ^ ^
1714 .sp
1715 the string they capture is "ab(cd)ef", the contents of the top level
1716 parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE
1717 has to obtain extra memory to store data during a recursion, which it does by
1718 using \fBpcre_malloc\fP, freeing it via \fBpcre_free\fP afterwards. If no
1719 memory can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
1720 .P
1721 Do not confuse the (?R) item with the condition (R), which tests for recursion.
1722 Consider this pattern, which matches text in angle brackets, allowing for
1723 arbitrary nesting. Only digits are allowed in nested brackets (that is, when
1724 recursing), whereas any characters are permitted at the outer level.
1725 .sp
1726 < (?: (?(R) \ed++ | [^<>]*+) | (?R)) * >
1727 .sp
1728 In this pattern, (?(R) is the start of a conditional subpattern, with two
1729 different alternatives for the recursive and non-recursive cases. The (?R) item
1730 is the actual recursive call.
1731 .
1732 .
1733 .\" HTML <a name="subpatternsassubroutines"></a>
1734 .SH "SUBPATTERNS AS SUBROUTINES"
1735 .rs
1736 .sp
1737 If the syntax for a recursive subpattern reference (either by number or by
1738 name) is used outside the parentheses to which it refers, it operates like a
1739 subroutine in a programming language. The "called" subpattern may be defined
1740 before or after the reference. An earlier example pointed out that the pattern
1741 .sp
1742 (sens|respons)e and \e1ibility
1743 .sp
1744 matches "sense and sensibility" and "response and responsibility", but not
1745 "sense and responsibility". If instead the pattern
1746 .sp
1747 (sens|respons)e and (?1)ibility
1748 .sp
1749 is used, it does match "sense and responsibility" as well as the other two
1750 strings. Another example is given in the discussion of DEFINE above.
1751 .P
1752 Like recursive subpatterns, a "subroutine" call is always treated as an atomic
1753 group. That is, once it has matched some of the subject string, it is never
1754 re-entered, even if it contains untried alternatives and there is a subsequent
1755 matching failure.
1756 .P
1757 When a subpattern is used as a subroutine, processing options such as
1758 case-independence are fixed when the subpattern is defined. They cannot be
1759 changed for different calls. For example, consider this pattern:
1760 .sp
1761 (abc)(?i:(?1))
1762 .sp
1763 It matches "abcabc". It does not match "abcABC" because the change of
1764 processing option does not affect the called subpattern.
1765 .
1766 .
1767 .SH CALLOUTS
1768 .rs
1769 .sp
1770 Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
1771 code to be obeyed in the middle of matching a regular expression. This makes it
1772 possible, amongst other things, to extract different substrings that match the
1773 same pair of parentheses when there is a repetition.
1774 .P
1775 PCRE provides a similar feature, but of course it cannot obey arbitrary Perl
1776 code. The feature is called "callout". The caller of PCRE provides an external
1777 function by putting its entry point in the global variable \fIpcre_callout\fP.
1778 By default, this variable contains NULL, which disables all calling out.
1779 .P
1780 Within a regular expression, (?C) indicates the points at which the external
1781 function is to be called. If you want to identify different callout points, you
1782 can put a number less than 256 after the letter C. The default value is zero.
1783 For example, this pattern has two callout points:
1784 .sp
1785 (?C1)\dabc(?C2)def
1786 .sp
1787 If the PCRE_AUTO_CALLOUT flag is passed to \fBpcre_compile()\fP, callouts are
1788 automatically installed before each item in the pattern. They are all numbered
1789 255.
1790 .P
1791 During matching, when PCRE reaches a callout point (and \fIpcre_callout\fP is
1792 set), the external function is called. It is provided with the number of the
1793 callout, the position in the pattern, and, optionally, one item of data
1794 originally supplied by the caller of \fBpcre_exec()\fP. The callout function
1795 may cause matching to proceed, to backtrack, or to fail altogether. A complete
1796 description of the interface to the callout function is given in the
1797 .\" HREF
1798 \fBpcrecallout\fP
1799 .\"
1800 documentation.
1801 .
1802 .
1803 .SH "SEE ALSO"
1804 .rs
1805 .sp
1806 \fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3).
1807 .P
1808 .in 0
1809 Last updated: 06 December 2006
1810 .br
1811 Copyright (c) 1997-2006 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12