Parent Directory
|
Revision Log
|
Patch
revision 49 by nigel, Sat Feb 24 21:39:33 2007 UTC | revision 75 by nigel, Sat Feb 24 21:40:37 2007 UTC | |
---|---|---|
# | Line 1 | Line 1 |
1 | ----------------------------------------------------------------------------- | |
2 | This file contains a concatenation of the PCRE man pages, converted to plain | |
3 | text format for ease of searching with a text editor, or for use on systems | |
4 | that do not have a man page processor. The small individual files that give | |
5 | synopses of each function in the library have not been included. There are | |
6 | separate text files for the pcregrep and pcretest commands. | |
7 | ----------------------------------------------------------------------------- | |
8 | ||
9 | PCRE(3) PCRE(3) | |
10 | ||
11 | ||
12 | ||
13 | NAME | NAME |
14 | pcre - Perl-compatible regular expressions. | PCRE - Perl-compatible regular expressions |
15 | ||
16 | INTRODUCTION | |
17 | ||
18 | The PCRE library is a set of functions that implement regular expres- | |
19 | sion pattern matching using the same syntax and semantics as Perl, with | |
20 | just a few differences. The current implementation of PCRE (release | |
21 | 5.x) corresponds approximately with Perl 5.8, including support for | |
22 | UTF-8 encoded strings and Unicode general category properties. However, | |
23 | this support has to be explicitly enabled; it is not the default. | |
24 | ||
25 | PCRE is written in C and released as a C library. A number of people | |
26 | have written wrappers and interfaces of various kinds. A C++ class is | |
27 | included in these contributions, which can be found in the Contrib | |
28 | directory at the primary FTP site, which is: | |
29 | ||
30 | ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre | |
31 | ||
32 | Details of exactly which Perl regular expression features are and are | |
33 | not supported by PCRE are given in separate documents. See the pcrepat- | |
34 | tern and pcrecompat pages. | |
35 | ||
36 | Some features of PCRE can be included, excluded, or changed when the | |
37 | library is built. The pcre_config() function makes it possible for a | |
38 | client to discover which features are available. The features them- | |
39 | selves are described in the pcrebuild page. Documentation about build- | |
40 | ing PCRE for various operating systems can be found in the README file | |
41 | in the source distribution. | |
42 | ||
43 | ||
44 | USER DOCUMENTATION | |
45 | ||
46 | The user documentation for PCRE comprises a number of different sec- | |
47 | tions. In the "man" format, each of these is a separate "man page". In | |
48 | the HTML format, each is a separate page, linked from the index page. | |
49 | In the plain text format, all the sections are concatenated, for ease | |
50 | of searching. The sections are as follows: | |
51 | ||
52 | pcre this document | |
53 | pcreapi details of PCRE's native API | |
54 | pcrebuild options for building PCRE | |
55 | pcrecallout details of the callout feature | |
56 | pcrecompat discussion of Perl compatibility | |
57 | pcregrep description of the pcregrep command | |
58 | pcrepartial details of the partial matching facility | |
59 | pcrepattern syntax and semantics of supported | |
60 | regular expressions | |
61 | pcreperform discussion of performance issues | |
62 | pcreposix the POSIX-compatible API | |
63 | pcreprecompile details of saving and re-using precompiled patterns | |
64 | pcresample discussion of the sample program | |
65 | pcretest description of the pcretest testing command | |
66 | ||
67 | In addition, in the "man" and HTML formats, there is a short page for | |
68 | each library function, listing its arguments and results. | |
69 | ||
70 | ||
71 | SYNOPSIS | LIMITATIONS |
72 | #include <pcre.h> | |
73 | There are some size limitations in PCRE but it is hoped that they will | |
74 | never in practice be relevant. | |
75 | ||
76 | pcre *pcre_compile(const char *pattern, int options, | The maximum length of a compiled pattern is 65539 (sic) bytes if PCRE |
77 | const char **errptr, int *erroffset, | is compiled with the default internal linkage size of 2. If you want to |
78 | const unsigned char *tableptr); | process regular expressions that are truly enormous, you can compile |
79 | PCRE with an internal linkage size of 3 or 4 (see the README file in | |
80 | the source distribution and the pcrebuild documentation for details). | |
81 | In these cases the limit is substantially larger. However, the speed | |
82 | of execution will be slower. | |
83 | ||
84 | All values in repeating quantifiers must be less than 65536. The maxi- | |
85 | mum number of capturing subpatterns is 65535. | |
86 | ||
87 | There is no limit to the number of non-capturing subpatterns, but the | |
88 | maximum depth of nesting of all kinds of parenthesized subpattern, | |
89 | including capturing subpatterns, assertions, and other types of subpat- | |
90 | tern, is 200. | |
91 | ||
92 | The maximum length of a subject string is the largest positive number | |
93 | that an integer variable can hold. However, PCRE uses recursion to han- | |
94 | dle subpatterns and indefinite repetition. This means that the avail- | |
95 | able stack space may limit the size of a subject string that can be | |
96 | processed by certain patterns. | |
97 | ||
98 | ||
99 | UTF-8 AND UNICODE PROPERTY SUPPORT | |
100 | ||
101 | From release 3.3, PCRE has had some support for character strings | |
102 | encoded in the UTF-8 format. For release 4.0 this was greatly extended | |
103 | to cover most common requirements, and in release 5.0 additional sup- | |
104 | port for Unicode general category properties was added. | |
105 | ||
106 | In order process UTF-8 strings, you must build PCRE to include UTF-8 | |
107 | support in the code, and, in addition, you must call pcre_compile() | |
108 | with the PCRE_UTF8 option flag. When you do this, both the pattern and | |
109 | any subject strings that are matched against it are treated as UTF-8 | |
110 | strings instead of just strings of bytes. | |
111 | ||
112 | If you compile PCRE with UTF-8 support, but do not use it at run time, | |
113 | the library will be a bit bigger, but the additional run time overhead | |
114 | is limited to testing the PCRE_UTF8 flag in several places, so should | |
115 | not be very large. | |
116 | ||
117 | If PCRE is built with Unicode character property support (which implies | |
118 | UTF-8 support), the escape sequences \p{..}, \P{..}, and \X are sup- | |
119 | ported. The available properties that can be tested are limited to the | |
120 | general category properties such as Lu for an upper case letter or Nd | |
121 | for a decimal number. A full list is given in the pcrepattern documen- | |
122 | tation. The PCRE library is increased in size by about 90K when Unicode | |
123 | property support is included. | |
124 | ||
125 | The following comments apply when PCRE is running in UTF-8 mode: | |
126 | ||
127 | 1. When you set the PCRE_UTF8 flag, the strings passed as patterns and | |
128 | subjects are checked for validity on entry to the relevant functions. | |
129 | If an invalid UTF-8 string is passed, an error return is given. In some | |
130 | situations, you may already know that your strings are valid, and | |
131 | therefore want to skip these checks in order to improve performance. If | |
132 | you set the PCRE_NO_UTF8_CHECK flag at compile time or at run time, | |
133 | PCRE assumes that the pattern or subject it is given (respectively) | |
134 | contains only valid UTF-8 codes. In this case, it does not diagnose an | |
135 | invalid UTF-8 string. If you pass an invalid UTF-8 string to PCRE when | |
136 | PCRE_NO_UTF8_CHECK is set, the results are undefined. Your program may | |
137 | crash. | |
138 | ||
139 | 2. In a pattern, the escape sequence \x{...}, where the contents of the | |
140 | braces is a string of hexadecimal digits, is interpreted as a UTF-8 | |
141 | character whose code number is the given hexadecimal number, for exam- | |
142 | ple: \x{1234}. If a non-hexadecimal digit appears between the braces, | |
143 | the item is not recognized. This escape sequence can be used either as | |
144 | a literal, or within a character class. | |
145 | ||
146 | 3. The original hexadecimal escape sequence, \xhh, matches a two-byte | |
147 | UTF-8 character if the value is greater than 127. | |
148 | ||
149 | 4. Repeat quantifiers apply to complete UTF-8 characters, not to indi- | |
150 | vidual bytes, for example: \x{100}{3}. | |
151 | ||
152 | 5. The dot metacharacter matches one UTF-8 character instead of a sin- | |
153 | gle byte. | |
154 | ||
155 | 6. The escape sequence \C can be used to match a single byte in UTF-8 | |
156 | mode, but its use can lead to some strange effects. | |
157 | ||
158 | 7. The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly | |
159 | test characters of any code value, but the characters that PCRE recog- | |
160 | nizes as digits, spaces, or word characters remain the same set as | |
161 | before, all with values less than 256. This remains true even when PCRE | |
162 | includes Unicode property support, because to do otherwise would slow | |
163 | down PCRE in many common cases. If you really want to test for a wider | |
164 | sense of, say, "digit", you must use Unicode property tests such as | |
165 | \p{Nd}. | |
166 | ||
167 | 8. Similarly, characters that match the POSIX named character classes | |
168 | are all low-valued characters. | |
169 | ||
170 | 9. Case-insensitive matching applies only to characters whose values | |
171 | are less than 128, unless PCRE is built with Unicode property support. | |
172 | Even when Unicode property support is available, PCRE still uses its | |
173 | own character tables when checking the case of low-valued characters, | |
174 | so as not to degrade performance. The Unicode property information is | |
175 | used only for characters with higher values. | |
176 | ||
pcre_extra *pcre_study(const pcre *code, int options, | ||
const char **errptr); | ||
177 | ||
178 | int pcre_exec(const pcre *code, const pcre_extra *extra, | AUTHOR |
const char *subject, int length, int startoffset, | ||
int options, int *ovector, int ovecsize); | ||
179 | ||
180 | int pcre_copy_substring(const char *subject, int *ovector, | Philip Hazel <ph10@cam.ac.uk> |
181 | int stringcount, int stringnumber, char *buffer, | University Computing Service, |
182 | int buffersize); | Cambridge CB2 3QG, England. |
183 | Phone: +44 1223 334714 | |
184 | ||
185 | Last updated: 09 September 2004 | |
186 | Copyright (c) 1997-2004 University of Cambridge. | |
187 | ----------------------------------------------------------------------------- | |
188 | ||
189 | int pcre_get_substring(const char *subject, int *ovector, | PCRE(3) PCRE(3) |
int stringcount, int stringnumber, | ||
const char **stringptr); | ||
190 | ||
int pcre_get_substring_list(const char *subject, | ||
int *ovector, int stringcount, const char ***listptr); | ||
191 | ||
void pcre_free_substring(const char *stringptr); | ||
192 | ||
193 | void pcre_free_substring_list(const char **stringptr); | NAME |
194 | PCRE - Perl-compatible regular expressions | |
195 | ||
196 | const unsigned char *pcre_maketables(void); | PCRE BUILD-TIME OPTIONS |
197 | ||
198 | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, | This document describes the optional features of PCRE that can be |
199 | int what, void *where); | selected when the library is compiled. They are all selected, or dese- |
200 | lected, by providing options to the configure script that is run before | |
201 | the make command. The complete list of options for configure (which | |
202 | includes the standard ones such as the selection of the installation | |
203 | directory) can be obtained by running | |
204 | ||
205 | ./configure --help | |
206 | ||
207 | The following sections describe certain options whose names begin with | |
208 | --enable or --disable. These settings specify changes to the defaults | |
209 | for the configure command. Because of the way that configure works, | |
210 | --enable and --disable always come in pairs, so the complementary | |
211 | option always exists as well, but as it specifies the default, it is | |
212 | not described. | |
213 | ||
int pcre_info(const pcre *code, int *optptr, *firstcharptr); | ||
214 | ||
215 | char *pcre_version(void); | UTF-8 SUPPORT |
216 | ||
217 | void *(*pcre_malloc)(size_t); | To build PCRE with support for UTF-8 character strings, add |
218 | ||
219 | void (*pcre_free)(void *); | --enable-utf8 |
220 | ||
221 | to the configure command. Of itself, this does not make PCRE treat | |
222 | strings as UTF-8. As well as compiling PCRE with this option, you also | |
223 | have have to set the PCRE_UTF8 option when you call the pcre_compile() | |
224 | function. | |
225 | ||
226 | ||
227 | UNICODE CHARACTER PROPERTY SUPPORT | |
228 | ||
229 | DESCRIPTION | UTF-8 support allows PCRE to process character values greater than 255 |
230 | The PCRE library is a set of functions that implement regu- | in the strings that it handles. On its own, however, it does not pro- |
231 | lar expression pattern matching using the same syntax and | vide any facilities for accessing the properties of such characters. If |
232 | semantics as Perl 5, with just a few differences (see | you want to be able to use the pattern escapes \P, \p, and \X, which |
233 | refer to Unicode character properties, you must add | |
234 | ||
235 | --enable-unicode-properties | |
236 | ||
237 | to the configure command. This implies UTF-8 support, even if you have | |
238 | not explicitly requested it. | |
239 | ||
240 | Including Unicode property support adds around 90K of tables to the | |
241 | PCRE library, approximately doubling its size. Only the general cate- | |
242 | gory properties such as Lu and Nd are supported. Details are given in | |
243 | the pcrepattern documentation. | |
244 | ||
245 | ||
246 | CODE VALUE OF NEWLINE | |
247 | ||
248 | By default, PCRE treats character 10 (linefeed) as the newline charac- | |
249 | ter. This is the normal newline character on Unix-like systems. You can | |
250 | compile PCRE to use character 13 (carriage return) instead by adding | |
251 | ||
252 | --enable-newline-is-cr | |
253 | ||
254 | to the configure command. For completeness there is also a --enable- | |
255 | newline-is-lf option, which explicitly specifies linefeed as the new- | |
256 | line character. | |
257 | ||
258 | ||
259 | BUILDING SHARED AND STATIC LIBRARIES | |
260 | ||
261 | The PCRE building process uses libtool to build both shared and static | |
262 | Unix libraries by default. You can suppress one of these by adding one | |
263 | of | |
264 | ||
265 | --disable-shared | |
266 | --disable-static | |
267 | ||
268 | to the configure command, as required. | |
269 | ||
270 | ||
271 | POSIX MALLOC USAGE | |
272 | ||
273 | When PCRE is called through the POSIX interface (see the pcreposix doc- | |
274 | umentation), additional working storage is required for holding the | |
275 | pointers to capturing substrings, because PCRE requires three integers | |
276 | per substring, whereas the POSIX interface provides only two. If the | |
277 | number of expected substrings is small, the wrapper function uses space | |
278 | on the stack, because this is faster than using malloc() for each call. | |
279 | The default threshold above which the stack is no longer used is 10; it | |
280 | can be changed by adding a setting such as | |
281 | ||
282 | --with-posix-malloc-threshold=20 | |
283 | ||
284 | to the configure command. | |
285 | ||
286 | ||
287 | LIMITING PCRE RESOURCE USAGE | |
288 | ||
289 | Internally, PCRE has a function called match(), which it calls repeat- | |
290 | edly (possibly recursively) when matching a pattern. By controlling the | |
291 | maximum number of times this function may be called during a single | |
292 | matching operation, a limit can be placed on the resources used by a | |
293 | single call to pcre_exec(). The limit can be changed at run time, as | |
294 | described in the pcreapi documentation. The default is 10 million, but | |
295 | this can be changed by adding a setting such as | |
296 | ||
297 | --with-match-limit=500000 | |
298 | ||
299 | to the configure command. | |
300 | ||
301 | ||
302 | HANDLING VERY LARGE PATTERNS | |
303 | ||
304 | Within a compiled pattern, offset values are used to point from one | |
305 | part to another (for example, from an opening parenthesis to an alter- | |
306 | nation metacharacter). By default, two-byte values are used for these | |
307 | offsets, leading to a maximum size for a compiled pattern of around | |
308 | 64K. This is sufficient to handle all but the most gigantic patterns. | |
309 | Nevertheless, some people do want to process enormous patterns, so it | |
310 | is possible to compile PCRE to use three-byte or four-byte offsets by | |
311 | adding a setting such as | |
312 | ||
313 | --with-link-size=3 | |
314 | ||
315 | to the configure command. The value given must be 2, 3, or 4. Using | |
316 | longer offsets slows down the operation of PCRE because it has to load | |
317 | additional bytes when handling them. | |
318 | ||
319 | If you build PCRE with an increased link size, test 2 (and test 5 if | |
320 | you are using UTF-8) will fail. Part of the output of these tests is a | |
321 | representation of the compiled pattern, and this changes with the link | |
322 | size. | |
323 | ||
324 | ||
325 | AVOIDING EXCESSIVE STACK USAGE | |
326 | ||
327 | PCRE implements backtracking while matching by making recursive calls | |
328 | to an internal function called match(). In environments where the size | |
329 | of the stack is limited, this can severely limit PCRE's operation. (The | |
330 | Unix environment does not usually suffer from this problem.) An alter- | |
331 | native approach that uses memory from the heap to remember data, | |
332 | instead of using recursive function calls, has been implemented to work | |
333 | round this problem. If you want to build a version of PCRE that works | |
334 | this way, add | |
335 | ||
336 | --disable-stack-for-recursion | |
337 | ||
338 | to the configure command. With this configuration, PCRE will use the | |
339 | pcre_stack_malloc and pcre_stack_free variables to call memory manage- | |
340 | ment functions. Separate functions are provided because the usage is | |
341 | very predictable: the block sizes requested are always the same, and | |
342 | the blocks are always freed in reverse order. A calling program might | |
343 | be able to implement optimized functions that perform better than the | |
344 | standard malloc() and free() functions. PCRE runs noticeably more | |
345 | slowly when built in this way. | |
346 | ||
347 | ||
348 | USING EBCDIC CODE | |
349 | ||
350 | PCRE assumes by default that it will run in an environment where the | |
351 | character code is ASCII (or Unicode, which is a superset of ASCII). | |
352 | PCRE can, however, be compiled to run in an EBCDIC environment by | |
353 | adding | |
354 | ||
355 | --enable-ebcdic | |
356 | ||
357 | to the configure command. | |
358 | ||
359 | Last updated: 09 September 2004 | |
360 | Copyright (c) 1997-2004 University of Cambridge. | |
361 | ----------------------------------------------------------------------------- | |
362 | ||
363 | PCRE(3) PCRE(3) | |
364 | ||
365 | ||
366 | ||
367 | NAME | |
368 | PCRE - Perl-compatible regular expressions | |
369 | ||
370 | PCRE NATIVE API | |
371 | ||
372 | #include <pcre.h> | |
373 | ||
374 | pcre *pcre_compile(const char *pattern, int options, | |
375 | const char **errptr, int *erroffset, | |
376 | const unsigned char *tableptr); | |
377 | ||
378 | pcre_extra *pcre_study(const pcre *code, int options, | |
379 | const char **errptr); | |
380 | ||
381 | int pcre_exec(const pcre *code, const pcre_extra *extra, | |
382 | const char *subject, int length, int startoffset, | |
383 | int options, int *ovector, int ovecsize); | |
384 | ||
385 | int pcre_copy_named_substring(const pcre *code, | |
386 | const char *subject, int *ovector, | |
387 | int stringcount, const char *stringname, | |
388 | char *buffer, int buffersize); | |
389 | ||
390 | int pcre_copy_substring(const char *subject, int *ovector, | |
391 | int stringcount, int stringnumber, char *buffer, | |
392 | int buffersize); | |
393 | ||
394 | int pcre_get_named_substring(const pcre *code, | |
395 | const char *subject, int *ovector, | |
396 | int stringcount, const char *stringname, | |
397 | const char **stringptr); | |
398 | ||
399 | int pcre_get_stringnumber(const pcre *code, | |
400 | const char *name); | |
401 | ||
402 | int pcre_get_substring(const char *subject, int *ovector, | |
403 | int stringcount, int stringnumber, | |
404 | const char **stringptr); | |
405 | ||
406 | int pcre_get_substring_list(const char *subject, | |
407 | int *ovector, int stringcount, const char ***listptr); | |
408 | ||
409 | void pcre_free_substring(const char *stringptr); | |
410 | ||
411 | void pcre_free_substring_list(const char **stringptr); | |
412 | ||
413 | const unsigned char *pcre_maketables(void); | |
414 | ||
415 | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, | |
416 | int what, void *where); | |
417 | ||
418 | int pcre_info(const pcre *code, int *optptr, int *firstcharptr); | |
419 | ||
420 | int pcre_config(int what, void *where); | |
421 | ||
422 | char *pcre_version(void); | |
423 | ||
424 | void *(*pcre_malloc)(size_t); | |
425 | ||
426 | void (*pcre_free)(void *); | |
427 | ||
428 | void *(*pcre_stack_malloc)(size_t); | |
429 | ||
430 | void (*pcre_stack_free)(void *); | |
431 | ||
432 | int (*pcre_callout)(pcre_callout_block *); | |
433 | ||
434 | ||
435 | PCRE API OVERVIEW | |
436 | ||
437 | PCRE has its own native API, which is described in this document. There | |
438 | is also a set of wrapper functions that correspond to the POSIX regular | |
439 | expression API. These are described in the pcreposix documentation. | |
440 | ||
441 | The native API function prototypes are defined in the header file | |
442 | pcre.h, and on Unix systems the library itself is called libpcre. It | |
443 | can normally be accessed by adding -lpcre to the command for linking an | |
444 | application that uses PCRE. The header file defines the macros | |
445 | PCRE_MAJOR and PCRE_MINOR to contain the major and minor release num- | |
446 | bers for the library. Applications can use these to include support | |
447 | for different releases of PCRE. | |
448 | ||
449 | The functions pcre_compile(), pcre_study(), and pcre_exec() are used | |
450 | for compiling and matching regular expressions. A sample program that | |
451 | demonstrates the simplest way of using them is provided in the file | |
452 | called pcredemo.c in the source distribution. The pcresample documenta- | |
453 | tion describes how to run it. | |
454 | ||
455 | In addition to the main compiling and matching functions, there are | |
456 | convenience functions for extracting captured substrings from a matched | |
457 | subject string. They are: | |
458 | ||
459 | pcre_copy_substring() | |
460 | pcre_copy_named_substring() | |
461 | pcre_get_substring() | |
462 | pcre_get_named_substring() | |
463 | pcre_get_substring_list() | |
464 | pcre_get_stringnumber() | |
465 | ||
466 | pcre_free_substring() and pcre_free_substring_list() are also provided, | |
467 | to free the memory used for extracted strings. | |
468 | ||
469 | The function pcre_maketables() is used to build a set of character | |
470 | tables in the current locale for passing to pcre_compile() or | |
471 | pcre_exec(). This is an optional facility that is provided for spe- | |
472 | cialist use. Most commonly, no special tables are passed, in which case | |
473 | internal tables that are generated when PCRE is built are used. | |
474 | ||
475 | The function pcre_fullinfo() is used to find out information about a | |
476 | compiled pattern; pcre_info() is an obsolete version that returns only | |
477 | some of the available information, but is retained for backwards com- | |
478 | patibility. The function pcre_version() returns a pointer to a string | |
479 | containing the version of PCRE and its date of release. | |
480 | ||
481 | The global variables pcre_malloc and pcre_free initially contain the | |
482 | entry points of the standard malloc() and free() functions, respec- | |
483 | tively. PCRE calls the memory management functions via these variables, | |
484 | so a calling program can replace them if it wishes to intercept the | |
485 | calls. This should be done before calling any PCRE functions. | |
486 | ||
487 | below). The current implementation corresponds to Perl | The global variables pcre_stack_malloc and pcre_stack_free are also |
488 | 5.005, with some additional features from later versions. | indirections to memory management functions. These special functions |
489 | This includes some experimental, incomplete support for | are used only when PCRE is compiled to use the heap for remembering |
490 | UTF-8 encoded strings. Details of exactly what is and what | data, instead of recursive function calls. This is a non-standard way |
491 | is not supported are given below. | of building PCRE, for use in environments that have limited stacks. |
492 | Because of the greater use of memory management, it runs more slowly. | |
493 | Separate functions are provided so that special-purpose external code | |
494 | can be used for this case. When used, these functions are always called | |
495 | in a stack-like manner (last obtained, first freed), and always for | |
496 | memory blocks of the same size. | |
497 | ||
498 | PCRE has its own native API, which is described in this | The global variable pcre_callout initially contains NULL. It can be set |
499 | document. There is also a set of wrapper functions that | by the caller to a "callout" function, which PCRE will then call at |
500 | correspond to the POSIX regular expression API. These are | specified points during a matching operation. Details are given in the |
501 | described in the pcreposix documentation. | pcrecallout documentation. |
502 | ||
The native API function prototypes are defined in the header | ||
file pcre.h, and on Unix systems the library itself is | ||
called libpcre.a, so can be accessed by adding -lpcre to the | ||
command for linking an application which calls it. The | ||
header file defines the macros PCRE_MAJOR and PCRE_MINOR to | ||
contain the major and minor release numbers for the library. | ||
Applications can use these to include support for different | ||
releases. | ||
503 | ||
504 | The functions pcre_compile(), pcre_study(), and pcre_exec() | MULTITHREADING |
are used for compiling and matching regular expressions. | ||
505 | ||
506 | The functions pcre_copy_substring(), pcre_get_substring(), | The PCRE functions can be used in multi-threading applications, with |
507 | and pcre_get_substring_list() are convenience functions for | the proviso that the memory management functions pointed to by |
508 | extracting captured substrings from a matched subject | pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the |
509 | string; pcre_free_substring() and pcre_free_substring_list() | callout function pointed to by pcre_callout, are shared by all threads. |
are also provided, to free the memory used for extracted | ||
strings. | ||
510 | ||
511 | The function pcre_maketables() is used (optionally) to build | The compiled form of a regular expression is not altered during match- |
512 | a set of character tables in the current locale for passing | ing, so the same compiled pattern can safely be used by several threads |
513 | to pcre_compile(). | at once. |
514 | ||
The function pcre_fullinfo() is used to find out information | ||
about a compiled pattern; pcre_info() is an obsolete version | ||
which returns only some of the available information, but is | ||
retained for backwards compatibility. The function | ||
pcre_version() returns a pointer to a string containing the | ||
version of PCRE and its date of release. | ||
515 | ||
516 | The global variables pcre_malloc and pcre_free initially | SAVING PRECOMPILED PATTERNS FOR LATER USE |
contain the entry points of the standard malloc() and free() | ||
functions respectively. PCRE calls the memory management | ||
functions via these variables, so a calling program can | ||
replace them if it wishes to intercept the calls. This | ||
should be done before calling any PCRE functions. | ||
517 | ||
518 | The compiled form of a regular expression can be saved and re-used at a | |
519 | later time, possibly by a different program, and even on a host other | |
520 | than the one on which it was compiled. Details are given in the | |
521 | pcreprecompile documentation. | |
522 | ||
523 | ||
524 | MULTI-THREADING | CHECKING BUILD-TIME OPTIONS |
The PCRE functions can be used in multi-threading | ||
525 | ||
526 | int pcre_config(int what, void *where); | |
527 | ||
528 | The function pcre_config() makes it possible for a PCRE client to dis- | |
529 | cover which optional features have been compiled into the PCRE library. | |
530 | The pcrebuild documentation has more details about these optional fea- | |
531 | tures. | |
532 | ||
533 | The first argument for pcre_config() is an integer, specifying which | |
534 | information is required; the second argument is a pointer to a variable | |
535 | into which the information is placed. The following information is | |
536 | available: | |
537 | ||
538 | PCRE_CONFIG_UTF8 | |
539 | ||
540 | SunOS 5.8 Last change: 2 | The output is an integer that is set to one if UTF-8 support is avail- |
541 | able; otherwise it is set to zero. | |
542 | ||
543 | PCRE_CONFIG_UNICODE_PROPERTIES | |
544 | ||
545 | The output is an integer that is set to one if support for Unicode | |
546 | character properties is available; otherwise it is set to zero. | |
547 | ||
548 | applications, with the proviso that the memory management | PCRE_CONFIG_NEWLINE |
functions pointed to by pcre_malloc and pcre_free are shared | ||
by all threads. | ||
549 | ||
550 | The compiled form of a regular expression is not altered | The output is an integer that is set to the value of the code that is |
551 | during matching, so the same compiled pattern can safely be | used for the newline character. It is either linefeed (10) or carriage |
552 | used by several threads at once. | return (13), and should normally be the standard character for your |
553 | operating system. | |
554 | ||
555 | PCRE_CONFIG_LINK_SIZE | |
556 | ||
557 | The output is an integer that contains the number of bytes used for | |
558 | internal linkage in compiled regular expressions. The value is 2, 3, or | |
559 | 4. Larger values allow larger regular expressions to be compiled, at | |
560 | the expense of slower matching. The default value of 2 is sufficient | |
561 | for all but the most massive patterns, since it allows the compiled | |
562 | pattern to be up to 64K in size. | |
563 | ||
564 | PCRE_CONFIG_POSIX_MALLOC_THRESHOLD | |
565 | ||
566 | The output is an integer that contains the threshold above which the | |
567 | POSIX interface uses malloc() for output vectors. Further details are | |
568 | given in the pcreposix documentation. | |
569 | ||
570 | PCRE_CONFIG_MATCH_LIMIT | |
571 | ||
572 | The output is an integer that gives the default limit for the number of | |
573 | internal matching function calls in a pcre_exec() execution. Further | |
574 | details are given with pcre_exec() below. | |
575 | ||
576 | PCRE_CONFIG_STACKRECURSE | |
577 | ||
578 | The output is an integer that is set to one if internal recursion is | |
579 | implemented by recursive function calls that use the stack to remember | |
580 | their state. This is the usual way that PCRE is compiled. The output is | |
581 | zero if PCRE was compiled to use blocks of data on the heap instead of | |
582 | recursive function calls. In this case, pcre_stack_malloc and | |
583 | pcre_stack_free are called to manage memory blocks on the heap, thus | |
584 | avoiding the use of the stack. | |
585 | ||
586 | ||
587 | COMPILING A PATTERN | COMPILING A PATTERN |
The function pcre_compile() is called to compile a pattern | ||
into an internal form. The pattern is a C string terminated | ||
by a binary zero, and is passed in the argument pattern. A | ||
pointer to a single block of memory that is obtained via | ||
pcre_malloc is returned. This contains the compiled code and | ||
related data. The pcre type is defined for this for conveni- | ||
ence, but in fact pcre is just a typedef for void, since the | ||
contents of the block are not externally defined. It is up | ||
to the caller to free the memory when it is no longer | ||
required. | ||
The size of a compiled pattern is roughly proportional to | ||
the length of the pattern string, except that each character | ||
class (other than those containing just a single character, | ||
negated or not) requires 33 bytes, and repeat quantifiers | ||
with a minimum greater than one or a bounded maximum cause | ||
the relevant portions of the compiled pattern to be repli- | ||
cated. | ||
The options argument contains independent bits that affect | ||
the compilation. It should be zero if no options are | ||
required. Some of the options, in particular, those that are | ||
compatible with Perl, can also be set and unset from within | ||
the pattern (see the detailed description of regular expres- | ||
sions below). For these options, the contents of the options | ||
argument specifies their initial settings at the start of | ||
compilation and execution. The PCRE_ANCHORED option can be | ||
set at the time of matching as well as at compile time. | ||
If errptr is NULL, pcre_compile() returns NULL immediately. | ||
Otherwise, if compilation of a pattern fails, pcre_compile() | ||
returns NULL, and sets the variable pointed to by errptr to | ||
point to a textual error message. The offset from the start | ||
of the pattern to the character where the error was | ||
discovered is placed in the variable pointed to by | ||
erroffset, which must not be NULL. If it is, an immediate | ||
error is given. | ||
If the final argument, tableptr, is NULL, PCRE uses a | ||
default set of character tables which are built when it is | ||
compiled, using the default C locale. Otherwise, tableptr | ||
must be the result of a call to pcre_maketables(). See the | ||
section on locale support below. | ||
The following option bits are defined in the header file: | ||
PCRE_ANCHORED | ||
If this bit is set, the pattern is forced to be "anchored", | ||
that is, it is constrained to match only at the start of the | ||
string which is being searched (the "subject string"). This | ||
effect can also be achieved by appropriate constructs in the | ||
pattern itself, which is the only way to do it in Perl. | ||
PCRE_CASELESS | ||
If this bit is set, letters in the pattern match both upper | ||
and lower case letters. It is equivalent to Perl's /i | ||
option. | ||
PCRE_DOLLAR_ENDONLY | ||
If this bit is set, a dollar metacharacter in the pattern | ||
matches only at the end of the subject string. Without this | ||
option, a dollar also matches immediately before the final | ||
character if it is a newline (but not before any other new- | ||
lines). The PCRE_DOLLAR_ENDONLY option is ignored if | ||
PCRE_MULTILINE is set. There is no equivalent to this option | ||
in Perl. | ||
PCRE_DOTALL | ||
If this bit is set, a dot metacharater in the pattern | ||
matches all characters, including newlines. Without it, new- | ||
lines are excluded. This option is equivalent to Perl's /s | ||
option. A negative class such as [^a] always matches a new- | ||
line character, independent of the setting of this option. | ||
PCRE_EXTENDED | ||
If this bit is set, whitespace data characters in the pat- | ||
tern are totally ignored except when escaped or inside a | ||
character class, and characters between an unescaped # out- | ||
side a character class and the next newline character, | ||
inclusive, are also ignored. This is equivalent to Perl's /x | ||
option, and makes it possible to include comments inside | ||
complicated patterns. Note, however, that this applies only | ||
to data characters. Whitespace characters may never appear | ||
within special character sequences in a pattern, for example | ||
within the sequence (?( which introduces a conditional sub- | ||
pattern. | ||
PCRE_EXTRA | ||
This option was invented in order to turn on additional | ||
functionality of PCRE that is incompatible with Perl, but it | ||
is currently of very little use. When set, 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 at present no other features controlled | ||
by this option. It can also be set by a (?X) option setting | ||
within a pattern. | ||
PCRE_MULTILINE | ||
By default, PCRE treats the subject string as consisting of | ||
a single "line" of characters (even if it actually contains | ||
several newlines). The "start of line" metacharacter (^) | ||
matches only at the start of the string, while the "end of | ||
line" metacharacter ($) matches only at the end of the | ||
string, or before a terminating newline (unless | ||
PCRE_DOLLAR_ENDONLY is set). This is the same as Perl. | ||
When PCRE_MULTILINE it is set, the "start of line" and "end | ||
of line" constructs match immediately following or immedi- | ||
ately before any newline in the subject string, respec- | ||
tively, as well as at the very start and end. This is | ||
equivalent to Perl's /m option. If there are no "\n" charac- | ||
ters in a subject string, or no occurrences of ^ or $ in a | ||
pattern, setting PCRE_MULTILINE has no effect. | ||
PCRE_UNGREEDY | ||
This option inverts the "greediness" of the quantifiers so | ||
that they are not greedy by default, but become greedy if | ||
followed by "?". It is not compatible with Perl. It can also | ||
be set by a (?U) option setting within the pattern. | ||
PCRE_UTF8 | ||
This option causes PCRE to regard both the pattern and the | ||
subject as strings of UTF-8 characters instead of just byte | ||
strings. However, it is available only if PCRE has been | ||
built to include UTF-8 support. If not, the use of this | ||
option provokes an error. Support for UTF-8 is new, experi- | ||
mental, and incomplete. Details of exactly what it entails | ||
are given below. | ||
588 | ||
589 | pcre *pcre_compile(const char *pattern, int options, | |
590 | const char **errptr, int *erroffset, | |
591 | const unsigned char *tableptr); | |
592 | ||
593 | The function pcre_compile() is called to compile a pattern into an | |
594 | internal form. The pattern is a C string terminated by a binary zero, | |
595 | and is passed in the pattern argument. A pointer to a single block of | |
596 | memory that is obtained via pcre_malloc is returned. This contains the | |
597 | compiled code and related data. The pcre type is defined for the | |
598 | returned block; this is a typedef for a structure whose contents are | |
599 | not externally defined. It is up to the caller to free the memory when | |
600 | it is no longer required. | |
601 | ||
602 | Although the compiled code of a PCRE regex is relocatable, that is, it | |
603 | does not depend on memory location, the complete pcre data block is not | |
604 | fully relocatable, because it may contain a copy of the tableptr argu- | |
605 | ment, which is an address (see below). | |
606 | ||
607 | The options argument contains independent bits that affect the compila- | |
608 | tion. It should be zero if no options are required. The available | |
609 | options are described below. Some of them, in particular, those that | |
610 | are compatible with Perl, can also be set and unset from within the | |
611 | pattern (see the detailed description in the pcrepattern documenta- | |
612 | tion). For these options, the contents of the options argument speci- | |
613 | fies their initial settings at the start of compilation and execution. | |
614 | The PCRE_ANCHORED option can be set at the time of matching as well as | |
615 | at compile time. | |
616 | ||
617 | If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise, | |
618 | if compilation of a pattern fails, pcre_compile() returns NULL, and | |
619 | sets the variable pointed to by errptr to point to a textual error mes- | |
620 | sage. The offset from the start of the pattern to the character where | |
621 | the error was discovered is placed in the variable pointed to by | |
622 | erroffset, which must not be NULL. If it is, an immediate error is | |
623 | given. | |
624 | ||
625 | If the final argument, tableptr, is NULL, PCRE uses a default set of | |
626 | character tables that are built when PCRE is compiled, using the | |
627 | default C locale. Otherwise, tableptr must be an address that is the | |
628 | result of a call to pcre_maketables(). This value is stored with the | |
629 | compiled pattern, and used again by pcre_exec(), unless another table | |
630 | pointer is passed to it. For more discussion, see the section on locale | |
631 | support below. | |
632 | ||
633 | This code fragment shows a typical straightforward call to pcre_com- | |
634 | pile(): | |
635 | ||
636 | pcre *re; | |
637 | const char *error; | |
638 | int erroffset; | |
639 | re = pcre_compile( | |
640 | "^A.*Z", /* the pattern */ | |
641 | 0, /* default options */ | |
642 | &error, /* for error message */ | |
643 | &erroffset, /* for error offset */ | |
644 | NULL); /* use default character tables */ | |
645 | ||
646 | The following names for option bits are defined in the pcre.h header | |
647 | file: | |
648 | ||
649 | PCRE_ANCHORED | |
650 | ||
651 | If this bit is set, the pattern is forced to be "anchored", that is, it | |
652 | is constrained to match only at the first matching point in the string | |
653 | that is being searched (the "subject string"). This effect can also be | |
654 | achieved by appropriate constructs in the pattern itself, which is the | |
655 | only way to do it in Perl. | |
656 | ||
657 | PCRE_AUTO_CALLOUT | |
658 | ||
659 | If this bit is set, pcre_compile() automatically inserts callout items, | |
660 | all with number 255, before each pattern item. For discussion of the | |
661 | callout facility, see the pcrecallout documentation. | |
662 | ||
663 | PCRE_CASELESS | |
664 | ||
665 | If this bit is set, letters in the pattern match both upper and lower | |
666 | case letters. It is equivalent to Perl's /i option, and it can be | |
667 | changed within a pattern by a (?i) option setting. When running in | |
668 | UTF-8 mode, case support for high-valued characters is available only | |
669 | when PCRE is built with Unicode character property support. | |
670 | ||
671 | PCRE_DOLLAR_ENDONLY | |
672 | ||
673 | If this bit is set, a dollar metacharacter in the pattern matches only | |
674 | at the end of the subject string. Without this option, a dollar also | |
675 | matches immediately before the final character if it is a newline (but | |
676 | not before any other newlines). The PCRE_DOLLAR_ENDONLY option is | |
677 | ignored if PCRE_MULTILINE is set. There is no equivalent to this option | |
678 | in Perl, and no way to set it within a pattern. | |
679 | ||
680 | PCRE_DOTALL | |
681 | ||
682 | If this bit is set, a dot metacharater in the pattern matches all char- | |
683 | acters, including newlines. Without it, newlines are excluded. This | |
684 | option is equivalent to Perl's /s option, and it can be changed within | |
685 | a pattern by a (?s) option setting. A negative class such as [^a] | |
686 | always matches a newline character, independent of the setting of this | |
687 | option. | |
688 | ||
689 | PCRE_EXTENDED | |
690 | ||
691 | If this bit is set, whitespace data characters in the pattern are | |
692 | totally ignored except when escaped or inside a character class. | |
693 | Whitespace does not include the VT character (code 11). In addition, | |
694 | characters between an unescaped # outside a character class and the | |
695 | next newline character, inclusive, are also ignored. This is equivalent | |
696 | to Perl's /x option, and it can be changed within a pattern by a (?x) | |
697 | option setting. | |
698 | ||
699 | This option makes it possible to include comments inside complicated | |
700 | patterns. Note, however, that this applies only to data characters. | |
701 | Whitespace characters may never appear within special character | |
702 | sequences in a pattern, for example within the sequence (?( which | |
703 | introduces a conditional subpattern. | |
704 | ||
705 | PCRE_EXTRA | |
706 | ||
707 | This option was invented in order to turn on additional functionality | |
708 | of PCRE that is incompatible with Perl, but it is currently of very | |
709 | little use. When set, any backslash in a pattern that is followed by a | |
710 | letter that has no special meaning causes an error, thus reserving | |
711 | these combinations for future expansion. By default, as in Perl, a | |
712 | backslash followed by a letter with no special meaning is treated as a | |
713 | literal. There are at present no other features controlled by this | |
714 | option. It can also be set by a (?X) option setting within a pattern. | |
715 | ||
716 | PCRE_MULTILINE | |
717 | ||
718 | By default, PCRE treats the subject string as consisting of a single | |
719 | line of characters (even if it actually contains newlines). The "start | |
720 | of line" metacharacter (^) matches only at the start of the string, | |
721 | while the "end of line" metacharacter ($) matches only at the end of | |
722 | the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY | |
723 | is set). This is the same as Perl. | |
724 | ||
725 | When PCRE_MULTILINE it is set, the "start of line" and "end of line" | |
726 | constructs match immediately following or immediately before any new- | |
727 | line in the subject string, respectively, as well as at the very start | |
728 | and end. This is equivalent to Perl's /m option, and it can be changed | |
729 | within a pattern by a (?m) option setting. If there are no "\n" charac- | |
730 | ters in a subject string, or no occurrences of ^ or $ in a pattern, | |
731 | setting PCRE_MULTILINE has no effect. | |
732 | ||
733 | PCRE_NO_AUTO_CAPTURE | |
734 | ||
735 | If this option is set, it disables the use of numbered capturing paren- | |
736 | theses in the pattern. Any opening parenthesis that is not followed by | |
737 | ? behaves as if it were followed by ?: but named parentheses can still | |
738 | be used for capturing (and they acquire numbers in the usual way). | |
739 | There is no equivalent of this option in Perl. | |
740 | ||
741 | PCRE_UNGREEDY | |
742 | ||
743 | This option inverts the "greediness" of the quantifiers so that they | |
744 | are not greedy by default, but become greedy if followed by "?". It is | |
745 | not compatible with Perl. It can also be set by a (?U) option setting | |
746 | within the pattern. | |
747 | ||
748 | PCRE_UTF8 | |
749 | ||
750 | This option causes PCRE to regard both the pattern and the subject as | |
751 | strings of UTF-8 characters instead of single-byte character strings. | |
752 | However, it is available only when PCRE is built to include UTF-8 sup- | |
753 | port. If not, the use of this option provokes an error. Details of how | |
754 | this option changes the behaviour of PCRE are given in the section on | |
755 | UTF-8 support in the main pcre page. | |
756 | ||
757 | PCRE_NO_UTF8_CHECK | |
758 | ||
759 | When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is | |
760 | automatically checked. If an invalid UTF-8 sequence of bytes is found, | |
761 | pcre_compile() returns an error. If you already know that your pattern | |
762 | is valid, and you want to skip this check for performance reasons, you | |
763 | can set the PCRE_NO_UTF8_CHECK option. When it is set, the effect of | |
764 | passing an invalid UTF-8 string as a pattern is undefined. It may cause | |
765 | your program to crash. Note that this option can also be passed to | |
766 | pcre_exec(), to suppress the UTF-8 validity checking of subject | |
767 | strings. | |
768 | ||
769 | ||
770 | STUDYING A PATTERN | STUDYING A PATTERN |
When a pattern is going to be used several times, it is | ||
worth spending more time analyzing it in order to speed up | ||
the time taken for matching. The function pcre_study() takes | ||
a pointer to a compiled pattern as its first argument, and | ||
returns a pointer to a pcre_extra block (another void | ||
typedef) containing additional information about the pat- | ||
tern; this can be passed to pcre_exec(). If no additional | ||
information is available, NULL is returned. | ||
The second argument contains option bits. At present, no | ||
options are defined for pcre_study(), and this argument | ||
should always be zero. | ||
The third argument for pcre_study() is a pointer to an error | ||
message. If studying succeeds (even if no data is returned), | ||
the variable it points to is set to NULL. Otherwise it | ||
points to a textual error message. | ||
At present, studying a pattern is useful only for non- | ||
anchored patterns that do not have a single fixed starting | ||
character. A bitmap of possible starting characters is | ||
created. | ||
771 | ||
772 | pcre_extra *pcre_study(const pcre *code, int options, | |
773 | const char **errptr); | |
774 | ||
775 | If a compiled pattern is going to be used several times, it is worth | |
776 | spending more time analyzing it in order to speed up the time taken for | |
777 | matching. The function pcre_study() takes a pointer to a compiled pat- | |
778 | tern as its first argument. If studying the pattern produces additional | |
779 | information that will help speed up matching, pcre_study() returns a | |
780 | pointer to a pcre_extra block, in which the study_data field points to | |
781 | the results of the study. | |
782 | ||
783 | The returned value from pcre_study() can be passed directly to | |
784 | pcre_exec(). However, a pcre_extra block also contains other fields | |
785 | that can be set by the caller before the block is passed; these are | |
786 | described below in the section on matching a pattern. | |
787 | ||
788 | If studying the pattern does not produce any additional information, | |
789 | pcre_study() returns NULL. In that circumstance, if the calling program | |
790 | wants to pass any of the other fields to pcre_exec(), it must set up | |
791 | its own pcre_extra block. | |
792 | ||
793 | The second argument of pcre_study() contains option bits. At present, | |
794 | no options are defined, and this argument should always be zero. | |
795 | ||
796 | The third argument for pcre_study() is a pointer for an error message. | |
797 | If studying succeeds (even if no data is returned), the variable it | |
798 | points to is set to NULL. Otherwise it points to a textual error mes- | |
799 | sage. You should therefore test the error pointer for NULL after call- | |
800 | ing pcre_study(), to be sure that it has run successfully. | |
801 | ||
802 | This is a typical call to pcre_study(): | |
803 | ||
804 | pcre_extra *pe; | |
805 | pe = pcre_study( | |
806 | re, /* result of pcre_compile() */ | |
807 | 0, /* no options exist */ | |
808 | &error); /* set to NULL or points to a message */ | |
809 | ||
810 | At present, studying a pattern is useful only for non-anchored patterns | |
811 | that do not have a single fixed starting character. A bitmap of possi- | |
812 | ble starting bytes is created. | |
813 | ||
814 | ||
815 | LOCALE SUPPORT | LOCALE SUPPORT |
PCRE handles caseless matching, and determines whether char- | ||
acters are letters, digits, or whatever, by reference to a | ||
set of tables. The library contains a default set of tables | ||
which is created in the default C locale when PCRE is com- | ||
piled. This is used when the final argument of | ||
pcre_compile() is NULL, and is sufficient for many applica- | ||
tions. | ||
An alternative set of tables can, however, be supplied. Such | ||
tables are built by calling the pcre_maketables() function, | ||
which has no arguments, in the relevant locale. The result | ||
can then be passed to pcre_compile() as often as necessary. | ||
For example, to build and use tables that are appropriate | ||
for the French locale (where accented characters with codes | ||
greater than 128 are treated as letters), the following code | ||
could be used: | ||
setlocale(LC_CTYPE, "fr"); | ||
tables = pcre_maketables(); | ||
re = pcre_compile(..., tables); | ||
The tables are built in memory that is obtained via | ||
pcre_malloc. The pointer that is passed to pcre_compile is | ||
saved with the compiled pattern, and the same tables are | ||
used via this pointer by pcre_study() and pcre_exec(). Thus | ||
for any single pattern, compilation, studying and matching | ||
all happen in the same locale, but different patterns can be | ||
compiled in different locales. It is the caller's responsi- | ||
bility to ensure that the memory containing the tables | ||
remains available for as long as it is needed. | ||
816 | ||
817 | PCRE handles caseless matching, and determines whether characters are | |
818 | letters, digits, or whatever, by reference to a set of tables, indexed | |
819 | by character value. (When running in UTF-8 mode, this applies only to | |
820 | characters with codes less than 128. Higher-valued codes never match | |
821 | escapes such as \w or \d, but can be tested with \p if PCRE is built | |
822 | with Unicode character property support.) | |
823 | ||
824 | An internal set of tables is created in the default C locale when PCRE | |
825 | is built. This is used when the final argument of pcre_compile() is | |
826 | NULL, and is sufficient for many applications. An alternative set of | |
827 | tables can, however, be supplied. These may be created in a different | |
828 | locale from the default. As more and more applications change to using | |
829 | Unicode, the need for this locale support is expected to die away. | |
830 | ||
831 | External tables are built by calling the pcre_maketables() function, | |
832 | which has no arguments, in the relevant locale. The result can then be | |
833 | passed to pcre_compile() or pcre_exec() as often as necessary. For | |
834 | example, to build and use tables that are appropriate for the French | |
835 | locale (where accented characters with values greater than 128 are | |
836 | treated as letters), the following code could be used: | |
837 | ||
838 | setlocale(LC_CTYPE, "fr_FR"); | |
839 | tables = pcre_maketables(); | |
840 | re = pcre_compile(..., tables); | |
841 | ||
842 | When pcre_maketables() runs, the tables are built in memory that is | |
843 | obtained via pcre_malloc. It is the caller's responsibility to ensure | |
844 | that the memory containing the tables remains available for as long as | |
845 | it is needed. | |
846 | ||
847 | The pointer that is passed to pcre_compile() is saved with the compiled | |
848 | pattern, and the same tables are used via this pointer by pcre_study() | |
849 | and normally also by pcre_exec(). Thus, by default, for any single pat- | |
850 | tern, compilation, studying and matching all happen in the same locale, | |
851 | but different patterns can be compiled in different locales. | |
852 | ||
853 | It is possible to pass a table pointer or NULL (indicating the use of | |
854 | the internal tables) to pcre_exec(). Although not intended for this | |
855 | purpose, this facility could be used to match a pattern in a different | |
856 | locale from the one in which it was compiled. Passing table pointers at | |
857 | run time is discussed below in the section on matching a pattern. | |
858 | ||
859 | ||
860 | INFORMATION ABOUT A PATTERN | INFORMATION ABOUT A PATTERN |
The pcre_fullinfo() function returns information about a | ||
compiled pattern. It replaces the obsolete pcre_info() func- | ||
tion, which is nevertheless retained for backwards compabil- | ||
ity (and is documented below). | ||
The first argument for pcre_fullinfo() is a pointer to the | ||
compiled pattern. The second argument is the result of | ||
pcre_study(), or NULL if the pattern was not studied. The | ||
third argument specifies which piece of information is | ||
required, while the fourth argument is a pointer to a vari- | ||
able to receive the data. The yield of the function is zero | ||
for success, or one of the following negative numbers: | ||
PCRE_ERROR_NULL the argument code was NULL | ||
the argument where was NULL | ||
PCRE_ERROR_BADMAGIC the "magic number" was not found | ||
PCRE_ERROR_BADOPTION the value of what was invalid | ||
The possible values for the third argument are defined in | ||
pcre.h, and are as follows: | ||
PCRE_INFO_OPTIONS | ||
Return a copy of the options with which the pattern was com- | ||
piled. The fourth argument should point to au unsigned long | ||
int variable. These option bits are those specified in the | ||
call to pcre_compile(), modified by any top-level option | ||
settings within the pattern itself, and with the | ||
PCRE_ANCHORED bit forcibly set if the form of the pattern | ||
implies that it can match only at the start of a subject | ||
string. | ||
PCRE_INFO_SIZE | ||
Return the size of the compiled pattern, that is, the value | ||
that was passed as the argument to pcre_malloc() when PCRE | ||
was getting memory in which to place the compiled data. The | ||
fourth argument should point to a size_t variable. | ||
PCRE_INFO_CAPTURECOUNT | ||
Return the number of capturing subpatterns in the pattern. | ||
The fourth argument should point to an int variable. | ||
PCRE_INFO_BACKREFMAX | ||
Return the number of the highest back reference in the | ||
pattern. The fourth argument should point to an int vari- | ||
able. Zero is returned if there are no back references. | ||
PCRE_INFO_FIRSTCHAR | ||
Return information about the first character of any matched | ||
string, for a non-anchored pattern. If there is a fixed | ||
first character, e.g. from a pattern such as | ||
(cat|cow|coyote), it is returned in the integer pointed to | ||
by where. Otherwise, if either | ||
(a) the pattern was compiled with the PCRE_MULTILINE option, | ||
and every branch starts with "^", or | ||
(b) every branch of the pattern starts with ".*" and | ||
PCRE_DOTALL is not set (if it were set, the pattern would be | ||
anchored), | ||
-1 is returned, indicating that the pattern matches only at | ||
the start of a subject string or after any "\n" within the | ||
string. Otherwise -2 is returned. For anchored patterns, -2 | ||
is returned. | ||
PCRE_INFO_FIRSTTABLE | ||
If the pattern was studied, and this resulted in the con- | ||
struction of a 256-bit table indicating a fixed set of char- | ||
acters for the first character in any matching string, a | ||
pointer to the table is returned. Otherwise NULL is | ||
returned. The fourth argument should point to an unsigned | ||
char * variable. | ||
PCRE_INFO_LASTLITERAL | ||
For a non-anchored pattern, return the value of the right- | ||
most literal character which must exist in any matched | ||
string, other than at its start. The fourth argument should | ||
point to an int variable. If there is no such character, or | ||
if the pattern is anchored, -1 is returned. For example, for | ||
the pattern /a\d+z\d+/ the returned value is 'z'. | ||
The pcre_info() function is now obsolete because its inter- | ||
face is too restrictive to return all the available data | ||
about a compiled pattern. New programs should use | ||
pcre_fullinfo() instead. The yield of pcre_info() is the | ||
number of capturing subpatterns, or one of the following | ||
negative numbers: | ||
PCRE_ERROR_NULL the argument code was NULL | ||
PCRE_ERROR_BADMAGIC the "magic number" was not found | ||
If the optptr argument is not NULL, a copy of the options | ||
with which the pattern was compiled is placed in the integer | ||
it points to (see PCRE_INFO_OPTIONS above). | ||
If the pattern is not anchored and the firstcharptr argument | ||
is not NULL, it is used to pass back information about the | ||
first character of any matched string (see | ||
PCRE_INFO_FIRSTCHAR above). | ||
861 | ||
862 | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, | |
863 | int what, void *where); | |
864 | ||
865 | The pcre_fullinfo() function returns information about a compiled pat- | |
866 | tern. It replaces the obsolete pcre_info() function, which is neverthe- | |
867 | less retained for backwards compability (and is documented below). | |
868 | ||
869 | The first argument for pcre_fullinfo() is a pointer to the compiled | |
870 | pattern. The second argument is the result of pcre_study(), or NULL if | |
871 | the pattern was not studied. The third argument specifies which piece | |
872 | of information is required, and the fourth argument is a pointer to a | |
873 | variable to receive the data. The yield of the function is zero for | |
874 | success, or one of the following negative numbers: | |
875 | ||
876 | PCRE_ERROR_NULL the argument code was NULL | |
877 | the argument where was NULL | |
878 | PCRE_ERROR_BADMAGIC the "magic number" was not found | |
879 | PCRE_ERROR_BADOPTION the value of what was invalid | |
880 | ||
881 | The "magic number" is placed at the start of each compiled pattern as | |
882 | an simple check against passing an arbitrary memory pointer. Here is a | |
883 | typical call of pcre_fullinfo(), to obtain the length of the compiled | |
884 | pattern: | |
885 | ||
886 | int rc; | |
887 | unsigned long int length; | |
888 | rc = pcre_fullinfo( | |
889 | re, /* result of pcre_compile() */ | |
890 | pe, /* result of pcre_study(), or NULL */ | |
891 | PCRE_INFO_SIZE, /* what is required */ | |
892 | &length); /* where to put the data */ | |
893 | ||
894 | The possible values for the third argument are defined in pcre.h, and | |
895 | are as follows: | |
896 | ||
897 | PCRE_INFO_BACKREFMAX | |
898 | ||
899 | Return the number of the highest back reference in the pattern. The | |
900 | fourth argument should point to an int variable. Zero is returned if | |
901 | there are no back references. | |
902 | ||
903 | PCRE_INFO_CAPTURECOUNT | |
904 | ||
905 | Return the number of capturing subpatterns in the pattern. The fourth | |
906 | argument should point to an int variable. | |
907 | ||
908 | PCRE_INFO_DEFAULTTABLES | |
909 | ||
910 | Return a pointer to the internal default character tables within PCRE. | |
911 | The fourth argument should point to an unsigned char * variable. This | |
912 | information call is provided for internal use by the pcre_study() func- | |
913 | tion. External callers can cause PCRE to use its internal tables by | |
914 | passing a NULL table pointer. | |
915 | ||
916 | PCRE_INFO_FIRSTBYTE | |
917 | ||
918 | Return information about the first byte of any matched string, for a | |
919 | non-anchored pattern. (This option used to be called | |
920 | PCRE_INFO_FIRSTCHAR; the old name is still recognized for backwards | |
921 | compatibility.) | |
922 | ||
923 | If there is a fixed first byte, for example, from a pattern such as | |
924 | (cat|cow|coyote), it is returned in the integer pointed to by where. | |
925 | Otherwise, if either | |
926 | ||
927 | (a) the pattern was compiled with the PCRE_MULTILINE option, and every | |
928 | branch starts with "^", or | |
929 | ||
930 | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not | |
931 | set (if it were set, the pattern would be anchored), | |
932 | ||
933 | -1 is returned, indicating that the pattern matches only at the start | |
934 | of a subject string or after any newline within the string. Otherwise | |
935 | -2 is returned. For anchored patterns, -2 is returned. | |
936 | ||
937 | PCRE_INFO_FIRSTTABLE | |
938 | ||
939 | If the pattern was studied, and this resulted in the construction of a | |
940 | 256-bit table indicating a fixed set of bytes for the first byte in any | |
941 | matching string, a pointer to the table is returned. Otherwise NULL is | |
942 | returned. The fourth argument should point to an unsigned char * vari- | |
943 | able. | |
944 | ||
945 | PCRE_INFO_LASTLITERAL | |
946 | ||
947 | Return the value of the rightmost literal byte that must exist in any | |
948 | matched string, other than at its start, if such a byte has been | |
949 | recorded. The fourth argument should point to an int variable. If there | |
950 | is no such byte, -1 is returned. For anchored patterns, a last literal | |
951 | byte is recorded only if it follows something of variable length. For | |
952 | example, for the pattern /^a\d+z\d+/ the returned value is "z", but for | |
953 | /^a\dz\d/ the returned value is -1. | |
954 | ||
955 | PCRE_INFO_NAMECOUNT | |
956 | PCRE_INFO_NAMEENTRYSIZE | |
957 | PCRE_INFO_NAMETABLE | |
958 | ||
959 | PCRE supports the use of named as well as numbered capturing parenthe- | |
960 | ses. The names are just an additional way of identifying the parenthe- | |
961 | ses, which still acquire numbers. A convenience function called | |
962 | pcre_get_named_substring() is provided for extracting an individual | |
963 | captured substring by name. It is also possible to extract the data | |
964 | directly, by first converting the name to a number in order to access | |
965 | the correct pointers in the output vector (described with pcre_exec() | |
966 | below). To do the conversion, you need to use the name-to-number map, | |
967 | which is described by these three values. | |
968 | ||
969 | The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT | |
970 | gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size | |
971 | of each entry; both of these return an int value. The entry size | |
972 | depends on the length of the longest name. PCRE_INFO_NAMETABLE returns | |
973 | a pointer to the first entry of the table (a pointer to char). The | |
974 | first two bytes of each entry are the number of the capturing parenthe- | |
975 | sis, most significant byte first. The rest of the entry is the corre- | |
976 | sponding name, zero terminated. The names are in alphabetical order. | |
977 | For example, consider the following pattern (assume PCRE_EXTENDED is | |
978 | set, so white space - including newlines - is ignored): | |
979 | ||
980 | (?P<date> (?P<year>(\d\d)?\d\d) - | |
981 | (?P<month>\d\d) - (?P<day>\d\d) ) | |
982 | ||
983 | There are four named subpatterns, so the table has four entries, and | |
984 | each entry in the table is eight bytes long. The table is as follows, | |
985 | with non-printing bytes shows in hexadecimal, and undefined bytes shown | |
986 | as ??: | |
987 | ||
988 | 00 01 d a t e 00 ?? | |
989 | 00 05 d a y 00 ?? ?? | |
990 | 00 04 m o n t h 00 | |
991 | 00 02 y e a r 00 ?? | |
992 | ||
993 | When writing code to extract data from named subpatterns using the | |
994 | name-to-number map, remember that the length of each entry is likely to | |
995 | be different for each compiled pattern. | |
996 | ||
997 | PCRE_INFO_OPTIONS | |
998 | ||
999 | Return a copy of the options with which the pattern was compiled. The | |
1000 | fourth argument should point to an unsigned long int variable. These | |
1001 | option bits are those specified in the call to pcre_compile(), modified | |
1002 | by any top-level option settings within the pattern itself. | |
1003 | ||
1004 | A pattern is automatically anchored by PCRE if all of its top-level | |
1005 | alternatives begin with one of the following: | |
1006 | ||
1007 | ^ unless PCRE_MULTILINE is set | |
1008 | \A always | |
1009 | \G always | |
1010 | .* if PCRE_DOTALL is set and there are no back | |
1011 | references to the subpattern in which .* appears | |
1012 | ||
1013 | For such patterns, the PCRE_ANCHORED bit is set in the options returned | |
1014 | by pcre_fullinfo(). | |
1015 | ||
1016 | PCRE_INFO_SIZE | |
1017 | ||
1018 | Return the size of the compiled pattern, that is, the value that was | |
1019 | passed as the argument to pcre_malloc() when PCRE was getting memory in | |
1020 | which to place the compiled data. The fourth argument should point to a | |
1021 | size_t variable. | |
1022 | ||
1023 | PCRE_INFO_STUDYSIZE | |
1024 | ||
1025 | Return the size of the data block pointed to by the study_data field in | |
1026 | a pcre_extra block. That is, it is the value that was passed to | |
1027 | pcre_malloc() when PCRE was getting memory into which to place the data | |
1028 | created by pcre_study(). The fourth argument should point to a size_t | |
1029 | variable. | |
1030 | ||
1031 | ||
1032 | OBSOLETE INFO FUNCTION | |
1033 | ||
1034 | int pcre_info(const pcre *code, int *optptr, int *firstcharptr); | |
1035 | ||
1036 | The pcre_info() function is now obsolete because its interface is too | |
1037 | restrictive to return all the available data about a compiled pattern. | |
1038 | New programs should use pcre_fullinfo() instead. The yield of | |
1039 | pcre_info() is the number of capturing subpatterns, or one of the fol- | |
1040 | lowing negative numbers: | |
1041 | ||
1042 | PCRE_ERROR_NULL the argument code was NULL | |
1043 | PCRE_ERROR_BADMAGIC the "magic number" was not found | |
1044 | ||
1045 | If the optptr argument is not NULL, a copy of the options with which | |
1046 | the pattern was compiled is placed in the integer it points to (see | |
1047 | PCRE_INFO_OPTIONS above). | |
1048 | ||
1049 | If the pattern is not anchored and the firstcharptr argument is not | |
1050 | NULL, it is used to pass back information about the first character of | |
1051 | any matched string (see PCRE_INFO_FIRSTBYTE above). | |
1052 | ||
1053 | ||
1054 | MATCHING A PATTERN | MATCHING A PATTERN |
The function pcre_exec() is called to match a subject string | ||
against a pre-compiled pattern, which is passed in the code | ||
argument. If the pattern has been studied, the result of the | ||
study should be passed in the extra argument. Otherwise this | ||
must be NULL. | ||
The PCRE_ANCHORED option can be passed in the options argu- | ||
ment, whose unused bits must be zero. However, if a pattern | ||
was compiled with PCRE_ANCHORED, or turned out to be | ||
anchored by virtue of its contents, it cannot be made | ||
unachored at matching time. | ||
There are also three further options that can be set only at | ||
matching time: | ||
PCRE_NOTBOL | ||
The first character of the string is not the beginning of a | ||
line, so the circumflex metacharacter should not match | ||
before it. Setting this without PCRE_MULTILINE (at compile | ||
time) causes circumflex never to match. | ||
PCRE_NOTEOL | ||
The end of the string is not the end of a line, so the dol- | ||
lar metacharacter should not match it nor (except in multi- | ||
line mode) a newline immediately before it. Setting this | ||
without PCRE_MULTILINE (at compile time) causes dollar never | ||
to match. | ||
PCRE_NOTEMPTY | ||
An empty string is not considered to be a valid match if | ||
this option is set. If there are alternatives in the pat- | ||
tern, they are tried. If all the alternatives match the | ||
empty string, the entire match fails. For example, if the | ||
pattern | ||
a?b? | ||
is applied to a string not beginning with "a" or "b", it | ||
matches the empty string at the start of the subject. With | ||
PCRE_NOTEMPTY set, this match is not valid, so PCRE searches | ||
further into the string for occurrences of "a" or "b". | ||
Perl has no direct equivalent of PCRE_NOTEMPTY, but it does | ||
make a special case of a pattern match of the empty string | ||
within its split() function, and when using the /g modifier. | ||
It is possible to emulate Perl's behaviour after matching a | ||
null string by first trying the match again at the same | ||
offset with PCRE_NOTEMPTY set, and then if that fails by | ||
advancing the starting offset (see below) and trying an | ||
ordinary match again. | ||
The subject string is passed as a pointer in subject, a | ||
length in length, and a starting offset in startoffset. | ||
Unlike the pattern string, it may contain binary zero char- | ||
acters. When the starting offset is zero, the search for a | ||
match starts at the beginning of the subject, and this is by | ||
far the most common case. | ||
A non-zero starting offset is useful when searching for | ||
another match in the same subject by calling pcre_exec() | ||
again after a previous success. Setting startoffset differs | ||
from just passing over a shortened string and setting | ||
PCRE_NOTBOL in the case of a pattern that begins with any | ||
kind of lookbehind. For example, consider the pattern | ||
\Biss\B | ||
which finds occurrences of "iss" in the middle of words. (\B | ||
matches only if the current position in the subject is not a | ||
word boundary.) When applied to the string "Mississipi" the | ||
first call to pcre_exec() finds the first occurrence. If | ||
pcre_exec() is called again with just the remainder of the | ||
subject, namely "issipi", it does not match, because \B is | ||
always false at the start of the subject, which is deemed to | ||
be a word boundary. However, if pcre_exec() is passed the | ||
entire string again, but with startoffset set to 4, it finds | ||
the second occurrence of "iss" because it is able to look | ||
behind the starting point to discover that it is preceded by | ||
a letter. | ||
If a non-zero starting offset is passed when the pattern is | ||
anchored, one attempt to match at the given offset is tried. | ||
This can only succeed if the pattern does not require the | ||
match to be at the start of the subject. | ||
In general, a pattern matches a certain portion of the sub- | ||
ject, and in addition, further substrings from the subject | ||
may be picked out by parts of the pattern. Following the | ||
usage in Jeffrey Friedl's book, this is called "capturing" | ||
in what follows, and the phrase "capturing subpattern" is | ||
used for a fragment of a pattern that picks out a substring. | ||
PCRE supports several other kinds of parenthesized subpat- | ||
tern that do not cause substrings to be captured. | ||
Captured substrings are returned to the caller via a vector | ||
of integer offsets whose address is passed in ovector. The | ||
number of elements in the vector is passed in ovecsize. The | ||
first two-thirds of the vector is used to pass back captured | ||
substrings, each substring using a pair of integers. The | ||
remaining third of the vector is used as workspace by | ||
pcre_exec() while matching capturing subpatterns, and is not | ||
available for passing back information. The length passed in | ||
ovecsize should always be a multiple of three. If it is not, | ||
it is rounded down. | ||
When a match has been successful, information about captured | ||
substrings is returned in pairs of integers, starting at the | ||
beginning of ovector, and continuing up to two-thirds of its | ||
length at the most. The first element of a pair is set to | ||
the offset of the first character in a substring, and the | ||
second is set to the offset of the first character after the | ||
end of a substring. The first pair, ovector[0] and ovec- | ||
tor[1], 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 | ||
pcre_exec() 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. | ||
Some convenience functions are provided for extracting the | ||
captured substrings as separate strings. These are described | ||
in the following section. | ||
It is possible for an capturing subpattern number n+1 to | ||
match some part of the subject when subpattern n has not | ||
been used at all. For example, if the string "abc" is | ||
matched against the pattern (a|(z))(bc) subpatterns 1 and 3 | ||
are matched, but 2 is not. When this happens, both offset | ||
values corresponding to the unused subpattern are set to -1. | ||
If a capturing subpattern is matched repeatedly, it is the | ||
last portion of the string that it matched that gets | ||
returned. | ||
If the vector is too small to hold all the captured sub- | ||
strings, it is used as far as possible (up to two-thirds of | ||
its length), and the function returns a value of zero. In | ||
particular, if the substring offsets are not of interest, | ||
pcre_exec() may be called with ovector passed as NULL and | ||
ovecsize as zero. However, if the pattern contains back | ||
references and the ovector isn't big enough to remember the | ||
related substrings, PCRE has to get additional memory for | ||
use during matching. Thus it is usually advisable to supply | ||
an ovector. | ||
Note that pcre_info() can be used to find out how many cap- | ||
turing subpatterns there are in a compiled pattern. The | ||
smallest size for ovector that will allow for n captured | ||
substrings in addition to the offsets of the substring | ||
matched by the whole pattern is (n+1)*3. | ||
If pcre_exec() fails, it returns a negative number. The fol- | ||
lowing are defined in the header file: | ||
PCRE_ERROR_NOMATCH (-1) | ||
The subject string did not match the pattern. | ||
PCRE_ERROR_NULL (-2) | ||
Either code or subject was passed as NULL, or ovector was | ||
NULL and ovecsize was not zero. | ||
PCRE_ERROR_BADOPTION (-3) | ||
An unrecognized bit was set in the options argument. | ||
PCRE_ERROR_BADMAGIC (-4) | ||
PCRE stores a 4-byte "magic number" at the start of the com- | ||
piled code, to catch the case when it is passed a junk | ||
pointer. This is the error it gives when the magic number | ||
isn't present. | ||
PCRE_ERROR_UNKNOWN_NODE (-5) | ||
While running the pattern match, an unknown item was encoun- | ||
tered in the compiled pattern. This error could be caused by | ||
a bug in PCRE or by overwriting of the compiled pattern. | ||
PCRE_ERROR_NOMEMORY (-6) | ||
If a pattern contains back references, but the ovector that | ||
is passed to pcre_exec() is not big enough to remember the | ||
referenced substrings, PCRE gets a block of memory at the | ||
start of matching to use for this purpose. If the call via | ||
pcre_malloc() fails, this error is given. The memory is | ||
freed at the end of matching. | ||
EXTRACTING CAPTURED SUBSTRINGS | ||
Captured substrings can be accessed directly by using the | ||
SunOS 5.8 Last change: 12 | ||
offsets returned by pcre_exec() in ovector. For convenience, | ||
the functions pcre_copy_substring(), pcre_get_substring(), | ||
and pcre_get_substring_list() are provided for extracting | ||
captured substrings as new, separate, zero-terminated | ||
strings. A substring that contains a binary zero is | ||
correctly extracted and has a further zero added on the end, | ||
but the result does not, of course, function as a C string. | ||
The first three arguments are the same for all three func- | ||
tions: subject is the subject string which has just been | ||
successfully matched, ovector is a pointer to the vector of | ||
integer offsets that was passed to pcre_exec(), and | ||
stringcount is the number of substrings that were captured | ||
by the match, including the substring that matched the | ||
entire regular expression. This is the value returned by | ||
pcre_exec if it is greater than zero. If pcre_exec() | ||
returned zero, indicating that it ran out of space in ovec- | ||
tor, the value passed as stringcount should be the size of | ||
the vector divided by three. | ||
The functions pcre_copy_substring() and pcre_get_substring() | ||
extract a single substring, whose number is given as string- | ||
number. A value of zero extracts the substring that matched | ||
the entire pattern, while higher values extract the captured | ||
substrings. For pcre_copy_substring(), the string is placed | ||
in buffer, whose length is given by buffersize, while for | ||
pcre_get_substring() a new block of memory is obtained via | ||
pcre_malloc, and its address is returned via stringptr. The | ||
yield of the function is the length of the string, not | ||
including the terminating zero, or one of | ||
PCRE_ERROR_NOMEMORY (-6) | ||
The buffer was too small for pcre_copy_substring(), or the | ||
attempt to get memory failed for pcre_get_substring(). | ||
PCRE_ERROR_NOSUBSTRING (-7) | ||
There is no substring whose number is stringnumber. | ||
The pcre_get_substring_list() function extracts all avail- | ||
able substrings and builds a list of pointers to them. All | ||
this is done in a single block of memory which is obtained | ||
via pcre_malloc. The address of the memory block is returned | ||
via listptr, which is also the start of the list of string | ||
pointers. The end of the list is marked by a NULL pointer. | ||
The yield of the function is zero if all went well, or | ||
PCRE_ERROR_NOMEMORY (-6) | ||
if the attempt to get the memory block failed. | ||
When any of these functions encounter a substring that is | ||
unset, which can happen when capturing subpattern number n+1 | ||
matches some part of the subject, but subpattern n has not | ||
been used at all, they return an empty string. This can be | ||
distinguished from a genuine zero-length substring by | ||
inspecting the appropriate offset in ovector, which is nega- | ||
tive for unset substrings. | ||
The two convenience functions pcre_free_substring() and | ||
pcre_free_substring_list() can be used to free the memory | ||
returned by a previous call of pcre_get_substring() or | ||
pcre_get_substring_list(), respectively. They do nothing | ||
more than call the function pointed to by pcre_free, which | ||
of course could be called directly from a C program. How- | ||
ever, PCRE is used in some situations where it is linked via | ||
a special interface to another programming language which | ||
cannot use pcre_free directly; it is for these cases that | ||
the functions are provided. | ||
1055 | ||
1056 | int pcre_exec(const pcre *code, const pcre_extra *extra, | |
1057 | const char *subject, int length, int startoffset, | |
1058 | int options, int *ovector, int ovecsize); | |
1059 | ||
1060 | The function pcre_exec() is called to match a subject string against a | |
1061 | compiled pattern, which is passed in the code argument. If the pattern | |
1062 | has been studied, the result of the study should be passed in the extra | |
1063 | argument. | |
1064 | ||
1065 | In most applications, the pattern will have been compiled (and option- | |
1066 | ally studied) in the same process that calls pcre_exec(). However, it | |
1067 | is possible to save compiled patterns and study data, and then use them | |
1068 | later in different processes, possibly even on different hosts. For a | |
1069 | discussion about this, see the pcreprecompile documentation. | |
1070 | ||
1071 | Here is an example of a simple call to pcre_exec(): | |
1072 | ||
1073 | int rc; | |
1074 | int ovector[30]; | |
1075 | rc = pcre_exec( | |
1076 | re, /* result of pcre_compile() */ | |
1077 | NULL, /* we didn't study the pattern */ | |
1078 | "some string", /* the subject string */ | |
1079 | 11, /* the length of the subject string */ | |
1080 | 0, /* start at offset 0 in the subject */ | |
1081 | 0, /* default options */ | |
1082 | ovector, /* vector of integers for substring information */ | |
1083 | 30); /* number of elements in the vector (NOT size in | |
1084 | bytes) */ | |
1085 | ||
1086 | Extra data for pcre_exec() | |
1087 | ||
1088 | If the extra argument is not NULL, it must point to a pcre_extra data | |
1089 | block. The pcre_study() function returns such a block (when it doesn't | |
1090 | return NULL), but you can also create one for yourself, and pass addi- | |
1091 | tional information in it. The fields in a pcre_extra block are as fol- | |
1092 | lows: | |
1093 | ||
1094 | unsigned long int flags; | |
1095 | void *study_data; | |
1096 | unsigned long int match_limit; | |
1097 | void *callout_data; | |
1098 | const unsigned char *tables; | |
1099 | ||
1100 | The flags field is a bitmap that specifies which of the other fields | |
1101 | are set. The flag bits are: | |
1102 | ||
1103 | PCRE_EXTRA_STUDY_DATA | |
1104 | PCRE_EXTRA_MATCH_LIMIT | |
1105 | PCRE_EXTRA_CALLOUT_DATA | |
1106 | PCRE_EXTRA_TABLES | |
1107 | ||
1108 | Other flag bits should be set to zero. The study_data field is set in | |
1109 | the pcre_extra block that is returned by pcre_study(), together with | |
1110 | the appropriate flag bit. You should not set this yourself, but you may | |
1111 | add to the block by setting the other fields and their corresponding | |
1112 | flag bits. | |
1113 | ||
1114 | The match_limit field provides a means of preventing PCRE from using up | |
1115 | a vast amount of resources when running patterns that are not going to | |
1116 | match, but which have a very large number of possibilities in their | |
1117 | search trees. The classic example is the use of nested unlimited | |
1118 | repeats. | |
1119 | ||
1120 | Internally, PCRE uses a function called match() which it calls repeat- | |
1121 | edly (sometimes recursively). The limit is imposed on the number of | |
1122 | times this function is called during a match, which has the effect of | |
1123 | limiting the amount of recursion and backtracking that can take place. | |
1124 | For patterns that are not anchored, the count starts from zero for each | |
1125 | position in the subject string. | |
1126 | ||
1127 | The default limit for the library can be set when PCRE is built; the | |
1128 | default default is 10 million, which handles all but the most extreme | |
1129 | cases. You can reduce the default by suppling pcre_exec() with a | |
1130 | pcre_extra block in which match_limit is set to a smaller value, and | |
1131 | PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is | |
1132 | exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT. | |
1133 | ||
1134 | The pcre_callout field is used in conjunction with the "callout" fea- | |
1135 | ture, which is described in the pcrecallout documentation. | |
1136 | ||
1137 | The tables field is used to pass a character tables pointer to | |
1138 | pcre_exec(); this overrides the value that is stored with the compiled | |
1139 | pattern. A non-NULL value is stored with the compiled pattern only if | |
1140 | custom tables were supplied to pcre_compile() via its tableptr argu- | |
1141 | ment. If NULL is passed to pcre_exec() using this mechanism, it forces | |
1142 | PCRE's internal tables to be used. This facility is helpful when re- | |
1143 | using patterns that have been saved after compiling with an external | |
1144 | set of tables, because the external tables might be at a different | |
1145 | address when pcre_exec() is called. See the pcreprecompile documenta- | |
1146 | tion for a discussion of saving compiled patterns for later use. | |
1147 | ||
1148 | Option bits for pcre_exec() | |
1149 | ||
1150 | The unused bits of the options argument for pcre_exec() must be zero. | |
1151 | The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL, | |
1152 | PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK and PCRE_PARTIAL. | |
1153 | ||
1154 | PCRE_ANCHORED | |
1155 | ||
1156 | The PCRE_ANCHORED option limits pcre_exec() to matching at the first | |
1157 | matching position. If a pattern was compiled with PCRE_ANCHORED, or | |
1158 | turned out to be anchored by virtue of its contents, it cannot be made | |
1159 | unachored at matching time. | |
1160 | ||
1161 | PCRE_NOTBOL | |
1162 | ||
1163 | This option specifies that first character of the subject string is not | |
1164 | the beginning of a line, so the circumflex metacharacter should not | |
1165 | match before it. Setting this without PCRE_MULTILINE (at compile time) | |
1166 | causes circumflex never to match. This option affects only the | |
1167 | behaviour of the circumflex metacharacter. It does not affect \A. | |
1168 | ||
1169 | PCRE_NOTEOL | |
1170 | ||
1171 | This option specifies that the end of the subject string is not the end | |
1172 | of a line, so the dollar metacharacter should not match it nor (except | |
1173 | in multiline mode) a newline immediately before it. Setting this with- | |
1174 | out PCRE_MULTILINE (at compile time) causes dollar never to match. This | |
1175 | option affects only the behaviour of the dollar metacharacter. It does | |
1176 | not affect \Z or \z. | |
1177 | ||
1178 | PCRE_NOTEMPTY | |
1179 | ||
1180 | An empty string is not considered to be a valid match if this option is | |
1181 | set. If there are alternatives in the pattern, they are tried. If all | |
1182 | the alternatives match the empty string, the entire match fails. For | |
1183 | example, if the pattern | |
1184 | ||
1185 | a?b? | |
1186 | ||
1187 | is applied to a string not beginning with "a" or "b", it matches the | |
1188 | empty string at the start of the subject. With PCRE_NOTEMPTY set, this | |
1189 | match is not valid, so PCRE searches further into the string for occur- | |
1190 | rences of "a" or "b". | |
1191 | ||
1192 | Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe- | |
1193 | cial case of a pattern match of the empty string within its split() | |
1194 | function, and when using the /g modifier. It is possible to emulate | |
1195 | Perl's behaviour after matching a null string by first trying the match | |
1196 | again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then | |
1197 | if that fails by advancing the starting offset (see below) and trying | |
1198 | an ordinary match again. There is some code that demonstrates how to do | |
1199 | this in the pcredemo.c sample program. | |
1200 | ||
1201 | PCRE_NO_UTF8_CHECK | |
1202 | ||
1203 | When PCRE_UTF8 is set at compile time, the validity of the subject as a | |
1204 | UTF-8 string is automatically checked when pcre_exec() is subsequently | |
1205 | called. The value of startoffset is also checked to ensure that it | |
1206 | points to the start of a UTF-8 character. If an invalid UTF-8 sequence | |
1207 | of bytes is found, pcre_exec() returns the error PCRE_ERROR_BADUTF8. If | |
1208 | startoffset contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is | |
1209 | returned. | |
1210 | ||
1211 | If you already know that your subject is valid, and you want to skip | |
1212 | these checks for performance reasons, you can set the | |
1213 | PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to | |
1214 | do this for the second and subsequent calls to pcre_exec() if you are | |
1215 | making repeated calls to find all the matches in a single subject | |
1216 | string. However, you should be sure that the value of startoffset | |
1217 | points to the start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is | |
1218 | set, the effect of passing an invalid UTF-8 string as a subject, or a | |
1219 | value of startoffset that does not point to the start of a UTF-8 char- | |
1220 | acter, is undefined. Your program may crash. | |
1221 | ||
1222 | PCRE_PARTIAL | |
1223 | ||
1224 | This option turns on the partial matching feature. If the subject | |
1225 | string fails to match the pattern, but at some point during the match- | |
1226 | ing process the end of the subject was reached (that is, the subject | |
1227 | partially matches the pattern and the failure to match occurred only | |
1228 | because there were not enough subject characters), pcre_exec() returns | |
1229 | PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is | |
1230 | used, there are restrictions on what may appear in the pattern. These | |
1231 | are discussed in the pcrepartial documentation. | |
1232 | ||
1233 | The string to be matched by pcre_exec() | |
1234 | ||
1235 | The subject string is passed to pcre_exec() as a pointer in subject, a | |
1236 | length in length, and a starting byte offset in startoffset. In UTF-8 | |
1237 | mode, the byte offset must point to the start of a UTF-8 character. | |
1238 | Unlike the pattern string, the subject may contain binary zero bytes. | |
1239 | When the starting offset is zero, the search for a match starts at the | |
1240 | beginning of the subject, and this is by far the most common case. | |
1241 | ||
1242 | A non-zero starting offset is useful when searching for another match | |
1243 | in the same subject by calling pcre_exec() again after a previous suc- | |
1244 | cess. Setting startoffset differs from just passing over a shortened | |
1245 | string and setting PCRE_NOTBOL in the case of a pattern that begins | |
1246 | with any kind of lookbehind. For example, consider the pattern | |
1247 | ||
1248 | \Biss\B | |
1249 | ||
1250 | which finds occurrences of "iss" in the middle of words. (\B matches | |
1251 | only if the current position in the subject is not a word boundary.) | |
1252 | When applied to the string "Mississipi" the first call to pcre_exec() | |
1253 | finds the first occurrence. If pcre_exec() is called again with just | |
1254 | the remainder of the subject, namely "issipi", it does not match, | |
1255 | because \B is always false at the start of the subject, which is deemed | |
1256 | to be a word boundary. However, if pcre_exec() is passed the entire | |
1257 | string again, but with startoffset set to 4, it finds the second occur- | |
1258 | rence of "iss" because it is able to look behind the starting point to | |
1259 | discover that it is preceded by a letter. | |
1260 | ||
1261 | If a non-zero starting offset is passed when the pattern is anchored, | |
1262 | one attempt to match at the given offset is made. This can only succeed | |
1263 | if the pattern does not require the match to be at the start of the | |
1264 | subject. | |
1265 | ||
1266 | How pcre_exec() returns captured substrings | |
1267 | ||
1268 | In general, a pattern matches a certain portion of the subject, and in | |
1269 | addition, further substrings from the subject may be picked out by | |
1270 | parts of the pattern. Following the usage in Jeffrey Friedl's book, | |
1271 | this is called "capturing" in what follows, and the phrase "capturing | |
1272 | subpattern" is used for a fragment of a pattern that picks out a sub- | |
1273 | string. PCRE supports several other kinds of parenthesized subpattern | |
1274 | that do not cause substrings to be captured. | |
1275 | ||
1276 | Captured substrings are returned to the caller via a vector of integer | |
1277 | offsets whose address is passed in ovector. The number of elements in | |
1278 | the vector is passed in ovecsize, which must be a non-negative number. | |
1279 | Note: this argument is NOT the size of ovector in bytes. | |
1280 | ||
1281 | The first two-thirds of the vector is used to pass back captured sub- | |
1282 | strings, each substring using a pair of integers. The remaining third | |
1283 | of the vector is used as workspace by pcre_exec() while matching cap- | |
1284 | turing subpatterns, and is not available for passing back information. | |
1285 | The length passed in ovecsize should always be a multiple of three. If | |
1286 | it is not, it is rounded down. | |
1287 | ||
1288 | When a match is successful, information about captured substrings is | |
1289 | returned in pairs of integers, starting at the beginning of ovector, | |
1290 | and continuing up to two-thirds of its length at the most. The first | |
1291 | element of a pair is set to the offset of the first character in a sub- | |
1292 | string, and the second is set to the offset of the first character | |
1293 | after the end of a substring. The first pair, ovector[0] and ovec- | |
1294 | tor[1], identify the portion of the subject string matched by the | |
1295 | entire pattern. The next pair is used for the first capturing subpat- | |
1296 | tern, and so on. The value returned by pcre_exec() is the number of | |
1297 | pairs that have been set. If there are no capturing subpatterns, the | |
1298 | return value from a successful match is 1, indicating that just the | |
1299 | first pair of offsets has been set. | |
1300 | ||
1301 | Some convenience functions are provided for extracting the captured | |
1302 | substrings as separate strings. These are described in the following | |
1303 | section. | |
1304 | ||
1305 | It is possible for an capturing subpattern number n+1 to match some | |
1306 | part of the subject when subpattern n has not been used at all. For | |
1307 | example, if the string "abc" is matched against the pattern (a|(z))(bc) | |
1308 | subpatterns 1 and 3 are matched, but 2 is not. When this happens, both | |
1309 | offset values corresponding to the unused subpattern are set to -1. | |
1310 | ||
1311 | If a capturing subpattern is matched repeatedly, it is the last portion | |
1312 | of the string that it matched that is returned. | |
1313 | ||
1314 | If the vector is too small to hold all the captured substring offsets, | |
1315 | it is used as far as possible (up to two-thirds of its length), and the | |
1316 | function returns a value of zero. In particular, if the substring off- | |
1317 | sets are not of interest, pcre_exec() may be called with ovector passed | |
1318 | as NULL and ovecsize as zero. However, if the pattern contains back | |
1319 | references and the ovector is not big enough to remember the related | |
1320 | substrings, PCRE has to get additional memory for use during matching. | |
1321 | Thus it is usually advisable to supply an ovector. | |
1322 | ||
1323 | Note that pcre_info() can be used to find out how many capturing sub- | |
1324 | patterns there are in a compiled pattern. The smallest size for ovector | |
1325 | that will allow for n captured substrings, in addition to the offsets | |
1326 | of the substring matched by the whole pattern, is (n+1)*3. | |
1327 | ||
1328 | Return values from pcre_exec() | |
1329 | ||
1330 | If pcre_exec() fails, it returns a negative number. The following are | |
1331 | defined in the header file: | |
1332 | ||
1333 | PCRE_ERROR_NOMATCH (-1) | |
1334 | ||
1335 | The subject string did not match the pattern. | |
1336 | ||
1337 | PCRE_ERROR_NULL (-2) | |
1338 | ||
1339 | Either code or subject was passed as NULL, or ovector was NULL and | |
1340 | ovecsize was not zero. | |
1341 | ||
1342 | PCRE_ERROR_BADOPTION (-3) | |
1343 | ||
1344 | An unrecognized bit was set in the options argument. | |
1345 | ||
1346 | PCRE_ERROR_BADMAGIC (-4) | |
1347 | ||
1348 | PCRE stores a 4-byte "magic number" at the start of the compiled code, | |
1349 | to catch the case when it is passed a junk pointer and to detect when a | |
1350 | pattern that was compiled in an environment of one endianness is run in | |
1351 | an environment with the other endianness. This is the error that PCRE | |
1352 | gives when the magic number is not present. | |
1353 | ||
1354 | PCRE_ERROR_UNKNOWN_NODE (-5) | |
1355 | ||
1356 | While running the pattern match, an unknown item was encountered in the | |
1357 | compiled pattern. This error could be caused by a bug in PCRE or by | |
1358 | overwriting of the compiled pattern. | |
1359 | ||
1360 | PCRE_ERROR_NOMEMORY (-6) | |
1361 | ||
1362 | If a pattern contains back references, but the ovector that is passed | |
1363 | to pcre_exec() is not big enough to remember the referenced substrings, | |
1364 | PCRE gets a block of memory at the start of matching to use for this | |
1365 | purpose. If the call via pcre_malloc() fails, this error is given. The | |
1366 | memory is automatically freed at the end of matching. | |
1367 | ||
1368 | PCRE_ERROR_NOSUBSTRING (-7) | |
1369 | ||
1370 | This error is used by the pcre_copy_substring(), pcre_get_substring(), | |
1371 | and pcre_get_substring_list() functions (see below). It is never | |
1372 | returned by pcre_exec(). | |
1373 | ||
1374 | PCRE_ERROR_MATCHLIMIT (-8) | |
1375 | ||
1376 | The recursion and backtracking limit, as specified by the match_limit | |
1377 | field in a pcre_extra structure (or defaulted) was reached. See the | |
1378 | description above. | |
1379 | ||
1380 | PCRE_ERROR_CALLOUT (-9) | |
1381 | ||
1382 | This error is never generated by pcre_exec() itself. It is provided for | |
1383 | use by callout functions that want to yield a distinctive error code. | |
1384 | See the pcrecallout documentation for details. | |
1385 | ||
1386 | PCRE_ERROR_BADUTF8 (-10) | |
1387 | ||
1388 | A string that contains an invalid UTF-8 byte sequence was passed as a | |
1389 | subject. | |
1390 | ||
1391 | PCRE_ERROR_BADUTF8_OFFSET (-11) | |
1392 | ||
1393 | The UTF-8 byte sequence that was passed as a subject was valid, but the | |
1394 | value of startoffset did not point to the beginning of a UTF-8 charac- | |
1395 | ter. | |
1396 | ||
1397 | PCRE_ERROR_PARTIAL (-12) | |
1398 | ||
1399 | The subject string did not match, but it did match partially. See the | |
1400 | pcrepartial documentation for details of partial matching. | |
1401 | ||
1402 | PCRE_ERROR_BAD_PARTIAL (-13) | |
1403 | ||
1404 | The PCRE_PARTIAL option was used with a compiled pattern containing | |
1405 | items that are not supported for partial matching. See the pcrepartial | |
1406 | documentation for details of partial matching. | |
1407 | ||
1408 | PCRE_ERROR_INTERNAL (-14) | |
1409 | ||
1410 | An unexpected internal error has occurred. This error could be caused | |
1411 | by a bug in PCRE or by overwriting of the compiled pattern. | |
1412 | ||
1413 | PCRE_ERROR_BADCOUNT (-15) | |
1414 | ||
1415 | This error is given if the value of the ovecsize argument is negative. | |
1416 | ||
1417 | ||
1418 | EXTRACTING CAPTURED SUBSTRINGS BY NUMBER | |
1419 | ||
1420 | int pcre_copy_substring(const char *subject, int *ovector, | |
1421 | int stringcount, int stringnumber, char *buffer, | |
1422 | int buffersize); | |
1423 | ||
1424 | int pcre_get_substring(const char *subject, int *ovector, | |
1425 | int stringcount, int stringnumber, | |
1426 | const char **stringptr); | |
1427 | ||
1428 | int pcre_get_substring_list(const char *subject, | |
1429 | int *ovector, int stringcount, const char ***listptr); | |
1430 | ||
1431 | Captured substrings can be accessed directly by using the offsets | |
1432 | returned by pcre_exec() in ovector. For convenience, the functions | |
1433 | pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub- | |
1434 | string_list() are provided for extracting captured substrings as new, | |
1435 | separate, zero-terminated strings. These functions identify substrings | |
1436 | by number. The next section describes functions for extracting named | |
1437 | substrings. A substring that contains a binary zero is correctly | |
1438 | extracted and has a further zero added on the end, but the result is | |
1439 | not, of course, a C string. | |
1440 | ||
1441 | The first three arguments are the same for all three of these func- | |
1442 | tions: subject is the subject string that has just been successfully | |
1443 | matched, ovector is a pointer to the vector of integer offsets that was | |
1444 | passed to pcre_exec(), and stringcount is the number of substrings that | |
1445 | were captured by the match, including the substring that matched the | |
1446 | entire regular expression. This is the value returned by pcre_exec() if | |
1447 | it is greater than zero. If pcre_exec() returned zero, indicating that | |
1448 | it ran out of space in ovector, the value passed as stringcount should | |
1449 | be the number of elements in the vector divided by three. | |
1450 | ||
1451 | The functions pcre_copy_substring() and pcre_get_substring() extract a | |
1452 | single substring, whose number is given as stringnumber. A value of | |
1453 | zero extracts the substring that matched the entire pattern, whereas | |
1454 | higher values extract the captured substrings. For pcre_copy_sub- | |
1455 | string(), the string is placed in buffer, whose length is given by | |
1456 | buffersize, while for pcre_get_substring() a new block of memory is | |
1457 | obtained via pcre_malloc, and its address is returned via stringptr. | |
1458 | The yield of the function is the length of the string, not including | |
1459 | the terminating zero, or one of | |
1460 | ||
1461 | PCRE_ERROR_NOMEMORY (-6) | |
1462 | ||
1463 | The buffer was too small for pcre_copy_substring(), or the attempt to | |
1464 | get memory failed for pcre_get_substring(). | |
1465 | ||
1466 | PCRE_ERROR_NOSUBSTRING (-7) | |
1467 | ||
1468 | There is no substring whose number is stringnumber. | |
1469 | ||
1470 | The pcre_get_substring_list() function extracts all available sub- | |
1471 | strings and builds a list of pointers to them. All this is done in a | |
1472 | single block of memory that is obtained via pcre_malloc. The address of | |
1473 | the memory block is returned via listptr, which is also the start of | |
1474 | the list of string pointers. The end of the list is marked by a NULL | |
1475 | pointer. The yield of the function is zero if all went well, or | |
1476 | ||
1477 | PCRE_ERROR_NOMEMORY (-6) | |
1478 | ||
1479 | if the attempt to get the memory block failed. | |
1480 | ||
1481 | When any of these functions encounter a substring that is unset, which | |
1482 | can happen when capturing subpattern number n+1 matches some part of | |
1483 | the subject, but subpattern n has not been used at all, they return an | |
1484 | empty string. This can be distinguished from a genuine zero-length sub- | |
1485 | string by inspecting the appropriate offset in ovector, which is nega- | |
1486 | tive for unset substrings. | |
1487 | ||
1488 | The two convenience functions pcre_free_substring() and pcre_free_sub- | |
1489 | string_list() can be used to free the memory returned by a previous | |
1490 | call of pcre_get_substring() or pcre_get_substring_list(), respec- | |
1491 | tively. They do nothing more than call the function pointed to by | |
1492 | pcre_free, which of course could be called directly from a C program. | |
1493 | However, PCRE is used in some situations where it is linked via a spe- | |
1494 | cial interface to another programming language which cannot use | |
1495 | pcre_free directly; it is for these cases that the functions are | |
1496 | provided. | |
1497 | ||
1498 | ||
1499 | EXTRACTING CAPTURED SUBSTRINGS BY NAME | |
1500 | ||
1501 | int pcre_get_stringnumber(const pcre *code, | |
1502 | const char *name); | |
1503 | ||
1504 | int pcre_copy_named_substring(const pcre *code, | |
1505 | const char *subject, int *ovector, | |
1506 | int stringcount, const char *stringname, | |
1507 | char *buffer, int buffersize); | |
1508 | ||
1509 | int pcre_get_named_substring(const pcre *code, | |
1510 | const char *subject, int *ovector, | |
1511 | int stringcount, const char *stringname, | |
1512 | const char **stringptr); | |
1513 | ||
1514 | To extract a substring by name, you first have to find associated num- | |
1515 | ber. For example, for this pattern | |
1516 | ||
1517 | (a+)b(?<xxx>\d+)... | |
1518 | ||
1519 | the number of the subpattern called "xxx" is 2. You can find the number | |
1520 | from the name by calling pcre_get_stringnumber(). The first argument is | |
1521 | the compiled pattern, and the second is the name. The yield of the | |
1522 | function is the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if | |
1523 | there is no subpattern of that name. | |
1524 | ||
1525 | Given the number, you can extract the substring directly, or use one of | |
1526 | the functions described in the previous section. For convenience, there | |
1527 | are also two functions that do the whole job. | |
1528 | ||
1529 | Most of the arguments of pcre_copy_named_substring() and | |
1530 | pcre_get_named_substring() are the same as those for the similarly | |
1531 | named functions that extract by number. As these are described in the | |
1532 | previous section, they are not re-described here. There are just two | |
1533 | differences: | |
1534 | ||
1535 | First, instead of a substring number, a substring name is given. Sec- | |
1536 | ond, there is an extra argument, given at the start, which is a pointer | |
1537 | to the compiled pattern. This is needed in order to gain access to the | |
1538 | name-to-number translation table. | |
1539 | ||
1540 | These functions call pcre_get_stringnumber(), and if it succeeds, they | |
1541 | then call pcre_copy_substring() or pcre_get_substring(), as appropri- | |
1542 | ate. | |
1543 | ||
1544 | Last updated: 09 September 2004 | |
1545 | Copyright (c) 1997-2004 University of Cambridge. | |
1546 | ----------------------------------------------------------------------------- | |
1547 | ||
1548 | PCRE(3) PCRE(3) | |
1549 | ||
LIMITATIONS | ||
There are some size limitations in PCRE but it is hoped that | ||
they will never in practice be relevant. The maximum length | ||
of a compiled pattern is 65539 (sic) bytes. All values in | ||
repeating quantifiers must be less than 65536. The maximum | ||
number of capturing subpatterns is 99. The maximum number | ||
of all parenthesized subpatterns, including capturing sub- | ||
patterns, assertions, and other types of subpattern, is 200. | ||
The maximum length of a subject string is the largest posi- | ||
tive number that an integer variable can hold. However, PCRE | ||
uses recursion to handle subpatterns and indefinite repeti- | ||
tion. This means that the available stack space may limit | ||
the size of a subject string that can be processed by cer- | ||
tain patterns. | ||
DIFFERENCES FROM PERL | ||
The differences described here are with respect to Perl | ||
5.005. | ||
1. By default, a whitespace character is any character that | ||
the C library function isspace() recognizes, though it is | ||
possible to compile PCRE with alternative character type | ||
tables. Normally isspace() matches space, formfeed, newline, | ||
carriage return, horizontal tab, and vertical tab. Perl 5 no | ||
longer includes vertical tab in its set of whitespace char- | ||
acters. The \v escape that was in the Perl documentation for | ||
a long time was never in fact recognized. However, the char- | ||
acter itself was treated as whitespace at least up to 5.002. | ||
In 5.004 and 5.005 it does not match \s. | ||
2. PCRE does not allow repeat quantifiers on lookahead | ||
assertions. Perl permits them, but they do not mean what you | ||
might think. For example, (?!a){3} does not assert that the | ||
next three characters are not "a". It just asserts that the | ||
next character is not "a" three times. | ||
3. Capturing subpatterns that occur inside negative looka- | ||
head assertions are counted, but their entries in the | ||
offsets vector are never set. Perl sets its numerical vari- | ||
ables from any such patterns that are matched before the | ||
assertion fails to match something (thereby succeeding), but | ||
only if the negative lookahead assertion contains just one | ||
branch. | ||
4. Though binary zero characters are supported in the sub- | ||
ject string, they are not allowed in a pattern string | ||
because it is passed as a normal C string, terminated by | ||
zero. The escape sequence "\0" can be used in the pattern to | ||
represent a binary zero. | ||
5. The following Perl escape sequences are not supported: | ||
\l, \u, \L, \U, \E, \Q. In fact these are implemented by | ||
Perl's general string-handling and are not part of its pat- | ||
tern matching engine. | ||
6. The Perl \G assertion is not supported as it is not | ||
relevant to single pattern matches. | ||
7. Fairly obviously, PCRE does not support the (?{code}) and | ||
(?p{code}) constructions. However, there is some experimen- | ||
tal support for recursive patterns using the non-Perl item | ||
(?R). | ||
8. There are at the time of writing some oddities in Perl | ||
5.005_02 concerned with the settings of captured strings | ||
when part of a pattern is repeated. For example, matching | ||
"aba" against the pattern /^(a(b)?)+$/ sets $2 to the value | ||
"b", but matching "aabbaa" against /^(aa(bb)?)+$/ leaves $2 | ||
unset. However, if the pattern is changed to | ||
/^(aa(b(b))?)+$/ then $2 (and $3) are set. | ||
In Perl 5.004 $2 is set in both cases, and that is also true | ||
of PCRE. If in the future Perl changes to a consistent state | ||
that is different, PCRE may change to follow. | ||
9. Another as yet unresolved discrepancy is that in Perl | ||
5.005_02 the pattern /^(a)?(?(1)a|b)+$/ matches the string | ||
"a", whereas in PCRE it does not. However, in both Perl and | ||
PCRE /^(a)?a/ matched against "a" leaves $1 unset. | ||
10. PCRE provides some extensions to the Perl regular | ||
expression facilities: | ||
(a) Although lookbehind assertions must match fixed length | ||
strings, each alternative branch of a lookbehind assertion | ||
can match a different length of string. Perl 5.005 requires | ||
them all to have the same length. | ||
(b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not | ||
set, the $ meta- character matches only at the very end of | ||
the string. | ||
(c) If PCRE_EXTRA is set, a backslash followed by a letter | ||
with no special meaning is faulted. | ||
(d) If PCRE_UNGREEDY is set, the greediness of the repeti- | ||
tion quantifiers is inverted, that is, by default they are | ||
not greedy, but if followed by a question mark they are. | ||
(e) PCRE_ANCHORED can be used to force a pattern to be tried | ||
only at the start of the subject. | ||
(f) The PCRE_NOTBOL, PCRE_NOTEOL, and PCRE_NOTEMPTY options | ||
for pcre_exec() have no Perl equivalents. | ||
(g) The (?R) construct allows for recursive pattern matching | ||
(Perl 5.6 can do this using the (?p{code}) construct, which | ||
PCRE cannot of course support.) | ||
REGULAR EXPRESSION DETAILS | ||
The syntax and semantics of the regular expressions sup- | ||
ported by PCRE are described below. Regular expressions are | ||
also described in the Perl documentation and in a number of | ||
other books, some of which have copious examples. Jeffrey | ||
Friedl's "Mastering Regular Expressions", published by | ||
O'Reilly (ISBN 1-56592-257), covers them in great detail. | ||
The description here is intended as reference documentation. | ||
The basic operation of PCRE is on strings of bytes. However, | ||
there is the beginnings of some support for UTF-8 character | ||
strings. To use this support you must configure PCRE to | ||
include it, and then call pcre_compile() with the PCRE_UTF8 | ||
option. How this affects the pattern matching is described | ||
in the final section of this document. | ||
A regular expression is a pattern that is matched against a | ||
subject string from left to right. Most characters stand for | ||
themselves in a pattern, and match the corresponding charac- | ||
ters in the subject. As a trivial example, the pattern | ||
The quick brown fox | ||
matches a portion of a subject string that is identical to | ||
itself. The power of regular expressions comes from the | ||
ability to include alternatives and repetitions in the pat- | ||
tern. These are encoded in the pattern by the use of meta- | ||
characters, which do not stand for themselves but instead | ||
are interpreted in some special way. | ||
There are two different sets of meta-characters: those that | ||
are recognized anywhere in the pattern except within square | ||
brackets, and those that are recognized in square brackets. | ||
Outside square brackets, the meta-characters are as follows: | ||
\ general escape character with several uses | ||
^ assert start of subject (or line, in multiline | ||
mode) | ||
$ assert end of subject (or line, in multiline mode) | ||
. match any character except newline (by default) | ||
[ start character class definition | ||
| start of alternative branch | ||
( start subpattern | ||
) end subpattern | ||
? extends the meaning of ( | ||
also 0 or 1 quantifier | ||
also quantifier minimizer | ||
* 0 or more quantifier | ||
+ 1 or more quantifier | ||
{ start min/max quantifier | ||
Part of a pattern that is in square brackets is called a | ||
"character class". In a character class the only meta- | ||
characters are: | ||
\ general escape character | ||
^ negate the class, but only if the first character | ||
- indicates character range | ||
] terminates the character class | ||
1550 | ||
The following sections describe the use of each of the | ||
meta-characters. | ||
1551 | ||
1552 | NAME | |
1553 | PCRE - Perl-compatible regular expressions | |
1554 | ||
1555 | PCRE CALLOUTS | |
1556 | ||
1557 | int (*pcre_callout)(pcre_callout_block *); | |
1558 | ||
1559 | PCRE provides a feature called "callout", which is a means of temporar- | |
1560 | ily passing control to the caller of PCRE in the middle of pattern | |
1561 | matching. The caller of PCRE provides an external function by putting | |
1562 | its entry point in the global variable pcre_callout. By default, this | |
1563 | variable contains NULL, which disables all calling out. | |
1564 | ||
1565 | Within a regular expression, (?C) indicates the points at which the | |
1566 | external function is to be called. Different callout points can be | |
1567 | identified by putting a number less than 256 after the letter C. The | |
1568 | default value is zero. For example, this pattern has two callout | |
1569 | points: | |
1570 | ||
1571 | (?C1)eabc(?C2)def | |
1572 | ||
1573 | If the PCRE_AUTO_CALLOUT option bit is set when pcre_compile() is | |
1574 | called, PCRE automatically inserts callouts, all with number 255, | |
1575 | before each item in the pattern. For example, if PCRE_AUTO_CALLOUT is | |
1576 | used with the pattern | |
1577 | ||
1578 | A(\d{2}|--) | |
1579 | ||
1580 | it is processed as if it were | |
1581 | ||
1582 | (?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255) | |
1583 | ||
1584 | Notice that there is a callout before and after each parenthesis and | |
1585 | alternation bar. Automatic callouts can be used for tracking the | |
1586 | progress of pattern matching. The pcretest command has an option that | |
1587 | sets automatic callouts; when it is used, the output indicates how the | |
1588 | pattern is matched. This is useful information when you are trying to | |
1589 | optimize the performance of a particular pattern. | |
1590 | ||
1591 | ||
1592 | MISSING CALLOUTS | |
1593 | ||
1594 | You should be aware that, because of optimizations in the way PCRE | |
1595 | matches patterns, callouts sometimes do not happen. For example, if the | |
1596 | pattern is | |
1597 | ||
1598 | ab(?C4)cd | |
1599 | ||
1600 | PCRE knows that any matching string must contain the letter "d". If the | |
1601 | subject string is "abyz", the lack of "d" means that matching doesn't | |
1602 | ever start, and the callout is never reached. However, with "abyd", | |
1603 | though the result is still no match, the callout is obeyed. | |
1604 | ||
1605 | ||
1606 | THE CALLOUT INTERFACE | |
1607 | ||
1608 | During matching, when PCRE reaches a callout point, the external func- | |
1609 | tion defined by pcre_callout is called (if it is set). The only argu- | |
1610 | ment is a pointer to a pcre_callout block. This structure contains the | |
1611 | following fields: | |
1612 | ||
1613 | int version; | |
1614 | int callout_number; | |
1615 | int *offset_vector; | |
1616 | const char *subject; | |
1617 | int subject_length; | |
1618 | int start_match; | |
1619 | int current_position; | |
1620 | int capture_top; | |
1621 | int capture_last; | |
1622 | void *callout_data; | |
1623 | int pattern_position; | |
1624 | int next_item_length; | |
1625 | ||
1626 | The version field is an integer containing the version number of the | |
1627 | block format. The initial version was 0; the current version is 1. The | |
1628 | version number will change again in future if additional fields are | |
1629 | added, but the intention is never to remove any of the existing fields. | |
1630 | ||
1631 | The callout_number field contains the number of the callout, as com- | |
1632 | piled into the pattern (that is, the number after ?C for manual call- | |
1633 | outs, and 255 for automatically generated callouts). | |
1634 | ||
1635 | The offset_vector field is a pointer to the vector of offsets that was | |
1636 | passed by the caller to pcre_exec(). The contents can be inspected in | |
1637 | order to extract substrings that have been matched so far, in the same | |
1638 | way as for extracting substrings after a match has completed. | |
1639 | ||
1640 | The subject and subject_length fields contain copies of the values that | |
1641 | were passed to pcre_exec(). | |
1642 | ||
1643 | The start_match field contains the offset within the subject at which | |
1644 | the current match attempt started. If the pattern is not anchored, the | |
1645 | callout function may be called several times from the same point in the | |
1646 | pattern for different starting points in the subject. | |
1647 | ||
1648 | The current_position field contains the offset within the subject of | |
1649 | the current match pointer. | |
1650 | ||
1651 | The capture_top field contains one more than the number of the highest | |
1652 | numbered captured substring so far. If no substrings have been cap- | |
1653 | tured, the value of capture_top is one. | |
1654 | ||
1655 | The capture_last field contains the number of the most recently cap- | |
1656 | tured substring. If no substrings have been captured, its value is -1. | |
1657 | ||
1658 | The callout_data field contains a value that is passed to pcre_exec() | |
1659 | by the caller specifically so that it can be passed back in callouts. | |
1660 | It is passed in the pcre_callout field of the pcre_extra data struc- | |
1661 | ture. If no such data was passed, the value of callout_data in a | |
1662 | pcre_callout block is NULL. There is a description of the pcre_extra | |
1663 | structure in the pcreapi documentation. | |
1664 | ||
1665 | The pattern_position field is present from version 1 of the pcre_call- | |
1666 | out structure. It contains the offset to the next item to be matched in | |
1667 | the pattern string. | |
1668 | ||
1669 | The next_item_length field is present from version 1 of the pcre_call- | |
1670 | out structure. It contains the length of the next item to be matched in | |
1671 | the pattern string. When the callout immediately precedes an alterna- | |
1672 | tion bar, a closing parenthesis, or the end of the pattern, the length | |
1673 | is zero. When the callout precedes an opening parenthesis, the length | |
1674 | is that of the entire subpattern. | |
1675 | ||
1676 | The pattern_position and next_item_length fields are intended to help | |
1677 | in distinguishing between different automatic callouts, which all have | |
1678 | the same callout number. However, they are set for all callouts. | |
1679 | ||
1680 | ||
1681 | RETURN VALUES | |
1682 | ||
1683 | The external callout function returns an integer to PCRE. If the value | |
1684 | is zero, matching proceeds as normal. If the value is greater than | |
1685 | zero, matching fails at the current point, but backtracking to test | |
1686 | other matching possibilities goes ahead, just as if a lookahead asser- | |
1687 | tion had failed. If the value is less than zero, the match is aban- | |
1688 | doned, and pcre_exec() returns the negative value. | |
1689 | ||
1690 | Negative values should normally be chosen from the set of | |
1691 | PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan- | |
1692 | dard "no match" failure. The error number PCRE_ERROR_CALLOUT is | |
1693 | reserved for use by callout functions; it will never be used by PCRE | |
1694 | itself. | |
1695 | ||
1696 | Last updated: 09 September 2004 | |
1697 | Copyright (c) 1997-2004 University of Cambridge. | |
1698 | ----------------------------------------------------------------------------- | |
1699 | ||
1700 | PCRE(3) PCRE(3) | |
1701 | ||
1702 | ||
1703 | ||
1704 | NAME | |
1705 | PCRE - Perl-compatible regular expressions | |
1706 | ||
1707 | DIFFERENCES BETWEEN PCRE AND PERL | |
1708 | ||
1709 | This document describes the differences in the ways that PCRE and Perl | |
1710 | handle regular expressions. The differences described here are with | |
1711 | respect to Perl 5.8. | |
1712 | ||
1713 | 1. PCRE does not have full UTF-8 support. Details of what it does have | |
1714 | are given in the section on UTF-8 support in the main pcre page. | |
1715 | ||
1716 | 2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl | |
1717 | permits them, but they do not mean what you might think. For example, | |
1718 | (?!a){3} does not assert that the next three characters are not "a". It | |
1719 | just asserts that the next character is not "a" three times. | |
1720 | ||
1721 | 3. Capturing subpatterns that occur inside negative lookahead asser- | |
1722 | tions are counted, but their entries in the offsets vector are never | |
1723 | set. Perl sets its numerical variables from any such patterns that are | |
1724 | matched before the assertion fails to match something (thereby succeed- | |
1725 | ing), but only if the negative lookahead assertion contains just one | |
1726 | branch. | |
1727 | ||
1728 | 4. Though binary zero characters are supported in the subject string, | |
1729 | they are not allowed in a pattern string because it is passed as a nor- | |
1730 | mal C string, terminated by zero. The escape sequence \0 can be used in | |
1731 | the pattern to represent a binary zero. | |
1732 | ||
1733 | 5. The following Perl escape sequences are not supported: \l, \u, \L, | |
1734 | \U, and \N. In fact these are implemented by Perl's general string-han- | |
1735 | dling and are not part of its pattern matching engine. If any of these | |
1736 | are encountered by PCRE, an error is generated. | |
1737 | ||
1738 | 6. The Perl escape sequences \p, \P, and \X are supported only if PCRE | |
1739 | is built with Unicode character property support. The properties that | |
1740 | can be tested with \p and \P are limited to the general category prop- | |
1741 | erties such as Lu and Nd. | |
1742 | ||
1743 | 7. PCRE does support the \Q...\E escape for quoting substrings. Charac- | |
1744 | ters in between are treated as literals. This is slightly different | |
1745 | from Perl in that $ and @ are also handled as literals inside the | |
1746 | quotes. In Perl, they cause variable interpolation (but of course PCRE | |
1747 | does not have variables). Note the following examples: | |
1748 | ||
1749 | Pattern PCRE matches Perl matches | |
1750 | ||
1751 | \Qabc$xyz\E abc$xyz abc followed by the | |
1752 | contents of $xyz | |
1753 | \Qabc\$xyz\E abc\$xyz abc\$xyz | |
1754 | \Qabc\E\$\Qxyz\E abc$xyz abc$xyz | |
1755 | ||
1756 | The \Q...\E sequence is recognized both inside and outside character | |
1757 | classes. | |
1758 | ||
1759 | 8. Fairly obviously, PCRE does not support the (?{code}) and (?p{code}) | |
1760 | constructions. However, there is support for recursive patterns using | |
1761 | the non-Perl items (?R), (?number), and (?P>name). Also, the PCRE | |
1762 | "callout" feature allows an external function to be called during pat- | |
1763 | tern matching. See the pcrecallout documentation for details. | |
1764 | ||
1765 | 9. There are some differences that are concerned with the settings of | |
1766 | captured strings when part of a pattern is repeated. For example, | |
1767 | matching "aba" against the pattern /^(a(b)?)+$/ in Perl leaves $2 | |
1768 | unset, but in PCRE it is set to "b". | |
1769 | ||
1770 | 10. PCRE provides some extensions to the Perl regular expression facil- | |
1771 | ities: | |
1772 | ||
1773 | (a) Although lookbehind assertions must match fixed length strings, | |
1774 | each alternative branch of a lookbehind assertion can match a different | |
1775 | length of string. Perl requires them all to have the same length. | |
1776 | ||
1777 | (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ | |
1778 | meta-character matches only at the very end of the string. | |
1779 | ||
1780 | (c) If PCRE_EXTRA is set, a backslash followed by a letter with no spe- | |
1781 | cial meaning is faulted. | |
1782 | ||
1783 | (d) If PCRE_UNGREEDY is set, the greediness of the repetition quanti- | |
1784 | fiers is inverted, that is, by default they are not greedy, but if fol- | |
1785 | lowed by a question mark they are. | |
1786 | ||
1787 | (e) PCRE_ANCHORED can be used at matching time to force a pattern to be | |
1788 | tried only at the first matching position in the subject string. | |
1789 | ||
1790 | (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAP- | |
1791 | TURE options for pcre_exec() have no Perl equivalents. | |
1792 | ||
1793 | (g) The (?R), (?number), and (?P>name) constructs allows for recursive | |
1794 | pattern matching (Perl can do this using the (?p{code}) construct, | |
1795 | which PCRE cannot support.) | |
1796 | ||
1797 | (h) PCRE supports named capturing substrings, using the Python syntax. | |
1798 | ||
1799 | (i) PCRE supports the possessive quantifier "++" syntax, taken from | |
1800 | Sun's Java package. | |
1801 | ||
1802 | (j) The (R) condition, for testing recursion, is a PCRE extension. | |
1803 | ||
1804 | (k) The callout facility is PCRE-specific. | |
1805 | ||
1806 | (l) The partial matching facility is PCRE-specific. | |
1807 | ||
1808 | (m) Patterns compiled by PCRE can be saved and re-used at a later time, | |
1809 | even on different hosts that have the other endianness. | |
1810 | ||
1811 | Last updated: 09 September 2004 | |
1812 | Copyright (c) 1997-2004 University of Cambridge. | |
1813 | ----------------------------------------------------------------------------- | |
1814 | ||
1815 | PCRE(3) PCRE(3) | |
1816 | ||
1817 | ||
1818 | ||
1819 | NAME | |
1820 | PCRE - Perl-compatible regular expressions | |
1821 | ||
1822 | PCRE REGULAR EXPRESSION DETAILS | |
1823 | ||
1824 | The syntax and semantics of the regular expressions supported by PCRE | |
1825 | are described below. Regular expressions are also described in the Perl | |
1826 | documentation and in a number of books, some of which have copious | |
1827 | examples. Jeffrey Friedl's "Mastering Regular Expressions", published | |
1828 | by O'Reilly, covers regular expressions in great detail. This descrip- | |
1829 | tion of PCRE's regular expressions is intended as reference material. | |
1830 | ||
1831 | The original operation of PCRE was on strings of one-byte characters. | |
1832 | However, there is now also support for UTF-8 character strings. To use | |
1833 | this, you must build PCRE to include UTF-8 support, and then call | |
1834 | pcre_compile() with the PCRE_UTF8 option. How this affects pattern | |
1835 | matching is mentioned in several places below. There is also a summary | |
1836 | of UTF-8 features in the section on UTF-8 support in the main pcre | |
1837 | page. | |
1838 | ||
1839 | A regular expression is a pattern that is matched against a subject | |
1840 | string from left to right. Most characters stand for themselves in a | |
1841 | pattern, and match the corresponding characters in the subject. As a | |
1842 | trivial example, the pattern | |
1843 | ||
1844 | The quick brown fox | |
1845 | ||
1846 | matches a portion of a subject string that is identical to itself. The | |
1847 | power of regular expressions comes from the ability to include alterna- | |
1848 | tives and repetitions in the pattern. These are encoded in the pattern | |
1849 | by the use of metacharacters, which do not stand for themselves but | |
1850 | instead are interpreted in some special way. | |
1851 | ||
1852 | There are two different sets of metacharacters: those that are recog- | |
1853 | nized anywhere in the pattern except within square brackets, and those | |
1854 | that are recognized in square brackets. Outside square brackets, the | |
1855 | metacharacters are as follows: | |
1856 | ||
1857 | \ general escape character with several uses | |
1858 | ^ assert start of string (or line, in multiline mode) | |
1859 | $ assert end of string (or line, in multiline mode) | |
1860 | . match any character except newline (by default) | |
1861 | [ start character class definition | |
1862 | | start of alternative branch | |
1863 | ( start subpattern | |
1864 | ) end subpattern | |
1865 | ? extends the meaning of ( | |
1866 | also 0 or 1 quantifier | |
1867 | also quantifier minimizer | |
1868 | * 0 or more quantifier | |
1869 | + 1 or more quantifier | |
1870 | also "possessive quantifier" | |
1871 | { start min/max quantifier | |
1872 | ||
1873 | Part of a pattern that is in square brackets is called a "character | |
1874 | class". In a character class the only metacharacters are: | |
1875 | ||
1876 | \ general escape character | |
1877 | ^ negate the class, but only if the first character | |
1878 | - indicates character range | |
1879 | [ POSIX character class (only if followed by POSIX | |
1880 | syntax) | |
1881 | ] terminates the character class | |
1882 | ||
1883 | The following sections describe the use of each of the metacharacters. | |
1884 | ||
1885 | ||
1886 | BACKSLASH | BACKSLASH |
The backslash character has several uses. Firstly, if it is | ||
followed by a non-alphameric character, it takes away any | ||
special meaning that character may have. This use of | ||
backslash as an escape character applies both inside and | ||
outside character classes. | ||
For example, if you want to match a "*" character, you write | ||
"\*" in the pattern. This applies whether or not the follow- | ||
ing character would otherwise be interpreted as a meta- | ||
character, so it is always safe to precede a non-alphameric | ||
with "\" to specify that it stands for itself. In particu- | ||
lar, if you want to match a backslash, you write "\\". | ||
If a pattern is compiled with the PCRE_EXTENDED option, whi- | ||
tespace in the pattern (other than in a character class) and | ||
characters between a "#" outside a character class and the | ||
next newline character are ignored. An escaping backslash | ||
can be used to include a whitespace or "#" character as part | ||
of the pattern. | ||
A second use of backslash provides a way of encoding non- | ||
printing characters in patterns in a visible manner. There | ||
is no restriction on the appearance of non-printing charac- | ||
ters, apart from the binary zero that terminates a pattern, | ||
but when a pattern is being prepared by text editing, it is | ||
usually easier to use one of the following escape sequences | ||
than the binary character it represents: | ||
\a alarm, that is, the BEL character (hex 07) | ||
\cx "control-x", where x is any character | ||
\e escape (hex 1B) | ||
\f formfeed (hex 0C) | ||
\n newline (hex 0A) | ||
\r carriage return (hex 0D) | ||
\t tab (hex 09) | ||
\xhh character with hex code hh | ||
\ddd character with octal code ddd, or backreference | ||
The precise effect of "\cx" is as follows: if "x" is a lower | ||
case letter, it is converted to upper case. Then bit 6 of | ||
the character (hex 40) is inverted. Thus "\cz" becomes hex | ||
1A, but "\c{" becomes hex 3B, while "\c;" becomes hex 7B. | ||
After "\x", up to two hexadecimal digits are read (letters | ||
can be in upper or lower case). | ||
After "\0" up to two further octal digits are read. In both | ||
cases, if there are fewer than two digits, just those that | ||
are present are used. Thus the sequence "\0\x\07" specifies | ||
two binary zeros followed by a BEL character. Make sure you | ||
supply two digits after the initial zero if the character | ||
that follows is itself an octal digit. | ||
The handling of a backslash followed by a digit other than 0 | ||
is complicated. Outside a character class, PCRE reads it | ||
and any following digits as a decimal number. If the number | ||
is less than 10, or if there have been at least that many | ||
previous capturing left parentheses in the expression, the | ||
entire sequence is taken as a back reference. A description | ||
of how this works is given later, following the discussion | ||
of parenthesized subpatterns. | ||
Inside a character class, or if the decimal number is | ||
greater than 9 and there have not been that many capturing | ||
subpatterns, PCRE re-reads up to three octal digits follow- | ||
ing the backslash, and generates a single byte from the | ||
least significant 8 bits of the value. Any subsequent digits | ||
stand for themselves. For example: | ||
\040 is another way of writing a space | ||
\40 is the same, provided there are fewer than 40 | ||
previous capturing subpatterns | ||
\7 is always a back reference | ||
\11 might be a back reference, or another way of | ||
writing a tab | ||
\011 is always a tab | ||
\0113 is a tab followed by the character "3" | ||
\113 is the character with octal code 113 (since there | ||
can be no more than 99 back references) | ||
\377 is a byte consisting entirely of 1 bits | ||
\81 is either a back reference, or a binary zero | ||
followed by the two characters "8" and "1" | ||
Note that octal values of 100 or greater must not be intro- | ||
duced by a leading zero, because no more than three octal | ||
digits are ever read. | ||
All the sequences that define a single byte value can be | ||
used both inside and outside character classes. In addition, | ||
inside a character class, the sequence "\b" is interpreted | ||
as the backspace character (hex 08). Outside a character | ||
class it has a different meaning (see below). | ||
The third use of backslash is for specifying generic charac- | ||
ter types: | ||
\d any decimal digit | ||
\D any character that is not a decimal digit | ||
\s any whitespace character | ||
\S any character that is not a whitespace character | ||
\w any "word" character | ||
\W any "non-word" character | ||
Each pair of escape sequences partitions the complete set of | ||
characters into two disjoint sets. Any given character | ||
matches one, and only one, of each pair. | ||
A "word" character is any letter or digit or the underscore | ||
character, that is, any character which can be part of a | ||
Perl "word". The definition of letters and digits is con- | ||
trolled by PCRE's character tables, and may vary if locale- | ||
specific matching is taking place (see "Locale support" | ||
above). For example, in the "fr" (French) locale, some char- | ||
acter codes greater than 128 are used for accented letters, | ||
and these are matched by \w. | ||
These character type sequences can appear both inside and | ||
outside character classes. They each match one character of | ||
the appropriate type. If the current matching point is at | ||
the end of the subject string, all of them fail, since there | ||
is no character to match. | ||
The fourth use of backslash is for certain simple asser- | ||
tions. An assertion specifies a condition that has to be met | ||
at a particular point in a match, without consuming any | ||
characters from the subject string. The use of subpatterns | ||
for more complicated assertions is described below. The | ||
backslashed assertions are | ||
\b word boundary | ||
\B not a word boundary | ||
\A start of subject (independent of multiline mode) | ||
\Z end of subject or newline at end (independent of | ||
multiline mode) | ||
\z end of subject (independent of multiline mode) | ||
These assertions may not appear in character classes (but | ||
note that "\b" has a different meaning, namely the backspace | ||
character, inside a character class). | ||
A word boundary is a position in the subject string where | ||
the current character and the previous character do not both | ||
match \w or \W (i.e. one matches \w and the other matches | ||
\W), or the start or end of the string if the first or last | ||
character matches \w, respectively. | ||
The \A, \Z, and \z assertions differ from the traditional | ||
circumflex and dollar (described below) in that they only | ||
ever match at the very start and end of the subject string, | ||
whatever options are set. They are not affected by the | ||
PCRE_NOTBOL or PCRE_NOTEOL options. If the startoffset argu- | ||
ment of pcre_exec() is non-zero, \A can never match. The | ||
difference between \Z and \z is that \Z matches before a | ||
newline that is the last character of the string as well as | ||
at the end of the string, whereas \z matches only at the | ||
end. | ||
1887 | ||
1888 | The backslash character has several uses. Firstly, if it is followed by | |
1889 | a non-alphanumeric character, it takes away any special meaning that | |
1890 | character may have. This use of backslash as an escape character | |
1891 | applies both inside and outside character classes. | |
1892 | ||
1893 | For example, if you want to match a * character, you write \* in the | |
1894 | pattern. This escaping action applies whether or not the following | |
1895 | character would otherwise be interpreted as a metacharacter, so it is | |
1896 | always safe to precede a non-alphanumeric with backslash to specify | |
1897 | that it stands for itself. In particular, if you want to match a back- | |
1898 | slash, you write \\. | |
1899 | ||
1900 | If a pattern is compiled with the PCRE_EXTENDED option, whitespace in | |
1901 | the pattern (other than in a character class) and characters between a | |
1902 | # outside a character class and the next newline character are ignored. | |
1903 | An escaping backslash can be used to include a whitespace or # charac- | |
1904 | ter as part of the pattern. | |
1905 | ||
1906 | If you want to remove the special meaning from a sequence of charac- | |
1907 | ters, you can do so by putting them between \Q and \E. This is differ- | |
1908 | ent from Perl in that $ and @ are handled as literals in \Q...\E | |
1909 | sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- | |
1910 | tion. Note the following examples: | |
1911 | ||
1912 | Pattern PCRE matches Perl matches | |
1913 | ||
1914 | \Qabc$xyz\E abc$xyz abc followed by the | |
1915 | contents of $xyz | |
1916 | \Qabc\$xyz\E abc\$xyz abc\$xyz | |
1917 | \Qabc\E\$\Qxyz\E abc$xyz abc$xyz | |
1918 | ||
1919 | The \Q...\E sequence is recognized both inside and outside character | |
1920 | classes. | |
1921 | ||
1922 | Non-printing characters | |
1923 | ||
1924 | A second use of backslash provides a way of encoding non-printing char- | |
1925 | acters in patterns in a visible manner. There is no restriction on the | |
1926 | appearance of non-printing characters, apart from the binary zero that | |
1927 | terminates a pattern, but when a pattern is being prepared by text | |
1928 | editing, it is usually easier to use one of the following escape | |
1929 | sequences than the binary character it represents: | |
1930 | ||
1931 | \a alarm, that is, the BEL character (hex 07) | |
1932 | \cx "control-x", where x is any character | |
1933 | \e escape (hex 1B) | |
1934 | \f formfeed (hex 0C) | |
1935 | \n newline (hex 0A) | |
1936 | \r carriage return (hex 0D) | |
1937 | \t tab (hex 09) | |
1938 | \ddd character with octal code ddd, or backreference | |
1939 | \xhh character with hex code hh | |
1940 | \x{hhh..} character with hex code hhh... (UTF-8 mode only) | |
1941 | ||
1942 | The precise effect of \cx is as follows: if x is a lower case letter, | |
1943 | it is converted to upper case. Then bit 6 of the character (hex 40) is | |
1944 | inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; | |
1945 | becomes hex 7B. | |
1946 | ||
1947 | After \x, from zero to two hexadecimal digits are read (letters can be | |
1948 | in upper or lower case). In UTF-8 mode, any number of hexadecimal dig- | |
1949 | its may appear between \x{ and }, but the value of the character code | |
1950 | must be less than 2**31 (that is, the maximum hexadecimal value is | |
1951 | 7FFFFFFF). If characters other than hexadecimal digits appear between | |
1952 | \x{ and }, or if there is no terminating }, this form of escape is not | |
1953 | recognized. Instead, the initial \x will be interpreted as a basic hex- | |
1954 | adecimal escape, with no following digits, giving a character whose | |
1955 | value is zero. | |
1956 | ||
1957 | Characters whose value is less than 256 can be defined by either of the | |
1958 | two syntaxes for \x when PCRE is in UTF-8 mode. There is no difference | |
1959 | in the way they are handled. For example, \xdc is exactly the same as | |
1960 | \x{dc}. | |
1961 | ||
1962 | After \0 up to two further octal digits are read. In both cases, if | |
1963 | there are fewer than two digits, just those that are present are used. | |
1964 | Thus the sequence \0\x\07 specifies two binary zeros followed by a BEL | |
1965 | character (code value 7). Make sure you supply two digits after the | |
1966 | initial zero if the pattern character that follows is itself an octal | |
1967 | digit. | |
1968 | ||
1969 | The handling of a backslash followed by a digit other than 0 is compli- | |
1970 | cated. Outside a character class, PCRE reads it and any following dig- | |
1971 | its as a decimal number. If the number is less than 10, or if there | |
1972 | have been at least that many previous capturing left parentheses in the | |
1973 | expression, the entire sequence is taken as a back reference. A | |
1974 | description of how this works is given later, following the discussion | |
1975 | of parenthesized subpatterns. | |
1976 | ||
1977 | Inside a character class, or if the decimal number is greater than 9 | |
1978 | and there have not been that many capturing subpatterns, PCRE re-reads | |
1979 | up to three octal digits following the backslash, and generates a sin- | |
1980 | gle byte from the least significant 8 bits of the value. Any subsequent | |
1981 | digits stand for themselves. For example: | |
1982 | ||
1983 | \040 is another way of writing a space | |
1984 | \40 is the same, provided there are fewer than 40 | |
1985 | previous capturing subpatterns | |
1986 | \7 is always a back reference | |
1987 | \11 might be a back reference, or another way of | |
1988 | writing a tab | |
1989 | \011 is always a tab | |
1990 | \0113 is a tab followed by the character "3" | |
1991 | \113 might be a back reference, otherwise the | |
1992 | character with octal code 113 | |
1993 | \377 might be a back reference, otherwise | |
1994 | the byte consisting entirely of 1 bits | |
1995 | \81 is either a back reference, or a binary zero | |
1996 | followed by the two characters "8" and "1" | |
1997 | ||
1998 | Note that octal values of 100 or greater must not be introduced by a | |
1999 | leading zero, because no more than three octal digits are ever read. | |
2000 | ||
2001 | All the sequences that define a single byte value or a single UTF-8 | |
2002 | character (in UTF-8 mode) can be used both inside and outside character | |
2003 | classes. In addition, inside a character class, the sequence \b is | |
2004 | interpreted as the backspace character (hex 08), and the sequence \X is | |
2005 | interpreted as the character "X". Outside a character class, these | |
2006 | sequences have different meanings (see below). | |
2007 | ||
2008 | Generic character types | |
2009 | ||
2010 | The third use of backslash is for specifying generic character types. | |
2011 | The following are always recognized: | |
2012 | ||
2013 | \d any decimal digit | |
2014 | \D any character that is not a decimal digit | |
2015 | \s any whitespace character | |
2016 | \S any character that is not a whitespace character | |
2017 | \w any "word" character | |
2018 | \W any "non-word" character | |
2019 | ||
2020 | Each pair of escape sequences partitions the complete set of characters | |
2021 | into two disjoint sets. Any given character matches one, and only one, | |
2022 | of each pair. | |
2023 | ||
2024 | These character type sequences can appear both inside and outside char- | |
2025 | acter classes. They each match one character of the appropriate type. | |
2026 | If the current matching point is at the end of the subject string, all | |
2027 | of them fail, since there is no character to match. | |
2028 | ||
2029 | For compatibility with Perl, \s does not match the VT character (code | |
2030 | 11). This makes it different from the the POSIX "space" class. The \s | |
2031 | characters are HT (9), LF (10), FF (12), CR (13), and space (32). | |
2032 | ||
2033 | A "word" character is an underscore or any character less than 256 that | |
2034 | is a letter or digit. The definition of letters and digits is con- | |
2035 | trolled by PCRE's low-valued character tables, and may vary if locale- | |
2036 | specific matching is taking place (see "Locale support" in the pcreapi | |
2037 | page). For example, in the "fr_FR" (French) locale, some character | |
2038 | codes greater than 128 are used for accented letters, and these are | |
2039 | matched by \w. | |
2040 | ||
2041 | In UTF-8 mode, characters with values greater than 128 never match \d, | |
2042 | \s, or \w, and always match \D, \S, and \W. This is true even when Uni- | |
2043 | code character property support is available. | |
2044 | ||
2045 | Unicode character properties | |
2046 | ||
2047 | When PCRE is built with Unicode character property support, three addi- | |
2048 | tional escape sequences to match generic character types are available | |
2049 | when UTF-8 mode is selected. They are: | |
2050 | ||
2051 | \p{xx} a character with the xx property | |
2052 | \P{xx} a character without the xx property | |
2053 | \X an extended Unicode sequence | |
2054 | ||
2055 | The property names represented by xx above are limited to the Unicode | |
2056 | general category properties. Each character has exactly one such prop- | |
2057 | erty, specified by a two-letter abbreviation. For compatibility with | |
2058 | Perl, negation can be specified by including a circumflex between the | |
2059 | opening brace and the property name. For example, \p{^Lu} is the same | |
2060 | as \P{Lu}. | |
2061 | ||
2062 | If only one letter is specified with \p or \P, it includes all the | |
2063 | properties that start with that letter. In this case, in the absence of | |
2064 | negation, the curly brackets in the escape sequence are optional; these | |
2065 | two examples have the same effect: | |
2066 | ||
2067 | \p{L} | |
2068 | \pL | |
2069 | ||
2070 | The following property codes are supported: | |
2071 | ||
2072 | C Other | |
2073 | Cc Control | |
2074 | Cf Format | |
2075 | Cn Unassigned | |
2076 | Co Private use | |
2077 | Cs Surrogate | |
2078 | ||
2079 | L Letter | |
2080 | Ll Lower case letter | |
2081 | Lm Modifier letter | |
2082 | Lo Other letter | |
2083 | Lt Title case letter | |
2084 | Lu Upper case letter | |
2085 | ||
2086 | M Mark | |
2087 | Mc Spacing mark | |
2088 | Me Enclosing mark | |
2089 | Mn Non-spacing mark | |
2090 | ||
2091 | N Number | |
2092 | Nd Decimal number | |
2093 | Nl Letter number | |
2094 | No Other number | |
2095 | ||
2096 | P Punctuation | |
2097 | Pc Connector punctuation | |
2098 | Pd Dash punctuation | |
2099 | Pe Close punctuation | |
2100 | Pf Final punctuation | |
2101 | Pi Initial punctuation | |
2102 | Po Other punctuation | |
2103 | Ps Open punctuation | |
2104 | ||
2105 | S Symbol | |
2106 | Sc Currency symbol | |
2107 | Sk Modifier symbol | |
2108 | Sm Mathematical symbol | |
2109 | So Other symbol | |
2110 | ||
2111 | Z Separator | |
2112 | Zl Line separator | |
2113 | Zp Paragraph separator | |
2114 | Zs Space separator | |
2115 | ||
2116 | Extended properties such as "Greek" or "InMusicalSymbols" are not sup- | |
2117 | ported by PCRE. | |
2118 | ||
2119 | Specifying caseless matching does not affect these escape sequences. | |
2120 | For example, \p{Lu} always matches only upper case letters. | |
2121 | ||
2122 | The \X escape matches any number of Unicode characters that form an | |
2123 | extended Unicode sequence. \X is equivalent to | |
2124 | ||
2125 | (?>\PM\pM*) | |
2126 | ||
2127 | That is, it matches a character without the "mark" property, followed | |
2128 | by zero or more characters with the "mark" property, and treats the | |
2129 | sequence as an atomic group (see below). Characters with the "mark" | |
2130 | property are typically accents that affect the preceding character. | |
2131 | ||
2132 | Matching characters by Unicode property is not fast, because PCRE has | |
2133 | to search a structure that contains data for over fifteen thousand | |
2134 | characters. That is why the traditional escape sequences such as \d and | |
2135 | \w do not use Unicode properties in PCRE. | |
2136 | ||
2137 | Simple assertions | |
2138 | ||
2139 | The fourth use of backslash is for certain simple assertions. An asser- | |
2140 | tion specifies a condition that has to be met at a particular point in | |
2141 | a match, without consuming any characters from the subject string. The | |
2142 | use of subpatterns for more complicated assertions is described below. | |
2143 | The backslashed assertions are: | |
2144 | ||
2145 | \b matches at a word boundary | |
2146 | \B matches when not at a word boundary | |
2147 | \A matches at start of subject | |
2148 | \Z matches at end of subject or before newline at end | |
2149 | \z matches at end of subject | |
2150 | \G matches at first matching position in subject | |
2151 | ||
2152 | These assertions may not appear in character classes (but note that \b | |
2153 | has a different meaning, namely the backspace character, inside a char- | |
2154 | acter class). | |
2155 | ||
2156 | A word boundary is a position in the subject string where the current | |
2157 | character and the previous character do not both match \w or \W (i.e. | |
2158 | one matches \w and the other matches \W), or the start or end of the | |
2159 | string if the first or last character matches \w, respectively. | |
2160 | ||
2161 | The \A, \Z, and \z assertions differ from the traditional circumflex | |
2162 | and dollar (described in the next section) in that they only ever match | |
2163 | at the very start and end of the subject string, whatever options are | |
2164 | set. Thus, they are independent of multiline mode. These three asser- | |
2165 | tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which | |
2166 | affect only the behaviour of the circumflex and dollar metacharacters. | |
2167 | However, if the startoffset argument of pcre_exec() is non-zero, indi- | |
2168 | cating that matching is to start at a point other than the beginning of | |
2169 | the subject, \A can never match. The difference between \Z and \z is | |
2170 | that \Z matches before a newline that is the last character of the | |
2171 | string as well as at the end of the string, whereas \z matches only at | |
2172 | the end. | |
2173 | ||
2174 | The \G assertion is true only when the current matching position is at | |
2175 | the start point of the match, as specified by the startoffset argument | |
2176 | of pcre_exec(). It differs from \A when the value of startoffset is | |
2177 | non-zero. By calling pcre_exec() multiple times with appropriate argu- | |
2178 | ments, you can mimic Perl's /g option, and it is in this kind of imple- | |
2179 | mentation where \G can be useful. | |
2180 | ||
2181 | Note, however, that PCRE's interpretation of \G, as the start of the | |
2182 | current match, is subtly different from Perl's, which defines it as the | |
2183 | end of the previous match. In Perl, these can be different when the | |
2184 | previously matched string was empty. Because PCRE does just one match | |
2185 | at a time, it cannot reproduce this behaviour. | |
2186 | ||
2187 | If all the alternatives of a pattern begin with \G, the expression is | |
2188 | anchored to the starting match position, and the "anchored" flag is set | |
2189 | in the compiled regular expression. | |
2190 | ||
2191 | ||
2192 | CIRCUMFLEX AND DOLLAR | CIRCUMFLEX AND DOLLAR |
Outside a character class, in the default matching mode, the | ||
circumflex character is an assertion which is true only if | ||
the current matching point is at the start of the subject | ||
string. If the startoffset argument of pcre_exec() is non- | ||
zero, circumflex can never match. Inside a character class, | ||
circumflex has an entirely different meaning (see below). | ||
Circumflex need not be the first character of the pattern if | ||
a number of alternatives are involved, but it should be the | ||
first thing in each alternative in which it appears if the | ||
pattern is ever to match that branch. If all possible alter- | ||
natives start with a circumflex, that is, if the pattern is | ||
constrained to match only at the start of the subject, it is | ||
said to be an "anchored" pattern. (There are also other con- | ||
structs that can cause a pattern to be anchored.) | ||
A dollar character is an assertion which is true only if the | ||
current matching point is at the end of the subject string, | ||
or immediately before a newline character that is the last | ||
character in the string (by default). Dollar need not be the | ||
last character of the pattern if a number of alternatives | ||
are involved, but it should be the last item in any branch | ||
in which it appears. Dollar has no special meaning in a | ||
character class. | ||
The meaning of dollar can be changed so that it matches only | ||
at the very end of the string, by setting the | ||
PCRE_DOLLAR_ENDONLY option at compile or matching time. This | ||
does not affect the \Z assertion. | ||
The meanings of the circumflex and dollar characters are | ||
changed if the PCRE_MULTILINE option is set. When this is | ||
the case, they match immediately after and immediately | ||
before an internal "\n" character, respectively, in addition | ||
to matching at the start and end of the subject string. For | ||
example, the pattern /^abc$/ matches the subject string | ||
"def\nabc" in multiline mode, but not otherwise. Conse- | ||
quently, patterns that are anchored in single line mode | ||
because all branches start with "^" are not anchored in mul- | ||
tiline mode, and a match for circumflex is possible when the | ||
startoffset argument of pcre_exec() is non-zero. The | ||
PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is | ||
set. | ||
Note that the sequences \A, \Z, and \z can be used to match | ||
the start and end of the subject in both modes, and if all | ||
branches of a pattern start with \A is it always anchored, | ||
whether PCRE_MULTILINE is set or not. | ||
2193 | ||
2194 | Outside a character class, in the default matching mode, the circumflex | |
2195 | character is an assertion that is true only if the current matching | |
2196 | point is at the start of the subject string. If the startoffset argu- | |
2197 | ment of pcre_exec() is non-zero, circumflex can never match if the | |
2198 | PCRE_MULTILINE option is unset. Inside a character class, circumflex | |
2199 | has an entirely different meaning (see below). | |
2200 | ||
2201 | Circumflex need not be the first character of the pattern if a number | |
2202 | of alternatives are involved, but it should be the first thing in each | |
2203 | alternative in which it appears if the pattern is ever to match that | |
2204 | branch. If all possible alternatives start with a circumflex, that is, | |
2205 | if the pattern is constrained to match only at the start of the sub- | |
2206 | ject, it is said to be an "anchored" pattern. (There are also other | |
2207 | constructs that can cause a pattern to be anchored.) | |
2208 | ||
2209 | A dollar character is an assertion that is true only if the current | |
2210 | matching point is at the end of the subject string, or immediately | |
2211 | before a newline character that is the last character in the string (by | |
2212 | default). Dollar need not be the last character of the pattern if a | |
2213 | number of alternatives are involved, but it should be the last item in | |
2214 | any branch in which it appears. Dollar has no special meaning in a | |
2215 | character class. | |
2216 | ||
2217 | The meaning of dollar can be changed so that it matches only at the | |
2218 | very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at | |
2219 | compile time. This does not affect the \Z assertion. | |
2220 | ||
2221 | The meanings of the circumflex and dollar characters are changed if the | |
2222 | PCRE_MULTILINE option is set. When this is the case, they match immedi- | |
2223 | ately after and immediately before an internal newline character, | |
2224 | respectively, in addition to matching at the start and end of the sub- | |
2225 | ject string. For example, the pattern /^abc$/ matches the subject | |
2226 | string "def\nabc" (where \n represents a newline character) in multi- | |
2227 | line mode, but not otherwise. Consequently, patterns that are anchored | |
2228 | in single line mode because all branches start with ^ are not anchored | |
2229 | in multiline mode, and a match for circumflex is possible when the | |
2230 | startoffset argument of pcre_exec() is non-zero. The PCRE_DOL- | |
2231 | LAR_ENDONLY option is ignored if PCRE_MULTILINE is set. | |
2232 | ||
2233 | Note that the sequences \A, \Z, and \z can be used to match the start | |
2234 | and end of the subject in both modes, and if all branches of a pattern | |
2235 | start with \A it is always anchored, whether PCRE_MULTILINE is set or | |
2236 | not. | |
2237 | ||
2238 | ||
2239 | FULL STOP (PERIOD, DOT) | FULL STOP (PERIOD, DOT) |
Outside a character class, a dot in the pattern matches any | ||
one character in the subject, including a non-printing char- | ||
acter, but not (by default) newline. If the PCRE_DOTALL | ||
option is set, dots match newlines as well. The handling of | ||
dot is entirely independent of the handling of circumflex | ||
and dollar, the only relationship being that they both | ||
involve newline characters. Dot has no special meaning in a | ||
character class. | ||
SQUARE BRACKETS | ||
An opening square bracket introduces a character class, ter- | ||
minated by a closing square bracket. A closing square | ||
bracket on its own is not special. If a closing square | ||
bracket is required as a member of the class, it should be | ||
the first data character in the class (after an initial cir- | ||
cumflex, if present) or escaped with a backslash. | ||
A character class matches a single character in the subject; | ||
the character must be in the set of characters defined by | ||
the class, unless the first character in the class is a cir- | ||
cumflex, in which case the subject character must not be in | ||
the set defined by the class. If a circumflex is actually | ||
required as a member of the class, ensure it is not the | ||
first character, or escape it with a backslash. | ||
For example, the character class [aeiou] matches any lower | ||
case vowel, while [^aeiou] matches any character that is not | ||
a lower case vowel. Note that a circumflex is just a con- | ||
venient notation for specifying the characters which are in | ||
the class by enumerating those that are not. It is not an | ||
assertion: it still consumes a character from the subject | ||
string, and fails if the current pointer is at the end of | ||
the string. | ||
When caseless matching is set, any letters in a class | ||
represent both their upper case and lower case versions, so | ||
for example, a caseless [aeiou] matches "A" as well as "a", | ||
and a caseless [^aeiou] does not match "A", whereas a case- | ||
ful version would. | ||
The newline character is never treated in any special way in | ||
character classes, whatever the setting of the PCRE_DOTALL | ||
or PCRE_MULTILINE options is. A class such as [^a] will | ||
always match a newline. | ||
The minus (hyphen) character can be used to specify a range | ||
of characters in a character class. For example, [d-m] | ||
matches any letter between d and m, inclusive. If a minus | ||
character is required in a class, it must be escaped with a | ||
backslash or appear in a position where it cannot be inter- | ||
preted as indicating a range, typically as the first or last | ||
character in the class. | ||
It is not possible to have the literal character "]" as the | ||
end character of a range. A pattern such as [W-]46] is | ||
interpreted as a class of two characters ("W" and "-") fol- | ||
lowed by a literal string "46]", so it would match "W46]" or | ||
"-46]". However, if the "]" is escaped with a backslash it | ||
is interpreted as the end of range, so [W-\]46] is inter- | ||
preted as a single class containing a range followed by two | ||
separate characters. The octal or hexadecimal representation | ||
of "]" can also be used to end a range. | ||
Ranges operate in ASCII collating sequence. They can also be | ||
used for characters specified numerically, for example | ||
[\000-\037]. If a range that includes letters is used when | ||
caseless matching is set, it matches the letters in either | ||
case. For example, [W-c] is equivalent to [][\^_`wxyzabc], | ||
matched caselessly, and if character tables for the "fr" | ||
locale are in use, [\xc8-\xcb] matches accented E characters | ||
in both cases. | ||
The character types \d, \D, \s, \S, \w, and \W may also | ||
appear in a character class, and add the characters that | ||
they match to the class. For example, [\dABCDEF] matches any | ||
hexadecimal digit. A circumflex can conveniently be used | ||
with the upper case character types to specify a more res- | ||
tricted set of characters than the matching lower case type. | ||
For example, the class [^\W_] matches any letter or digit, | ||
but not underscore. | ||
All non-alphameric characters other than \, -, ^ (at the | ||
start) and the terminating ] are non-special in character | ||
classes, but it does no harm if they are escaped. | ||
2240 | ||
2241 | Outside a character class, a dot in the pattern matches any one charac- | |
2242 | ter in the subject, including a non-printing character, but not (by | |
2243 | default) newline. In UTF-8 mode, a dot matches any UTF-8 character, | |
2244 | which might be more than one byte long, except (by default) newline. If | |
2245 | the PCRE_DOTALL option is set, dots match newlines as well. The han- | |
2246 | dling of dot is entirely independent of the handling of circumflex and | |
2247 | dollar, the only relationship being that they both involve newline | |
2248 | characters. Dot has no special meaning in a character class. | |
2249 | ||
2250 | ||
2251 | MATCHING A SINGLE BYTE | |
2252 | ||
2253 | Outside a character class, the escape sequence \C matches any one byte, | |
2254 | both in and out of UTF-8 mode. Unlike a dot, it can match a newline. | |
2255 | The feature is provided in Perl in order to match individual bytes in | |
2256 | UTF-8 mode. Because it breaks up UTF-8 characters into individual | |
2257 | bytes, what remains in the string may be a malformed UTF-8 string. For | |
2258 | this reason, the \C escape sequence is best avoided. | |
2259 | ||
2260 | PCRE does not allow \C to appear in lookbehind assertions (described | |
2261 | below), because in UTF-8 mode this would make it impossible to calcu- | |
2262 | late the length of the lookbehind. | |
2263 | ||
2264 | ||
2265 | SQUARE BRACKETS AND CHARACTER CLASSES | |
2266 | ||
2267 | An opening square bracket introduces a character class, terminated by a | |
2268 | closing square bracket. A closing square bracket on its own is not spe- | |
2269 | cial. If a closing square bracket is required as a member of the class, | |
2270 | it should be the first data character in the class (after an initial | |
2271 | circumflex, if present) or escaped with a backslash. | |
2272 | ||
2273 | A character class matches a single character in the subject. In UTF-8 | |
2274 | mode, the character may occupy more than one byte. A matched character | |
2275 | must be in the set of characters defined by the class, unless the first | |
2276 | character in the class definition is a circumflex, in which case the | |
2277 | subject character must not be in the set defined by the class. If a | |
2278 | circumflex is actually required as a member of the class, ensure it is | |
2279 | not the first character, or escape it with a backslash. | |
2280 | ||
2281 | For example, the character class [aeiou] matches any lower case vowel, | |
2282 | while [^aeiou] matches any character that is not a lower case vowel. | |
2283 | Note that a circumflex is just a convenient notation for specifying the | |
2284 | characters that are in the class by enumerating those that are not. A | |
2285 | class that starts with a circumflex is not an assertion: it still con- | |
2286 | sumes a character from the subject string, and therefore it fails if | |
2287 | the current pointer is at the end of the string. | |
2288 | ||
2289 | In UTF-8 mode, characters with values greater than 255 can be included | |
2290 | in a class as a literal string of bytes, or by using the \x{ escaping | |
2291 | mechanism. | |
2292 | ||
2293 | When caseless matching is set, any letters in a class represent both | |
2294 | their upper case and lower case versions, so for example, a caseless | |
2295 | [aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not | |
2296 | match "A", whereas a caseful version would. When running in UTF-8 mode, | |
2297 | PCRE supports the concept of case for characters with values greater | |
2298 | than 128 only when it is compiled with Unicode property support. | |
2299 | ||
2300 | The newline character is never treated in any special way in character | |
2301 | classes, whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE | |
2302 | options is. A class such as [^a] will always match a newline. | |
2303 | ||
2304 | The minus (hyphen) character can be used to specify a range of charac- | |
2305 | ters in a character class. For example, [d-m] matches any letter | |
2306 | between d and m, inclusive. If a minus character is required in a | |
2307 | class, it must be escaped with a backslash or appear in a position | |
2308 | where it cannot be interpreted as indicating a range, typically as the | |
2309 | first or last character in the class. | |
2310 | ||
2311 | It is not possible to have the literal character "]" as the end charac- | |
2312 | ter of a range. A pattern such as [W-]46] is interpreted as a class of | |
2313 | two characters ("W" and "-") followed by a literal string "46]", so it | |
2314 | would match "W46]" or "-46]". However, if the "]" is escaped with a | |
2315 | backslash it is interpreted as the end of range, so [W-\]46] is inter- | |
2316 | preted as a class containing a range followed by two other characters. | |
2317 | The octal or hexadecimal representation of "]" can also be used to end | |
2318 | a range. | |
2319 | ||
2320 | Ranges operate in the collating sequence of character values. They can | |
2321 | also be used for characters specified numerically, for example | |
2322 | [\000-\037]. In UTF-8 mode, ranges can include characters whose values | |
2323 | are greater than 255, for example [\x{100}-\x{2ff}]. | |
2324 | ||
2325 | If a range that includes letters is used when caseless matching is set, | |
2326 | it matches the letters in either case. For example, [W-c] is equivalent | |
2327 | to [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if | |
2328 | character tables for the "fr_FR" locale are in use, [\xc8-\xcb] matches | |
2329 | accented E characters in both cases. In UTF-8 mode, PCRE supports the | |
2330 | concept of case for characters with values greater than 128 only when | |
2331 | it is compiled with Unicode property support. | |
2332 | ||
2333 | The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear | |
2334 | in a character class, and add the characters that they match to the | |
2335 | class. For example, [\dABCDEF] matches any hexadecimal digit. A circum- | |
2336 | flex can conveniently be used with the upper case character types to | |
2337 | specify a more restricted set of characters than the matching lower | |
2338 | case type. For example, the class [^\W_] matches any letter or digit, | |
2339 | but not underscore. | |
2340 | ||
2341 | The only metacharacters that are recognized in character classes are | |
2342 | backslash, hyphen (only where it can be interpreted as specifying a | |
2343 | range), circumflex (only at the start), opening square bracket (only | |
2344 | when it can be interpreted as introducing a POSIX class name - see the | |
2345 | next section), and the terminating closing square bracket. However, | |
2346 | escaping other non-alphanumeric characters does no harm. | |
2347 | ||
2348 | ||
2349 | POSIX CHARACTER CLASSES | POSIX CHARACTER CLASSES |
Perl 5.6 (not yet released at the time of writing) is going | ||
to support the POSIX notation for character classes, which | ||
uses names enclosed by [: and :] within the enclosing | ||
square brackets. PCRE supports this notation. For example, | ||
[01[:alpha:]%] | ||
matches "0", "1", any alphabetic character, or "%". The sup- | ||
ported class names are | ||
alnum letters and digits | ||
alpha letters | ||
ascii character codes 0 - 127 | ||
cntrl control characters | ||
digit decimal digits (same as \d) | ||
graph printing characters, excluding space | ||
lower lower case letters | ||
print printing characters, including space | ||
punct printing characters, excluding letters and digits | ||
space white space (same as \s) | ||
upper upper case letters | ||
word "word" characters (same as \w) | ||
xdigit hexadecimal digits | ||
The names "ascii" and "word" are Perl extensions. Another | ||
Perl extension is negation, which is indicated by a ^ char- | ||
acter after the colon. For example, | ||
[12[:^digit:]] | ||
matches "1", "2", or any non-digit. PCRE (and Perl) also | ||
recogize the POSIX syntax [.ch.] and [=ch=] where "ch" is a | ||
"collating element", but these are not supported, and an | ||
error is given if they are encountered. | ||
2350 | ||
2351 | Perl supports the POSIX notation for character classes. This uses names | |
2352 | enclosed by [: and :] within the enclosing square brackets. PCRE also | |
2353 | supports this notation. For example, | |
2354 | ||
2355 | [01[:alpha:]%] | |
2356 | ||
2357 | matches "0", "1", any alphabetic character, or "%". The supported class | |
2358 | names are | |
2359 | ||
2360 | alnum letters and digits | |
2361 | alpha letters | |
2362 | ascii character codes 0 - 127 | |
2363 | blank space or tab only | |
2364 | cntrl control characters | |
2365 | digit decimal digits (same as \d) | |
2366 | graph printing characters, excluding space | |
2367 | lower lower case letters | |
2368 | print printing characters, including space | |
2369 | punct printing characters, excluding letters and digits | |
2370 | space white space (not quite the same as \s) | |
2371 | upper upper case letters | |
2372 | word "word" characters (same as \w) | |
2373 | xdigit hexadecimal digits | |
2374 | ||
2375 | The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), | |
2376 | and space (32). Notice that this list includes the VT character (code | |
2377 | 11). This makes "space" different to \s, which does not include VT (for | |
2378 | Perl compatibility). | |
2379 | ||
2380 | The name "word" is a Perl extension, and "blank" is a GNU extension | |
2381 | from Perl 5.8. Another Perl extension is negation, which is indicated | |
2382 | by a ^ character after the colon. For example, | |
2383 | ||
2384 | [12[:^digit:]] | |
2385 | ||
2386 | matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the | |
2387 | POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but | |
2388 | these are not supported, and an error is given if they are encountered. | |
2389 | ||
2390 | In UTF-8 mode, characters with values greater than 128 do not match any | |
2391 | of the POSIX character classes. | |
2392 | ||
2393 | ||
2394 | VERTICAL BAR | VERTICAL BAR |
Vertical bar characters are used to separate alternative | ||
patterns. For example, the pattern | ||
2395 | ||
2396 | gilbert|sullivan | Vertical bar characters are used to separate alternative patterns. For |
2397 | example, the pattern | |
2398 | ||
2399 | matches either "gilbert" or "sullivan". Any number of alter- | gilbert|sullivan |
natives may appear, and an empty alternative is permitted | ||
(matching the empty string). The matching process tries | ||
each alternative in turn, from left to right, and the first | ||
one that succeeds is used. If the alternatives are within a | ||
subpattern (defined below), "succeeds" means matching the | ||
rest of the main pattern as well as the alternative in the | ||
subpattern. | ||
2400 | ||
2401 | matches either "gilbert" or "sullivan". Any number of alternatives may | |
2402 | appear, and an empty alternative is permitted (matching the empty | |
2403 | string). The matching process tries each alternative in turn, from | |
2404 | left to right, and the first one that succeeds is used. If the alterna- | |
2405 | tives are within a subpattern (defined below), "succeeds" means match- | |
2406 | ing the rest of the main pattern as well as the alternative in the sub- | |
2407 | pattern. | |
2408 | ||
2409 | ||
2410 | INTERNAL OPTION SETTING | INTERNAL OPTION SETTING |
The settings of PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, | ||
and PCRE_EXTENDED can be changed from within the pattern by | ||
a sequence of Perl option letters enclosed between "(?" and | ||
")". The option letters are | ||
i for PCRE_CASELESS | ||
m for PCRE_MULTILINE | ||
s for PCRE_DOTALL | ||
x for PCRE_EXTENDED | ||
For example, (?im) sets caseless, multiline matching. It is | ||
also possible to unset these options by preceding the letter | ||
with a hyphen, and a combined setting and unsetting such as | ||
(?im-sx), which sets PCRE_CASELESS and PCRE_MULTILINE while | ||
unsetting PCRE_DOTALL and PCRE_EXTENDED, is also permitted. | ||
If a letter appears both before and after the hyphen, the | ||
option is unset. | ||
The scope of these option changes depends on where in the | ||
pattern the setting occurs. For settings that are outside | ||
any subpattern (defined below), the effect is the same as if | ||
the options were set or unset at the start of matching. The | ||
following patterns all behave in exactly the same way: | ||
(?i)abc | ||
a(?i)bc | ||
ab(?i)c | ||
abc(?i) | ||
which in turn is the same as compiling the pattern abc with | ||
PCRE_CASELESS set. In other words, such "top level" set- | ||
tings apply to the whole pattern (unless there are other | ||
changes inside subpatterns). If there is more than one set- | ||
ting of the same option at top level, the rightmost setting | ||
is used. | ||
If an option change occurs inside a subpattern, the effect | ||
is different. This is a change of behaviour in Perl 5.005. | ||
An option change inside a subpattern affects only that part | ||
of the subpattern that follows it, so | ||
(a(?i)b)c | ||
matches abc and aBc and no other strings (assuming | ||
PCRE_CASELESS is not used). By this means, options can be | ||
made to have different settings in different parts of the | ||
pattern. Any changes made in one alternative do carry on | ||
into subsequent branches within the same subpattern. For | ||
example, | ||
(a(?i)b|c) | ||
matches "ab", "aB", "c", and "C", even though when matching | ||
"C" the first branch is abandoned before the option setting. | ||
This is because the effects of option settings happen at | ||
compile time. There would be some very weird behaviour oth- | ||
erwise. | ||
The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can | ||
be changed in the same way as the Perl-compatible options by | ||
using the characters U and X respectively. The (?X) flag | ||
setting is special in that it must always occur earlier in | ||
the pattern than any of the additional features it turns on, | ||
even when it is at top level. It is best put at the start. | ||
2411 | ||
2412 | The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and | |
2413 | PCRE_EXTENDED options can be changed from within the pattern by a | |
2414 | sequence of Perl option letters enclosed between "(?" and ")". The | |
2415 | option letters are | |
2416 | ||
2417 | i for PCRE_CASELESS | |
2418 | m for PCRE_MULTILINE | |
2419 | s for PCRE_DOTALL | |
2420 | x for PCRE_EXTENDED | |
2421 | ||
2422 | For example, (?im) sets caseless, multiline matching. It is also possi- | |
2423 | ble to unset these options by preceding the letter with a hyphen, and a | |
2424 | combined setting and unsetting such as (?im-sx), which sets PCRE_CASE- | |
2425 | LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, | |
2426 | is also permitted. If a letter appears both before and after the | |
2427 | hyphen, the option is unset. | |
2428 | ||
2429 | When an option change occurs at top level (that is, not inside subpat- | |
2430 | tern parentheses), the change applies to the remainder of the pattern | |
2431 | that follows. If the change is placed right at the start of a pattern, | |
2432 | PCRE extracts it into the global options (and it will therefore show up | |
2433 | in data extracted by the pcre_fullinfo() function). | |
2434 | ||
2435 | An option change within a subpattern affects only that part of the cur- | |
2436 | rent pattern that follows it, so | |
2437 | ||
2438 | (a(?i)b)c | |
2439 | ||
2440 | matches abc and aBc and no other strings (assuming PCRE_CASELESS is not | |
2441 | used). By this means, options can be made to have different settings | |
2442 | in different parts of the pattern. Any changes made in one alternative | |
2443 | do carry on into subsequent branches within the same subpattern. For | |
2444 | example, | |
2445 | ||
2446 | (a(?i)b|c) | |
2447 | ||
2448 | matches "ab", "aB", "c", and "C", even though when matching "C" the | |
2449 | first branch is abandoned before the option setting. This is because | |
2450 | the effects of option settings happen at compile time. There would be | |
2451 | some very weird behaviour otherwise. | |
2452 | ||
2453 | The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changed | |
2454 | in the same way as the Perl-compatible options by using the characters | |
2455 | U and X respectively. The (?X) flag setting is special in that it must | |
2456 | always occur earlier in the pattern than any of the additional features | |
2457 | it turns on, even when it is at top level. It is best to put it at the | |
2458 | start. | |
2459 | ||
2460 | ||
2461 | SUBPATTERNS | SUBPATTERNS |
Subpatterns are delimited by parentheses (round brackets), | ||
which can be nested. Marking part of a pattern as a subpat- | ||
tern does two things: | ||
1. It localizes a set of alternatives. For example, the pat- | ||
tern | ||
cat(aract|erpillar|) | ||
matches one of the words "cat", "cataract", or "caterpil- | ||
lar". Without the parentheses, it would match "cataract", | ||
"erpillar" or the empty string. | ||
2. It sets up the subpattern as a capturing subpattern (as | ||
defined above). When the whole pattern matches, that por- | ||
tion of the subject string that matched the subpattern is | ||
passed back to the caller via the ovector argument of | ||
pcre_exec(). Opening parentheses are counted from left to | ||
right (starting from 1) to obtain the numbers of the captur- | ||
ing subpatterns. | ||
For example, if the string "the red king" is matched against | ||
the pattern | ||
the ((red|white) (king|queen)) | ||
the captured substrings are "red king", "red", and "king", | ||
and are numbered 1, 2, and 3. | ||
The fact that plain parentheses fulfil two functions is not | ||
always helpful. There are often times when a grouping sub- | ||
pattern is required without a capturing requirement. If an | ||
opening parenthesis is followed by "?:", the subpattern does | ||
not do any capturing, and is not counted when computing the | ||
number of any subsequent capturing subpatterns. For example, | ||
if the string "the white queen" is matched against the pat- | ||
tern | ||
the ((?:red|white) (king|queen)) | ||
the captured substrings are "white queen" and "queen", and | ||
are numbered 1 and 2. The maximum number of captured sub- | ||
strings is 99, and the maximum number of all subpatterns, | ||
both capturing and non-capturing, is 200. | ||
As a convenient shorthand, if any option settings are | ||
required at the start of a non-capturing subpattern, the | ||
option letters may appear between the "?" and the ":". Thus | ||
the two patterns | ||
(?i:saturday|sunday) | ||
(?:(?i)saturday|sunday) | ||
match exactly the same set of strings. Because alternative | ||
branches are tried from left to right, and options are not | ||
reset until the end of the subpattern is reached, an option | ||
setting in one branch does affect subsequent branches, so | ||
the above patterns match "SUNDAY" as well as "Saturday". | ||
2462 | ||
2463 | Subpatterns are delimited by parentheses (round brackets), which can be | |
2464 | nested. Turning part of a pattern into a subpattern does two things: | |
2465 | ||
2466 | 1. It localizes a set of alternatives. For example, the pattern | |
2467 | ||
2468 | cat(aract|erpillar|) | |
2469 | ||
2470 | matches one of the words "cat", "cataract", or "caterpillar". Without | |
2471 | the parentheses, it would match "cataract", "erpillar" or the empty | |
2472 | string. | |
2473 | ||
2474 | 2. It sets up the subpattern as a capturing subpattern. This means | |
2475 | that, when the whole pattern matches, that portion of the subject | |
2476 | string that matched the subpattern is passed back to the caller via the | |
2477 | ovector argument of pcre_exec(). Opening parentheses are counted from | |
2478 | left to right (starting from 1) to obtain numbers for the capturing | |
2479 | subpatterns. | |
2480 | ||
2481 | For example, if the string "the red king" is matched against the pat- | |
2482 | tern | |
2483 | ||
2484 | the ((red|white) (king|queen)) | |
2485 | ||
2486 | the captured substrings are "red king", "red", and "king", and are num- | |
2487 | bered 1, 2, and 3, respectively. | |
2488 | ||
2489 | The fact that plain parentheses fulfil two functions is not always | |
2490 | helpful. There are often times when a grouping subpattern is required | |
2491 | without a capturing requirement. If an opening parenthesis is followed | |
2492 | by a question mark and a colon, the subpattern does not do any captur- | |
2493 | ing, and is not counted when computing the number of any subsequent | |
2494 | capturing subpatterns. For example, if the string "the white queen" is | |
2495 | matched against the pattern | |
2496 | ||
2497 | the ((?:red|white) (king|queen)) | |
2498 | ||
2499 | the captured substrings are "white queen" and "queen", and are numbered | |
2500 | 1 and 2. The maximum number of capturing subpatterns is 65535, and the | |
2501 | maximum depth of nesting of all subpatterns, both capturing and non- | |
2502 | capturing, is 200. | |
2503 | ||
2504 | As a convenient shorthand, if any option settings are required at the | |
2505 | start of a non-capturing subpattern, the option letters may appear | |
2506 | between the "?" and the ":". Thus the two patterns | |
2507 | ||
2508 | (?i:saturday|sunday) | |
2509 | (?:(?i)saturday|sunday) | |
2510 | ||
2511 | match exactly the same set of strings. Because alternative branches are | |
2512 | tried from left to right, and options are not reset until the end of | |
2513 | the subpattern is reached, an option setting in one branch does affect | |
2514 | subsequent branches, so the above patterns match "SUNDAY" as well as | |
2515 | "Saturday". | |
2516 | ||
2517 | ||
2518 | NAMED SUBPATTERNS | |
2519 | ||
2520 | Identifying capturing parentheses by number is simple, but it can be | |
2521 | very hard to keep track of the numbers in complicated regular expres- | |
2522 | sions. Furthermore, if an expression is modified, the numbers may | |
2523 | change. To help with this difficulty, PCRE supports the naming of sub- | |
2524 | patterns, something that Perl does not provide. The Python syntax | |
2525 | (?P<name>...) is used. Names consist of alphanumeric characters and | |
2526 | underscores, and must be unique within a pattern. | |
2527 | ||
2528 | Named capturing parentheses are still allocated numbers as well as | |
2529 | names. The PCRE API provides function calls for extracting the name-to- | |
2530 | number translation table from a compiled pattern. There is also a con- | |
2531 | venience function for extracting a captured substring by name. For fur- | |
2532 | ther details see the pcreapi documentation. | |
2533 | ||
2534 | ||
2535 | REPETITION | REPETITION |
Repetition is specified by quantifiers, which can follow any | ||
of the following items: | ||
2536 | ||
2537 | a single character, possibly escaped | Repetition is specified by quantifiers, which can follow any of the |
2538 | the . metacharacter | following items: |
2539 | a character class | |
2540 | a back reference (see next section) | a literal data character |
2541 | a parenthesized subpattern (unless it is an assertion - | the . metacharacter |
2542 | see below) | the \C escape sequence |
2543 | the \X escape sequence (in UTF-8 mode with Unicode properties) | |
2544 | The general repetition quantifier specifies a minimum and | an escape such as \d that matches a single character |
2545 | maximum number of permitted matches, by giving the two | a character class |
2546 | numbers in curly brackets (braces), separated by a comma. | a back reference (see next section) |
2547 | The numbers must be less than 65536, and the first must be | a parenthesized subpattern (unless it is an assertion) |
2548 | less than or equal to the second. For example: | |
2549 | The general repetition quantifier specifies a minimum and maximum num- | |
2550 | z{2,4} | ber of permitted matches, by giving the two numbers in curly brackets |
2551 | (braces), separated by a comma. The numbers must be less than 65536, | |
2552 | matches "zz", "zzz", or "zzzz". A closing brace on its own | and the first must be less than or equal to the second. For example: |
2553 | is not a special character. If the second number is omitted, | |
2554 | but the comma is present, there is no upper limit; if the | z{2,4} |
2555 | second number and the comma are both omitted, the quantifier | |
2556 | specifies an exact number of required matches. Thus | matches "zz", "zzz", or "zzzz". A closing brace on its own is not a |
2557 | special character. If the second number is omitted, but the comma is | |
2558 | [aeiou]{3,} | present, there is no upper limit; if the second number and the comma |
2559 | are both omitted, the quantifier specifies an exact number of required | |
2560 | matches at least 3 successive vowels, but may match many | matches. Thus |
2561 | more, while | |
2562 | [aeiou]{3,} | |
2563 | \d{8} | |
2564 | matches at least 3 successive vowels, but may match many more, while | |
2565 | matches exactly 8 digits. An opening curly bracket that | |
2566 | appears in a position where a quantifier is not allowed, or | \d{8} |
2567 | one that does not match the syntax of a quantifier, is taken | |
2568 | as a literal character. For example, {,6} is not a quantif- | matches exactly 8 digits. An opening curly bracket that appears in a |
2569 | ier, but a literal string of four characters. | position where a quantifier is not allowed, or one that does not match |
2570 | the syntax of a quantifier, is taken as a literal character. For exam- | |
2571 | The quantifier {0} is permitted, causing the expression to | ple, {,6} is not a quantifier, but a literal string of four characters. |
2572 | behave as if the previous item and the quantifier were not | |
2573 | present. | In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to |
2574 | individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char- | |
2575 | For convenience (and historical compatibility) the three | acters, each of which is represented by a two-byte sequence. Similarly, |
2576 | most common quantifiers have single-character abbreviations: | when Unicode property support is available, \X{3} matches three Unicode |
2577 | extended sequences, each of which may be several bytes long (and they | |
2578 | * is equivalent to {0,} | may be of different lengths). |
2579 | + is equivalent to {1,} | |
2580 | ? is equivalent to {0,1} | The quantifier {0} is permitted, causing the expression to behave as if |
2581 | the previous item and the quantifier were not present. | |
2582 | It is possible to construct infinite loops by following a | |
2583 | subpattern that can match no characters with a quantifier | For convenience (and historical compatibility) the three most common |
2584 | that has no upper limit, for example: | quantifiers have single-character abbreviations: |
2585 | ||
2586 | (a?)* | * is equivalent to {0,} |
2587 | + is equivalent to {1,} | |
2588 | Earlier versions of Perl and PCRE used to give an error at | ? is equivalent to {0,1} |
2589 | compile time for such patterns. However, because there are | |
2590 | cases where this can be useful, such patterns are now | It is possible to construct infinite loops by following a subpattern |
2591 | accepted, but if any repetition of the subpattern does in | that can match no characters with a quantifier that has no upper limit, |
2592 | fact match no characters, the loop is forcibly broken. | for example: |
2593 | ||
2594 | By default, the quantifiers are "greedy", that is, they | (a?)* |
2595 | match as much as possible (up to the maximum number of per- | |
2596 | mitted times), without causing the rest of the pattern to | Earlier versions of Perl and PCRE used to give an error at compile time |
2597 | fail. The classic example of where this gives problems is in | for such patterns. However, because there are cases where this can be |
2598 | trying to match comments in C programs. These appear between | useful, such patterns are now accepted, but if any repetition of the |
2599 | the sequences /* and */ and within the sequence, individual | subpattern does in fact match no characters, the loop is forcibly bro- |
2600 | * and / characters may appear. An attempt to match C com- | ken. |
2601 | ments by applying the pattern | |
2602 | By default, the quantifiers are "greedy", that is, they match as much | |
2603 | /\*.*\*/ | as possible (up to the maximum number of permitted times), without |
2604 | causing the rest of the pattern to fail. The classic example of where | |
2605 | to the string | this gives problems is in trying to match comments in C programs. These |
2606 | appear between /* and */ and within the comment, individual * and / | |
2607 | /* first command */ not comment /* second comment */ | characters may appear. An attempt to match C comments by applying the |
2608 | pattern | |
2609 | fails, because it matches the entire string due to the | |
2610 | greediness of the .* item. | /\*.*\*/ |
2611 | ||
2612 | However, if a quantifier is followed by a question mark, it | to the string |
2613 | ceases to be greedy, and instead matches the minimum number | |
2614 | of times possible, so the pattern | /* first comment */ not comment /* second comment */ |
2615 | ||
2616 | /\*.*?\*/ | fails, because it matches the entire string owing to the greediness of |
2617 | the .* item. | |
2618 | does the right thing with the C comments. The meaning of the | |
2619 | various quantifiers is not otherwise changed, just the pre- | However, if a quantifier is followed by a question mark, it ceases to |
2620 | ferred number of matches. Do not confuse this use of ques- | be greedy, and instead matches the minimum number of times possible, so |
2621 | tion mark with its use as a quantifier in its own right. | the pattern |
2622 | Because it has two uses, it can sometimes appear doubled, as | |
2623 | in | /\*.*?\*/ |
2624 | ||
2625 | \d??\d | does the right thing with the C comments. The meaning of the various |
2626 | quantifiers is not otherwise changed, just the preferred number of | |
2627 | which matches one digit by preference, but can match two if | matches. Do not confuse this use of question mark with its use as a |
2628 | that is the only way the rest of the pattern matches. | quantifier in its own right. Because it has two uses, it can sometimes |
2629 | appear doubled, as in | |
2630 | If the PCRE_UNGREEDY option is set (an option which is not | |
2631 | available in Perl), the quantifiers are not greedy by | \d??\d |
2632 | default, but individual ones can be made greedy by following | |
2633 | them with a question mark. In other words, it inverts the | which matches one digit by preference, but can match two if that is the |
2634 | default behaviour. | only way the rest of the pattern matches. |
2635 | ||
2636 | When a parenthesized subpattern is quantified with a minimum | If the PCRE_UNGREEDY option is set (an option which is not available in |
2637 | repeat count that is greater than 1 or with a limited max- | Perl), the quantifiers are not greedy by default, but individual ones |
2638 | imum, more store is required for the compiled pattern, in | can be made greedy by following them with a question mark. In other |
2639 | proportion to the size of the minimum or maximum. | words, it inverts the default behaviour. |
2640 | ||
2641 | If a pattern starts with .* or .{0,} and the PCRE_DOTALL | When a parenthesized subpattern is quantified with a minimum repeat |
2642 | option (equivalent to Perl's /s) is set, thus allowing the . | count that is greater than 1 or with a limited maximum, more memory is |
2643 | to match newlines, the pattern is implicitly anchored, | required for the compiled pattern, in proportion to the size of the |
2644 | because whatever follows will be tried against every charac- | minimum or maximum. |
2645 | ter position in the subject string, so there is no point in | |
2646 | retrying the overall match at any position after the first. | If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv- |
2647 | PCRE treats such a pattern as though it were preceded by \A. | alent to Perl's /s) is set, thus allowing the . to match newlines, the |
2648 | In cases where it is known that the subject string contains | pattern is implicitly anchored, because whatever follows will be tried |
2649 | no newlines, it is worth setting PCRE_DOTALL when the pat- | against every character position in the subject string, so there is no |
2650 | tern begins with .* in order to obtain this optimization, or | point in retrying the overall match at any position after the first. |
2651 | alternatively using ^ to indicate anchoring explicitly. | PCRE normally treats such a pattern as though it were preceded by \A. |
2652 | ||
2653 | When a capturing subpattern is repeated, the value captured | In cases where it is known that the subject string contains no new- |
2654 | is the substring that matched the final iteration. For exam- | lines, it is worth setting PCRE_DOTALL in order to obtain this opti- |
2655 | ple, after | mization, or alternatively using ^ to indicate anchoring explicitly. |
2656 | ||
2657 | (tweedle[dume]{3}\s*)+ | However, there is one situation where the optimization cannot be used. |
2658 | When .* is inside capturing parentheses that are the subject of a | |
2659 | has matched "tweedledum tweedledee" the value of the cap- | backreference elsewhere in the pattern, a match at the start may fail, |
2660 | tured substring is "tweedledee". However, if there are | and a later one succeed. Consider, for example: |
nested capturing subpatterns, the corresponding captured | ||
values may have been set in previous iterations. For exam- | ||
ple, after | ||
2661 | ||
2662 | /(a|(b))+/ | (.*)abc\1 |
2663 | ||
2664 | matches "aba" the value of the second captured substring is | If the subject is "xyz123abc123" the match point is the fourth charac- |
2665 | "b". | ter. For this reason, such a pattern is not implicitly anchored. |
2666 | ||
2667 | When a capturing subpattern is repeated, the value captured is the sub- | |
2668 | string that matched the final iteration. For example, after | |
2669 | ||
2670 | (tweedle[dume]{3}\s*)+ | |
2671 | ||
2672 | has matched "tweedledum tweedledee" the value of the captured substring | |
2673 | is "tweedledee". However, if there are nested capturing subpatterns, | |
2674 | the corresponding captured values may have been set in previous itera- | |
2675 | tions. For example, after | |
2676 | ||
2677 | /(a|(b))+/ | |