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