/[pcre]/code/tags/pcre-2.05/pcre.3
ViewVC logotype

Diff of /code/tags/pcre-2.05/pcre.3

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3 by nigel, Sat Feb 24 21:38:01 2007 UTC revision 33 by nigel, Sat Feb 24 21:39:01 2007 UTC
# Line 8  pcre - Perl-compatible regular expressio Line 8  pcre - Perl-compatible regular expressio
8  .br  .br
9  .B pcre *pcre_compile(const char *\fIpattern\fR, int \fIoptions\fR,  .B pcre *pcre_compile(const char *\fIpattern\fR, int \fIoptions\fR,
10  .ti +5n  .ti +5n
11  .B char **\fIerrptr\fR, int *\fIerroffset\fR);  .B const char **\fIerrptr\fR, int *\fIerroffset\fR,
12    .ti +5n
13    .B const unsigned char *\fItableptr\fR);
14  .PP  .PP
15  .br  .br
16  .B pcre_extra *pcre_study(const pcre *\fIcode\fR, int \fIoptions\fR,  .B pcre_extra *pcre_study(const pcre *\fIcode\fR, int \fIoptions\fR,
17  .ti +5n  .ti +5n
18  .B char **\fIerrptr\fR);  .B const char **\fIerrptr\fR);
19  .PP  .PP
20  .br  .br
21  .B int pcre_exec(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"  .B int pcre_exec(const pcre *\fIcode\fR, "const pcre_extra *\fIextra\fR,"
# Line 23  pcre - Perl-compatible regular expressio Line 25  pcre - Perl-compatible regular expressio
25  .B int *\fIovector\fR, int \fIovecsize\fR);  .B int *\fIovector\fR, int \fIovecsize\fR);
26  .PP  .PP
27  .br  .br
28  .B int pcre_info(const pcre *\fIcode\fR, int *\fIoptptr\fR, int  .B int pcre_copy_substring(const char *\fIsubject\fR, int *\fIovector\fR,
29  .B *\fIfirstcharptr\fR);  .ti +5n
30    .B int \fIstringcount\fR, int \fIstringnumber\fR, char *\fIbuffer\fR,
31    .ti +5n
32    .B int \fIbuffersize\fR);
33  .PP  .PP
34  .br  .br
35  .B char *pcre_version(void);  .B int pcre_get_substring(const char *\fIsubject\fR, int *\fIovector\fR,
36    .ti +5n
37    .B int \fIstringcount\fR, int \fIstringnumber\fR,
38    .ti +5n
39    .B const char **\fIstringptr\fR);
40  .PP  .PP
41  .br  .br
42  .B void *(*pcre_malloc)(size_t);  .B int pcre_get_substring_list(const char *\fIsubject\fR,
43    .ti +5n
44    .B int *\fIovector\fR, int \fIstringcount\fR, "const char ***\fIlistptr\fR);"
45  .PP  .PP
46  .br  .br
47  .B void (*pcre_free)(void *);  .B const unsigned char *pcre_maketables(void);
48  .PP  .PP
49  .br  .br
50  .B unsigned char *pcre_cbits[128];  .B int pcre_info(const pcre *\fIcode\fR, int *\fIoptptr\fR, int
51    .B *\fIfirstcharptr\fR);
52  .PP  .PP
53  .br  .br
54  .B unsigned char *pcre_ctypes[256];  .B char *pcre_version(void);
55  .PP  .PP
56  .br  .br
57  .B unsigned char *pcre_fcc[256];  .B void *(*pcre_malloc)(size_t);
58  .PP  .PP
59  .br  .br
60  .B unsigned char *pcre_lcc[256];  .B void (*pcre_free)(void *);
61    
62    
63    
64  .SH DESCRIPTION  .SH DESCRIPTION
65  The PCRE library is a set of functions that implement regular expression  The PCRE library is a set of functions that implement regular expression
66  pattern matching using the same syntax and semantics as Perl 5, with just a few  pattern matching using the same syntax and semantics as Perl 5, with just a few
67  differences (see below). The current implementation corresponds to Perl 5.004.  differences (see below). The current implementation corresponds to Perl 5.005.
68    
69  PCRE has its own native API, which is described in this man page. There is also  PCRE has its own native API, which is described in this man page. There is also
70  a set of wrapper functions that correspond to the POSIX API. See  a set of wrapper functions that correspond to the POSIX API. See
71  \fBpcreposix (3)\fR.  \fBpcreposix (3)\fR.
72    
73  The three functions \fBpcre_compile()\fR, \fBpcre_study()\fR, and  The functions \fBpcre_compile()\fR, \fBpcre_study()\fR, and \fBpcre_exec()\fR
74  \fBpcre_exec()\fR are used for compiling and matching regular expressions. The  are used for compiling and matching regular expressions, while
75  function \fBpcre_info()\fR is used to find out information about a compiled  \fBpcre_copy_substring()\fR, \fBpcre_get_substring()\fR, and
76    \fBpcre_get_substring_list()\fR are convenience functions for extracting
77    captured substrings from a matched subject string. The function
78    \fBpcre_maketables()\fR is used (optionally) to build a set of character tables
79    in the current locale for passing to \fBpcre_compile()\fR.
80    
81    The function \fBpcre_info()\fR is used to find out information about a compiled
82  pattern, while the function \fBpcre_version()\fR returns a pointer to a string  pattern, while the function \fBpcre_version()\fR returns a pointer to a string
83  containing the version of PCRE and its date of release.  containing the version of PCRE and its date of release.
84    
# Line 70  respectively. PCRE calls the memory mana Line 88  respectively. PCRE calls the memory mana
88  so a calling program can replace them if it wishes to intercept the calls. This  so a calling program can replace them if it wishes to intercept the calls. This
89  should be done before calling any PCRE functions.  should be done before calling any PCRE functions.
90    
 The other global variables are character tables. They are initialized when PCRE  
 is compiled, from source that is generated by reference to the C character type  
 functions, but which the maintainer of PCRE is free to modify. In principle  
 they could also be modified at runtime. See PCRE's README file for more  
 details.  
   
91    
92  .SH MULTI-THREADING  .SH MULTI-THREADING
93  The PCRE functions can be used in multi-threading applications, with the  The PCRE functions can be used in multi-threading applications, with the
94  proviso that the character tables and the memory management functions pointed  proviso that the memory management functions pointed to by \fBpcre_malloc\fR
95  to by \fBpcre_malloc\fR and \fBpcre_free\fR will be shared by all threads.  and \fBpcre_free\fR are shared by all threads.
96    
97  The compiled form of a regular expression is not altered during matching, so  The compiled form of a regular expression is not altered during matching, so
98  the same compiled pattern can safely be used by several threads at once.  the same compiled pattern can safely be used by several threads at once.
# Line 89  the same compiled pattern can safely be Line 101  the same compiled pattern can safely be
101  .SH COMPILING A PATTERN  .SH COMPILING A PATTERN
102  The function \fBpcre_compile()\fR is called to compile a pattern into an  The function \fBpcre_compile()\fR is called to compile a pattern into an
103  internal form. The pattern is a C string terminated by a binary zero, and  internal form. The pattern is a C string terminated by a binary zero, and
104  is passed in the argument \fIpattern\fR. A pointer to the compiled code block  is passed in the argument \fIpattern\fR. A pointer to a single block of memory
105  is returned. The \fBpcre\fR type is defined for this for convenience, but in  that is obtained via \fBpcre_malloc\fR is returned. This contains the
106  fact \fBpcre\fR is just a typedef for \fBvoid\fR, since the contents of the  compiled code and related data. The \fBpcre\fR type is defined for this for
107  block are not defined.  convenience, but in fact \fBpcre\fR is just a typedef for \fBvoid\fR, since the
108    contents of the block are not externally defined. It is up to the caller to
109    free the memory when it is no longer required.
110  .PP  .PP
111  The size of a compiled pattern is roughly proportional to the length of the  The size of a compiled pattern is roughly proportional to the length of the
112  pattern string, except that each character class (other than those containing  pattern string, except that each character class (other than those containing
# Line 101  quantifiers with a minimum greater than Line 115  quantifiers with a minimum greater than
115  relevant portions of the compiled pattern to be replicated.  relevant portions of the compiled pattern to be replicated.
116  .PP  .PP
117  The \fIoptions\fR argument contains independent bits that affect the  The \fIoptions\fR argument contains independent bits that affect the
118  compilation. It should be zero if no options are required. Those options that  compilation. It should be zero if no options are required. Some of the options,
119  are compabible with Perl can also be set at compile time from within the  in particular, those that are compatible with Perl, can also be set and unset
120  pattern (see the detailed description of regular expressions below) and all  from within the pattern (see the detailed description of regular expressions
121  options except PCRE_EXTENDED and PCRE_EXTRA can be set at the time of matching.  below). For these options, the contents of the \fIoptions\fR argument specifies
122    their initial settings at the start of compilation and execution. The
123    PCRE_ANCHORED option can be set at the time of matching as well as at compile
124    time.
125  .PP  .PP
126  If \fIerrptr\fR is NULL, \fBpcre_compile()\fR returns NULL immediately.  If \fIerrptr\fR is NULL, \fBpcre_compile()\fR returns NULL immediately.
127  Otherwise, if compilation of a pattern fails, \fBpcre_compile()\fR returns  Otherwise, if compilation of a pattern fails, \fBpcre_compile()\fR returns
128  NULL, and sets the variable pointed to by \fIerrptr\fR to point to a textual  NULL, and sets the variable pointed to by \fIerrptr\fR to point to a textual
129  error message.  error message. The offset from the start of the pattern to the character where
130    the error was discovered is placed in the variable pointed to by
131  The offset from the start of the pattern to the character where the error was  \fIerroffset\fR, which must not be NULL. If it is, an immediate error is given.
132  discovered is placed in the variable pointed to by \fIerroffset\fR, which must  .PP
133  not be NULL. If it is, an immediate error is given.  If the final argument, \fItableptr\fR, is NULL, PCRE uses a default set of
134    character tables which are built when it is compiled, using the default C
135    locale. Otherwise, \fItableptr\fR must be the result of a call to
136    \fBpcre_maketables()\fR. See the section on locale support below.
137  .PP  .PP
138  The following option bits are defined in the header file:  The following option bits are defined in the header file:
139    
# Line 127  constructs in the pattern itself, which Line 147  constructs in the pattern itself, which
147    PCRE_CASELESS    PCRE_CASELESS
148    
149  If this bit is set, letters in the pattern match both upper and lower case  If this bit is set, letters in the pattern match both upper and lower case
150  letters in any subject string. It is equivalent to Perl's /i option.  letters. It is equivalent to Perl's /i option.
151    
152    PCRE_DOLLAR_ENDONLY    PCRE_DOLLAR_ENDONLY
153    
154  If this bit is set, a dollar metacharacter in the pattern matches only at the  If this bit is set, a dollar metacharacter in the pattern matches only at the
155  end of the subject string. By default, it also matches immediately before the  end of the subject string. Without this option, a dollar also matches
156  final character if it is a newline (but not before any other newlines). The  immediately before the final character if it is a newline (but not before any
157  PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set. There is no  other newlines). The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
158  equivalent to this option in Perl.  set. There is no equivalent to this option in Perl.
159    
160    PCRE_DOTALL    PCRE_DOTALL
161    
162  If this bit is set, a dot metacharater in the pattern matches all characters,  If this bit is set, a dot metacharater in the pattern matches all characters,
163  including newlines. By default, newlines are excluded. This option is  including newlines. Without it, newlines are excluded. This option is
164  equivalent to Perl's /s option. A negative class such as [^a] always matches a  equivalent to Perl's /s option. A negative class such as [^a] always matches a
165  newline character, independent of the setting of this option.  newline character, independent of the setting of this option.
166    
167    PCRE_EXTENDED    PCRE_EXTENDED
168    
169  If this bit is set, whitespace characters in the pattern are totally ignored  If this bit is set, whitespace data characters in the pattern are totally
170  except when escaped or inside a character class, and characters between an  ignored except when escaped or inside a character class, and characters between
171  unescaped # outside a character class and the next newline character,  an unescaped # outside a character class and the next newline character,
172  inclusive, are also ignored. This is equivalent to Perl's /x option, and makes  inclusive, are also ignored. This is equivalent to Perl's /x option, and makes
173  it possible to include comments inside complicated patterns.  it possible to include comments inside complicated patterns. Note, however,
174    that this applies only to data characters. Whitespace characters may never
175    appear within special character sequences in a pattern, for example within the
176    sequence (?( which introduces a conditional subpattern.
177    
178      PCRE_EXTRA
179    
180    This option turns on additional functionality of PCRE that is incompatible with
181    Perl. Any backslash in a pattern that is followed by a letter that has no
182    special meaning causes an error, thus reserving these combinations for future
183    expansion. By default, as in Perl, a backslash followed by a letter with no
184    special meaning is treated as a literal. There are at present no other features
185    controlled by this option.
186    
187    PCRE_MULTILINE    PCRE_MULTILINE
188    
# Line 158  By default, PCRE treats the subject stri Line 190  By default, PCRE treats the subject stri
190  characters (even if it actually contains several newlines). The "start of line"  characters (even if it actually contains several newlines). The "start of line"
191  metacharacter (^) matches only at the start of the string, while the "end of  metacharacter (^) matches only at the start of the string, while the "end of
192  line" metacharacter ($) matches only at the end of the string, or before a  line" metacharacter ($) matches only at the end of the string, or before a
193  terminating newline. This is the same as Perl.  terminating newline (unless PCRE_DOLLAR_ENDONLY is set). This is the same as
194    Perl.
195    
196  When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs  When PCRE_MULTILINE it is set, the "start of line" and "end of line" constructs
197  match immediately following or immediately before any newline in the subject  match immediately following or immediately before any newline in the subject
# Line 167  to Perl's /m option. If there are no "\\ Line 200  to Perl's /m option. If there are no "\\
200  no occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no  no occurrences of ^ or $ in a pattern, setting PCRE_MULTILINE has no
201  effect.  effect.
202    
203    PCRE_EXTRA    PCRE_UNGREEDY
   
 This option turns on additional functionality of PCRE that is incompatible with  
 Perl. Any backslash in a pattern that is followed by a letter that has no  
 special meaning causes an error, thus reserving these combinations for future  
 expansion. By default, as in Perl, a backslash followed by a letter with no  
 special meaning is treated as a literal. There are two extra features currently  
 provided, and both are in some sense experimental additions that are useful for  
 influencing the progress of a match.  
   
   (1) The sequence \\X inserts a Prolog-like "cut" into the expression.  
   
   (2) Once a subpattern enclosed in (?>subpat) brackets has matched,  
       backtracking never goes back into the pattern.  
   
 See below for further details of both of these.  
204    
205    This option inverts the "greediness" of the quantifiers so that they are not
206    greedy by default, but become greedy if followed by "?". It is not compatible
207    with Perl. It can also be set by a (?U) option setting within the pattern.
208    
209    
210  .SH STUDYING A PATTERN  .SH STUDYING A PATTERN
# Line 195  typedef) containing additional informati Line 216  typedef) containing additional informati
216  passed to \fBpcre_exec()\fR. If no additional information is available, NULL  passed to \fBpcre_exec()\fR. If no additional information is available, NULL
217  is returned.  is returned.
218    
219  The second argument contains option bits. The only one currently supported is  The second argument contains option bits. At present, no options are defined
220  PCRE_CASELESS. It forces the studying to be done in a caseless manner, even if  for \fBpcre_study()\fR, and this argument should always be zero.
 the original pattern was compiled without PCRE_CASELESS. When the result of  
 \fBpcre_study()\fR is passed to \fBpcre_exec()\fR, it is used only if its  
 caseless state is the same as that of the matching process. A pattern that is  
 compiled without PCRE_CASELESS can be studied with and without PCRE_CASELESS,  
 and the appropriate data passed to \fBpcre_exec()\fR with and without the  
 PCRE_CASELESS flag.  
221    
222  The third argument for \fBpcre_study()\fR is a pointer to an error message. If  The third argument for \fBpcre_study()\fR is a pointer to an error message. If
223  studying succeeds (even if no data is returned), the variable it points to is  studying succeeds (even if no data is returned), the variable it points to is
# Line 213  not have a single fixed starting charact Line 228  not have a single fixed starting charact
228  characters is created.  characters is created.
229    
230    
231    .SH LOCALE SUPPORT
232    PCRE handles caseless matching, and determines whether characters are letters,
233    digits, or whatever, by reference to a set of tables. The library contains a
234    default set of tables which is created in the default C locale when PCRE is
235    compiled. This is used when the final argument of \fBpcre_compile()\fR is NULL,
236    and is sufficient for many applications.
237    
238    An alternative set of tables can, however, be supplied. Such tables are built
239    by calling the \fBpcre_maketables()\fR function, which has no arguments, in the
240    relevant locale. The result can then be passed to \fBpcre_compile()\ as often
241    as necessary. For example, to build and use tables that are appropriate for the
242    French locale (where accented characters with codes greater than 128 are
243    treated as letters), the following code could be used:
244    
245      setlocale(LC_CTYPE, "fr");
246      tables = pcre_maketables();
247      re = pcre_compile(..., tables);
248    
249    The tables are built in memory that is obtained via \fBpcre_malloc\fR. The
250    pointer that is passed to \fBpcre_compile\fR is saved with the compiled
251    pattern, and the same tables are used via this pointer by \fBpcre_study()\fR
252    and \fBpcre_match()\fR. Thus for any single pattern, compilation, studying and
253    matching all happen in the same locale, but different patterns can be compiled
254    in different locales. It is the caller's responsibility to ensure that the
255    memory containing the tables remains available for as long as it is needed.
256    
257    
258    .SH INFORMATION ABOUT A PATTERN
259    The \fBpcre_info()\fR function returns information about a compiled pattern.
260    Its yield is the number of capturing subpatterns, or one of the following
261    negative numbers:
262    
263      PCRE_ERROR_NULL       the argument \fIcode\fR was NULL
264      PCRE_ERROR_BADMAGIC   the "magic number" was not found
265    
266    If the \fIoptptr\fR argument is not NULL, a copy of the options with which the
267    pattern was compiled is placed in the integer it points to. These option bits
268    are those specified in the call to \fBpcre_compile()\fR, modified by any
269    top-level option settings within the pattern itself, and with the PCRE_ANCHORED
270    bit set if the form of the pattern implies that it can match only at the start
271    of a subject string.
272    
273    If the pattern is not anchored and the \fIfirstcharptr\fR argument is not NULL,
274    it is used to pass back information about the first character of any matched
275    string. If there is a fixed first character, e.g. from a pattern such as
276    (cat|cow|coyote), then it is returned in the integer pointed to by
277    \fIfirstcharptr\fR. Otherwise, if either
278    
279      (a) the pattern was compiled with the PCRE_MULTILINE option, and every branch
280          starts with "^", or
281    
282      (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not set
283          (if it were set, the pattern would be anchored),
284    
285    then -1 is returned, indicating that the pattern matches only at the
286    start of a subject string or after any "\\n" within the string. Otherwise -2 is
287    returned.
288    
289    
290  .SH MATCHING A PATTERN  .SH MATCHING A PATTERN
291  The function \fBpcre_exec()\fR is called to match a subject string against a  The function \fBpcre_exec()\fR is called to match a subject string against a
292  pre-compiled pattern, which is passed in the \fIcode\fR argument. If the  pre-compiled pattern, which is passed in the \fIcode\fR argument. If the
# Line 222  pattern has been studied, the result of Line 296  pattern has been studied, the result of
296  The subject string is passed as a pointer in \fIsubject\fR and a length in  The subject string is passed as a pointer in \fIsubject\fR and a length in
297  \fIlength\fR. Unlike the pattern string, it may contain binary zero characters.  \fIlength\fR. Unlike the pattern string, it may contain binary zero characters.
298    
299  The options PCRE_ANCHORED, PCRE_CASELESS, PCRE_DOLLAR_ENDONLY, PCRE_DOTALL, and  The PCRE_ANCHORED option can be passed in the \fIoptions\fR argument, whose
300  PCRE_MULTILINE can be passed in the \fIoptions\fR argument, whose unused bits  unused bits must be zero. However, if a pattern was compiled with
301  must be zero. However, if a pattern is compiled with any of these options, they  PCRE_ANCHORED, or turned out to be anchored by virtue of its contents, it
302  cannot be unset when it is obeyed.  cannot be made unachored at matching time.
303    
304  There are also two further options that can be set only at matching time:  There are also two further options that can be set only at matching time:
305    
# Line 233  There are also two further options that Line 307  There are also two further options that
307    
308  The first character of the string is not the beginning of a line, so the  The first character of the string is not the beginning of a line, so the
309  circumflex metacharacter should not match before it. Setting this without  circumflex metacharacter should not match before it. Setting this without
310  PCRE_MULTILINE (at either compile or match time) causes circumflex never to  PCRE_MULTILINE (at compile time) causes circumflex never to match.
 match.  
311    
312    PCRE_NOTEOL    PCRE_NOTEOL
313    
314  The end of the string is not the end of a line, so the dollar metacharacter  The end of the string is not the end of a line, so the dollar metacharacter
315  should not match it. Setting this without PCRE_MULTILINE (at either compile or  should not match it nor (except in multiline mode) a newline immediately before
316  match time) causes dollar never to match.  it. Setting this without PCRE_MULTILINE (at compile time) causes dollar never
317    to match.
318    
319  In general, a pattern matches a certain portion of the subject, and in  In general, a pattern matches a certain portion of the subject, and in
320  addition, further substrings from the subject may be picked out by parts of the  addition, further substrings from the subject may be picked out by parts of the
# Line 251  kinds of parenthesized subpattern that d Line 325  kinds of parenthesized subpattern that d
325    
326  Captured substrings are returned to the caller via a vector of integer offsets  Captured substrings are returned to the caller via a vector of integer offsets
327  whose address is passed in \fIovector\fR. The number of elements in the vector  whose address is passed in \fIovector\fR. The number of elements in the vector
328  is passed in \fIovecsize\fR. This should always be an even number, because the  is passed in \fIovecsize\fR. The first two-thirds of the vector is used to pass
329  elements are used in pairs. If an odd number is passed, it is rounded down.  back captured substrings, each substring using a pair of integers. The
330    remaining third of the vector is used as workspace by \fBpcre_exec()\fR while
331    matching capturing subpatterns, and is not available for passing back
332    information. The length passed in \fIovecsize\fR should always be a multiple of
333    three. If it is not, it is rounded down.
334    
335    When a match has been successful, information about captured substrings is
336    returned in pairs of integers, starting at the beginning of \fIovector\fR, and
337    continuing up to two-thirds of its length at the most. The first element of a
338    pair is set to the offset of the first character in a substring, and the second
339    is set to the offset of the first character after the end of a substring. The
340    first pair, \fIovector[0]\fR and \fIovector[1]\fR, identify the portion of the
341    subject string matched by the entire pattern. The next pair is used for the
342    first capturing subpattern, and so on. The value returned by \fBpcre_exec()\fR
343    is the number of pairs that have been set. If there are no capturing
344    subpatterns, the return value from a successful match is 1, indicating that
345    just the first pair of offsets has been set.
346    
347  The first element of a pair is set to the offset of the first character in a  Some convenience functions are provided for extracting the captured substrings
348  substring, and the second is set to the offset of the first character after the  as separate strings. These are described in the following section.
 end of a substring. The first pair, \fIovector[0]\fR and \fIovector[1]\fR,  
 identify the portion of the subject string matched by the entire pattern. The  
 next pair is used for the first capturing subpattern, and so on. The value  
 returned by \fBpcre_exec()\fR is the number of pairs that have been set. If  
 there are no capturing subpatterns, the return value from a successful match  
 is 1, indicating that just the first pair of offsets has been set.  
349    
350  It is possible for an capturing subpattern number \fIn+1\fR to match some  It is possible for an capturing subpattern number \fIn+1\fR to match some
351  part of the subject when subpattern \fIn\fR has not been used at all. For  part of the subject when subpattern \fIn\fR has not been used at all. For
352  example, if the string "abc" is matched against the pattern "(a|(z))(bc)",  example, if the string "abc" is matched against the pattern (a|(z))(bc)
353  subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset  subpatterns 1 and 3 are matched, but 2 is not. When this happens, both offset
354  values corresponding to the unused subpattern are set to -1.  values corresponding to the unused subpattern are set to -1.
355    
# Line 273  If a capturing subpattern is matched rep Line 357  If a capturing subpattern is matched rep
357  string that it matched that gets returned.  string that it matched that gets returned.
358    
359  If the vector is too small to hold all the captured substrings, it is used as  If the vector is too small to hold all the captured substrings, it is used as
360  far as possible, and the function returns a value of zero. In particular, if  far as possible (up to two-thirds of its length), and the function returns a
361  the substring offsets are not of interest, \fBpcre_exec()\fR may be called with  value of zero. In particular, if the substring offsets are not of interest,
362  \fIovector\fR passed as NULL and \fIovecsize\fR as zero. However, if the  \fBpcre_exec()\fR may be called with \fIovector\fR passed as NULL and
363  pattern contains back references and the \fIovector\fR isn't big enough to  \fIovecsize\fR as zero. However, if the pattern contains back references and
364  remember the related substrings, PCRE has to get additional memory for use  the \fIovector\fR isn't big enough to remember the related substrings, PCRE has
365  during matching. Thus it is usually advisable to supply an \fIovector\fR.  to get additional memory for use during matching. Thus it is usually advisable
366    to supply an \fIovector\fR.
367    
368  Note that \fBpcre_info()\fR can be used to find out how many capturing  Note that \fBpcre_info()\fR can be used to find out how many capturing
369  subpatterns there are in a compiled pattern.  subpatterns there are in a compiled pattern. The smallest size for
370    \fIovector\fR that will allow for \fIn\fR captured substrings in addition to
371    the offsets of the substring matched by the whole pattern is (\fIn\fR+1)*3.
372    
373  If \fBpcre_exec()\fR fails, it returns a negative number. The following are  If \fBpcre_exec()\fR fails, it returns a negative number. The following are
374  defined in the header file:  defined in the header file:
# Line 290  defined in the header file: Line 377  defined in the header file:
377    
378  The subject string did not match the pattern.  The subject string did not match the pattern.
379    
380    PCRE_ERROR_BADREF         (-2)    PCRE_ERROR_NULL           (-2)
   
 There was a back-reference in the pattern to a capturing subpattern that had  
 not previously been set.  
   
   PCRE_ERROR_NULL           (-3)  
381    
382  Either \fIcode\fR or \fIsubject\fR was passed as NULL, or \fIovector\fR was  Either \fIcode\fR or \fIsubject\fR was passed as NULL, or \fIovector\fR was
383  NULL and \fIovecsize\fR was not zero.  NULL and \fIovecsize\fR was not zero.
384    
385    PCRE_ERROR_BADOPTION      (-4)    PCRE_ERROR_BADOPTION      (-3)
386    
387  An unrecognized bit was set in the \fIoptions\fR argument.  An unrecognized bit was set in the \fIoptions\fR argument.
388    
389    PCRE_ERROR_BADMAGIC       (-5)    PCRE_ERROR_BADMAGIC       (-4)
390    
391  PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch  PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch
392  the case when it is passed a junk pointer. This is the error it gives when the  the case when it is passed a junk pointer. This is the error it gives when the
393  magic number isn't present.  magic number isn't present.
394    
395    PCRE_ERROR_UNKNOWN_NODE   (-6)    PCRE_ERROR_UNKNOWN_NODE   (-5)
396    
397  While running the pattern match, an unknown item was encountered in the  While running the pattern match, an unknown item was encountered in the
398  compiled pattern. This error could be caused by a bug in PCRE or by overwriting  compiled pattern. This error could be caused by a bug in PCRE or by overwriting
399  of the compiled pattern.  of the compiled pattern.
400    
401    PCRE_ERROR_NOMEMORY       (-7)    PCRE_ERROR_NOMEMORY       (-6)
402    
403  If a pattern contains back references, but the \fIovector\fR that is passed to  If a pattern contains back references, but the \fIovector\fR that is passed to
404  \fBpcre_exec()\fR is not big enough to remember the referenced substrings, PCRE  \fBpcre_exec()\fR is not big enough to remember the referenced substrings, PCRE
# Line 325  call via \fBpcre_malloc()\fR fails, this Line 407  call via \fBpcre_malloc()\fR fails, this
407  the end of matching.  the end of matching.
408    
409    
410  .SH INFORMATION ABOUT A PATTERN  .SH EXTRACTING CAPTURED SUBSTRINGS
411  The \fBpcre_info()\fR function returns information about a compiled pattern.  Captured substrings can be accessed directly by using the offsets returned by
412  Its yield is the number of capturing subpatterns, or one of the following  \fBpcre_exec()\fR in \fIovector\fR. For convenience, the functions
413  negative numbers:  \fBpcre_copy_substring()\fR, \fBpcre_get_substring()\fR, and
414    \fBpcre_get_substring_list()\fR are provided for extracting captured substrings
415    PCRE_ERROR_NULL       the argument \fIcode\fR was NULL  as new, separate, zero-terminated strings. A substring that contains a binary
416    PCRE_ERROR_BADMAGIC   the "magic number" was not found  zero is correctly extracted and has a further zero added on the end, but the
417    result does not, of course, function as a C string.
418  If the \fIoptptr\fR argument is not NULL, a copy of the options with which the  
419  pattern was compiled is placed in the integer it points to.  The first three arguments are the same for all three functions: \fIsubject\fR
420    is the subject string which has just been successfully matched, \fIovector\fR
421    is a pointer to the vector of integer offsets that was passed to
422    \fBpcre_exec()\fR, and \fIstringcount\fR is the number of substrings that
423    were captured by the match, including the substring that matched the entire
424    regular expression. This is the value returned by \fBpcre_exec\fR if it
425    is greater than zero. If \fBpcre_exec()\fR returned zero, indicating that it
426    ran out of space in \fIovector\fR, then the value passed as
427    \fIstringcount\fR should be the size of the vector divided by three.
428    
429    The functions \fBpcre_copy_substring()\fR and \fBpcre_get_substring()\fR
430    extract a single substring, whose number is given as \fIstringnumber\fR. A
431    value of zero extracts the substring that matched the entire pattern, while
432    higher values extract the captured substrings. For \fBpcre_copy_substring()\fR,
433    the string is placed in \fIbuffer\fR, whose length is given by
434    \fIbuffersize\fR, while for \fBpcre_get_substring()\fR a new block of store is
435    obtained via \fBpcre_malloc\fR, and its address is returned via
436    \fIstringptr\fR. The yield of the function is the length of the string, not
437    including the terminating zero, or one of
438    
439      PCRE_ERROR_NOMEMORY       (-6)
440    
441    The buffer was too small for \fBpcre_copy_substring()\fR, or the attempt to get
442    memory failed for \fBpcre_get_substring()\fR.
443    
444      PCRE_ERROR_NOSUBSTRING    (-7)
445    
446    There is no substring whose number is \fIstringnumber\fR.
447    
448    The \fBpcre_get_substring_list()\fR function extracts all available substrings
449    and builds a list of pointers to them. All this is done in a single block of
450    memory which is obtained via \fBpcre_malloc\fR. The address of the memory block
451    is returned via \fIlistptr\fR, which is also the start of the list of string
452    pointers. The end of the list is marked by a NULL pointer. The yield of the
453    function is zero if all went well, or
454    
455      PCRE_ERROR_NOMEMORY       (-6)
456    
457    if the attempt to get the memory block failed.
458    
459    When any of these functions encounter a substring that is unset, which can
460    happen when capturing subpattern number \fIn+1\fR matches some part of the
461    subject, but subpattern \fIn\fR has not been used at all, they return an empty
462    string. This can be distinguished from a genuine zero-length substring by
463    inspecting the appropriate offset in \fIovector\fR, which is negative for unset
464    substrings.
465    
 If the \fIfirstcharptr\fR argument is not NULL, is is used to pass back  
 information about the first character of any matched string. If there is a  
 fixed first character, e.g. from a pattern such as (cat|cow|coyote), then it is  
 returned in the integer pointed to by \fIfirstcharptr\fR. Otherwise, if the  
 pattern was compiled with the PCRE_MULTILINE option, and every branch started  
 with "^", then -1 is returned, indicating that the pattern will match at the  
 start of a subject string or after any "\\n" within the string. Otherwise -2 is  
 returned.  
466    
467    
468  .SH LIMITATIONS  .SH LIMITATIONS
# Line 353  The maximum length of a compiled pattern Line 472  The maximum length of a compiled pattern
472  All values in repeating quantifiers must be less than 65536.  All values in repeating quantifiers must be less than 65536.
473  The maximum number of capturing subpatterns is 99.  The maximum number of capturing subpatterns is 99.
474  The maximum number of all parenthesized subpatterns, including capturing  The maximum number of all parenthesized subpatterns, including capturing
475  subpatterns and assertions, is 200.  subpatterns, assertions, and other types of subpattern, is 200.
476    
477  The maximum length of a subject string is the largest positive number that an  The maximum length of a subject string is the largest positive number that an
478  integer variable can hold. However, PCRE uses recursion to handle subpatterns  integer variable can hold. However, PCRE uses recursion to handle subpatterns
# Line 362  the size of a subject string that can be Line 481  the size of a subject string that can be
481    
482    
483  .SH DIFFERENCES FROM PERL  .SH DIFFERENCES FROM PERL
484  The differences described here are with respect to Perl 5.004.  The differences described here are with respect to Perl 5.005.
485    
486  1. By default, a whitespace character is any character that the C library  1. By default, a whitespace character is any character that the C library
487  function \fBisspace()\fR recognizes, though it is possible to compile PCRE with  function \fBisspace()\fR recognizes, though it is possible to compile PCRE with
# Line 371  formfeed, newline, carriage return, hori Line 490  formfeed, newline, carriage return, hori
490  no longer includes vertical tab in its set of whitespace characters. The \\v  no longer includes vertical tab in its set of whitespace characters. The \\v
491  escape that was in the Perl documentation for a long time was never in fact  escape that was in the Perl documentation for a long time was never in fact
492  recognized. However, the character itself was treated as whitespace at least  recognized. However, the character itself was treated as whitespace at least
493  up to 5.002. In 5.004 it does not match \\s.  up to 5.002. In 5.004 and 5.005 it does not match \\s.
494    
495  2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl permits  2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl permits
496  them, but they do not mean what you might think. For example, "(?!a){3}" does  them, but they do not mean what you might think. For example, (?!a){3} does
497  not assert that the next three characters are not "a". It just asserts that the  not assert that the next three characters are not "a". It just asserts that the
498  next character is not "a" three times.  next character is not "a" three times.
499    
# Line 396  are not part of its pattern matching eng Line 515  are not part of its pattern matching eng
515  6. The Perl \\G assertion is not supported as it is not relevant to single  6. The Perl \\G assertion is not supported as it is not relevant to single
516  pattern matches.  pattern matches.
517    
518  7. If a backreference can never be matched, PCRE diagnoses an error. In a case  7. Fairly obviously, PCRE does not support the (?{code}) construction.
 like  
519    
520    /(123)\\2/  8. There are at the time of writing some oddities in Perl 5.005_02 concerned
521    with the settings of captured strings when part of a pattern is repeated. For
522    example, matching "aba" against the pattern /^(a(b)?)+$/ sets $2 to the value
523    "b", but matching "aabbaa" against /^(aa(bb)?)+$/ leaves $2 unset. However, if
524    the pattern is changed to /^(aa(b(b))?)+$/ then $2 (and $3) get set.
525    
526  the error occurs at compile time. Perl gives no compile time error; version  In Perl 5.004 $2 is set in both cases, and that is also true of PCRE. If in the
527  5.004 either always fails to match, or gives a segmentation fault at runtime.  future Perl changes to a consistent state that is different, PCRE may change to
528  In more complicated cases such as  follow.
529    
530    /(1)(2)(3)(4)(5)(6)(7)(8)(9)(10\\10)/  9. Another as yet unresolved discrepancy is that in Perl 5.005_02 the pattern
531    /^(a)?(?(1)a|b)+$/ matches the string "a", whereas in PCRE it does not.
532    However, in both Perl and PCRE /^(a)?a/ matched against "a" leaves $1 unset.
533    
534  PCRE returns PCRE_ERROR_BADREF at run time. Perl always fails to match.  10. PCRE provides some extensions to the Perl regular expression facilities:
535    
536  8. PCRE provides some extensions to the Perl regular expression facilities:  (a) Although lookbehind assertions must match fixed length strings, each
537    alternative branch of a lookbehind assertion can match a different length of
538    string. Perl 5.005 requires them all to have the same length.
539    
540  (a) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ meta-  (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ meta-
541  character matches only at the very end of the string.  character matches only at the very end of the string.
542    
543  (b) If PCRE_EXTRA is set, the \\X assertion (a Prolog-like "cut") is  (c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
544  recognized, and a backslash followed by a letter with no special meaning is  meaning is faulted.
545  faulted. There is also a new kind of parenthesized subpattern starting with (?>  
546  which has a block on backtracking into it once it has matched.  (d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
547    inverted, that is, by default they are not greedy, but if followed by a
548    question mark they are.
549    
550    
551  .SH REGULAR EXPRESSION DETAILS  .SH REGULAR EXPRESSION DETAILS
# Line 484  non-alphameric with "\\" to specify that Line 612  non-alphameric with "\\" to specify that
612  if you want to match a backslash, you write "\\\\".  if you want to match a backslash, you write "\\\\".
613    
614  If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the  If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
615  pattern and characters between a "#" outside a character class and the next  pattern (other than in a character class) and characters between a "#" outside
616  newline character are ignored. An escaping backslash can be used to include a  a character class and the next newline character are ignored. An escaping
617  whitespace or "#" character as part of the pattern.  backslash can be used to include a whitespace or "#" character as part of the
618    pattern.
619    
620  A second use of backslash provides a way of encoding non-printing characters  A second use of backslash provides a way of encoding non-printing characters
621  in patterns in a visible manner. There is no restriction on the appearance of  in patterns in a visible manner. There is no restriction on the appearance of
# Line 503  represents: Line 632  represents:
632    \\r     carriage return (hex 0D)    \\r     carriage return (hex 0D)
633    \\t     tab (hex 09)    \\t     tab (hex 09)
634    \\xhh   character with hex code hh    \\xhh   character with hex code hh
635    \\ddd   character with octal code ddd or backreference    \\ddd   character with octal code ddd, or backreference
636    
637  The precise effect of "\\cx" is as follows: if "x" is a lower case letter, it  The precise effect of "\\cx" is as follows: if "x" is a lower case letter, it
638  is converted to upper case. Then bit 6 of the character (hex 40) is inverted.  is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
# Line 516  lower case). Line 645  lower case).
645  After "\\0" up to two further octal digits are read. In both cases, if there  After "\\0" up to two further octal digits are read. In both cases, if there
646  are fewer than two digits, just those that are present are used. Thus the  are fewer than two digits, just those that are present are used. Thus the
647  sequence "\\0\\x\\07" specifies two binary zeros followed by a BEL character.  sequence "\\0\\x\\07" specifies two binary zeros followed by a BEL character.
648  Make sure you supply two digits if the character that follows could otherwise  Make sure you supply two digits after the initial zero if the character that
649  be taken as another digit.  follows is itself an octal digit.
650    
651  The handling of a backslash followed by a digit other than 0 is complicated.  The handling of a backslash followed by a digit other than 0 is complicated.
652  Outside a character class, PCRE reads it and any following digits as a decimal  Outside a character class, PCRE reads it and any following digits as a decimal
# Line 567  Each pair of escape sequences partitions Line 696  Each pair of escape sequences partitions
696  two disjoint sets. Any given character matches one, and only one, of each pair.  two disjoint sets. Any given character matches one, and only one, of each pair.
697    
698  A "word" character is any letter or digit or the underscore character, that is,  A "word" character is any letter or digit or the underscore character, that is,
699  any character which can be part of a Perl "word". These character type  any character which can be part of a Perl "word". The definition of letters and
700  sequences can appear both inside and outside character classes. They each match  digits is controlled by PCRE's character tables, and may vary if locale-
701  one character of the appropriate type. If the current matching point is at the  specific matching is taking place (see "Locale support" above). For example, in
702  end of the subject string, all of them fail, since there is no character to  the "fr" (French) locale, some character codes greater than 128 are used for
703  match.  accented letters, and these are matched by \\w.
704    
705  The fourth use of backslash is for certain assertions. An assertion specifies a  These character type sequences can appear both inside and outside character
706  condition that has to be met at a particular point in a match, without  classes. They each match one character of the appropriate type. If the current
707  consuming any characters from the subject string. The backslashed assertions  matching point is at the end of the subject string, all of them fail, since
708  are  there is no character to match.
709    
710    The fourth use of backslash is for certain simple assertions. An assertion
711    specifies a condition that has to be met at a particular point in a match,
712    without consuming any characters from the subject string. The use of
713    subpatterns for more complicated assertions is described below. The backslashed
714    assertions are
715    
716    \\b     word boundary    \\b     word boundary
717    \\B     not a word boundary    \\B     not a word boundary
718    \\A     start of subject (independent of multiline mode)    \\A     start of subject (independent of multiline mode)
719    \\Z     end of subject (independent of multiline mode)    \\Z     end of subject or newline at end (independent of multiline mode)
720      \\z     end of subject (independent of multiline mode)
721    
722  Assertions may not appear in character classes (but note that "\\b" has a  These assertions may not appear in character classes (but note that "\\b" has a
723  different meaning, namely the backspace character, inside a character class).  different meaning, namely the backspace character, inside a character class).
724    
725  A word boundary is a position in the subject string where the current character  A word boundary is a position in the subject string where the current character
726  and the previous character do not both match "\\w" or "\\W" (i.e. one matches  and the previous character do not both match \\w or \\W (i.e. one matches
727  "\\w" and the other matches "\\W"), or the start or end of the string if the  \\w and the other matches \\W), or the start or end of the string if the
728  first or last character matches "\\w", respectively. More complicated  first or last character matches \\w, respectively.
729  assertions are also supported (see below).  
730    The \\A, \\Z, and \\z assertions differ from the traditional circumflex and
731  The "\\A" and "\\Z" assertions differ from the traditional "^" and "$"  dollar (described below) in that they only ever match at the very start and end
732  (described below) in that they only ever match at the very start and end of the  of the subject string, whatever options are set. They are not affected by the
733  subject string, respectively, whatever options are set.  PCRE_NOTBOL or PCRE_NOTEOL options. The difference between \\Z and \\z is that
734    \\Z matches before a newline that is the last character of the string as well
735  When the PCRE_EXTRA flag is set on a call to \fBpcre_compile()\fR, the  as at the end of the string, whereas \\z matches only at the end.
 additional assertion \\X, which has no equivalent in Perl, is recognized.  
 This operates like the "cut" operation in Prolog: it prevents the matching  
 operation from backtracking past it. For example, if the expression  
   
   .*/foo  
   
 is matched against the string "/foo/this/is/not" then after the initial greedy  
 .* has swallowed the whole string, it keeps backtracking right the way to the  
 beginning before failing. If, on the other hand, the expression is  
   
   .*/\\Xfoo  
   
 then once it has discovered that "/not" is not "/foo", backtracking ceases, and  
 the match fails. See also the section on "once-only" subpatterns below.  
   
736    
737    
738  .SH CIRCUMFLEX AND DOLLAR  .SH CIRCUMFLEX AND DOLLAR
739  Outside a character class, the circumflex character is an assertion which is  Outside a character class, in the default matching mode, the circumflex
740  true only if the current matching point is at the start of the subject string,  character is an assertion which is true only if the current matching point is
741  in the default matching mode. Inside a character class, circumflex has an  at the start of the subject string. Inside a character class, circumflex has an
742  entirely different meaning (see below).  entirely different meaning (see below).
743    
744  Circumflex need not be the first character of the pattern if a number of  Circumflex need not be the first character of the pattern if a number of
# Line 637  Dollar has no special meaning in a chara Line 758  Dollar has no special meaning in a chara
758    
759  The meaning of dollar can be changed so that it matches only at the very end of  The meaning of dollar can be changed so that it matches only at the very end of
760  the string, by setting the PCRE_DOLLAR_ENDONLY option at compile or matching  the string, by setting the PCRE_DOLLAR_ENDONLY option at compile or matching
761  time.  time. This does not affect the \\Z assertion.
762    
763  The meanings of the circumflex and dollar characters are changed if the  The meanings of the circumflex and dollar characters are changed if the
764  PCRE_MULTILINE option is set at compile or matching time. When this is the  PCRE_MULTILINE option is set. When this is the case, they match immediately
765  case, they match immediately after and immediately before an internal "\\n"  after and immediately before an internal "\\n" character, respectively, in
766  character, respectively, in addition to matching at the start and end of the  addition to matching at the start and end of the subject string. For example,
767  subject string. For example, the pattern /^abc$/ matches the subject string  the pattern /^abc$/ matches the subject string "def\\nabc" in multiline mode,
768  "def\\nabc" in multiline mode, but not otherwise. Consequently, patterns that  but not otherwise. Consequently, patterns that are anchored in single line mode
769  are anchored in single line mode because all branches start with "^" are not  because all branches start with "^" are not anchored in multiline mode. The
770  anchored in multiline mode. The PCRE_DOLLAR_ENDONLY option is ignored if  PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
771  PCRE_MULTILINE is set.  
772    Note that the sequences \\A, \\Z, and \\z can be used to match the start and
773  Note that the sequences "\\A" and "\\Z" can be used to match the start and end  end of the subject in both modes, and if all branches of a pattern start with
774  of the subject in both modes, and if all branches of a pattern start with "\\A"  \\A is it always anchored, whether PCRE_MULTILINE is set or not.
 is it always anchored.  
775    
776    
777  .SH FULL STOP (PERIOD, DOT)  .SH FULL STOP (PERIOD, DOT)
# Line 668  An opening square bracket introduces a c Line 788  An opening square bracket introduces a c
788  square bracket. A closing square bracket on its own is not special. If a  square bracket. A closing square bracket on its own is not special. If a
789  closing square bracket is required as a member of the class, it should be the  closing square bracket is required as a member of the class, it should be the
790  first data character in the class (after an initial circumflex, if present) or  first data character in the class (after an initial circumflex, if present) or
791  escaped with \\.  escaped with a backslash.
792    
793  A character class matches a single character in the subject; the character must  A character class matches a single character in the subject; the character must
794  be in the set of characters defined by the class, unless the first character in  be in the set of characters defined by the class, unless the first character in
795  the class is a circumflex, in which case the subject character must not be in  the class is a circumflex, in which case the subject character must not be in
796  the set defined by the class. If a circumflex is actually required as a member  the set defined by the class. If a circumflex is actually required as a member
797  of the class, ensure it is not the first character, or escape it with \\.  of the class, ensure it is not the first character, or escape it with a
798    backslash.
799    
800  For example, the character class [aeiou] matches any lower case vowel, while  For example, the character class [aeiou] matches any lower case vowel, while
801  [^aeiou] matches any character that is not a lower case vowel. Note that a  [^aeiou] matches any character that is not a lower case vowel. Note that a
# Line 683  are in the class by enumerating those th Line 804  are in the class by enumerating those th
804  still consumes a character from the subject string, and fails if the current  still consumes a character from the subject string, and fails if the current
805  pointer is at the end of the string.  pointer is at the end of the string.
806    
807    When caseless matching is set, any letters in a class represent both their
808    upper case and lower case versions, so for example, a caseless [aeiou] matches
809    "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
810    caseful version would.
811    
812  The newline character is never treated in any special way in character classes,  The newline character is never treated in any special way in character classes,
813  whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE options is. A class  whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE options is. A class
814  such as [^a] will always match a newline.  such as [^a] will always match a newline.
# Line 690  such as [^a] will always match a newline Line 816  such as [^a] will always match a newline
816  The minus (hyphen) character can be used to specify a range of characters in a  The minus (hyphen) character can be used to specify a range of characters in a
817  character class. For example, [d-m] matches any letter between d and m,  character class. For example, [d-m] matches any letter between d and m,
818  inclusive. If a minus character is required in a class, it must be escaped with  inclusive. If a minus character is required in a class, it must be escaped with
819  \\ or appear in a position where it cannot be interpreted as indicating a  a backslash or appear in a position where it cannot be interpreted as
820  range, typically as the first or last character in the class. It is not  indicating a range, typically as the first or last character in the class.
821  possible to have the character "]" as the end character of a range, since a  
822  sequence such as [w-] is interpreted as a class of two characters. The octal or  It is not possible to have the literal character "]" as the end character of a
823  hexadecimal representation of "]" can, however, be used to end a range.  range. A pattern such as [W-]46] is interpreted as a class of two characters
824    ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
825    "-46]". However, if the "]" is escaped with a backslash it is interpreted as
826    the end of range, so [W-\\]46] is interpreted as a single class containing a
827    range followed by two separate characters. The octal or hexadecimal
828    representation of "]" can also be used to end a range.
829    
830  Ranges operate in ASCII collating sequence. They can also be used for  Ranges operate in ASCII collating sequence. They can also be used for
831  characters specified numerically, for example [\\000-\\037]. If a range such as  characters specified numerically, for example [\\000-\\037]. If a range that
832  [W-c] is used when PCRE_CASELESS is set, it matches the letters involved in  includes letters is used when caseless matching is set, it matches the letters
833  either case.  in either case. For example, [W-c] is equivalent to [][\\^_`wxyzabc], matched
834    caselessly, and if character tables for the "fr" locale are in use,
835    [\\xc8-\\xcb] matches accented E characters in both cases.
836    
837  The character types \\d, \\D, \\s, \\S, \\w, and \\W may also appear in a  The character types \\d, \\D, \\s, \\S, \\w, and \\W may also appear in a
838  character class, and add the characters that they match to the class. For  character class, and add the characters that they match to the class. For
839  example, the class [^\\W_] matches any letter or digit.  example, [\\dABCDEF] matches any hexadecimal digit. A circumflex can
840    conveniently be used with the upper case character types to specify a more
841    restricted set of characters than the matching lower case type. For example,
842    the class [^\\W_] matches any letter or digit, but not underscore.
843    
844  All non-alphameric characters other than \\, -, ^ (at the start) and the  All non-alphameric characters other than \\, -, ^ (at the start) and the
845  terminating ] are non-special in character classes, but it does no harm if they  terminating ] are non-special in character classes, but it does no harm if they
# Line 711  are escaped. Line 847  are escaped.
847    
848    
849  .SH VERTICAL BAR  .SH VERTICAL BAR
850  Vertical bar characters are used to separate alternative patterns. The matching  Vertical bar characters are used to separate alternative patterns. For example,
851  process tries all the alternatives in turn. For example, the pattern  the pattern
852    
853    gilbert|sullivan    gilbert|sullivan
854    
855  matches either "gilbert" or "sullivan". Any number of alternatives can be used,  matches either "gilbert" or "sullivan". Any number of alternatives may appear,
856  and an empty alternative is permitted (matching the empty string).  and an empty alternative is permitted (matching the empty string).
857    The matching process tries each alternative in turn, from left to right,
858    and the first one that succeeds is used. If the alternatives are within a
859    subpattern (defined below), "succeeds" means matching the rest of the main
860    pattern as well as the alternative in the subpattern.
861    
862    
863    .SH INTERNAL OPTION SETTING
864    The settings of PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and PCRE_EXTENDED
865    can be changed from within the pattern by a sequence of Perl option letters
866    enclosed between "(?" and ")". The option letters are
867    
868      i  for PCRE_CASELESS
869      m  for PCRE_MULTILINE
870      s  for PCRE_DOTALL
871      x  for PCRE_EXTENDED
872    
873    For example, (?im) sets caseless, multiline matching. It is also possible to
874    unset these options by preceding the letter with a hyphen, and a combined
875    setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
876    PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
877    permitted. If a letter appears both before and after the hyphen, the option is
878    unset.
879    
880    The scope of these option changes depends on where in the pattern the setting
881    occurs. For settings that are outside any subpattern (defined below), the
882    effect is the same as if the options were set or unset at the start of
883    matching. The following patterns all behave in exactly the same way:
884    
885      (?i)abc
886      a(?i)bc
887      ab(?i)c
888      abc(?i)
889    
890    which in turn is the same as compiling the pattern abc with PCRE_CASELESS set.
891    In other words, such "top level" settings apply to the whole pattern (unless
892    there are other changes inside subpatterns). If there is more than one setting
893    of the same option at top level, the rightmost setting is used.
894    
895    If an option change occurs inside a subpattern, the effect is different. This
896    is a change of behaviour in Perl 5.005. An option change inside a subpattern
897    affects only that part of the subpattern that follows it, so
898    
899      (a(?i)b)c
900    
901    matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
902    By this means, options can be made to have different settings in different
903    parts of the pattern. Any changes made in one alternative do carry on
904    into subsequent branches within the same subpattern. For example,
905    
906      (a(?i)b|c)
907    
908    matches "ab", "aB", "c", and "C", even though when matching "C" the first
909    branch is abandoned before the option setting. This is because the effects of
910    option settings happen at compile time. There would be some very weird
911    behaviour otherwise.
912    
913    The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changed in the
914    same way as the Perl-compatible options by using the characters U and X
915    respectively. The (?X) flag setting is special in that it must always occur
916    earlier in the pattern than any of the additional features it turns on, even
917    when it is at top level. It is best put at the start.
918    
919    
920  .SH SUBPATTERNS  .SH SUBPATTERNS
# Line 757  the captured substrings are "white queen Line 954  the captured substrings are "white queen
954  2. The maximum number of captured substrings is 99, and the maximum number of  2. The maximum number of captured substrings is 99, and the maximum number of
955  all subpatterns, both capturing and non-capturing, is 200.  all subpatterns, both capturing and non-capturing, is 200.
956    
957    As a convenient shorthand, if any option settings are required at the start of
958  .SH BACK REFERENCES  a non-capturing subpattern, the option letters may appear between the "?" and
959  Outside a character class, a backslash followed by a digit greater than 0 (and  the ":". Thus the two patterns
960  possibly further digits) is a back reference to a capturing subpattern earlier  
961  (i.e. to its left) in the pattern, provided there have been that many previous    (?i:saturday|sunday)
962  capturing left parentheses. However, if the decimal number following the    (?:(?i)saturday|sunday)
963  backslash is less than 10, it is always taken as a back reference, and causes  
964  an error if there have not been that many previous capturing left parentheses.  match exactly the same set of strings. Because alternative branches are tried
965  See the section entitled "Backslash" above for further details of the handling  from left to right, and options are not reset until the end of the subpattern
966  of digits following a backslash.  is reached, an option setting in one branch does affect subsequent branches, so
967    the above patterns match "SUNDAY" as well as "Saturday".
 A back reference matches whatever actually matched the capturing subpattern in  
 the current subject string, rather than anything matching the subpattern  
 itself. So the pattern  
   
    (sens|respons)e and \\1ibility  
   
 matches "sense and sensibility" and "response and responsibility", but not  
 "sense and responsibility".  
   
 There may be more than one back reference to the same subpattern. If a  
 subpattern has not actually been used in a particular match, then any back  
 references to it always fail. For example, the pattern  
   
   (a|(bc))\\2  
   
 always fails if it starts to match "a" rather than "bc". Because there may be  
 up to 99 back references, all digits following the backslash are taken  
 as part of a potential back reference number. If the pattern continues with a  
 digit character, then some delimiter must be used to terminate the back  
 reference. If the PCRE_EXTENDED option is set, this can be whitespace.  
 Otherwise an empty comment can be used.  
968    
969    
970  .SH REPETITION  .SH REPETITION
# Line 798  items: Line 974  items:
974    a single character, possibly escaped    a single character, possibly escaped
975    the . metacharacter    the . metacharacter
976    a character class    a character class
977    a back reference    a back reference (see next section)
978    a parenthesized subpattern    a parenthesized subpattern (unless it is an assertion - see below)
979    
980  The general repetition quantifier specifies a minimum and maximum number of  The general repetition quantifier specifies a minimum and maximum number of
981  permitted matches, by giving the two numbers in curly brackets (braces),  permitted matches, by giving the two numbers in curly brackets (braces),
# Line 821  matches at least 3 successive vowels, bu Line 997  matches at least 3 successive vowels, bu
997    
998  matches exactly 8 digits. An opening curly bracket that appears in a position  matches exactly 8 digits. An opening curly bracket that appears in a position
999  where a quantifier is not allowed, or one that does not match the syntax of a  where a quantifier is not allowed, or one that does not match the syntax of a
1000  quantifier, is taken as a literal character. For example, "{,6}" is not a  quantifier, is taken as a literal character. For example, {,6} is not a
1001  quantifier, but a literal string of four characters.  quantifier, but a literal string of four characters.
1002    
1003  The quantifier {0} is permitted, causing the expression to behave as if the  The quantifier {0} is permitted, causing the expression to behave as if the
# Line 834  quantifiers have single-character abbrev Line 1010  quantifiers have single-character abbrev
1010    +    is equivalent to {1,}    +    is equivalent to {1,}
1011    ?    is equivalent to {0,1}    ?    is equivalent to {0,1}
1012    
1013    It is possible to construct infinite loops by following a subpattern that can
1014    match no characters with a quantifier that has no upper limit, for example:
1015    
1016      (a?)*
1017    
1018    Earlier versions of Perl and PCRE used to give an error at compile time for
1019    such patterns. However, because there are cases where this can be useful, such
1020    patterns are now accepted, but if any repetition of the subpattern does in fact
1021    match no characters, the loop is forcibly broken.
1022    
1023  By default, the quantifiers are "greedy", that is, they match as much as  By default, the quantifiers are "greedy", that is, they match as much as
1024  possible (up to the maximum number of permitted times), without causing the  possible (up to the maximum number of permitted times), without causing the
1025  rest of the pattern to fail. The classic example of where this gives problems  rest of the pattern to fail. The classic example of where this gives problems
# Line 861  quantifiers is not otherwise changed, ju Line 1047  quantifiers is not otherwise changed, ju
1047  Do not confuse this use of question mark with its use as a quantifier in its  Do not confuse this use of question mark with its use as a quantifier in its
1048  own right. Because it has two uses, it can sometimes appear doubled, as in  own right. Because it has two uses, it can sometimes appear doubled, as in
1049    
1050     \\d??\\d    \\d??\\d
1051    
1052  which matches one digit by preference, but can match two if that is the only  which matches one digit by preference, but can match two if that is the only
1053  way the rest of the pattern matches.  way the rest of the pattern matches.
1054    
1055  When a parenthesized subpattern is quantified a with minimum repeat count that  If the PCRE_UNGREEDY option is set (an option which is not available in Perl)
1056    then the quantifiers are not greedy by default, but individual ones can be made
1057    greedy by following them with a question mark. In other words, it inverts the
1058    default behaviour.
1059    
1060    When a parenthesized subpattern is quantified with a minimum repeat count that
1061  is greater than 1 or with a limited maximum, more store is required for the  is greater than 1 or with a limited maximum, more store is required for the
1062  compiled pattern, in proportion to the size of the minimum or maximum.  compiled pattern, in proportion to the size of the minimum or maximum.
1063    
1064  If a pattern starts with .* then it is implicitly anchored, since whatever  If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
1065  follows will be tried against every character position in the subject string.  to Perl's /s) is set, thus allowing the . to match newlines, then the pattern
1066  PCRE treats this as though it were preceded by \\A.  is implicitly anchored, because whatever follows will be tried against every
1067    character position in the subject string, so there is no point in retrying the
1068    overall match at any position after the first. PCRE treats such a pattern as
1069    though it were preceded by \\A. In cases where it is known that the subject
1070    string contains no newlines, it is worth setting PCRE_DOTALL when the pattern
1071    begins with .* in order to obtain this optimization, or alternatively using ^
1072    to indicate anchoring explicitly.
1073    
1074  When a capturing subpattern is repeated, the value captured is the substring  When a capturing subpattern is repeated, the value captured is the substring
1075  that matched the final iteration. For example,  that matched the final iteration. For example, after
1076    
1077      (tweedle[dume]{3}\\s*)+
1078    
1079    has matched "tweedledum tweedledee" the value of the captured substring is
1080    "tweedledee". However, if there are nested capturing subpatterns, the
1081    corresponding captured values may have been set in previous iterations. For
1082    example, after
1083    
1084      /(a|(b))+/
1085    
1086    matches "aba" the value of the second captured substring is "b".
1087    
1088    
1089     (\s*tweedle[dume]{3})+\\1  .SH BACK REFERENCES
1090    Outside a character class, a backslash followed by a digit greater than 0 (and
1091    possibly further digits) is a back reference to a capturing subpattern earlier
1092    (i.e. to its left) in the pattern, provided there have been that many previous
1093    capturing left parentheses.
1094    
1095    However, if the decimal number following the backslash is less than 10, it is
1096    always taken as a back reference, and causes an error only if there are not
1097    that many capturing left parentheses in the entire pattern. In other words, the
1098    parentheses that are referenced need not be to the left of the reference for
1099    numbers less than 10. See the section entitled "Backslash" above for further
1100    details of the handling of digits following a backslash.
1101    
1102  matches "tweedledum tweedledee tweedledee" but not "tweedledum tweedledee  A back reference matches whatever actually matched the capturing subpattern in
1103  tweedledum".  the current subject string, rather than anything matching the subpattern
1104    itself. So the pattern
1105    
1106      (sens|respons)e and \\1ibility
1107    
1108    matches "sense and sensibility" and "response and responsibility", but not
1109    "sense and responsibility". If caseful matching is in force at the time of the
1110    back reference, then the case of letters is relevant. For example,
1111    
1112      ((?i)rah)\\s+\\1
1113    
1114    matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
1115    capturing subpattern is matched caselessly.
1116    
1117    There may be more than one back reference to the same subpattern. If a
1118    subpattern has not actually been used in a particular match, then any back
1119    references to it always fail. For example, the pattern
1120    
1121      (a|(bc))\\2
1122    
1123    always fails if it starts to match "a" rather than "bc". Because there may be
1124    up to 99 back references, all digits following the backslash are taken
1125    as part of a potential back reference number. If the pattern continues with a
1126    digit character, then some delimiter must be used to terminate the back
1127    reference. If the PCRE_EXTENDED option is set, this can be whitespace.
1128    Otherwise an empty comment can be used.
1129    
1130    A back reference that occurs inside the parentheses to which it refers fails
1131    when the subpattern is first used, so, for example, (a\\1) never matches.
1132    However, such references can be useful inside repeated subpatterns. For
1133    example, the pattern
1134    
1135      (a|b\\1)+
1136    
1137    matches any number of "a"s and also "aba", "ababaa" etc. At each iteration of
1138    the subpattern, the back reference matches the character string corresponding
1139    to the previous iteration. In order for this to work, the pattern must be such
1140    that the first iteration does not need to match the back reference. This can be
1141    done using alternation, as in the example above, or by a quantifier with a
1142    minimum of zero.
1143    
1144    
1145  .SH ASSERTIONS  .SH ASSERTIONS
1146  An assertion is a test on the characters following the current matching point  An assertion is a test on the characters following or preceding the current
1147  that does not actually consume any of those characters. The simple assertions  matching point that does not actually consume any characters. The simple
1148  coded as \\b, \\B, \\A, \\Z, ^ and $ are described above. More complicated  assertions coded as \\b, \\B, \\A, \\Z, \\z, ^ and $ are described above. More
1149  assertions are coded as subpatterns starting with (?= for positive assertions,  complicated assertions are coded as subpatterns. There are two kinds: those
1150  and (?! for negative assertions. For example,  that look ahead of the current position in the subject string, and those that
1151    look behind it.
1152    
1153    An assertion subpattern is matched in the normal way, except that it does not
1154    cause the current matching position to be changed. Lookahead assertions start
1155    with (?= for positive assertions and (?! for negative assertions. For example,
1156    
1157    \\w+(?=;)    \\w+(?=;)
1158    
# Line 904  apparently similar pattern Line 1168  apparently similar pattern
1168    
1169  does not find an occurrence of "bar" that is preceded by something other than  does not find an occurrence of "bar" that is preceded by something other than
1170  "foo"; it finds any occurrence of "bar" whatsoever, because the assertion  "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
1171  (?!foo) is always true when the next three characters are "bar".  (?!foo) is always true when the next three characters are "bar". A
1172    lookbehind assertion is needed to achieve this effect.
1173    
1174    Lookbehind assertions start with (?<= for positive assertions and (?<! for
1175    negative assertions. For example,
1176    
1177      (?<!foo)bar
1178    
1179    does find an occurrence of "bar" that is not preceded by "foo". The contents of
1180    a lookbehind assertion are restricted such that all the strings it matches must
1181    have a fixed length. However, if there are several alternatives, they do not
1182    all have to have the same fixed length. Thus
1183    
1184      (?<=bullock|donkey)
1185    
1186    is permitted, but
1187    
1188      (?<!dogs?|cats?)
1189    
1190    causes an error at compile time. Branches that match different length strings
1191    are permitted only at the top level of a lookbehind assertion. This is an
1192    extension compared with Perl 5.005, which requires all branches to match the
1193    same length of string. An assertion such as
1194    
1195      (?<=ab(c|de))
1196    
1197    is not permitted, because its single top-level branch can match two different
1198    lengths, but it is acceptable if rewritten to use two top-level branches:
1199    
1200      (?<=abc|abde)
1201    
1202    The implementation of lookbehind assertions is, for each alternative, to
1203    temporarily move the current position back by the fixed width and then try to
1204    match. If there are insufficient characters before the current position, the
1205    match is deemed to fail. Lookbehinds in conjunction with once-only subpatterns
1206    can be particularly useful for matching at the ends of strings; an example is
1207    given at the end of the section on once-only subpatterns.
1208    
1209    Several assertions (of any sort) may occur in succession. For example,
1210    
1211      (?<=\\d{3})(?<!999)foo
1212    
1213    matches "foo" preceded by three digits that are not "999". Furthermore,
1214    assertions can be nested in any combination. For example,
1215    
1216      (?<=(?<!foo)bar)baz
1217    
1218    matches an occurrence of "baz" that is preceded by "bar" which in turn is not
1219    preceded by "foo".
1220    
1221  Assertion subpatterns are not capturing subpatterns, and may not be repeated,  Assertion subpatterns are not capturing subpatterns, and may not be repeated,
1222  because it makes no sense to assert the same thing several times. If an  because it makes no sense to assert the same thing several times. If an
# Line 917  Assertions count towards the maximum of Line 1229  Assertions count towards the maximum of
1229    
1230    
1231  .SH ONCE-ONLY SUBPATTERNS  .SH ONCE-ONLY SUBPATTERNS
 The facility described in this section is available only when the PCRE_EXTRA  
 option is set at compile time. It is an extension to Perl regular expressions.  
   
1232  With both maximizing and minimizing repetition, failure of what follows  With both maximizing and minimizing repetition, failure of what follows
1233  normally causes the repeated item to be re-evaluated to see if a different  normally causes the repeated item to be re-evaluated to see if a different
1234  number of repeats allows the rest of the pattern to match. Sometimes it is  number of repeats allows the rest of the pattern to match. Sometimes it is
1235  useful to prevent this, either to change the nature of the match, or to cause  useful to prevent this, either to change the nature of the match, or to cause
1236  it fail earlier than it otherwise might when the author or the pattern knows  it fail earlier than it otherwise might, when the author of the pattern knows
1237  there is no point in carrying on.  there is no point in carrying on.
1238    
1239  Consider, for example, the pattern \\d+foo when applied to the subject line  Consider, for example, the pattern \\d+foo when applied to the subject line
1240    
1241     123456bar    123456bar
1242    
1243  After matching all 6 digits and then failing to match "foo", the normal  After matching all 6 digits and then failing to match "foo", the normal
1244  action of the matcher is to try again with only 5 digits matching the \\d+  action of the matcher is to try again with only 5 digits matching the \\d+
# Line 939  has matched, it is not to be re-evaluate Line 1248  has matched, it is not to be re-evaluate
1248  give up immediately on failing to match "foo" the first time. The notation is  give up immediately on failing to match "foo" the first time. The notation is
1249  another kind of special parenthesis, starting with (?> as in this example:  another kind of special parenthesis, starting with (?> as in this example:
1250    
1251    (?>\d+)bar    (?>\\d+)bar
1252    
1253  This kind of parenthesis "locks up" the  part of the pattern it contains once  This kind of parenthesis "locks up" the  part of the pattern it contains once
1254  it has matched, and a failure further into the pattern is prevented from  it has matched, and a failure further into the pattern is prevented from
1255  backtracking into it. Backtracking past it to previous items, however, works as  backtracking into it. Backtracking past it to previous items, however, works as
1256  normal.  normal.
1257    
1258  For simple cases such as the above example, this feature can be though of as a  An alternative description is that a subpattern of this type matches the string
1259  maximizing repeat that must swallow everything it can. So, while both \\d+ and  of characters that an identical standalone pattern would match, if anchored at
1260  \\d+? are prepared to adjust the number of digits they match in order to make  the current point in the subject string.
1261  the rest of the pattern match, (?>\\d+) can only match an entire sequence of  
1262  digits.  Once-only subpatterns are not capturing subpatterns. Simple cases such as the
1263    above example can be thought of as a maximizing repeat that must swallow
1264    everything it can. So, while both \\d+ and \\d+? are prepared to adjust the
1265    number of digits they match in order to make the rest of the pattern match,
1266    (?>\\d+) can only match an entire sequence of digits.
1267    
1268  This construction can of course contain arbitrarily complicated subpatterns,  This construction can of course contain arbitrarily complicated subpatterns,
1269  and it can be nested. Contrast with the \\X assertion, which is a Prolog-like  and it can be nested.
1270  "cut".  
1271    Once-only subpatterns can be used in conjunction with lookbehind assertions to
1272    specify efficient matching at the end of the subject string. Consider a simple
1273    pattern such as
1274    
1275      abcd$
1276    
1277    when applied to a long string which does not match it. Because matching
1278    proceeds from left to right, PCRE will look for each "a" in the subject and
1279    then see if what follows matches the rest of the pattern. If the pattern is
1280    specified as
1281    
1282      ^.*abcd$
1283    
1284    then the initial .* matches the entire string at first, but when this fails, it
1285    backtracks to match all but the last character, then all but the last two
1286    characters, and so on. Once again the search for "a" covers the entire string,
1287    from right to left, so we are no better off. However, if the pattern is written
1288    as
1289    
1290      ^(?>.*)(?<=abcd)
1291    
1292    then there can be no backtracking for the .* item; it can match only the entire
1293    string. The subsequent lookbehind assertion does a single test on the last four
1294    characters. If it fails, the match fails immediately. For long strings, this
1295    approach makes a significant difference to the processing time.
1296    
1297    
1298    .SH CONDITIONAL SUBPATTERNS
1299    It is possible to cause the matching process to obey a subpattern
1300    conditionally or to choose between two alternative subpatterns, depending on
1301    the result of an assertion, or whether a previous capturing subpattern matched
1302    or not. The two possible forms of conditional subpattern are
1303    
1304      (?(condition)yes-pattern)
1305      (?(condition)yes-pattern|no-pattern)
1306    
1307    If the condition is satisfied, the yes-pattern is used; otherwise the
1308    no-pattern (if present) is used. If there are more than two alternatives in the
1309    subpattern, a compile-time error occurs.
1310    
1311    There are two kinds of condition. If the text between the parentheses consists
1312    of a sequence of digits, then the condition is satisfied if the capturing
1313    subpattern of that number has previously matched. Consider the following
1314    pattern, which contains non-significant white space to make it more readable
1315    (assume the PCRE_EXTENDED option) and to divide it into three parts for ease
1316    of discussion:
1317    
1318      ( \\( )?    [^()]+    (?(1) \\) )
1319    
1320    The first part matches an optional opening parenthesis, and if that
1321    character is present, sets it as the first captured substring. The second part
1322    matches one or more characters that are not parentheses. The third part is a
1323    conditional subpattern that tests whether the first set of parentheses matched
1324    or not. If they did, that is, if subject started with an opening parenthesis,
1325    the condition is true, and so the yes-pattern is executed and a closing
1326    parenthesis is required. Otherwise, since no-pattern is not present, the
1327    subpattern matches nothing. In other words, this pattern matches a sequence of
1328    non-parentheses, optionally enclosed in parentheses.
1329    
1330    If the condition is not a sequence of digits, it must be an assertion. This may
1331    be a positive or negative lookahead or lookbehind assertion. Consider this
1332    pattern, again containing non-significant white space, and with the two
1333    alternatives on the second line:
1334    
1335      (?(?=[^a-z]*[a-z])
1336      \\d{2}[a-z]{3}-\\d{2}  |  \\d{2}-\\d{2}-\\d{2} )
1337    
1338    The condition is a positive lookahead assertion that matches an optional
1339    sequence of non-letters followed by a letter. In other words, it tests for the
1340    presence of at least one letter in the subject. If a letter is found, the
1341    subject is matched against the first alternative; otherwise it is matched
1342    against the second. This pattern matches strings in one of the two forms
1343    dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
1344    
1345    
1346  .SH COMMENTS  .SH COMMENTS
# Line 967  character class introduces a comment tha Line 1353  character class introduces a comment tha
1353  character in the pattern.  character in the pattern.
1354    
1355    
 .SH INTERNAL FLAG SETTING  
 If the sequence (?i) occurs anywhere in a pattern, it has the effect of setting  
 the PCRE_CASELESS option, that is, all letters are matched in a  
 case-independent manner. The option applies to the whole pattern, not just to  
 the portion that follows it.  
   
 If the sequence (?m) occurs anywhere in a pattern, it has the effect of setting  
 the PCRE_MULTILINE option, that is, subject strings matched by this pattern are  
 treated as consisting of multiple lines.  
   
 If the sequence (?s) occurs anywhere in a pattern, it has the effect of setting  
 the PCRE_DOTALL option, so that dot metacharacters match newlines as well as  
 all other characters.  
   
 If the sequence (?x) occurs anywhere in a pattern, it has the effect of setting  
 the PCRE_EXTENDED option, that is, whitespace is ignored and # introduces a  
 comment that lasts till the next newline. The option applies to the whole  
 pattern, not just to the portion that follows it.  
   
 If more than one option is required, they can be specified jointly, for example  
 as (?ix) or (?mi).  
   
   
1356  .SH PERFORMANCE  .SH PERFORMANCE
1357  Certain items that may appear in patterns are more efficient than others. It is  Certain items that may appear in patterns are more efficient than others. It is
1358  more efficient to use a character class like [aeiou] than a set of alternatives  more efficient to use a character class like [aeiou] than a set of alternatives
# Line 998  required behaviour is usually the most e Line 1361  required behaviour is usually the most e
1361  contains a lot of discussion about optimizing regular expressions for efficient  contains a lot of discussion about optimizing regular expressions for efficient
1362  performance.  performance.
1363    
1364  The use of PCRE_MULTILINE causes additional processing and should be avoided  When a pattern begins with .* and the PCRE_DOTALL option is set, the pattern is
1365  when it is not necessary. Caseless matching of character classes is more  implicitly anchored by PCRE, since it can match only at the start of a subject
1366  efficient if PCRE_CASELESS is set when the pattern is compiled.  string. However, if PCRE_DOTALL is not set, PCRE cannot make this optimization,
1367    because the . metacharacter does not then match a newline, and if the subject
1368    string contains newlines, the pattern may match from the character immediately
1369    following one of them instead of from the very start. For example, the pattern
1370    
1371       (.*) second
1372    
1373    matches the subject "first\\nand second" (where \\n stands for a newline
1374    character) with the first captured substring being "and". In order to do this,
1375    PCRE has to retry the match starting after every newline in the subject.
1376    
1377    If you are using such a pattern with subject strings that do not contain
1378    newlines, the best performance is obtained by setting PCRE_DOTALL, or starting
1379    the pattern with ^.* to indicate explicit anchoring. That saves PCRE from
1380    having to scan along the subject looking for a newline to restart at.
1381    
1382  .SH AUTHOR  .SH AUTHOR
1383  Philip Hazel <ph10@cam.ac.uk>  Philip Hazel <ph10@cam.ac.uk>
# Line 1014  Cambridge CB2 3QG, England. Line 1390  Cambridge CB2 3QG, England.
1390  .br  .br
1391  Phone: +44 1223 334714  Phone: +44 1223 334714
1392    
1393  Copyright (c) 1997 University of Cambridge.  Copyright (c) 1997-1999 University of Cambridge.

Legend:
Removed from v.3  
changed lines
  Added in v.33

  ViewVC Help
Powered by ViewVC 1.1.5