6 |
separate text files for the pcregrep and pcretest commands. |
separate text files for the pcregrep and pcretest commands. |
7 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
8 |
|
|
|
PCRE(3) PCRE(3) |
|
|
|
|
9 |
|
|
10 |
|
|
11 |
NAME |
NAME |
12 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
13 |
|
|
14 |
|
|
15 |
INTRODUCTION |
INTRODUCTION |
16 |
|
|
17 |
The PCRE library is a set of functions that implement regular expres- |
The PCRE library is a set of functions that implement regular expres- |
18 |
sion pattern matching using the same syntax and semantics as Perl, with |
sion pattern matching using the same syntax and semantics as Perl, with |
19 |
just a few differences. The current implementation of PCRE (release |
just a few differences. The current implementation of PCRE (release |
20 |
5.x) corresponds approximately with Perl 5.8, including support for |
6.x) corresponds approximately with Perl 5.8, including support for |
21 |
UTF-8 encoded strings and Unicode general category properties. However, |
UTF-8 encoded strings and Unicode general category properties. However, |
22 |
this support has to be explicitly enabled; it is not the default. |
this support has to be explicitly enabled; it is not the default. |
23 |
|
|
24 |
|
In addition to the Perl-compatible matching function, PCRE also con- |
25 |
|
tains an alternative matching function that matches the same compiled |
26 |
|
patterns in a different way. In certain circumstances, the alternative |
27 |
|
function has some advantages. For a discussion of the two matching |
28 |
|
algorithms, see the pcrematching page. |
29 |
|
|
30 |
PCRE is written in C and released as a C library. A number of people |
PCRE is written in C and released as a C library. A number of people |
31 |
have written wrappers and interfaces of various kinds. A C++ class is |
have written wrappers and interfaces of various kinds. In particular, |
32 |
included in these contributions, which can be found in the Contrib |
Google Inc. have provided a comprehensive C++ wrapper. This is now |
33 |
directory at the primary FTP site, which is: |
included as part of the PCRE distribution. The pcrecpp page has details |
34 |
|
of this interface. Other people's contributions can be found in the |
35 |
|
Contrib directory at the primary FTP site, which is: |
36 |
|
|
37 |
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre |
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre |
38 |
|
|
47 |
ing PCRE for various operating systems can be found in the README file |
ing PCRE for various operating systems can be found in the README file |
48 |
in the source distribution. |
in the source distribution. |
49 |
|
|
50 |
|
The library contains a number of undocumented internal functions and |
51 |
|
data tables that are used by more than one of the exported external |
52 |
|
functions, but which are not intended for use by external callers. |
53 |
|
Their names all begin with "_pcre_", which hopefully will not provoke |
54 |
|
any name clashes. |
55 |
|
|
56 |
|
|
57 |
USER DOCUMENTATION |
USER DOCUMENTATION |
58 |
|
|
63 |
of searching. The sections are as follows: |
of searching. The sections are as follows: |
64 |
|
|
65 |
pcre this document |
pcre this document |
66 |
pcreapi details of PCRE's native API |
pcreapi details of PCRE's native C API |
67 |
pcrebuild options for building PCRE |
pcrebuild options for building PCRE |
68 |
pcrecallout details of the callout feature |
pcrecallout details of the callout feature |
69 |
pcrecompat discussion of Perl compatibility |
pcrecompat discussion of Perl compatibility |
70 |
|
pcrecpp details of the C++ wrapper |
71 |
pcregrep description of the pcregrep command |
pcregrep description of the pcregrep command |
72 |
|
pcrematching discussion of the two matching algorithms |
73 |
pcrepartial details of the partial matching facility |
pcrepartial details of the partial matching facility |
74 |
pcrepattern syntax and semantics of supported |
pcrepattern syntax and semantics of supported |
75 |
regular expressions |
regular expressions |
76 |
pcreperform discussion of performance issues |
pcreperform discussion of performance issues |
77 |
pcreposix the POSIX-compatible API |
pcreposix the POSIX-compatible C API |
78 |
pcreprecompile details of saving and re-using precompiled patterns |
pcreprecompile details of saving and re-using precompiled patterns |
79 |
pcresample discussion of the sample program |
pcresample discussion of the sample program |
80 |
pcretest description of the pcretest testing command |
pcretest description of the pcretest testing command |
81 |
|
|
82 |
In addition, in the "man" and HTML formats, there is a short page for |
In addition, in the "man" and HTML formats, there is a short page for |
83 |
each library function, listing its arguments and results. |
each C library function, listing its arguments and results. |
84 |
|
|
85 |
|
|
86 |
LIMITATIONS |
LIMITATIONS |
105 |
tern, is 200. |
tern, is 200. |
106 |
|
|
107 |
The maximum length of a subject string is the largest positive number |
The maximum length of a subject string is the largest positive number |
108 |
that an integer variable can hold. However, PCRE uses recursion to han- |
that an integer variable can hold. However, when using the traditional |
109 |
dle subpatterns and indefinite repetition. This means that the avail- |
matching function, PCRE uses recursion to handle subpatterns and indef- |
110 |
able stack space may limit the size of a subject string that can be |
inite repetition. This means that the available stack space may limit |
111 |
processed by certain patterns. |
the size of a subject string that can be processed by certain patterns. |
112 |
|
|
113 |
|
|
114 |
UTF-8 AND UNICODE PROPERTY SUPPORT |
UTF-8 AND UNICODE PROPERTY SUPPORT |
115 |
|
|
116 |
From release 3.3, PCRE has had some support for character strings |
From release 3.3, PCRE has had some support for character strings |
117 |
encoded in the UTF-8 format. For release 4.0 this was greatly extended |
encoded in the UTF-8 format. For release 4.0 this was greatly extended |
118 |
to cover most common requirements, and in release 5.0 additional sup- |
to cover most common requirements, and in release 5.0 additional sup- |
119 |
port for Unicode general category properties was added. |
port for Unicode general category properties was added. |
120 |
|
|
121 |
In order process UTF-8 strings, you must build PCRE to include UTF-8 |
In order process UTF-8 strings, you must build PCRE to include UTF-8 |
122 |
support in the code, and, in addition, you must call pcre_compile() |
support in the code, and, in addition, you must call pcre_compile() |
123 |
with the PCRE_UTF8 option flag. When you do this, both the pattern and |
with the PCRE_UTF8 option flag. When you do this, both the pattern and |
124 |
any subject strings that are matched against it are treated as UTF-8 |
any subject strings that are matched against it are treated as UTF-8 |
125 |
strings instead of just strings of bytes. |
strings instead of just strings of bytes. |
126 |
|
|
127 |
If you compile PCRE with UTF-8 support, but do not use it at run time, |
If you compile PCRE with UTF-8 support, but do not use it at run time, |
128 |
the library will be a bit bigger, but the additional run time overhead |
the library will be a bit bigger, but the additional run time overhead |
129 |
is limited to testing the PCRE_UTF8 flag in several places, so should |
is limited to testing the PCRE_UTF8 flag in several places, so should |
130 |
not be very large. |
not be very large. |
131 |
|
|
132 |
If PCRE is built with Unicode character property support (which implies |
If PCRE is built with Unicode character property support (which implies |
133 |
UTF-8 support), the escape sequences \p{..}, \P{..}, and \X are sup- |
UTF-8 support), the escape sequences \p{..}, \P{..}, and \X are sup- |
134 |
ported. The available properties that can be tested are limited to the |
ported. The available properties that can be tested are limited to the |
135 |
general category properties such as Lu for an upper case letter or Nd |
general category properties such as Lu for an upper case letter or Nd |
136 |
for a decimal number. A full list is given in the pcrepattern documen- |
for a decimal number. A full list is given in the pcrepattern documen- |
137 |
tation. The PCRE library is increased in size by about 90K when Unicode |
tation. The PCRE library is increased in size by about 90K when Unicode |
138 |
property support is included. |
property support is included. |
139 |
|
|
140 |
The following comments apply when PCRE is running in UTF-8 mode: |
The following comments apply when PCRE is running in UTF-8 mode: |
141 |
|
|
142 |
1. When you set the PCRE_UTF8 flag, the strings passed as patterns and |
1. When you set the PCRE_UTF8 flag, the strings passed as patterns and |
143 |
subjects are checked for validity on entry to the relevant functions. |
subjects are checked for validity on entry to the relevant functions. |
144 |
If an invalid UTF-8 string is passed, an error return is given. In some |
If an invalid UTF-8 string is passed, an error return is given. In some |
145 |
situations, you may already know that your strings are valid, and |
situations, you may already know that your strings are valid, and |
146 |
therefore want to skip these checks in order to improve performance. If |
therefore want to skip these checks in order to improve performance. If |
147 |
you set the PCRE_NO_UTF8_CHECK flag at compile time or at run time, |
you set the PCRE_NO_UTF8_CHECK flag at compile time or at run time, |
148 |
PCRE assumes that the pattern or subject it is given (respectively) |
PCRE assumes that the pattern or subject it is given (respectively) |
149 |
contains only valid UTF-8 codes. In this case, it does not diagnose an |
contains only valid UTF-8 codes. In this case, it does not diagnose an |
150 |
invalid UTF-8 string. If you pass an invalid UTF-8 string to PCRE when |
invalid UTF-8 string. If you pass an invalid UTF-8 string to PCRE when |
151 |
PCRE_NO_UTF8_CHECK is set, the results are undefined. Your program may |
PCRE_NO_UTF8_CHECK is set, the results are undefined. Your program may |
152 |
crash. |
crash. |
153 |
|
|
154 |
2. In a pattern, the escape sequence \x{...}, where the contents of the |
2. In a pattern, the escape sequence \x{...}, where the contents of the |
155 |
braces is a string of hexadecimal digits, is interpreted as a UTF-8 |
braces is a string of hexadecimal digits, is interpreted as a UTF-8 |
156 |
character whose code number is the given hexadecimal number, for exam- |
character whose code number is the given hexadecimal number, for exam- |
157 |
ple: \x{1234}. If a non-hexadecimal digit appears between the braces, |
ple: \x{1234}. If a non-hexadecimal digit appears between the braces, |
158 |
the item is not recognized. This escape sequence can be used either as |
the item is not recognized. This escape sequence can be used either as |
159 |
a literal, or within a character class. |
a literal, or within a character class. |
160 |
|
|
161 |
3. The original hexadecimal escape sequence, \xhh, matches a two-byte |
3. The original hexadecimal escape sequence, \xhh, matches a two-byte |
162 |
UTF-8 character if the value is greater than 127. |
UTF-8 character if the value is greater than 127. |
163 |
|
|
164 |
4. Repeat quantifiers apply to complete UTF-8 characters, not to indi- |
4. Repeat quantifiers apply to complete UTF-8 characters, not to indi- |
165 |
vidual bytes, for example: \x{100}{3}. |
vidual bytes, for example: \x{100}{3}. |
166 |
|
|
167 |
5. The dot metacharacter matches one UTF-8 character instead of a sin- |
5. The dot metacharacter matches one UTF-8 character instead of a sin- |
168 |
gle byte. |
gle byte. |
169 |
|
|
170 |
6. The escape sequence \C can be used to match a single byte in UTF-8 |
6. The escape sequence \C can be used to match a single byte in UTF-8 |
171 |
mode, but its use can lead to some strange effects. |
mode, but its use can lead to some strange effects. This facility is |
172 |
|
not available in the alternative matching function, pcre_dfa_exec(). |
173 |
|
|
174 |
7. The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly |
7. The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly |
175 |
test characters of any code value, but the characters that PCRE recog- |
test characters of any code value, but the characters that PCRE recog- |
193 |
|
|
194 |
AUTHOR |
AUTHOR |
195 |
|
|
196 |
Philip Hazel <ph10@cam.ac.uk> |
Philip Hazel |
197 |
University Computing Service, |
University Computing Service, |
198 |
Cambridge CB2 3QG, England. |
Cambridge CB2 3QG, England. |
|
Phone: +44 1223 334714 |
|
199 |
|
|
200 |
Last updated: 09 September 2004 |
Putting an actual email address here seems to have been a spam magnet, |
201 |
Copyright (c) 1997-2004 University of Cambridge. |
so I've taken it away. If you want to email me, use my initial and sur- |
202 |
----------------------------------------------------------------------------- |
name, separated by a dot, at the domain ucs.cam.ac.uk. |
203 |
|
|
204 |
PCRE(3) PCRE(3) |
Last updated: 07 March 2005 |
205 |
|
Copyright (c) 1997-2005 University of Cambridge. |
206 |
|
----------------------------------------------------------------------------- |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
NAME |
NAME |
211 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
212 |
|
|
213 |
|
|
214 |
PCRE BUILD-TIME OPTIONS |
PCRE BUILD-TIME OPTIONS |
215 |
|
|
216 |
This document describes the optional features of PCRE that can be |
This document describes the optional features of PCRE that can be |
305 |
LIMITING PCRE RESOURCE USAGE |
LIMITING PCRE RESOURCE USAGE |
306 |
|
|
307 |
Internally, PCRE has a function called match(), which it calls repeat- |
Internally, PCRE has a function called match(), which it calls repeat- |
308 |
edly (possibly recursively) when matching a pattern. By controlling the |
edly (possibly recursively) when matching a pattern with the |
309 |
maximum number of times this function may be called during a single |
pcre_exec() function. By controlling the maximum number of times this |
310 |
matching operation, a limit can be placed on the resources used by a |
function may be called during a single matching operation, a limit can |
311 |
single call to pcre_exec(). The limit can be changed at run time, as |
be placed on the resources used by a single call to pcre_exec(). The |
312 |
described in the pcreapi documentation. The default is 10 million, but |
limit can be changed at run time, as described in the pcreapi documen- |
313 |
this can be changed by adding a setting such as |
tation. The default is 10 million, but this can be changed by adding a |
314 |
|
setting such as |
315 |
|
|
316 |
--with-match-limit=500000 |
--with-match-limit=500000 |
317 |
|
|
318 |
to the configure command. |
to the configure command. This setting has no effect on the |
319 |
|
pcre_dfa_exec() matching function. |
320 |
|
|
321 |
|
|
322 |
HANDLING VERY LARGE PATTERNS |
HANDLING VERY LARGE PATTERNS |
344 |
|
|
345 |
AVOIDING EXCESSIVE STACK USAGE |
AVOIDING EXCESSIVE STACK USAGE |
346 |
|
|
347 |
PCRE implements backtracking while matching by making recursive calls |
When matching with the pcre_exec() function, PCRE implements backtrack- |
348 |
to an internal function called match(). In environments where the size |
ing by making recursive calls to an internal function called match(). |
349 |
of the stack is limited, this can severely limit PCRE's operation. (The |
In environments where the size of the stack is limited, this can se- |
350 |
Unix environment does not usually suffer from this problem.) An alter- |
verely limit PCRE's operation. (The Unix environment does not usually |
351 |
native approach that uses memory from the heap to remember data, |
suffer from this problem.) An alternative approach that uses memory |
352 |
instead of using recursive function calls, has been implemented to work |
from the heap to remember data, instead of using recursive function |
353 |
round this problem. If you want to build a version of PCRE that works |
calls, has been implemented to work round this problem. If you want to |
354 |
this way, add |
build a version of PCRE that works this way, add |
355 |
|
|
356 |
--disable-stack-for-recursion |
--disable-stack-for-recursion |
357 |
|
|
362 |
the blocks are always freed in reverse order. A calling program might |
the blocks are always freed in reverse order. A calling program might |
363 |
be able to implement optimized functions that perform better than the |
be able to implement optimized functions that perform better than the |
364 |
standard malloc() and free() functions. PCRE runs noticeably more |
standard malloc() and free() functions. PCRE runs noticeably more |
365 |
slowly when built in this way. |
slowly when built in this way. This option affects only the pcre_exec() |
366 |
|
function; it is not relevant for the the pcre_dfa_exec() function. |
367 |
|
|
368 |
|
|
369 |
USING EBCDIC CODE |
USING EBCDIC CODE |
370 |
|
|
371 |
PCRE assumes by default that it will run in an environment where the |
PCRE assumes by default that it will run in an environment where the |
372 |
character code is ASCII (or Unicode, which is a superset of ASCII). |
character code is ASCII (or Unicode, which is a superset of ASCII). |
373 |
PCRE can, however, be compiled to run in an EBCDIC environment by |
PCRE can, however, be compiled to run in an EBCDIC environment by |
374 |
adding |
adding |
375 |
|
|
376 |
--enable-ebcdic |
--enable-ebcdic |
377 |
|
|
378 |
to the configure command. |
to the configure command. |
379 |
|
|
380 |
Last updated: 09 September 2004 |
Last updated: 28 February 2005 |
381 |
Copyright (c) 1997-2004 University of Cambridge. |
Copyright (c) 1997-2005 University of Cambridge. |
382 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
383 |
|
|
384 |
PCRE(3) PCRE(3) |
|
385 |
|
|
386 |
|
NAME |
387 |
|
PCRE - Perl-compatible regular expressions |
388 |
|
|
389 |
|
|
390 |
|
PCRE MATCHING ALGORITHMS |
391 |
|
|
392 |
|
This document describes the two different algorithms that are available |
393 |
|
in PCRE for matching a compiled regular expression against a given sub- |
394 |
|
ject string. The "standard" algorithm is the one provided by the |
395 |
|
pcre_exec() function. This works in the same was as Perl's matching |
396 |
|
function, and provides a Perl-compatible matching operation. |
397 |
|
|
398 |
|
An alternative algorithm is provided by the pcre_dfa_exec() function; |
399 |
|
this operates in a different way, and is not Perl-compatible. It has |
400 |
|
advantages and disadvantages compared with the standard algorithm, and |
401 |
|
these are described below. |
402 |
|
|
403 |
|
When there is only one possible way in which a given subject string can |
404 |
|
match a pattern, the two algorithms give the same answer. A difference |
405 |
|
arises, however, when there are multiple possibilities. For example, if |
406 |
|
the pattern |
407 |
|
|
408 |
|
^<.*> |
409 |
|
|
410 |
|
is matched against the string |
411 |
|
|
412 |
|
<something> <something else> <something further> |
413 |
|
|
414 |
|
there are three possible answers. The standard algorithm finds only one |
415 |
|
of them, whereas the DFA algorithm finds all three. |
416 |
|
|
417 |
|
|
418 |
|
REGULAR EXPRESSIONS AS TREES |
419 |
|
|
420 |
|
The set of strings that are matched by a regular expression can be rep- |
421 |
|
resented as a tree structure. An unlimited repetition in the pattern |
422 |
|
makes the tree of infinite size, but it is still a tree. Matching the |
423 |
|
pattern to a given subject string (from a given starting point) can be |
424 |
|
thought of as a search of the tree. There are two standard ways to |
425 |
|
search a tree: depth-first and breadth-first, and these correspond to |
426 |
|
the two matching algorithms provided by PCRE. |
427 |
|
|
428 |
|
|
429 |
|
THE STANDARD MATCHING ALGORITHM |
430 |
|
|
431 |
|
In the terminology of Jeffrey Friedl's book Mastering Regular Expres- |
432 |
|
sions, the standard algorithm is an "NFA algorithm". It conducts a |
433 |
|
depth-first search of the pattern tree. That is, it proceeds along a |
434 |
|
single path through the tree, checking that the subject matches what is |
435 |
|
required. When there is a mismatch, the algorithm tries any alterna- |
436 |
|
tives at the current point, and if they all fail, it backs up to the |
437 |
|
previous branch point in the tree, and tries the next alternative |
438 |
|
branch at that level. This often involves backing up (moving to the |
439 |
|
left) in the subject string as well. The order in which repetition |
440 |
|
branches are tried is controlled by the greedy or ungreedy nature of |
441 |
|
the quantifier. |
442 |
|
|
443 |
|
If a leaf node is reached, a matching string has been found, and at |
444 |
|
that point the algorithm stops. Thus, if there is more than one possi- |
445 |
|
ble match, this algorithm returns the first one that it finds. Whether |
446 |
|
this is the shortest, the longest, or some intermediate length depends |
447 |
|
on the way the greedy and ungreedy repetition quantifiers are specified |
448 |
|
in the pattern. |
449 |
|
|
450 |
|
Because it ends up with a single path through the tree, it is rela- |
451 |
|
tively straightforward for this algorithm to keep track of the sub- |
452 |
|
strings that are matched by portions of the pattern in parentheses. |
453 |
|
This provides support for capturing parentheses and back references. |
454 |
|
|
455 |
|
|
456 |
|
THE DFA MATCHING ALGORITHM |
457 |
|
|
458 |
|
DFA stands for "deterministic finite automaton", but you do not need to |
459 |
|
understand the origins of that name. This algorithm conducts a breadth- |
460 |
|
first search of the tree. Starting from the first matching point in the |
461 |
|
subject, it scans the subject string from left to right, once, charac- |
462 |
|
ter by character, and as it does this, it remembers all the paths |
463 |
|
through the tree that represent valid matches. |
464 |
|
|
465 |
|
The scan continues until either the end of the subject is reached, or |
466 |
|
there are no more unterminated paths. At this point, terminated paths |
467 |
|
represent the different matching possibilities (if there are none, the |
468 |
|
match has failed). Thus, if there is more than one possible match, |
469 |
|
this algorithm finds all of them, and in particular, it finds the long- |
470 |
|
est. In PCRE, there is an option to stop the algorithm after the first |
471 |
|
match (which is necessarily the shortest) has been found. |
472 |
|
|
473 |
|
Note that all the matches that are found start at the same point in the |
474 |
|
subject. If the pattern |
475 |
|
|
476 |
|
cat(er(pillar)?) |
477 |
|
|
478 |
|
is matched against the string "the caterpillar catchment", the result |
479 |
|
will be the three strings "cat", "cater", and "caterpillar" that start |
480 |
|
at the fourth character of the subject. The algorithm does not automat- |
481 |
|
ically move on to find matches that start at later positions. |
482 |
|
|
483 |
|
There are a number of features of PCRE regular expressions that are not |
484 |
|
supported by the DFA matching algorithm. They are as follows: |
485 |
|
|
486 |
|
1. Because the algorithm finds all possible matches, the greedy or |
487 |
|
ungreedy nature of repetition quantifiers is not relevant. Greedy and |
488 |
|
ungreedy quantifiers are treated in exactly the same way. |
489 |
|
|
490 |
|
2. When dealing with multiple paths through the tree simultaneously, it |
491 |
|
is not straightforward to keep track of captured substrings for the |
492 |
|
different matching possibilities, and PCRE's implementation of this |
493 |
|
algorithm does not attempt to do this. This means that no captured sub- |
494 |
|
strings are available. |
495 |
|
|
496 |
|
3. Because no substrings are captured, back references within the pat- |
497 |
|
tern are not supported, and cause errors if encountered. |
498 |
|
|
499 |
|
4. For the same reason, conditional expressions that use a backrefer- |
500 |
|
ence as the condition are not supported. |
501 |
|
|
502 |
|
5. Callouts are supported, but the value of the capture_top field is |
503 |
|
always 1, and the value of the capture_last field is always -1. |
504 |
|
|
505 |
|
6. The \C escape sequence, which (in the standard algorithm) matches a |
506 |
|
single byte, even in UTF-8 mode, is not supported because the DFA algo- |
507 |
|
rithm moves through the subject string one character at a time, for all |
508 |
|
active paths through the tree. |
509 |
|
|
510 |
|
|
511 |
|
ADVANTAGES OF THE DFA ALGORITHM |
512 |
|
|
513 |
|
Using the DFA matching algorithm provides the following advantages: |
514 |
|
|
515 |
|
1. All possible matches (at a single point in the subject) are automat- |
516 |
|
ically found, and in particular, the longest match is found. To find |
517 |
|
more than one match using the standard algorithm, you have to do kludgy |
518 |
|
things with callouts. |
519 |
|
|
520 |
|
2. There is much better support for partial matching. The restrictions |
521 |
|
on the content of the pattern that apply when using the standard algo- |
522 |
|
rithm for partial matching do not apply to the DFA algorithm. For non- |
523 |
|
anchored patterns, the starting position of a partial match is avail- |
524 |
|
able. |
525 |
|
|
526 |
|
3. Because the DFA algorithm scans the subject string just once, and |
527 |
|
never needs to backtrack, it is possible to pass very long subject |
528 |
|
strings to the matching function in several pieces, checking for par- |
529 |
|
tial matching each time. |
530 |
|
|
531 |
|
|
532 |
|
DISADVANTAGES OF THE DFA ALGORITHM |
533 |
|
|
534 |
|
The DFA algorithm suffers from a number of disadvantages: |
535 |
|
|
536 |
|
1. It is substantially slower than the standard algorithm. This is |
537 |
|
partly because it has to search for all possible matches, but is also |
538 |
|
because it is less susceptible to optimization. |
539 |
|
|
540 |
|
2. Capturing parentheses and back references are not supported. |
541 |
|
|
542 |
|
3. The "atomic group" feature of PCRE regular expressions is supported, |
543 |
|
but does not provide the advantage that it does for the standard algo- |
544 |
|
rithm. |
545 |
|
|
546 |
|
Last updated: 28 February 2005 |
547 |
|
Copyright (c) 1997-2005 University of Cambridge. |
548 |
|
----------------------------------------------------------------------------- |
549 |
|
|
550 |
|
|
551 |
|
|
552 |
NAME |
NAME |
553 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
554 |
|
|
555 |
|
|
556 |
PCRE NATIVE API |
PCRE NATIVE API |
557 |
|
|
558 |
#include <pcre.h> |
#include <pcre.h> |
561 |
const char **errptr, int *erroffset, |
const char **errptr, int *erroffset, |
562 |
const unsigned char *tableptr); |
const unsigned char *tableptr); |
563 |
|
|
564 |
|
pcre *pcre_compile2(const char *pattern, int options, |
565 |
|
int *errorcodeptr, |
566 |
|
const char **errptr, int *erroffset, |
567 |
|
const unsigned char *tableptr); |
568 |
|
|
569 |
pcre_extra *pcre_study(const pcre *code, int options, |
pcre_extra *pcre_study(const pcre *code, int options, |
570 |
const char **errptr); |
const char **errptr); |
571 |
|
|
573 |
const char *subject, int length, int startoffset, |
const char *subject, int length, int startoffset, |
574 |
int options, int *ovector, int ovecsize); |
int options, int *ovector, int ovecsize); |
575 |
|
|
576 |
|
int pcre_dfa_exec(const pcre *code, const pcre_extra *extra, |
577 |
|
const char *subject, int length, int startoffset, |
578 |
|
int options, int *ovector, int ovecsize, |
579 |
|
int *workspace, int wscount); |
580 |
|
|
581 |
int pcre_copy_named_substring(const pcre *code, |
int pcre_copy_named_substring(const pcre *code, |
582 |
const char *subject, int *ovector, |
const char *subject, int *ovector, |
583 |
int stringcount, const char *stringname, |
int stringcount, const char *stringname, |
613 |
|
|
614 |
int pcre_info(const pcre *code, int *optptr, int *firstcharptr); |
int pcre_info(const pcre *code, int *optptr, int *firstcharptr); |
615 |
|
|
616 |
|
int pcre_refcount(pcre *code, int adjust); |
617 |
|
|
618 |
int pcre_config(int what, void *where); |
int pcre_config(int what, void *where); |
619 |
|
|
620 |
char *pcre_version(void); |
char *pcre_version(void); |
634 |
|
|
635 |
PCRE has its own native API, which is described in this document. There |
PCRE has its own native API, which is described in this document. There |
636 |
is also a set of wrapper functions that correspond to the POSIX regular |
is also a set of wrapper functions that correspond to the POSIX regular |
637 |
expression API. These are described in the pcreposix documentation. |
expression API. These are described in the pcreposix documentation. |
638 |
|
Both of these APIs define a set of C function calls. A C++ wrapper is |
639 |
|
distributed with PCRE. It is documented in the pcrecpp page. |
640 |
|
|
641 |
The native API function prototypes are defined in the header file |
The native API C function prototypes are defined in the header file |
642 |
pcre.h, and on Unix systems the library itself is called libpcre. It |
pcre.h, and on Unix systems the library itself is called libpcre. It |
643 |
can normally be accessed by adding -lpcre to the command for linking an |
can normally be accessed by adding -lpcre to the command for linking an |
644 |
application that uses PCRE. The header file defines the macros |
application that uses PCRE. The header file defines the macros |
645 |
PCRE_MAJOR and PCRE_MINOR to contain the major and minor release num- |
PCRE_MAJOR and PCRE_MINOR to contain the major and minor release num- |
646 |
bers for the library. Applications can use these to include support |
bers for the library. Applications can use these to include support |
647 |
for different releases of PCRE. |
for different releases of PCRE. |
648 |
|
|
649 |
The functions pcre_compile(), pcre_study(), and pcre_exec() are used |
The functions pcre_compile(), pcre_compile2(), pcre_study(), and |
650 |
for compiling and matching regular expressions. A sample program that |
pcre_exec() are used for compiling and matching regular expressions in |
651 |
demonstrates the simplest way of using them is provided in the file |
a Perl-compatible manner. A sample program that demonstrates the sim- |
652 |
called pcredemo.c in the source distribution. The pcresample documenta- |
plest way of using them is provided in the file called pcredemo.c in |
653 |
tion describes how to run it. |
the source distribution. The pcresample documentation describes how to |
654 |
|
run it. |
655 |
In addition to the main compiling and matching functions, there are |
|
656 |
convenience functions for extracting captured substrings from a matched |
A second matching function, pcre_dfa_exec(), which is not Perl-compati- |
657 |
subject string. They are: |
ble, is also provided. This uses a different algorithm for the match- |
658 |
|
ing. This allows it to find all possible matches (at a given point in |
659 |
|
the subject), not just one. However, this algorithm does not return |
660 |
|
captured substrings. A description of the two matching algorithms and |
661 |
|
their advantages and disadvantages is given in the pcrematching docu- |
662 |
|
mentation. |
663 |
|
|
664 |
|
In addition to the main compiling and matching functions, there are |
665 |
|
convenience functions for extracting captured substrings from a subject |
666 |
|
string that is matched by pcre_exec(). They are: |
667 |
|
|
668 |
pcre_copy_substring() |
pcre_copy_substring() |
669 |
pcre_copy_named_substring() |
pcre_copy_named_substring() |
675 |
pcre_free_substring() and pcre_free_substring_list() are also provided, |
pcre_free_substring() and pcre_free_substring_list() are also provided, |
676 |
to free the memory used for extracted strings. |
to free the memory used for extracted strings. |
677 |
|
|
678 |
The function pcre_maketables() is used to build a set of character |
The function pcre_maketables() is used to build a set of character |
679 |
tables in the current locale for passing to pcre_compile() or |
tables in the current locale for passing to pcre_compile(), |
680 |
pcre_exec(). This is an optional facility that is provided for spe- |
pcre_exec(), or pcre_dfa_exec(). This is an optional facility that is |
681 |
cialist use. Most commonly, no special tables are passed, in which case |
provided for specialist use. Most commonly, no special tables are |
682 |
internal tables that are generated when PCRE is built are used. |
passed, in which case internal tables that are generated when PCRE is |
683 |
|
built are used. |
684 |
|
|
685 |
The function pcre_fullinfo() is used to find out information about a |
The function pcre_fullinfo() is used to find out information about a |
686 |
compiled pattern; pcre_info() is an obsolete version that returns only |
compiled pattern; pcre_info() is an obsolete version that returns only |
688 |
patibility. The function pcre_version() returns a pointer to a string |
patibility. The function pcre_version() returns a pointer to a string |
689 |
containing the version of PCRE and its date of release. |
containing the version of PCRE and its date of release. |
690 |
|
|
691 |
|
The function pcre_refcount() maintains a reference count in a data |
692 |
|
block containing a compiled pattern. This is provided for the benefit |
693 |
|
of object-oriented applications. |
694 |
|
|
695 |
The global variables pcre_malloc and pcre_free initially contain the |
The global variables pcre_malloc and pcre_free initially contain the |
696 |
entry points of the standard malloc() and free() functions, respec- |
entry points of the standard malloc() and free() functions, respec- |
697 |
tively. PCRE calls the memory management functions via these variables, |
tively. PCRE calls the memory management functions via these variables, |
701 |
The global variables pcre_stack_malloc and pcre_stack_free are also |
The global variables pcre_stack_malloc and pcre_stack_free are also |
702 |
indirections to memory management functions. These special functions |
indirections to memory management functions. These special functions |
703 |
are used only when PCRE is compiled to use the heap for remembering |
are used only when PCRE is compiled to use the heap for remembering |
704 |
data, instead of recursive function calls. This is a non-standard way |
data, instead of recursive function calls, when running the pcre_exec() |
705 |
of building PCRE, for use in environments that have limited stacks. |
function. This is a non-standard way of building PCRE, for use in envi- |
706 |
Because of the greater use of memory management, it runs more slowly. |
ronments that have limited stacks. Because of the greater use of memory |
707 |
Separate functions are provided so that special-purpose external code |
management, it runs more slowly. Separate functions are provided so |
708 |
can be used for this case. When used, these functions are always called |
that special-purpose external code can be used for this case. When |
709 |
in a stack-like manner (last obtained, first freed), and always for |
used, these functions are always called in a stack-like manner (last |
710 |
memory blocks of the same size. |
obtained, first freed), and always for memory blocks of the same size. |
711 |
|
|
712 |
The global variable pcre_callout initially contains NULL. It can be set |
The global variable pcre_callout initially contains NULL. It can be set |
713 |
by the caller to a "callout" function, which PCRE will then call at |
by the caller to a "callout" function, which PCRE will then call at |
714 |
specified points during a matching operation. Details are given in the |
specified points during a matching operation. Details are given in the |
715 |
pcrecallout documentation. |
pcrecallout documentation. |
716 |
|
|
717 |
|
|
718 |
MULTITHREADING |
MULTITHREADING |
719 |
|
|
720 |
The PCRE functions can be used in multi-threading applications, with |
The PCRE functions can be used in multi-threading applications, with |
721 |
the proviso that the memory management functions pointed to by |
the proviso that the memory management functions pointed to by |
722 |
pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the |
pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the |
723 |
callout function pointed to by pcre_callout, are shared by all threads. |
callout function pointed to by pcre_callout, are shared by all threads. |
724 |
|
|
725 |
The compiled form of a regular expression is not altered during match- |
The compiled form of a regular expression is not altered during match- |
726 |
ing, so the same compiled pattern can safely be used by several threads |
ing, so the same compiled pattern can safely be used by several threads |
727 |
at once. |
at once. |
728 |
|
|
730 |
SAVING PRECOMPILED PATTERNS FOR LATER USE |
SAVING PRECOMPILED PATTERNS FOR LATER USE |
731 |
|
|
732 |
The compiled form of a regular expression can be saved and re-used at a |
The compiled form of a regular expression can be saved and re-used at a |
733 |
later time, possibly by a different program, and even on a host other |
later time, possibly by a different program, and even on a host other |
734 |
than the one on which it was compiled. Details are given in the |
than the one on which it was compiled. Details are given in the |
735 |
pcreprecompile documentation. |
pcreprecompile documentation. |
736 |
|
|
737 |
|
|
739 |
|
|
740 |
int pcre_config(int what, void *where); |
int pcre_config(int what, void *where); |
741 |
|
|
742 |
The function pcre_config() makes it possible for a PCRE client to dis- |
The function pcre_config() makes it possible for a PCRE client to dis- |
743 |
cover which optional features have been compiled into the PCRE library. |
cover which optional features have been compiled into the PCRE library. |
744 |
The pcrebuild documentation has more details about these optional fea- |
The pcrebuild documentation has more details about these optional fea- |
745 |
tures. |
tures. |
746 |
|
|
747 |
The first argument for pcre_config() is an integer, specifying which |
The first argument for pcre_config() is an integer, specifying which |
748 |
information is required; the second argument is a pointer to a variable |
information is required; the second argument is a pointer to a variable |
749 |
into which the information is placed. The following information is |
into which the information is placed. The following information is |
750 |
available: |
available: |
751 |
|
|
752 |
PCRE_CONFIG_UTF8 |
PCRE_CONFIG_UTF8 |
753 |
|
|
754 |
The output is an integer that is set to one if UTF-8 support is avail- |
The output is an integer that is set to one if UTF-8 support is avail- |
755 |
able; otherwise it is set to zero. |
able; otherwise it is set to zero. |
756 |
|
|
757 |
PCRE_CONFIG_UNICODE_PROPERTIES |
PCRE_CONFIG_UNICODE_PROPERTIES |
758 |
|
|
759 |
The output is an integer that is set to one if support for Unicode |
The output is an integer that is set to one if support for Unicode |
760 |
character properties is available; otherwise it is set to zero. |
character properties is available; otherwise it is set to zero. |
761 |
|
|
762 |
PCRE_CONFIG_NEWLINE |
PCRE_CONFIG_NEWLINE |
763 |
|
|
764 |
The output is an integer that is set to the value of the code that is |
The output is an integer that is set to the value of the code that is |
765 |
used for the newline character. It is either linefeed (10) or carriage |
used for the newline character. It is either linefeed (10) or carriage |
766 |
return (13), and should normally be the standard character for your |
return (13), and should normally be the standard character for your |
767 |
operating system. |
operating system. |
768 |
|
|
769 |
PCRE_CONFIG_LINK_SIZE |
PCRE_CONFIG_LINK_SIZE |
770 |
|
|
771 |
The output is an integer that contains the number of bytes used for |
The output is an integer that contains the number of bytes used for |
772 |
internal linkage in compiled regular expressions. The value is 2, 3, or |
internal linkage in compiled regular expressions. The value is 2, 3, or |
773 |
4. Larger values allow larger regular expressions to be compiled, at |
4. Larger values allow larger regular expressions to be compiled, at |
774 |
the expense of slower matching. The default value of 2 is sufficient |
the expense of slower matching. The default value of 2 is sufficient |
775 |
for all but the most massive patterns, since it allows the compiled |
for all but the most massive patterns, since it allows the compiled |
776 |
pattern to be up to 64K in size. |
pattern to be up to 64K in size. |
777 |
|
|
778 |
PCRE_CONFIG_POSIX_MALLOC_THRESHOLD |
PCRE_CONFIG_POSIX_MALLOC_THRESHOLD |
779 |
|
|
780 |
The output is an integer that contains the threshold above which the |
The output is an integer that contains the threshold above which the |
781 |
POSIX interface uses malloc() for output vectors. Further details are |
POSIX interface uses malloc() for output vectors. Further details are |
782 |
given in the pcreposix documentation. |
given in the pcreposix documentation. |
783 |
|
|
784 |
PCRE_CONFIG_MATCH_LIMIT |
PCRE_CONFIG_MATCH_LIMIT |
785 |
|
|
786 |
The output is an integer that gives the default limit for the number of |
The output is an integer that gives the default limit for the number of |
787 |
internal matching function calls in a pcre_exec() execution. Further |
internal matching function calls in a pcre_exec() execution. Further |
788 |
details are given with pcre_exec() below. |
details are given with pcre_exec() below. |
789 |
|
|
790 |
PCRE_CONFIG_STACKRECURSE |
PCRE_CONFIG_STACKRECURSE |
791 |
|
|
792 |
The output is an integer that is set to one if internal recursion is |
The output is an integer that is set to one if internal recursion when |
793 |
implemented by recursive function calls that use the stack to remember |
running pcre_exec() is implemented by recursive function calls that use |
794 |
their state. This is the usual way that PCRE is compiled. The output is |
the stack to remember their state. This is the usual way that PCRE is |
795 |
zero if PCRE was compiled to use blocks of data on the heap instead of |
compiled. The output is zero if PCRE was compiled to use blocks of data |
796 |
recursive function calls. In this case, pcre_stack_malloc and |
on the heap instead of recursive function calls. In this case, |
797 |
pcre_stack_free are called to manage memory blocks on the heap, thus |
pcre_stack_malloc and pcre_stack_free are called to manage memory |
798 |
avoiding the use of the stack. |
blocks on the heap, thus avoiding the use of the stack. |
799 |
|
|
800 |
|
|
801 |
COMPILING A PATTERN |
COMPILING A PATTERN |
804 |
const char **errptr, int *erroffset, |
const char **errptr, int *erroffset, |
805 |
const unsigned char *tableptr); |
const unsigned char *tableptr); |
806 |
|
|
807 |
The function pcre_compile() is called to compile a pattern into an |
pcre *pcre_compile2(const char *pattern, int options, |
808 |
internal form. The pattern is a C string terminated by a binary zero, |
int *errorcodeptr, |
809 |
and is passed in the pattern argument. A pointer to a single block of |
const char **errptr, int *erroffset, |
810 |
memory that is obtained via pcre_malloc is returned. This contains the |
const unsigned char *tableptr); |
|
compiled code and related data. The pcre type is defined for the |
|
|
returned block; this is a typedef for a structure whose contents are |
|
|
not externally defined. It is up to the caller to free the memory when |
|
|
it is no longer required. |
|
811 |
|
|
812 |
Although the compiled code of a PCRE regex is relocatable, that is, it |
Either of the functions pcre_compile() or pcre_compile2() can be called |
813 |
|
to compile a pattern into an internal form. The only difference between |
814 |
|
the two interfaces is that pcre_compile2() has an additional argument, |
815 |
|
errorcodeptr, via which a numerical error code can be returned. |
816 |
|
|
817 |
|
The pattern is a C string terminated by a binary zero, and is passed in |
818 |
|
the pattern argument. A pointer to a single block of memory that is |
819 |
|
obtained via pcre_malloc is returned. This contains the compiled code |
820 |
|
and related data. The pcre type is defined for the returned block; this |
821 |
|
is a typedef for a structure whose contents are not externally defined. |
822 |
|
It is up to the caller to free the memory when it is no longer |
823 |
|
required. |
824 |
|
|
825 |
|
Although the compiled code of a PCRE regex is relocatable, that is, it |
826 |
does not depend on memory location, the complete pcre data block is not |
does not depend on memory location, the complete pcre data block is not |
827 |
fully relocatable, because it may contain a copy of the tableptr argu- |
fully relocatable, because it may contain a copy of the tableptr argu- |
828 |
ment, which is an address (see below). |
ment, which is an address (see below). |
829 |
|
|
830 |
The options argument contains independent bits that affect the compila- |
The options argument contains independent bits that affect the compila- |
831 |
tion. It should be zero if no options are required. The available |
tion. It should be zero if no options are required. The available |
832 |
options are described below. Some of them, in particular, those that |
options are described below. Some of them, in particular, those that |
833 |
are compatible with Perl, can also be set and unset from within the |
are compatible with Perl, can also be set and unset from within the |
834 |
pattern (see the detailed description in the pcrepattern documenta- |
pattern (see the detailed description in the pcrepattern documenta- |
835 |
tion). For these options, the contents of the options argument speci- |
tion). For these options, the contents of the options argument speci- |
836 |
fies their initial settings at the start of compilation and execution. |
fies their initial settings at the start of compilation and execution. |
837 |
The PCRE_ANCHORED option can be set at the time of matching as well as |
The PCRE_ANCHORED option can be set at the time of matching as well as |
838 |
at compile time. |
at compile time. |
839 |
|
|
840 |
If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise, |
If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise, |
841 |
if compilation of a pattern fails, pcre_compile() returns NULL, and |
if compilation of a pattern fails, pcre_compile() returns NULL, and |
842 |
sets the variable pointed to by errptr to point to a textual error mes- |
sets the variable pointed to by errptr to point to a textual error mes- |
843 |
sage. The offset from the start of the pattern to the character where |
sage. The offset from the start of the pattern to the character where |
844 |
the error was discovered is placed in the variable pointed to by |
the error was discovered is placed in the variable pointed to by |
845 |
erroffset, which must not be NULL. If it is, an immediate error is |
erroffset, which must not be NULL. If it is, an immediate error is |
846 |
given. |
given. |
847 |
|
|
848 |
|
If pcre_compile2() is used instead of pcre_compile(), and the error- |
849 |
|
codeptr argument is not NULL, a non-zero error code number is returned |
850 |
|
via this argument in the event of an error. This is in addition to the |
851 |
|
textual error message. Error codes and messages are listed below. |
852 |
|
|
853 |
If the final argument, tableptr, is NULL, PCRE uses a default set of |
If the final argument, tableptr, is NULL, PCRE uses a default set of |
854 |
character tables that are built when PCRE is compiled, using the |
character tables that are built when PCRE is compiled, using the |
855 |
default C locale. Otherwise, tableptr must be an address that is the |
default C locale. Otherwise, tableptr must be an address that is the |
892 |
|
|
893 |
If this bit is set, letters in the pattern match both upper and lower |
If this bit is set, letters in the pattern match both upper and lower |
894 |
case letters. It is equivalent to Perl's /i option, and it can be |
case letters. It is equivalent to Perl's /i option, and it can be |
895 |
changed within a pattern by a (?i) option setting. When running in |
changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE |
896 |
UTF-8 mode, case support for high-valued characters is available only |
always understands the concept of case for characters whose values are |
897 |
when PCRE is built with Unicode character property support. |
less than 128, so caseless matching is always possible. For characters |
898 |
|
with higher values, the concept of case is supported if PCRE is com- |
899 |
|
piled with Unicode property support, but not otherwise. If you want to |
900 |
|
use caseless matching for characters 128 and above, you must ensure |
901 |
|
that PCRE is compiled with Unicode property support as well as with |
902 |
|
UTF-8 support. |
903 |
|
|
904 |
PCRE_DOLLAR_ENDONLY |
PCRE_DOLLAR_ENDONLY |
905 |
|
|
906 |
If this bit is set, a dollar metacharacter in the pattern matches only |
If this bit is set, a dollar metacharacter in the pattern matches only |
907 |
at the end of the subject string. Without this option, a dollar also |
at the end of the subject string. Without this option, a dollar also |
908 |
matches immediately before the final character if it is a newline (but |
matches immediately before the final character if it is a newline (but |
909 |
not before any other newlines). The PCRE_DOLLAR_ENDONLY option is |
not before any other newlines). The PCRE_DOLLAR_ENDONLY option is |
910 |
ignored if PCRE_MULTILINE is set. There is no equivalent to this option |
ignored if PCRE_MULTILINE is set. There is no equivalent to this option |
911 |
in Perl, and no way to set it within a pattern. |
in Perl, and no way to set it within a pattern. |
912 |
|
|
913 |
PCRE_DOTALL |
PCRE_DOTALL |
914 |
|
|
915 |
If this bit is set, a dot metacharater in the pattern matches all char- |
If this bit is set, a dot metacharater in the pattern matches all char- |
916 |
acters, including newlines. Without it, newlines are excluded. This |
acters, including newlines. Without it, newlines are excluded. This |
917 |
option is equivalent to Perl's /s option, and it can be changed within |
option is equivalent to Perl's /s option, and it can be changed within |
918 |
a pattern by a (?s) option setting. A negative class such as [^a] |
a pattern by a (?s) option setting. A negative class such as [^a] |
919 |
always matches a newline character, independent of the setting of this |
always matches a newline character, independent of the setting of this |
920 |
option. |
option. |
921 |
|
|
922 |
PCRE_EXTENDED |
PCRE_EXTENDED |
923 |
|
|
924 |
If this bit is set, whitespace data characters in the pattern are |
If this bit is set, whitespace data characters in the pattern are |
925 |
totally ignored except when escaped or inside a character class. |
totally ignored except when escaped or inside a character class. White- |
926 |
Whitespace does not include the VT character (code 11). In addition, |
space does not include the VT character (code 11). In addition, charac- |
927 |
characters between an unescaped # outside a character class and the |
ters between an unescaped # outside a character class and the next new- |
928 |
next newline character, inclusive, are also ignored. This is equivalent |
line character, inclusive, are also ignored. This is equivalent to |
929 |
to Perl's /x option, and it can be changed within a pattern by a (?x) |
Perl's /x option, and it can be changed within a pattern by a (?x) |
930 |
option setting. |
option setting. |
931 |
|
|
932 |
This option makes it possible to include comments inside complicated |
This option makes it possible to include comments inside complicated |
933 |
patterns. Note, however, that this applies only to data characters. |
patterns. Note, however, that this applies only to data characters. |
934 |
Whitespace characters may never appear within special character |
Whitespace characters may never appear within special character |
935 |
sequences in a pattern, for example within the sequence (?( which |
sequences in a pattern, for example within the sequence (?( which |
936 |
introduces a conditional subpattern. |
introduces a conditional subpattern. |
937 |
|
|
938 |
PCRE_EXTRA |
PCRE_EXTRA |
939 |
|
|
940 |
This option was invented in order to turn on additional functionality |
This option was invented in order to turn on additional functionality |
941 |
of PCRE that is incompatible with Perl, but it is currently of very |
of PCRE that is incompatible with Perl, but it is currently of very |
942 |
little use. When set, any backslash in a pattern that is followed by a |
little use. When set, any backslash in a pattern that is followed by a |
943 |
letter that has no special meaning causes an error, thus reserving |
letter that has no special meaning causes an error, thus reserving |
944 |
these combinations for future expansion. By default, as in Perl, a |
these combinations for future expansion. By default, as in Perl, a |
945 |
backslash followed by a letter with no special meaning is treated as a |
backslash followed by a letter with no special meaning is treated as a |
946 |
literal. There are at present no other features controlled by this |
literal. There are at present no other features controlled by this |
947 |
option. It can also be set by a (?X) option setting within a pattern. |
option. It can also be set by a (?X) option setting within a pattern. |
948 |
|
|
949 |
|
PCRE_FIRSTLINE |
950 |
|
|
951 |
|
If this option is set, an unanchored pattern is required to match |
952 |
|
before or at the first newline character in the subject string, though |
953 |
|
the matched text may continue over the newline. |
954 |
|
|
955 |
PCRE_MULTILINE |
PCRE_MULTILINE |
956 |
|
|
957 |
By default, PCRE treats the subject string as consisting of a single |
By default, PCRE treats the subject string as consisting of a single |
958 |
line of characters (even if it actually contains newlines). The "start |
line of characters (even if it actually contains newlines). The "start |
959 |
of line" metacharacter (^) matches only at the start of the string, |
of line" metacharacter (^) matches only at the start of the string, |
960 |
while the "end of line" metacharacter ($) matches only at the end of |
while the "end of line" metacharacter ($) matches only at the end of |
961 |
the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY |
the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY |
962 |
is set). This is the same as Perl. |
is set). This is the same as Perl. |
963 |
|
|
964 |
When PCRE_MULTILINE it is set, the "start of line" and "end of line" |
When PCRE_MULTILINE it is set, the "start of line" and "end of line" |
965 |
constructs match immediately following or immediately before any new- |
constructs match immediately following or immediately before any new- |
966 |
line in the subject string, respectively, as well as at the very start |
line in the subject string, respectively, as well as at the very start |
967 |
and end. This is equivalent to Perl's /m option, and it can be changed |
and end. This is equivalent to Perl's /m option, and it can be changed |
968 |
within a pattern by a (?m) option setting. If there are no "\n" charac- |
within a pattern by a (?m) option setting. If there are no "\n" charac- |
969 |
ters in a subject string, or no occurrences of ^ or $ in a pattern, |
ters in a subject string, or no occurrences of ^ or $ in a pattern, |
970 |
setting PCRE_MULTILINE has no effect. |
setting PCRE_MULTILINE has no effect. |
971 |
|
|
972 |
PCRE_NO_AUTO_CAPTURE |
PCRE_NO_AUTO_CAPTURE |
973 |
|
|
974 |
If this option is set, it disables the use of numbered capturing paren- |
If this option is set, it disables the use of numbered capturing paren- |
975 |
theses in the pattern. Any opening parenthesis that is not followed by |
theses in the pattern. Any opening parenthesis that is not followed by |
976 |
? behaves as if it were followed by ?: but named parentheses can still |
? behaves as if it were followed by ?: but named parentheses can still |
977 |
be used for capturing (and they acquire numbers in the usual way). |
be used for capturing (and they acquire numbers in the usual way). |
978 |
There is no equivalent of this option in Perl. |
There is no equivalent of this option in Perl. |
979 |
|
|
980 |
PCRE_UNGREEDY |
PCRE_UNGREEDY |
981 |
|
|
982 |
This option inverts the "greediness" of the quantifiers so that they |
This option inverts the "greediness" of the quantifiers so that they |
983 |
are not greedy by default, but become greedy if followed by "?". It is |
are not greedy by default, but become greedy if followed by "?". It is |
984 |
not compatible with Perl. It can also be set by a (?U) option setting |
not compatible with Perl. It can also be set by a (?U) option setting |
985 |
within the pattern. |
within the pattern. |
986 |
|
|
987 |
PCRE_UTF8 |
PCRE_UTF8 |
988 |
|
|
989 |
This option causes PCRE to regard both the pattern and the subject as |
This option causes PCRE to regard both the pattern and the subject as |
990 |
strings of UTF-8 characters instead of single-byte character strings. |
strings of UTF-8 characters instead of single-byte character strings. |
991 |
However, it is available only when PCRE is built to include UTF-8 sup- |
However, it is available only when PCRE is built to include UTF-8 sup- |
992 |
port. If not, the use of this option provokes an error. Details of how |
port. If not, the use of this option provokes an error. Details of how |
993 |
this option changes the behaviour of PCRE are given in the section on |
this option changes the behaviour of PCRE are given in the section on |
994 |
UTF-8 support in the main pcre page. |
UTF-8 support in the main pcre page. |
995 |
|
|
996 |
PCRE_NO_UTF8_CHECK |
PCRE_NO_UTF8_CHECK |
997 |
|
|
998 |
When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is |
When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is |
999 |
automatically checked. If an invalid UTF-8 sequence of bytes is found, |
automatically checked. If an invalid UTF-8 sequence of bytes is found, |
1000 |
pcre_compile() returns an error. If you already know that your pattern |
pcre_compile() returns an error. If you already know that your pattern |
1001 |
is valid, and you want to skip this check for performance reasons, you |
is valid, and you want to skip this check for performance reasons, you |
1002 |
can set the PCRE_NO_UTF8_CHECK option. When it is set, the effect of |
can set the PCRE_NO_UTF8_CHECK option. When it is set, the effect of |
1003 |
passing an invalid UTF-8 string as a pattern is undefined. It may cause |
passing an invalid UTF-8 string as a pattern is undefined. It may cause |
1004 |
your program to crash. Note that this option can also be passed to |
your program to crash. Note that this option can also be passed to |
1005 |
pcre_exec(), to suppress the UTF-8 validity checking of subject |
pcre_exec() and pcre_dfa_exec(), to suppress the UTF-8 validity check- |
1006 |
strings. |
ing of subject strings. |
1007 |
|
|
1008 |
|
|
1009 |
|
COMPILATION ERROR CODES |
1010 |
|
|
1011 |
|
The following table lists the error codes than may be returned by |
1012 |
|
pcre_compile2(), along with the error messages that may be returned by |
1013 |
|
both compiling functions. |
1014 |
|
|
1015 |
|
0 no error |
1016 |
|
1 \ at end of pattern |
1017 |
|
2 \c at end of pattern |
1018 |
|
3 unrecognized character follows \ |
1019 |
|
4 numbers out of order in {} quantifier |
1020 |
|
5 number too big in {} quantifier |
1021 |
|
6 missing terminating ] for character class |
1022 |
|
7 invalid escape sequence in character class |
1023 |
|
8 range out of order in character class |
1024 |
|
9 nothing to repeat |
1025 |
|
10 operand of unlimited repeat could match the empty string |
1026 |
|
11 internal error: unexpected repeat |
1027 |
|
12 unrecognized character after (? |
1028 |
|
13 POSIX named classes are supported only within a class |
1029 |
|
14 missing ) |
1030 |
|
15 reference to non-existent subpattern |
1031 |
|
16 erroffset passed as NULL |
1032 |
|
17 unknown option bit(s) set |
1033 |
|
18 missing ) after comment |
1034 |
|
19 parentheses nested too deeply |
1035 |
|
20 regular expression too large |
1036 |
|
21 failed to get memory |
1037 |
|
22 unmatched parentheses |
1038 |
|
23 internal error: code overflow |
1039 |
|
24 unrecognized character after (?< |
1040 |
|
25 lookbehind assertion is not fixed length |
1041 |
|
26 malformed number after (?( |
1042 |
|
27 conditional group contains more than two branches |
1043 |
|
28 assertion expected after (?( |
1044 |
|
29 (?R or (?digits must be followed by ) |
1045 |
|
30 unknown POSIX class name |
1046 |
|
31 POSIX collating elements are not supported |
1047 |
|
32 this version of PCRE is not compiled with PCRE_UTF8 support |
1048 |
|
33 spare error |
1049 |
|
34 character value in \x{...} sequence is too large |
1050 |
|
35 invalid condition (?(0) |
1051 |
|
36 \C not allowed in lookbehind assertion |
1052 |
|
37 PCRE does not support \L, \l, \N, \U, or \u |
1053 |
|
38 number after (?C is > 255 |
1054 |
|
39 closing ) for (?C expected |
1055 |
|
40 recursive call could loop indefinitely |
1056 |
|
41 unrecognized character after (?P |
1057 |
|
42 syntax error after (?P |
1058 |
|
43 two named groups have the same name |
1059 |
|
44 invalid UTF-8 string |
1060 |
|
45 support for \P, \p, and \X has not been compiled |
1061 |
|
46 malformed \P or \p sequence |
1062 |
|
47 unknown property name after \P or \p |
1063 |
|
|
1064 |
|
|
1065 |
STUDYING A PATTERN |
STUDYING A PATTERN |
1066 |
|
|
1067 |
pcre_extra *pcre_study(const pcre *code, int options, |
pcre_extra *pcre_study(const pcre *code, int options |
1068 |
const char **errptr); |
const char **errptr); |
1069 |
|
|
1070 |
If a compiled pattern is going to be used several times, it is worth |
If a compiled pattern is going to be used several times, it is worth |
1071 |
spending more time analyzing it in order to speed up the time taken for |
spending more time analyzing it in order to speed up the time taken for |
1072 |
matching. The function pcre_study() takes a pointer to a compiled pat- |
matching. The function pcre_study() takes a pointer to a compiled pat- |
1073 |
tern as its first argument. If studying the pattern produces additional |
tern as its first argument. If studying the pattern produces additional |
1074 |
information that will help speed up matching, pcre_study() returns a |
information that will help speed up matching, pcre_study() returns a |
1075 |
pointer to a pcre_extra block, in which the study_data field points to |
pointer to a pcre_extra block, in which the study_data field points to |
1076 |
the results of the study. |
the results of the study. |
1077 |
|
|
1078 |
The returned value from pcre_study() can be passed directly to |
The returned value from pcre_study() can be passed directly to |
1079 |
pcre_exec(). However, a pcre_extra block also contains other fields |
pcre_exec(). However, a pcre_extra block also contains other fields |
1080 |
that can be set by the caller before the block is passed; these are |
that can be set by the caller before the block is passed; these are |
1081 |
described below in the section on matching a pattern. |
described below in the section on matching a pattern. |
1082 |
|
|
1083 |
If studying the pattern does not produce any additional information, |
If studying the pattern does not produce any additional information |
1084 |
pcre_study() returns NULL. In that circumstance, if the calling program |
pcre_study() returns NULL. In that circumstance, if the calling program |
1085 |
wants to pass any of the other fields to pcre_exec(), it must set up |
wants to pass any of the other fields to pcre_exec(), it must set up |
1086 |
its own pcre_extra block. |
its own pcre_extra block. |
1087 |
|
|
1088 |
The second argument of pcre_study() contains option bits. At present, |
The second argument of pcre_study() contains option bits. At present, |
1089 |
no options are defined, and this argument should always be zero. |
no options are defined, and this argument should always be zero. |
1090 |
|
|
1091 |
The third argument for pcre_study() is a pointer for an error message. |
The third argument for pcre_study() is a pointer for an error message. |
1092 |
If studying succeeds (even if no data is returned), the variable it |
If studying succeeds (even if no data is returned), the variable it |
1093 |
points to is set to NULL. Otherwise it points to a textual error mes- |
points to is set to NULL. Otherwise it points to a textual error mes- |
1094 |
sage. You should therefore test the error pointer for NULL after call- |
sage. You should therefore test the error pointer for NULL after call- |
1095 |
ing pcre_study(), to be sure that it has run successfully. |
ing pcre_study(), to be sure that it has run successfully. |
1096 |
|
|
1097 |
This is a typical call to pcre_study(): |
This is a typical call to pcre_study(): |
1103 |
&error); /* set to NULL or points to a message */ |
&error); /* set to NULL or points to a message */ |
1104 |
|
|
1105 |
At present, studying a pattern is useful only for non-anchored patterns |
At present, studying a pattern is useful only for non-anchored patterns |
1106 |
that do not have a single fixed starting character. A bitmap of possi- |
that do not have a single fixed starting character. A bitmap of possi- |
1107 |
ble starting bytes is created. |
ble starting bytes is created. |
1108 |
|
|
1109 |
|
|
1110 |
LOCALE SUPPORT |
LOCALE SUPPORT |
1111 |
|
|
1112 |
PCRE handles caseless matching, and determines whether characters are |
PCRE handles caseless matching, and determines whether characters are |
1113 |
letters, digits, or whatever, by reference to a set of tables, indexed |
letters digits, or whatever, by reference to a set of tables, indexed |
1114 |
by character value. (When running in UTF-8 mode, this applies only to |
by character value. When running in UTF-8 mode, this applies only to |
1115 |
characters with codes less than 128. Higher-valued codes never match |
characters with codes less than 128. Higher-valued codes never match |
1116 |
escapes such as \w or \d, but can be tested with \p if PCRE is built |
escapes such as \w or \d, but can be tested with \p if PCRE is built |
1117 |
with Unicode character property support.) |
with Unicode character property support. |
1118 |
|
|
1119 |
An internal set of tables is created in the default C locale when PCRE |
An internal set of tables is created in the default C locale when PCRE |
1120 |
is built. This is used when the final argument of pcre_compile() is |
is built. This is used when the final argument of pcre_compile() is |
1121 |
NULL, and is sufficient for many applications. An alternative set of |
NULL, and is sufficient for many applications. An alternative set of |
1122 |
tables can, however, be supplied. These may be created in a different |
tables can, however, be supplied. These may be created in a different |
1123 |
locale from the default. As more and more applications change to using |
locale from the default. As more and more applications change to using |
1124 |
Unicode, the need for this locale support is expected to die away. |
Unicode, the need for this locale support is expected to die away. |
1125 |
|
|
1126 |
External tables are built by calling the pcre_maketables() function, |
External tables are built by calling the pcre_maketables() function, |
1127 |
which has no arguments, in the relevant locale. The result can then be |
which has no arguments, in the relevant locale. The result can then be |
1128 |
passed to pcre_compile() or pcre_exec() as often as necessary. For |
passed to pcre_compile() or pcre_exec() as often as necessary. For |
1129 |
example, to build and use tables that are appropriate for the French |
example, to build and use tables that are appropriate for the French |
1130 |
locale (where accented characters with values greater than 128 are |
locale (where accented characters with values greater than 128 are |
1131 |
treated as letters), the following code could be used: |
treated as letters), the following code could be used: |
1132 |
|
|
1133 |
setlocale(LC_CTYPE, "fr_FR"); |
setlocale(LC_CTYPE, "fr_FR"); |
1134 |
tables = pcre_maketables(); |
tables = pcre_maketables(); |
1135 |
re = pcre_compile(..., tables); |
re = pcre_compile(..., tables); |
1136 |
|
|
1137 |
When pcre_maketables() runs, the tables are built in memory that is |
When pcre_maketables() runs, the tables are built in memory that is |
1138 |
obtained via pcre_malloc. It is the caller's responsibility to ensure |
obtained via pcre_malloc. It is the caller's responsibility to ensure |
1139 |
that the memory containing the tables remains available for as long as |
that the memory containing the tables remains available for as long as |
1140 |
it is needed. |
it is needed. |
1141 |
|
|
1142 |
The pointer that is passed to pcre_compile() is saved with the compiled |
The pointer that is passed to pcre_compile() is saved with the compiled |
1143 |
pattern, and the same tables are used via this pointer by pcre_study() |
pattern, and the same tables are used via this pointer by pcre_study() |
1144 |
and normally also by pcre_exec(). Thus, by default, for any single pat- |
and normally also by pcre_exec(). Thus, by default, for any single pat- |
1145 |
tern, compilation, studying and matching all happen in the same locale, |
tern, compilation, studying and matching all happen in the same locale, |
1146 |
but different patterns can be compiled in different locales. |
but different patterns can be compiled in different locales. |
1147 |
|
|
1148 |
It is possible to pass a table pointer or NULL (indicating the use of |
It is possible to pass a table pointer or NULL (indicating the use of |
1149 |
the internal tables) to pcre_exec(). Although not intended for this |
the internal tables) to pcre_exec(). Although not intended for this |
1150 |
purpose, this facility could be used to match a pattern in a different |
purpose, this facility could be used to match a pattern in a different |
1151 |
locale from the one in which it was compiled. Passing table pointers at |
locale from the one in which it was compiled. Passing table pointers at |
1152 |
run time is discussed below in the section on matching a pattern. |
run time is discussed below in the section on matching a pattern. |
1153 |
|
|
1157 |
int pcre_fullinfo(const pcre *code, const pcre_extra *extra, |
int pcre_fullinfo(const pcre *code, const pcre_extra *extra, |
1158 |
int what, void *where); |
int what, void *where); |
1159 |
|
|
1160 |
The pcre_fullinfo() function returns information about a compiled pat- |
The pcre_fullinfo() function returns information about a compiled pat- |
1161 |
tern. It replaces the obsolete pcre_info() function, which is neverthe- |
tern. It replaces the obsolete pcre_info() function, which is neverthe- |
1162 |
less retained for backwards compability (and is documented below). |
less retained for backwards compability (and is documented below). |
1163 |
|
|
1164 |
The first argument for pcre_fullinfo() is a pointer to the compiled |
The first argument for pcre_fullinfo() is a pointer to the compiled |
1165 |
pattern. The second argument is the result of pcre_study(), or NULL if |
pattern. The second argument is the result of pcre_study(), or NULL if |
1166 |
the pattern was not studied. The third argument specifies which piece |
the pattern was not studied. The third argument specifies which piece |
1167 |
of information is required, and the fourth argument is a pointer to a |
of information is required, and the fourth argument is a pointer to a |
1168 |
variable to receive the data. The yield of the function is zero for |
variable to receive the data. The yield of the function is zero for |
1169 |
success, or one of the following negative numbers: |
success, or one of the following negative numbers: |
1170 |
|
|
1171 |
PCRE_ERROR_NULL the argument code was NULL |
PCRE_ERROR_NULL the argument code was NULL |
1173 |
PCRE_ERROR_BADMAGIC the "magic number" was not found |
PCRE_ERROR_BADMAGIC the "magic number" was not found |
1174 |
PCRE_ERROR_BADOPTION the value of what was invalid |
PCRE_ERROR_BADOPTION the value of what was invalid |
1175 |
|
|
1176 |
The "magic number" is placed at the start of each compiled pattern as |
The "magic number" is placed at the start of each compiled pattern as |
1177 |
an simple check against passing an arbitrary memory pointer. Here is a |
an simple check against passing an arbitrary memory pointer. Here is a |
1178 |
typical call of pcre_fullinfo(), to obtain the length of the compiled |
typical call of pcre_fullinfo(), to obtain the length of the compiled |
1179 |
pattern: |
pattern: |
1180 |
|
|
1181 |
int rc; |
int rc; |
1186 |
PCRE_INFO_SIZE, /* what is required */ |
PCRE_INFO_SIZE, /* what is required */ |
1187 |
&length); /* where to put the data */ |
&length); /* where to put the data */ |
1188 |
|
|
1189 |
The possible values for the third argument are defined in pcre.h, and |
The possible values for the third argument are defined in pcre.h, and |
1190 |
are as follows: |
are as follows: |
1191 |
|
|
1192 |
PCRE_INFO_BACKREFMAX |
PCRE_INFO_BACKREFMAX |
1193 |
|
|
1194 |
Return the number of the highest back reference in the pattern. The |
Return the number of the highest back reference in the pattern. The |
1195 |
fourth argument should point to an int variable. Zero is returned if |
fourth argument should point to an int variable. Zero is returned if |
1196 |
there are no back references. |
there are no back references. |
1197 |
|
|
1198 |
PCRE_INFO_CAPTURECOUNT |
PCRE_INFO_CAPTURECOUNT |
1199 |
|
|
1200 |
Return the number of capturing subpatterns in the pattern. The fourth |
Return the number of capturing subpatterns in the pattern. The fourth |
1201 |
argument should point to an int variable. |
argument should point to an int variable. |
1202 |
|
|
1203 |
PCRE_INFO_DEFAULTTABLES |
PCRE_INFO_DEFAULT_TABLES |
1204 |
|
|
1205 |
Return a pointer to the internal default character tables within PCRE. |
Return a pointer to the internal default character tables within PCRE. |
1206 |
The fourth argument should point to an unsigned char * variable. This |
The fourth argument should point to an unsigned char * variable. This |
1207 |
information call is provided for internal use by the pcre_study() func- |
information call is provided for internal use by the pcre_study() func- |
1208 |
tion. External callers can cause PCRE to use its internal tables by |
tion. External callers can cause PCRE to use its internal tables by |
1209 |
passing a NULL table pointer. |
passing a NULL table pointer. |
1210 |
|
|
1211 |
PCRE_INFO_FIRSTBYTE |
PCRE_INFO_FIRSTBYTE |
1212 |
|
|
1213 |
Return information about the first byte of any matched string, for a |
Return information about the first byte of any matched string, for a |
1214 |
non-anchored pattern. (This option used to be called |
non-anchored pattern. (This option used to be called |
1215 |
PCRE_INFO_FIRSTCHAR; the old name is still recognized for backwards |
PCRE_INFO_FIRSTCHAR; the old name is still recognized for backwards |
1216 |
compatibility.) |
compatibility.) |
1217 |
|
|
1218 |
If there is a fixed first byte, for example, from a pattern such as |
If there is a fixed first byte, for example, from a pattern such as |
1219 |
(cat|cow|coyote), it is returned in the integer pointed to by where. |
(cat|cow|coyote), it is returned in the integer pointed to by where. |
1220 |
Otherwise, if either |
Otherwise, if either |
1221 |
|
|
1222 |
(a) the pattern was compiled with the PCRE_MULTILINE option, and every |
(a) the pattern was compiled with the PCRE_MULTILINE option, and every |
1223 |
branch starts with "^", or |
branch starts with "^", or |
1224 |
|
|
1225 |
(b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not |
(b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not |
1226 |
set (if it were set, the pattern would be anchored), |
set (if it were set, the pattern would be anchored), |
1227 |
|
|
1228 |
-1 is returned, indicating that the pattern matches only at the start |
-1 is returned, indicating that the pattern matches only at the start |
1229 |
of a subject string or after any newline within the string. Otherwise |
of a subject string or after any newline within the string. Otherwise |
1230 |
-2 is returned. For anchored patterns, -2 is returned. |
-2 is returned. For anchored patterns, -2 is returned. |
1231 |
|
|
1232 |
PCRE_INFO_FIRSTTABLE |
PCRE_INFO_FIRSTTABLE |
1233 |
|
|
1234 |
If the pattern was studied, and this resulted in the construction of a |
If the pattern was studied, and this resulted in the construction of a |
1235 |
256-bit table indicating a fixed set of bytes for the first byte in any |
256-bit table indicating a fixed set of bytes for the first byte in any |
1236 |
matching string, a pointer to the table is returned. Otherwise NULL is |
matching string, a pointer to the table is returned. Otherwise NULL is |
1237 |
returned. The fourth argument should point to an unsigned char * vari- |
returned. The fourth argument should point to an unsigned char * vari- |
1238 |
able. |
able. |
1239 |
|
|
1240 |
PCRE_INFO_LASTLITERAL |
PCRE_INFO_LASTLITERAL |
1241 |
|
|
1242 |
Return the value of the rightmost literal byte that must exist in any |
Return the value of the rightmost literal byte that must exist in any |
1243 |
matched string, other than at its start, if such a byte has been |
matched string, other than at its start, if such a byte has been |
1244 |
recorded. The fourth argument should point to an int variable. If there |
recorded. The fourth argument should point to an int variable. If there |
1245 |
is no such byte, -1 is returned. For anchored patterns, a last literal |
is no such byte, -1 is returned. For anchored patterns, a last literal |
1246 |
byte is recorded only if it follows something of variable length. For |
byte is recorded only if it follows something of variable length. For |
1247 |
example, for the pattern /^a\d+z\d+/ the returned value is "z", but for |
example, for the pattern /^a\d+z\d+/ the returned value is "z", but for |
1248 |
/^a\dz\d/ the returned value is -1. |
/^a\dz\d/ the returned value is -1. |
1249 |
|
|
1251 |
PCRE_INFO_NAMEENTRYSIZE |
PCRE_INFO_NAMEENTRYSIZE |
1252 |
PCRE_INFO_NAMETABLE |
PCRE_INFO_NAMETABLE |
1253 |
|
|
1254 |
PCRE supports the use of named as well as numbered capturing parenthe- |
PCRE supports the use of named as well as numbered capturing parenthe- |
1255 |
ses. The names are just an additional way of identifying the parenthe- |
ses. The names are just an additional way of identifying the parenthe- |
1256 |
ses, which still acquire numbers. A convenience function called |
ses, which still acquire numbers. A convenience function called |
1257 |
pcre_get_named_substring() is provided for extracting an individual |
pcre_get_named_substring() is provided for extracting an individual |
1258 |
captured substring by name. It is also possible to extract the data |
captured substring by name. It is also possible to extract the data |
1259 |
directly, by first converting the name to a number in order to access |
directly, by first converting the name to a number in order to access |
1260 |
the correct pointers in the output vector (described with pcre_exec() |
the correct pointers in the output vector (described with pcre_exec() |
1261 |
below). To do the conversion, you need to use the name-to-number map, |
below). To do the conversion, you need to use the name-to-number map, |
1262 |
which is described by these three values. |
which is described by these three values. |
1263 |
|
|
1264 |
The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT |
The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT |
1265 |
gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size |
gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size |
1266 |
of each entry; both of these return an int value. The entry size |
of each entry; both of these return an int value. The entry size |
1267 |
depends on the length of the longest name. PCRE_INFO_NAMETABLE returns |
depends on the length of the longest name. PCRE_INFO_NAMETABLE returns |
1268 |
a pointer to the first entry of the table (a pointer to char). The |
a pointer to the first entry of the table (a pointer to char). The |
1269 |
first two bytes of each entry are the number of the capturing parenthe- |
first two bytes of each entry are the number of the capturing parenthe- |
1270 |
sis, most significant byte first. The rest of the entry is the corre- |
sis, most significant byte first. The rest of the entry is the corre- |
1271 |
sponding name, zero terminated. The names are in alphabetical order. |
sponding name, zero terminated. The names are in alphabetical order. |
1272 |
For example, consider the following pattern (assume PCRE_EXTENDED is |
For example, consider the following pattern (assume PCRE_EXTENDED is |
1273 |
set, so white space - including newlines - is ignored): |
set, so white space - including newlines - is ignored): |
1274 |
|
|
1275 |
(?P<date> (?P<year>(\d\d)?\d\d) - |
(?P<date> (?P<year>(\d\d)?\d\d) - |
1276 |
(?P<month>\d\d) - (?P<day>\d\d) ) |
(?P<month>\d\d) - (?P<day>\d\d) ) |
1277 |
|
|
1278 |
There are four named subpatterns, so the table has four entries, and |
There are four named subpatterns, so the table has four entries, and |
1279 |
each entry in the table is eight bytes long. The table is as follows, |
each entry in the table is eight bytes long. The table is as follows, |
1280 |
with non-printing bytes shows in hexadecimal, and undefined bytes shown |
with non-printing bytes shows in hexadecimal, and undefined bytes shown |
1281 |
as ??: |
as ??: |
1282 |
|
|
1285 |
00 04 m o n t h 00 |
00 04 m o n t h 00 |
1286 |
00 02 y e a r 00 ?? |
00 02 y e a r 00 ?? |
1287 |
|
|
1288 |
When writing code to extract data from named subpatterns using the |
When writing code to extract data from named subpatterns using the |
1289 |
name-to-number map, remember that the length of each entry is likely to |
name-to-number map, remember that the length of each entry is likely to |
1290 |
be different for each compiled pattern. |
be different for each compiled pattern. |
1291 |
|
|
1292 |
PCRE_INFO_OPTIONS |
PCRE_INFO_OPTIONS |
1293 |
|
|
1294 |
Return a copy of the options with which the pattern was compiled. The |
Return a copy of the options with which the pattern was compiled. The |
1295 |
fourth argument should point to an unsigned long int variable. These |
fourth argument should point to an unsigned long int variable. These |
1296 |
option bits are those specified in the call to pcre_compile(), modified |
option bits are those specified in the call to pcre_compile(), modified |
1297 |
by any top-level option settings within the pattern itself. |
by any top-level option settings within the pattern itself. |
1298 |
|
|
1299 |
A pattern is automatically anchored by PCRE if all of its top-level |
A pattern is automatically anchored by PCRE if all of its top-level |
1300 |
alternatives begin with one of the following: |
alternatives begin with one of the following: |
1301 |
|
|
1302 |
^ unless PCRE_MULTILINE is set |
^ unless PCRE_MULTILINE is set |
1310 |
|
|
1311 |
PCRE_INFO_SIZE |
PCRE_INFO_SIZE |
1312 |
|
|
1313 |
Return the size of the compiled pattern, that is, the value that was |
Return the size of the compiled pattern, that is, the value that was |
1314 |
passed as the argument to pcre_malloc() when PCRE was getting memory in |
passed as the argument to pcre_malloc() when PCRE was getting memory in |
1315 |
which to place the compiled data. The fourth argument should point to a |
which to place the compiled data. The fourth argument should point to a |
1316 |
size_t variable. |
size_t variable. |
1318 |
PCRE_INFO_STUDYSIZE |
PCRE_INFO_STUDYSIZE |
1319 |
|
|
1320 |
Return the size of the data block pointed to by the study_data field in |
Return the size of the data block pointed to by the study_data field in |
1321 |
a pcre_extra block. That is, it is the value that was passed to |
a pcre_extra block. That is, it is the value that was passed to |
1322 |
pcre_malloc() when PCRE was getting memory into which to place the data |
pcre_malloc() when PCRE was getting memory into which to place the data |
1323 |
created by pcre_study(). The fourth argument should point to a size_t |
created by pcre_study(). The fourth argument should point to a size_t |
1324 |
variable. |
variable. |
1325 |
|
|
1326 |
|
|
1328 |
|
|
1329 |
int pcre_info(const pcre *code, int *optptr, int *firstcharptr); |
int pcre_info(const pcre *code, int *optptr, int *firstcharptr); |
1330 |
|
|
1331 |
The pcre_info() function is now obsolete because its interface is too |
The pcre_info() function is now obsolete because its interface is too |
1332 |
restrictive to return all the available data about a compiled pattern. |
restrictive to return all the available data about a compiled pattern. |
1333 |
New programs should use pcre_fullinfo() instead. The yield of |
New programs should use pcre_fullinfo() instead. The yield of |
1334 |
pcre_info() is the number of capturing subpatterns, or one of the fol- |
pcre_info() is the number of capturing subpatterns, or one of the fol- |
1335 |
lowing negative numbers: |
lowing negative numbers: |
1336 |
|
|
1337 |
PCRE_ERROR_NULL the argument code was NULL |
PCRE_ERROR_NULL the argument code was NULL |
1338 |
PCRE_ERROR_BADMAGIC the "magic number" was not found |
PCRE_ERROR_BADMAGIC the "magic number" was not found |
1339 |
|
|
1340 |
If the optptr argument is not NULL, a copy of the options with which |
If the optptr argument is not NULL, a copy of the options with which |
1341 |
the pattern was compiled is placed in the integer it points to (see |
the pattern was compiled is placed in the integer it points to (see |
1342 |
PCRE_INFO_OPTIONS above). |
PCRE_INFO_OPTIONS above). |
1343 |
|
|
1344 |
If the pattern is not anchored and the firstcharptr argument is not |
If the pattern is not anchored and the firstcharptr argument is not |
1345 |
NULL, it is used to pass back information about the first character of |
NULL, it is used to pass back information about the first character of |
1346 |
any matched string (see PCRE_INFO_FIRSTBYTE above). |
any matched string (see PCRE_INFO_FIRSTBYTE above). |
1347 |
|
|
1348 |
|
|
1349 |
MATCHING A PATTERN |
REFERENCE COUNTS |
1350 |
|
|
1351 |
|
int pcre_refcount(pcre *code, int adjust); |
1352 |
|
|
1353 |
|
The pcre_refcount() function is used to maintain a reference count in |
1354 |
|
the data block that contains a compiled pattern. It is provided for the |
1355 |
|
benefit of applications that operate in an object-oriented manner, |
1356 |
|
where different parts of the application may be using the same compiled |
1357 |
|
pattern, but you want to free the block when they are all done. |
1358 |
|
|
1359 |
|
When a pattern is compiled, the reference count field is initialized to |
1360 |
|
zero. It is changed only by calling this function, whose action is to |
1361 |
|
add the adjust value (which may be positive or negative) to it. The |
1362 |
|
yield of the function is the new value. However, the value of the count |
1363 |
|
is constrained to lie between 0 and 65535, inclusive. If the new value |
1364 |
|
is outside these limits, it is forced to the appropriate limit value. |
1365 |
|
|
1366 |
|
Except when it is zero, the reference count is not correctly preserved |
1367 |
|
if a pattern is compiled on one host and then transferred to a host |
1368 |
|
whose byte-order is different. (This seems a highly unlikely scenario.) |
1369 |
|
|
1370 |
|
|
1371 |
|
MATCHING A PATTERN: THE TRADITIONAL FUNCTION |
1372 |
|
|
1373 |
int pcre_exec(const pcre *code, const pcre_extra *extra, |
int pcre_exec(const pcre *code, const pcre_extra *extra, |
1374 |
const char *subject, int length, int startoffset, |
const char *subject, int length, int startoffset, |
1375 |
int options, int *ovector, int ovecsize); |
int options, int *ovector, int ovecsize); |
1376 |
|
|
1377 |
The function pcre_exec() is called to match a subject string against a |
The function pcre_exec() is called to match a subject string against a |
1378 |
compiled pattern, which is passed in the code argument. If the pattern |
compiled pattern, which is passed in the code argument. If the pattern |
1379 |
has been studied, the result of the study should be passed in the extra |
has been studied, the result of the study should be passed in the extra |
1380 |
argument. |
argument. This function is the main matching facility of the library, |
1381 |
|
and it operates in a Perl-like manner. For specialist use there is also |
1382 |
|
an alternative matching function, which is described below in the sec- |
1383 |
|
tion about the pcre_dfa_exec() function. |
1384 |
|
|
1385 |
In most applications, the pattern will have been compiled (and option- |
In most applications, the pattern will have been compiled (and option- |
1386 |
ally studied) in the same process that calls pcre_exec(). However, it |
ally studied) in the same process that calls pcre_exec(). However, it |
1400 |
0, /* start at offset 0 in the subject */ |
0, /* start at offset 0 in the subject */ |
1401 |
0, /* default options */ |
0, /* default options */ |
1402 |
ovector, /* vector of integers for substring information */ |
ovector, /* vector of integers for substring information */ |
1403 |
30); /* number of elements in the vector (NOT size in |
30); /* number of elements (NOT size in bytes) */ |
|
bytes) */ |
|
1404 |
|
|
1405 |
Extra data for pcre_exec() |
Extra data for pcre_exec() |
1406 |
|
|
1407 |
If the extra argument is not NULL, it must point to a pcre_extra data |
If the extra argument is not NULL, it must point to a pcre_extra data |
1408 |
block. The pcre_study() function returns such a block (when it doesn't |
block. The pcre_study() function returns such a block (when it doesn't |
1409 |
return NULL), but you can also create one for yourself, and pass addi- |
return NULL), but you can also create one for yourself, and pass addi- |
1410 |
tional information in it. The fields in a pcre_extra block are as fol- |
tional information in it. The fields in a pcre_extra block are as fol- |
1411 |
lows: |
lows: |
1412 |
|
|
1413 |
unsigned long int flags; |
unsigned long int flags; |
1416 |
void *callout_data; |
void *callout_data; |
1417 |
const unsigned char *tables; |
const unsigned char *tables; |
1418 |
|
|
1419 |
The flags field is a bitmap that specifies which of the other fields |
The flags field is a bitmap that specifies which of the other fields |
1420 |
are set. The flag bits are: |
are set. The flag bits are: |
1421 |
|
|
1422 |
PCRE_EXTRA_STUDY_DATA |
PCRE_EXTRA_STUDY_DATA |
1424 |
PCRE_EXTRA_CALLOUT_DATA |
PCRE_EXTRA_CALLOUT_DATA |
1425 |
PCRE_EXTRA_TABLES |
PCRE_EXTRA_TABLES |
1426 |
|
|
1427 |
Other flag bits should be set to zero. The study_data field is set in |
Other flag bits should be set to zero. The study_data field is set in |
1428 |
the pcre_extra block that is returned by pcre_study(), together with |
the pcre_extra block that is returned by pcre_study(), together with |
1429 |
the appropriate flag bit. You should not set this yourself, but you may |
the appropriate flag bit. You should not set this yourself, but you may |
1430 |
add to the block by setting the other fields and their corresponding |
add to the block by setting the other fields and their corresponding |
1431 |
flag bits. |
flag bits. |
1432 |
|
|
1433 |
The match_limit field provides a means of preventing PCRE from using up |
The match_limit field provides a means of preventing PCRE from using up |
1434 |
a vast amount of resources when running patterns that are not going to |
a vast amount of resources when running patterns that are not going to |
1435 |
match, but which have a very large number of possibilities in their |
match, but which have a very large number of possibilities in their |
1436 |
search trees. The classic example is the use of nested unlimited |
search trees. The classic example is the use of nested unlimited |
1437 |
repeats. |
repeats. |
1438 |
|
|
1439 |
Internally, PCRE uses a function called match() which it calls repeat- |
Internally, PCRE uses a function called match() which it calls repeat- |
1440 |
edly (sometimes recursively). The limit is imposed on the number of |
edly (sometimes recursively). The limit is imposed on the number of |
1441 |
times this function is called during a match, which has the effect of |
times this function is called during a match, which has the effect of |
1442 |
limiting the amount of recursion and backtracking that can take place. |
limiting the amount of recursion and backtracking that can take place. |
1443 |
For patterns that are not anchored, the count starts from zero for each |
For patterns that are not anchored, the count starts from zero for each |
1444 |
position in the subject string. |
position in the subject string. |
1445 |
|
|
1446 |
The default limit for the library can be set when PCRE is built; the |
The default limit for the library can be set when PCRE is built; the |
1447 |
default default is 10 million, which handles all but the most extreme |
default default is 10 million, which handles all but the most extreme |
1448 |
cases. You can reduce the default by suppling pcre_exec() with a |
cases. You can reduce the default by suppling pcre_exec() with a |
1449 |
pcre_extra block in which match_limit is set to a smaller value, and |
pcre_extra block in which match_limit is set to a smaller value, and |
1450 |
PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is |
PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is |
1451 |
exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT. |
exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT. |
1452 |
|
|
1453 |
The pcre_callout field is used in conjunction with the "callout" fea- |
The pcre_callout field is used in conjunction with the "callout" fea- |
1454 |
ture, which is described in the pcrecallout documentation. |
ture, which is described in the pcrecallout documentation. |
1455 |
|
|
1456 |
The tables field is used to pass a character tables pointer to |
The tables field is used to pass a character tables pointer to |
1457 |
pcre_exec(); this overrides the value that is stored with the compiled |
pcre_exec(); this overrides the value that is stored with the compiled |
1458 |
pattern. A non-NULL value is stored with the compiled pattern only if |
pattern. A non-NULL value is stored with the compiled pattern only if |
1459 |
custom tables were supplied to pcre_compile() via its tableptr argu- |
custom tables were supplied to pcre_compile() via its tableptr argu- |
1460 |
ment. If NULL is passed to pcre_exec() using this mechanism, it forces |
ment. If NULL is passed to pcre_exec() using this mechanism, it forces |
1461 |
PCRE's internal tables to be used. This facility is helpful when re- |
PCRE's internal tables to be used. This facility is helpful when re- |
1462 |
using patterns that have been saved after compiling with an external |
using patterns that have been saved after compiling with an external |
1463 |
set of tables, because the external tables might be at a different |
set of tables, because the external tables might be at a different |
1464 |
address when pcre_exec() is called. See the pcreprecompile documenta- |
address when pcre_exec() is called. See the pcreprecompile documenta- |
1465 |
tion for a discussion of saving compiled patterns for later use. |
tion for a discussion of saving compiled patterns for later use. |
1466 |
|
|
1467 |
Option bits for pcre_exec() |
Option bits for pcre_exec() |
1468 |
|
|
1469 |
The unused bits of the options argument for pcre_exec() must be zero. |
The unused bits of the options argument for pcre_exec() must be zero. |
1470 |
The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL, |
The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL, |
1471 |
PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK and PCRE_PARTIAL. |
PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK and PCRE_PARTIAL. |
1472 |
|
|
1473 |
PCRE_ANCHORED |
PCRE_ANCHORED |
1474 |
|
|
1475 |
The PCRE_ANCHORED option limits pcre_exec() to matching at the first |
The PCRE_ANCHORED option limits pcre_exec() to matching at the first |
1476 |
matching position. If a pattern was compiled with PCRE_ANCHORED, or |
matching position. If a pattern was compiled with PCRE_ANCHORED, or |
1477 |
turned out to be anchored by virtue of its contents, it cannot be made |
turned out to be anchored by virtue of its contents, it cannot be made |
1478 |
unachored at matching time. |
unachored at matching time. |
1479 |
|
|
1480 |
PCRE_NOTBOL |
PCRE_NOTBOL |
1481 |
|
|
1482 |
This option specifies that first character of the subject string is not |
This option specifies that first character of the subject string is not |
1483 |
the beginning of a line, so the circumflex metacharacter should not |
the beginning of a line, so the circumflex metacharacter should not |
1484 |
match before it. Setting this without PCRE_MULTILINE (at compile time) |
match before it. Setting this without PCRE_MULTILINE (at compile time) |
1485 |
causes circumflex never to match. This option affects only the |
causes circumflex never to match. This option affects only the behav- |
1486 |
behaviour of the circumflex metacharacter. It does not affect \A. |
iour of the circumflex metacharacter. It does not affect \A. |
1487 |
|
|
1488 |
PCRE_NOTEOL |
PCRE_NOTEOL |
1489 |
|
|
1490 |
This option specifies that the end of the subject string is not the end |
This option specifies that the end of the subject string is not the end |
1491 |
of a line, so the dollar metacharacter should not match it nor (except |
of a line, so the dollar metacharacter should not match it nor (except |
1492 |
in multiline mode) a newline immediately before it. Setting this with- |
in multiline mode) a newline immediately before it. Setting this with- |
1493 |
out PCRE_MULTILINE (at compile time) causes dollar never to match. This |
out PCRE_MULTILINE (at compile time) causes dollar never to match. This |
1494 |
option affects only the behaviour of the dollar metacharacter. It does |
option affects only the behaviour of the dollar metacharacter. It does |
1495 |
not affect \Z or \z. |
not affect \Z or \z. |
1496 |
|
|
1497 |
PCRE_NOTEMPTY |
PCRE_NOTEMPTY |
1498 |
|
|
1499 |
An empty string is not considered to be a valid match if this option is |
An empty string is not considered to be a valid match if this option is |
1500 |
set. If there are alternatives in the pattern, they are tried. If all |
set. If there are alternatives in the pattern, they are tried. If all |
1501 |
the alternatives match the empty string, the entire match fails. For |
the alternatives match the empty string, the entire match fails. For |
1502 |
example, if the pattern |
example, if the pattern |
1503 |
|
|
1504 |
a?b? |
a?b? |
1505 |
|
|
1506 |
is applied to a string not beginning with "a" or "b", it matches the |
is applied to a string not beginning with "a" or "b", it matches the |
1507 |
empty string at the start of the subject. With PCRE_NOTEMPTY set, this |
empty string at the start of the subject. With PCRE_NOTEMPTY set, this |
1508 |
match is not valid, so PCRE searches further into the string for occur- |
match is not valid, so PCRE searches further into the string for occur- |
1509 |
rences of "a" or "b". |
rences of "a" or "b". |
1510 |
|
|
1511 |
Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe- |
Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe- |
1512 |
cial case of a pattern match of the empty string within its split() |
cial case of a pattern match of the empty string within its split() |
1513 |
function, and when using the /g modifier. It is possible to emulate |
function, and when using the /g modifier. It is possible to emulate |
1514 |
Perl's behaviour after matching a null string by first trying the match |
Perl's behaviour after matching a null string by first trying the match |
1515 |
again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then |
again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then |
1516 |
if that fails by advancing the starting offset (see below) and trying |
if that fails by advancing the starting offset (see below) and trying |
1517 |
an ordinary match again. There is some code that demonstrates how to do |
an ordinary match again. There is some code that demonstrates how to do |
1518 |
this in the pcredemo.c sample program. |
this in the pcredemo.c sample program. |
1519 |
|
|
1520 |
PCRE_NO_UTF8_CHECK |
PCRE_NO_UTF8_CHECK |
1521 |
|
|
1522 |
When PCRE_UTF8 is set at compile time, the validity of the subject as a |
When PCRE_UTF8 is set at compile time, the validity of the subject as a |
1523 |
UTF-8 string is automatically checked when pcre_exec() is subsequently |
UTF-8 string is automatically checked when pcre_exec() is subsequently |
1524 |
called. The value of startoffset is also checked to ensure that it |
called. The value of startoffset is also checked to ensure that it |
1525 |
points to the start of a UTF-8 character. If an invalid UTF-8 sequence |
points to the start of a UTF-8 character. If an invalid UTF-8 sequence |
1526 |
of bytes is found, pcre_exec() returns the error PCRE_ERROR_BADUTF8. If |
of bytes is found, pcre_exec() returns the error PCRE_ERROR_BADUTF8. If |
1527 |
startoffset contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is |
startoffset contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is |
1528 |
returned. |
returned. |
1529 |
|
|
1530 |
If you already know that your subject is valid, and you want to skip |
If you already know that your subject is valid, and you want to skip |
1531 |
these checks for performance reasons, you can set the |
these checks for performance reasons, you can set the |
1532 |
PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to |
PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to |
1533 |
do this for the second and subsequent calls to pcre_exec() if you are |
do this for the second and subsequent calls to pcre_exec() if you are |
1534 |
making repeated calls to find all the matches in a single subject |
making repeated calls to find all the matches in a single subject |
1535 |
string. However, you should be sure that the value of startoffset |
string. However, you should be sure that the value of startoffset |
1536 |
points to the start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is |
points to the start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is |
1537 |
set, the effect of passing an invalid UTF-8 string as a subject, or a |
set, the effect of passing an invalid UTF-8 string as a subject, or a |
1538 |
value of startoffset that does not point to the start of a UTF-8 char- |
value of startoffset that does not point to the start of a UTF-8 char- |
1539 |
acter, is undefined. Your program may crash. |
acter, is undefined. Your program may crash. |
1540 |
|
|
1541 |
PCRE_PARTIAL |
PCRE_PARTIAL |
1542 |
|
|
1543 |
This option turns on the partial matching feature. If the subject |
This option turns on the partial matching feature. If the subject |
1544 |
string fails to match the pattern, but at some point during the match- |
string fails to match the pattern, but at some point during the match- |
1545 |
ing process the end of the subject was reached (that is, the subject |
ing process the end of the subject was reached (that is, the subject |
1546 |
partially matches the pattern and the failure to match occurred only |
partially matches the pattern and the failure to match occurred only |
1547 |
because there were not enough subject characters), pcre_exec() returns |
because there were not enough subject characters), pcre_exec() returns |
1548 |
PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is |
PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is |
1549 |
used, there are restrictions on what may appear in the pattern. These |
used, there are restrictions on what may appear in the pattern. These |
1550 |
are discussed in the pcrepartial documentation. |
are discussed in the pcrepartial documentation. |
1551 |
|
|
1552 |
The string to be matched by pcre_exec() |
The string to be matched by pcre_exec() |
1553 |
|
|
1554 |
The subject string is passed to pcre_exec() as a pointer in subject, a |
The subject string is passed to pcre_exec() as a pointer in subject, a |
1555 |
length in length, and a starting byte offset in startoffset. In UTF-8 |
length in length, and a starting byte offset in startoffset. In UTF-8 |
1556 |
mode, the byte offset must point to the start of a UTF-8 character. |
mode, the byte offset must point to the start of a UTF-8 character. |
1557 |
Unlike the pattern string, the subject may contain binary zero bytes. |
Unlike the pattern string, the subject may contain binary zero bytes. |
1558 |
When the starting offset is zero, the search for a match starts at the |
When the starting offset is zero, the search for a match starts at the |
1559 |
beginning of the subject, and this is by far the most common case. |
beginning of the subject, and this is by far the most common case. |
1560 |
|
|
1561 |
A non-zero starting offset is useful when searching for another match |
A non-zero starting offset is useful when searching for another match |
1562 |
in the same subject by calling pcre_exec() again after a previous suc- |
in the same subject by calling pcre_exec() again after a previous suc- |
1563 |
cess. Setting startoffset differs from just passing over a shortened |
cess. Setting startoffset differs from just passing over a shortened |
1564 |
string and setting PCRE_NOTBOL in the case of a pattern that begins |
string and setting PCRE_NOTBOL in the case of a pattern that begins |
1565 |
with any kind of lookbehind. For example, consider the pattern |
with any kind of lookbehind. For example, consider the pattern |
1566 |
|
|
1567 |
\Biss\B |
\Biss\B |
1568 |
|
|
1569 |
which finds occurrences of "iss" in the middle of words. (\B matches |
which finds occurrences of "iss" in the middle of words. (\B matches |
1570 |
only if the current position in the subject is not a word boundary.) |
only if the current position in the subject is not a word boundary.) |
1571 |
When applied to the string "Mississipi" the first call to pcre_exec() |
When applied to the string "Mississipi" the first call to pcre_exec() |
1572 |
finds the first occurrence. If pcre_exec() is called again with just |
finds the first occurrence. If pcre_exec() is called again with just |
1573 |
the remainder of the subject, namely "issipi", it does not match, |
the remainder of the subject, namely "issipi", it does not match, |
1574 |
because \B is always false at the start of the subject, which is deemed |
because \B is always false at the start of the subject, which is deemed |
1575 |
to be a word boundary. However, if pcre_exec() is passed the entire |
to be a word boundary. However, if pcre_exec() is passed the entire |
1576 |
string again, but with startoffset set to 4, it finds the second occur- |
string again, but with startoffset set to 4, it finds the second occur- |
1577 |
rence of "iss" because it is able to look behind the starting point to |
rence of "iss" because it is able to look behind the starting point to |
1578 |
discover that it is preceded by a letter. |
discover that it is preceded by a letter. |
1579 |
|
|
1580 |
If a non-zero starting offset is passed when the pattern is anchored, |
If a non-zero starting offset is passed when the pattern is anchored, |
1581 |
one attempt to match at the given offset is made. This can only succeed |
one attempt to match at the given offset is made. This can only succeed |
1582 |
if the pattern does not require the match to be at the start of the |
if the pattern does not require the match to be at the start of the |
1583 |
subject. |
subject. |
1584 |
|
|
1585 |
How pcre_exec() returns captured substrings |
How pcre_exec() returns captured substrings |
1586 |
|
|
1587 |
In general, a pattern matches a certain portion of the subject, and in |
In general, a pattern matches a certain portion of the subject, and in |
1588 |
addition, further substrings from the subject may be picked out by |
addition, further substrings from the subject may be picked out by |
1589 |
parts of the pattern. Following the usage in Jeffrey Friedl's book, |
parts of the pattern. Following the usage in Jeffrey Friedl's book, |
1590 |
this is called "capturing" in what follows, and the phrase "capturing |
this is called "capturing" in what follows, and the phrase "capturing |
1591 |
subpattern" is used for a fragment of a pattern that picks out a sub- |
subpattern" is used for a fragment of a pattern that picks out a sub- |
1592 |
string. PCRE supports several other kinds of parenthesized subpattern |
string. PCRE supports several other kinds of parenthesized subpattern |
1593 |
that do not cause substrings to be captured. |
that do not cause substrings to be captured. |
1594 |
|
|
1595 |
Captured substrings are returned to the caller via a vector of integer |
Captured substrings are returned to the caller via a vector of integer |
1596 |
offsets whose address is passed in ovector. The number of elements in |
offsets whose address is passed in ovector. The number of elements in |
1597 |
the vector is passed in ovecsize, which must be a non-negative number. |
the vector is passed in ovecsize, which must be a non-negative number. |
1598 |
Note: this argument is NOT the size of ovector in bytes. |
Note: this argument is NOT the size of ovector in bytes. |
1599 |
|
|
1600 |
The first two-thirds of the vector is used to pass back captured sub- |
The first two-thirds of the vector is used to pass back captured sub- |
1601 |
strings, each substring using a pair of integers. The remaining third |
strings, each substring using a pair of integers. The remaining third |
1602 |
of the vector is used as workspace by pcre_exec() while matching cap- |
of the vector is used as workspace by pcre_exec() while matching cap- |
1603 |
turing subpatterns, and is not available for passing back information. |
turing subpatterns, and is not available for passing back information. |
1604 |
The length passed in ovecsize should always be a multiple of three. If |
The length passed in ovecsize should always be a multiple of three. If |
1605 |
it is not, it is rounded down. |
it is not, it is rounded down. |
1606 |
|
|
1607 |
When a match is successful, information about captured substrings is |
When a match is successful, information about captured substrings is |
1608 |
returned in pairs of integers, starting at the beginning of ovector, |
returned in pairs of integers, starting at the beginning of ovector, |
1609 |
and continuing up to two-thirds of its length at the most. The first |
and continuing up to two-thirds of its length at the most. The first |
1610 |
element of a pair is set to the offset of the first character in a sub- |
element of a pair is set to the offset of the first character in a sub- |
1611 |
string, and the second is set to the offset of the first character |
string, and the second is set to the offset of the first character |
1612 |
after the end of a substring. The first pair, ovector[0] and ovec- |
after the end of a substring. The first pair, ovector[0] and ovec- |
1613 |
tor[1], identify the portion of the subject string matched by the |
tor[1], identify the portion of the subject string matched by the |
1614 |
entire pattern. The next pair is used for the first capturing subpat- |
entire pattern. The next pair is used for the first capturing subpat- |
1615 |
tern, and so on. The value returned by pcre_exec() is the number of |
tern, and so on. The value returned by pcre_exec() is the number of |
1616 |
pairs that have been set. If there are no capturing subpatterns, the |
pairs that have been set. If there are no capturing subpatterns, the |
1617 |
return value from a successful match is 1, indicating that just the |
return value from a successful match is 1, indicating that just the |
1618 |
first pair of offsets has been set. |
first pair of offsets has been set. |
1619 |
|
|
1620 |
Some convenience functions are provided for extracting the captured |
Some convenience functions are provided for extracting the captured |
1621 |
substrings as separate strings. These are described in the following |
substrings as separate strings. These are described in the following |
1622 |
section. |
section. |
1623 |
|
|
1624 |
It is possible for an capturing subpattern number n+1 to match some |
It is possible for an capturing subpattern number n+1 to match some |
1625 |
part of the subject when subpattern n has not been used at all. For |
part of the subject when subpattern n has not been used at all. For |
1626 |
example, if the string "abc" is matched against the pattern (a|(z))(bc) |
example, if the string "abc" is matched against the pattern (a|(z))(bc) |
1627 |
subpatterns 1 and 3 are matched, but 2 is not. When this happens, both |
subpatterns 1 and 3 are matched, but 2 is not. When this happens, both |
1628 |
offset values corresponding to the unused subpattern are set to -1. |
offset values corresponding to the unused subpattern are set to -1. |
1629 |
|
|
1630 |
If a capturing subpattern is matched repeatedly, it is the last portion |
If a capturing subpattern is matched repeatedly, it is the last portion |
1631 |
of the string that it matched that is returned. |
of the string that it matched that is returned. |
1632 |
|
|
1633 |
If the vector is too small to hold all the captured substring offsets, |
If the vector is too small to hold all the captured substring offsets, |
1634 |
it is used as far as possible (up to two-thirds of its length), and the |
it is used as far as possible (up to two-thirds of its length), and the |
1635 |
function returns a value of zero. In particular, if the substring off- |
function returns a value of zero. In particular, if the substring off- |
1636 |
sets are not of interest, pcre_exec() may be called with ovector passed |
sets are not of interest, pcre_exec() may be called with ovector passed |
1637 |
as NULL and ovecsize as zero. However, if the pattern contains back |
as NULL and ovecsize as zero. However, if the pattern contains back |
1638 |
references and the ovector is not big enough to remember the related |
references and the ovector is not big enough to remember the related |
1639 |
substrings, PCRE has to get additional memory for use during matching. |
substrings, PCRE has to get additional memory for use during matching. |
1640 |
Thus it is usually advisable to supply an ovector. |
Thus it is usually advisable to supply an ovector. |
1641 |
|
|
1642 |
Note that pcre_info() can be used to find out how many capturing sub- |
Note that pcre_info() can be used to find out how many capturing sub- |
1643 |
patterns there are in a compiled pattern. The smallest size for ovector |
patterns there are in a compiled pattern. The smallest size for ovector |
1644 |
that will allow for n captured substrings, in addition to the offsets |
that will allow for n captured substrings, in addition to the offsets |
1645 |
of the substring matched by the whole pattern, is (n+1)*3. |
of the substring matched by the whole pattern, is (n+1)*3. |
1646 |
|
|
1647 |
Return values from pcre_exec() |
Return values from pcre_exec() |
1648 |
|
|
1649 |
If pcre_exec() fails, it returns a negative number. The following are |
If pcre_exec() fails, it returns a negative number. The following are |
1650 |
defined in the header file: |
defined in the header file: |
1651 |
|
|
1652 |
PCRE_ERROR_NOMATCH (-1) |
PCRE_ERROR_NOMATCH (-1) |
1655 |
|
|
1656 |
PCRE_ERROR_NULL (-2) |
PCRE_ERROR_NULL (-2) |
1657 |
|
|
1658 |
Either code or subject was passed as NULL, or ovector was NULL and |
Either code or subject was passed as NULL, or ovector was NULL and |
1659 |
ovecsize was not zero. |
ovecsize was not zero. |
1660 |
|
|
1661 |
PCRE_ERROR_BADOPTION (-3) |
PCRE_ERROR_BADOPTION (-3) |
1664 |
|
|
1665 |
PCRE_ERROR_BADMAGIC (-4) |
PCRE_ERROR_BADMAGIC (-4) |
1666 |
|
|
1667 |
PCRE stores a 4-byte "magic number" at the start of the compiled code, |
PCRE stores a 4-byte "magic number" at the start of the compiled code, |
1668 |
to catch the case when it is passed a junk pointer and to detect when a |
to catch the case when it is passed a junk pointer and to detect when a |
1669 |
pattern that was compiled in an environment of one endianness is run in |
pattern that was compiled in an environment of one endianness is run in |
1670 |
an environment with the other endianness. This is the error that PCRE |
an environment with the other endianness. This is the error that PCRE |
1671 |
gives when the magic number is not present. |
gives when the magic number is not present. |
1672 |
|
|
1673 |
PCRE_ERROR_UNKNOWN_NODE (-5) |
PCRE_ERROR_UNKNOWN_NODE (-5) |
1674 |
|
|
1675 |
While running the pattern match, an unknown item was encountered in the |
While running the pattern match, an unknown item was encountered in the |
1676 |
compiled pattern. This error could be caused by a bug in PCRE or by |
compiled pattern. This error could be caused by a bug in PCRE or by |
1677 |
overwriting of the compiled pattern. |
overwriting of the compiled pattern. |
1678 |
|
|
1679 |
PCRE_ERROR_NOMEMORY (-6) |
PCRE_ERROR_NOMEMORY (-6) |
1680 |
|
|
1681 |
If a pattern contains back references, but the ovector that is passed |
If a pattern contains back references, but the ovector that is passed |
1682 |
to pcre_exec() is not big enough to remember the referenced substrings, |
to pcre_exec() is not big enough to remember the referenced substrings, |
1683 |
PCRE gets a block of memory at the start of matching to use for this |
PCRE gets a block of memory at the start of matching to use for this |
1684 |
purpose. If the call via pcre_malloc() fails, this error is given. The |
purpose. If the call via pcre_malloc() fails, this error is given. The |
1685 |
memory is automatically freed at the end of matching. |
memory is automatically freed at the end of matching. |
1686 |
|
|
1687 |
PCRE_ERROR_NOSUBSTRING (-7) |
PCRE_ERROR_NOSUBSTRING (-7) |
1688 |
|
|
1689 |
This error is used by the pcre_copy_substring(), pcre_get_substring(), |
This error is used by the pcre_copy_substring(), pcre_get_substring(), |
1690 |
and pcre_get_substring_list() functions (see below). It is never |
and pcre_get_substring_list() functions (see below). It is never |
1691 |
returned by pcre_exec(). |
returned by pcre_exec(). |
1692 |
|
|
1693 |
PCRE_ERROR_MATCHLIMIT (-8) |
PCRE_ERROR_MATCHLIMIT (-8) |
1694 |
|
|
1695 |
The recursion and backtracking limit, as specified by the match_limit |
The recursion and backtracking limit, as specified by the match_limit |
1696 |
field in a pcre_extra structure (or defaulted) was reached. See the |
field in a pcre_extra structure (or defaulted) was reached. See the |
1697 |
description above. |
description above. |
1698 |
|
|
1699 |
PCRE_ERROR_CALLOUT (-9) |
PCRE_ERROR_CALLOUT (-9) |
1700 |
|
|
1701 |
This error is never generated by pcre_exec() itself. It is provided for |
This error is never generated by pcre_exec() itself. It is provided for |
1702 |
use by callout functions that want to yield a distinctive error code. |
use by callout functions that want to yield a distinctive error code. |
1703 |
See the pcrecallout documentation for details. |
See the pcrecallout documentation for details. |
1704 |
|
|
1705 |
PCRE_ERROR_BADUTF8 (-10) |
PCRE_ERROR_BADUTF8 (-10) |
1706 |
|
|
1707 |
A string that contains an invalid UTF-8 byte sequence was passed as a |
A string that contains an invalid UTF-8 byte sequence was passed as a |
1708 |
subject. |
subject. |
1709 |
|
|
1710 |
PCRE_ERROR_BADUTF8_OFFSET (-11) |
PCRE_ERROR_BADUTF8_OFFSET (-11) |
1711 |
|
|
1712 |
The UTF-8 byte sequence that was passed as a subject was valid, but the |
The UTF-8 byte sequence that was passed as a subject was valid, but the |
1713 |
value of startoffset did not point to the beginning of a UTF-8 charac- |
value of startoffset did not point to the beginning of a UTF-8 charac- |
1714 |
ter. |
ter. |
1715 |
|
|
1716 |
PCRE_ERROR_PARTIAL (-12) |
PCRE_ERROR_PARTIAL (-12) |
1717 |
|
|
1718 |
The subject string did not match, but it did match partially. See the |
The subject string did not match, but it did match partially. See the |
1719 |
pcrepartial documentation for details of partial matching. |
pcrepartial documentation for details of partial matching. |
1720 |
|
|
1721 |
PCRE_ERROR_BAD_PARTIAL (-13) |
PCRE_ERROR_BADPARTIAL (-13) |
1722 |
|
|
1723 |
The PCRE_PARTIAL option was used with a compiled pattern containing |
The PCRE_PARTIAL option was used with a compiled pattern containing |
1724 |
items that are not supported for partial matching. See the pcrepartial |
items that are not supported for partial matching. See the pcrepartial |
1725 |
documentation for details of partial matching. |
documentation for details of partial matching. |
1726 |
|
|
1727 |
PCRE_ERROR_INTERNAL (-14) |
PCRE_ERROR_INTERNAL (-14) |
1728 |
|
|
1729 |
An unexpected internal error has occurred. This error could be caused |
An unexpected internal error has occurred. This error could be caused |
1730 |
by a bug in PCRE or by overwriting of the compiled pattern. |
by a bug in PCRE or by overwriting of the compiled pattern. |
1731 |
|
|
1732 |
PCRE_ERROR_BADCOUNT (-15) |
PCRE_ERROR_BADCOUNT (-15) |
1733 |
|
|
1734 |
This error is given if the value of the ovecsize argument is negative. |
This error is given if the value of the ovecsize argument is negative. |
1735 |
|
|
1736 |
|
|
1737 |
EXTRACTING CAPTURED SUBSTRINGS BY NUMBER |
EXTRACTING CAPTURED SUBSTRINGS BY NUMBER |
1747 |
int pcre_get_substring_list(const char *subject, |
int pcre_get_substring_list(const char *subject, |
1748 |
int *ovector, int stringcount, const char ***listptr); |
int *ovector, int stringcount, const char ***listptr); |
1749 |
|
|
1750 |
Captured substrings can be accessed directly by using the offsets |
Captured substrings can be accessed directly by using the offsets |
1751 |
returned by pcre_exec() in ovector. For convenience, the functions |
returned by pcre_exec() in ovector. For convenience, the functions |
1752 |
pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub- |
pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub- |
1753 |
string_list() are provided for extracting captured substrings as new, |
string_list() are provided for extracting captured substrings as new, |
1754 |
separate, zero-terminated strings. These functions identify substrings |
separate, zero-terminated strings. These functions identify substrings |
1755 |
by number. The next section describes functions for extracting named |
by number. The next section describes functions for extracting named |
1756 |
substrings. A substring that contains a binary zero is correctly |
substrings. A substring that contains a binary zero is correctly |
1757 |
extracted and has a further zero added on the end, but the result is |
extracted and has a further zero added on the end, but the result is |
1758 |
not, of course, a C string. |
not, of course, a C string. |
1759 |
|
|
1760 |
The first three arguments are the same for all three of these func- |
The first three arguments are the same for all three of these func- |
1761 |
tions: subject is the subject string that has just been successfully |
tions: subject is the subject string that has just been successfully |
1762 |
matched, ovector is a pointer to the vector of integer offsets that was |
matched, ovector is a pointer to the vector of integer offsets that was |
1763 |
passed to pcre_exec(), and stringcount is the number of substrings that |
passed to pcre_exec(), and stringcount is the number of substrings that |
1764 |
were captured by the match, including the substring that matched the |
were captured by the match, including the substring that matched the |
1765 |
entire regular expression. This is the value returned by pcre_exec() if |
entire regular expression. This is the value returned by pcre_exec() if |
1766 |
it is greater than zero. If pcre_exec() returned zero, indicating that |
it is greater than zero. If pcre_exec() returned zero, indicating that |
1767 |
it ran out of space in ovector, the value passed as stringcount should |
it ran out of space in ovector, the value passed as stringcount should |
1768 |
be the number of elements in the vector divided by three. |
be the number of elements in the vector divided by three. |
1769 |
|
|
1770 |
The functions pcre_copy_substring() and pcre_get_substring() extract a |
The functions pcre_copy_substring() and pcre_get_substring() extract a |
1771 |
single substring, whose number is given as stringnumber. A value of |
single substring, whose number is given as stringnumber. A value of |
1772 |
zero extracts the substring that matched the entire pattern, whereas |
zero extracts the substring that matched the entire pattern, whereas |
1773 |
higher values extract the captured substrings. For pcre_copy_sub- |
higher values extract the captured substrings. For pcre_copy_sub- |
1774 |
string(), the string is placed in buffer, whose length is given by |
string(), the string is placed in buffer, whose length is given by |
1775 |
buffersize, while for pcre_get_substring() a new block of memory is |
buffersize, while for pcre_get_substring() a new block of memory is |
1776 |
obtained via pcre_malloc, and its address is returned via stringptr. |
obtained via pcre_malloc, and its address is returned via stringptr. |
1777 |
The yield of the function is the length of the string, not including |
The yield of the function is the length of the string, not including |
1778 |
the terminating zero, or one of |
the terminating zero, or one of |
1779 |
|
|
1780 |
PCRE_ERROR_NOMEMORY (-6) |
PCRE_ERROR_NOMEMORY (-6) |
1781 |
|
|
1782 |
The buffer was too small for pcre_copy_substring(), or the attempt to |
The buffer was too small for pcre_copy_substring(), or the attempt to |
1783 |
get memory failed for pcre_get_substring(). |
get memory failed for pcre_get_substring(). |
1784 |
|
|
1785 |
PCRE_ERROR_NOSUBSTRING (-7) |
PCRE_ERROR_NOSUBSTRING (-7) |
1786 |
|
|
1787 |
There is no substring whose number is stringnumber. |
There is no substring whose number is stringnumber. |
1788 |
|
|
1789 |
The pcre_get_substring_list() function extracts all available sub- |
The pcre_get_substring_list() function extracts all available sub- |
1790 |
strings and builds a list of pointers to them. All this is done in a |
strings and builds a list of pointers to them. All this is done in a |
1791 |
single block of memory that is obtained via pcre_malloc. The address of |
single block of memory that is obtained via pcre_malloc. The address of |
1792 |
the memory block is returned via listptr, which is also the start of |
the memory block is returned via listptr, which is also the start of |
1793 |
the list of string pointers. The end of the list is marked by a NULL |
the list of string pointers. The end of the list is marked by a NULL |
1794 |
pointer. The yield of the function is zero if all went well, or |
pointer. The yield of the function is zero if all went well, or |
1795 |
|
|
1796 |
PCRE_ERROR_NOMEMORY (-6) |
PCRE_ERROR_NOMEMORY (-6) |
1797 |
|
|
1798 |
if the attempt to get the memory block failed. |
if the attempt to get the memory block failed. |
1799 |
|
|
1800 |
When any of these functions encounter a substring that is unset, which |
When any of these functions encounter a substring that is unset, which |
1801 |
can happen when capturing subpattern number n+1 matches some part of |
can happen when capturing subpattern number n+1 matches some part of |
1802 |
the subject, but subpattern n has not been used at all, they return an |
the subject, but subpattern n has not been used at all, they return an |
1803 |
empty string. This can be distinguished from a genuine zero-length sub- |
empty string. This can be distinguished from a genuine zero-length sub- |
1804 |
string by inspecting the appropriate offset in ovector, which is nega- |
string by inspecting the appropriate offset in ovector, which is nega- |
1805 |
tive for unset substrings. |
tive for unset substrings. |
1806 |
|
|
1807 |
The two convenience functions pcre_free_substring() and pcre_free_sub- |
The two convenience functions pcre_free_substring() and pcre_free_sub- |
1808 |
string_list() can be used to free the memory returned by a previous |
string_list() can be used to free the memory returned by a previous |
1809 |
call of pcre_get_substring() or pcre_get_substring_list(), respec- |
call of pcre_get_substring() or pcre_get_substring_list(), respec- |
1810 |
tively. They do nothing more than call the function pointed to by |
tively. They do nothing more than call the function pointed to by |
1811 |
pcre_free, which of course could be called directly from a C program. |
pcre_free, which of course could be called directly from a C program. |
1812 |
However, PCRE is used in some situations where it is linked via a spe- |
However, PCRE is used in some situations where it is linked via a spe- |
1813 |
cial interface to another programming language which cannot use |
cial interface to another programming language which cannot use |
1814 |
pcre_free directly; it is for these cases that the functions are |
pcre_free directly; it is for these cases that the functions are pro- |
1815 |
provided. |
vided. |
1816 |
|
|
1817 |
|
|
1818 |
EXTRACTING CAPTURED SUBSTRINGS BY NAME |
EXTRACTING CAPTURED SUBSTRINGS BY NAME |
1830 |
int stringcount, const char *stringname, |
int stringcount, const char *stringname, |
1831 |
const char **stringptr); |
const char **stringptr); |
1832 |
|
|
1833 |
To extract a substring by name, you first have to find associated num- |
To extract a substring by name, you first have to find associated num- |
1834 |
ber. For example, for this pattern |
ber. For example, for this pattern |
1835 |
|
|
1836 |
(a+)b(?<xxx>\d+)... |
(a+)b(?<xxx>\d+)... |
1837 |
|
|
1838 |
the number of the subpattern called "xxx" is 2. You can find the number |
the number of the subpattern called "xxx" is 2. You can find the number |
1839 |
from the name by calling pcre_get_stringnumber(). The first argument is |
from the name by calling pcre_get_stringnumber(). The first argument is |
1840 |
the compiled pattern, and the second is the name. The yield of the |
the compiled pattern, and the second is the name. The yield of the |
1841 |
function is the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if |
function is the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if |
1842 |
there is no subpattern of that name. |
there is no subpattern of that name. |
1843 |
|
|
1844 |
Given the number, you can extract the substring directly, or use one of |
Given the number, you can extract the substring directly, or use one of |
1845 |
the functions described in the previous section. For convenience, there |
the functions described in the previous section. For convenience, there |
1846 |
are also two functions that do the whole job. |
are also two functions that do the whole job. |
1847 |
|
|
1848 |
Most of the arguments of pcre_copy_named_substring() and |
Most of the arguments of pcre_copy_named_substring() and |
1849 |
pcre_get_named_substring() are the same as those for the similarly |
pcre_get_named_substring() are the same as those for the similarly |
1850 |
named functions that extract by number. As these are described in the |
named functions that extract by number. As these are described in the |
1851 |
previous section, they are not re-described here. There are just two |
previous section, they are not re-described here. There are just two |
1852 |
differences: |
differences: |
1853 |
|
|
1854 |
First, instead of a substring number, a substring name is given. Sec- |
First, instead of a substring number, a substring name is given. Sec- |
1855 |
ond, there is an extra argument, given at the start, which is a pointer |
ond, there is an extra argument, given at the start, which is a pointer |
1856 |
to the compiled pattern. This is needed in order to gain access to the |
to the compiled pattern. This is needed in order to gain access to the |
1857 |
name-to-number translation table. |
name-to-number translation table. |
1858 |
|
|
1859 |
These functions call pcre_get_stringnumber(), and if it succeeds, they |
These functions call pcre_get_stringnumber(), and if it succeeds, they |
1860 |
then call pcre_copy_substring() or pcre_get_substring(), as appropri- |
then call pcre_copy_substring() or pcre_get_substring(), as appropri- |
1861 |
ate. |
ate. |
1862 |
|
|
|
Last updated: 09 September 2004 |
|
|
Copyright (c) 1997-2004 University of Cambridge. |
|
|
----------------------------------------------------------------------------- |
|
1863 |
|
|
1864 |
PCRE(3) PCRE(3) |
FINDING ALL POSSIBLE MATCHES |
1865 |
|
|
1866 |
|
The traditional matching function uses a similar algorithm to Perl, |
1867 |
|
which stops when it finds the first match, starting at a given point in |
1868 |
|
the subject. If you want to find all possible matches, or the longest |
1869 |
|
possible match, consider using the alternative matching function (see |
1870 |
|
below) instead. If you cannot use the alternative function, but still |
1871 |
|
need to find all possible matches, you can kludge it up by making use |
1872 |
|
of the callout facility, which is described in the pcrecallout documen- |
1873 |
|
tation. |
1874 |
|
|
1875 |
|
What you have to do is to insert a callout right at the end of the pat- |
1876 |
|
tern. When your callout function is called, extract and save the cur- |
1877 |
|
rent matched substring. Then return 1, which forces pcre_exec() to |
1878 |
|
backtrack and try other alternatives. Ultimately, when it runs out of |
1879 |
|
matches, pcre_exec() will yield PCRE_ERROR_NOMATCH. |
1880 |
|
|
1881 |
|
|
1882 |
|
MATCHING A PATTERN: THE ALTERNATIVE FUNCTION |
1883 |
|
|
1884 |
|
int pcre_dfa_exec(const pcre *code, const pcre_extra *extra, |
1885 |
|
const char *subject, int length, int startoffset, |
1886 |
|
int options, int *ovector, int ovecsize, |
1887 |
|
int *workspace, int wscount); |
1888 |
|
|
1889 |
|
The function pcre_dfa_exec() is called to match a subject string |
1890 |
|
against a compiled pattern, using a "DFA" matching algorithm. This has |
1891 |
|
different characteristics to the normal algorithm, and is not compati- |
1892 |
|
ble with Perl. Some of the features of PCRE patterns are not supported. |
1893 |
|
Nevertheless, there are times when this kind of matching can be useful. |
1894 |
|
For a discussion of the two matching algorithms, see the pcrematching |
1895 |
|
documentation. |
1896 |
|
|
1897 |
|
The arguments for the pcre_dfa_exec() function are the same as for |
1898 |
|
pcre_exec(), plus two extras. The ovector argument is used in a differ- |
1899 |
|
ent way, and this is described below. The other common arguments are |
1900 |
|
used in the same way as for pcre_exec(), so their description is not |
1901 |
|
repeated here. |
1902 |
|
|
1903 |
|
The two additional arguments provide workspace for the function. The |
1904 |
|
workspace vector should contain at least 20 elements. It is used for |
1905 |
|
keeping track of multiple paths through the pattern tree. More |
1906 |
|
workspace will be needed for patterns and subjects where there are a |
1907 |
|
lot of possible matches. |
1908 |
|
|
1909 |
|
Here is an example of a simple call to pcre_exec(): |
1910 |
|
|
1911 |
|
int rc; |
1912 |
|
int ovector[10]; |
1913 |
|
int wspace[20]; |
1914 |
|
rc = pcre_exec( |
1915 |
|
re, /* result of pcre_compile() */ |
1916 |
|
NULL, /* we didn't study the pattern */ |
1917 |
|
"some string", /* the subject string */ |
1918 |
|
11, /* the length of the subject string */ |
1919 |
|
0, /* start at offset 0 in the subject */ |
1920 |
|
0, /* default options */ |
1921 |
|
ovector, /* vector of integers for substring information */ |
1922 |
|
10, /* number of elements (NOT size in bytes) */ |
1923 |
|
wspace, /* working space vector */ |
1924 |
|
20); /* number of elements (NOT size in bytes) */ |
1925 |
|
|
1926 |
|
Option bits for pcre_dfa_exec() |
1927 |
|
|
1928 |
|
The unused bits of the options argument for pcre_dfa_exec() must be |
1929 |
|
zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL, |
1930 |
|
PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL, |
1931 |
|
PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last three of |
1932 |
|
these are the same as for pcre_exec(), so their description is not |
1933 |
|
repeated here. |
1934 |
|
|
1935 |
|
PCRE_PARTIAL |
1936 |
|
|
1937 |
|
This has the same general effect as it does for pcre_exec(), but the |
1938 |
|
details are slightly different. When PCRE_PARTIAL is set for |
1939 |
|
pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is converted into |
1940 |
|
PCRE_ERROR_PARTIAL if the end of the subject is reached, there have |
1941 |
|
been no complete matches, but there is still at least one matching pos- |
1942 |
|
sibility. The portion of the string that provided the partial match is |
1943 |
|
set as the first matching string. |
1944 |
|
|
1945 |
|
PCRE_DFA_SHORTEST |
1946 |
|
|
1947 |
|
Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to |
1948 |
|
stop as soon as it has found one match. Because of the way the DFA |
1949 |
|
algorithm works, this is necessarily the shortest possible match at the |
1950 |
|
first possible matching point in the subject string. |
1951 |
|
|
1952 |
|
PCRE_DFA_RESTART |
1953 |
|
|
1954 |
|
When pcre_dfa_exec() is called with the PCRE_PARTIAL option, and |
1955 |
|
returns a partial match, it is possible to call it again, with addi- |
1956 |
|
tional subject characters, and have it continue with the same match. |
1957 |
|
The PCRE_DFA_RESTART option requests this action; when it is set, the |
1958 |
|
workspace and wscount options must reference the same vector as before |
1959 |
|
because data about the match so far is left in them after a partial |
1960 |
|
match. There is more discussion of this facility in the pcrepartial |
1961 |
|
documentation. |
1962 |
|
|
1963 |
|
Successful returns from pcre_dfa_exec() |
1964 |
|
|
1965 |
|
When pcre_dfa_exec() succeeds, it may have matched more than one sub- |
1966 |
|
string in the subject. Note, however, that all the matches from one run |
1967 |
|
of the function start at the same point in the subject. The shorter |
1968 |
|
matches are all initial substrings of the longer matches. For example, |
1969 |
|
if the pattern |
1970 |
|
|
1971 |
|
<.*> |
1972 |
|
|
1973 |
|
is matched against the string |
1974 |
|
|
1975 |
|
This is <something> <something else> <something further> no more |
1976 |
|
|
1977 |
|
the three matched strings are |
1978 |
|
|
1979 |
|
<something> |
1980 |
|
<something> <something else> |
1981 |
|
<something> <something else> <something further> |
1982 |
|
|
1983 |
|
On success, the yield of the function is a number greater than zero, |
1984 |
|
which is the number of matched substrings. The substrings themselves |
1985 |
|
are returned in ovector. Each string uses two elements; the first is |
1986 |
|
the offset to the start, and the second is the offset to the end. All |
1987 |
|
the strings have the same start offset. (Space could have been saved by |
1988 |
|
giving this only once, but it was decided to retain some compatibility |
1989 |
|
with the way pcre_exec() returns data, even though the meaning of the |
1990 |
|
strings is different.) |
1991 |
|
|
1992 |
|
The strings are returned in reverse order of length; that is, the long- |
1993 |
|
est matching string is given first. If there were too many matches to |
1994 |
|
fit into ovector, the yield of the function is zero, and the vector is |
1995 |
|
filled with the longest matches. |
1996 |
|
|
1997 |
|
Error returns from pcre_dfa_exec() |
1998 |
|
|
1999 |
|
The pcre_dfa_exec() function returns a negative number when it fails. |
2000 |
|
Many of the errors are the same as for pcre_exec(), and these are |
2001 |
|
described above. There are in addition the following errors that are |
2002 |
|
specific to pcre_dfa_exec(): |
2003 |
|
|
2004 |
|
PCRE_ERROR_DFA_UITEM (-16) |
2005 |
|
|
2006 |
|
This return is given if pcre_dfa_exec() encounters an item in the pat- |
2007 |
|
tern that it does not support, for instance, the use of \C or a back |
2008 |
|
reference. |
2009 |
|
|
2010 |
|
PCRE_ERROR_DFA_UCOND (-17) |
2011 |
|
|
2012 |
|
This return is given if pcre_dfa_exec() encounters a condition item in |
2013 |
|
a pattern that uses a back reference for the condition. This is not |
2014 |
|
supported. |
2015 |
|
|
2016 |
|
PCRE_ERROR_DFA_UMLIMIT (-18) |
2017 |
|
|
2018 |
|
This return is given if pcre_dfa_exec() is called with an extra block |
2019 |
|
that contains a setting of the match_limit field. This is not supported |
2020 |
|
(it is meaningless). |
2021 |
|
|
2022 |
|
PCRE_ERROR_DFA_WSSIZE (-19) |
2023 |
|
|
2024 |
|
This return is given if pcre_dfa_exec() runs out of space in the |
2025 |
|
workspace vector. |
2026 |
|
|
2027 |
|
PCRE_ERROR_DFA_RECURSE (-20) |
2028 |
|
|
2029 |
|
When a recursive subpattern is processed, the matching function calls |
2030 |
|
itself recursively, using private vectors for ovector and workspace. |
2031 |
|
This error is given if the output vector is not large enough. This |
2032 |
|
should be extremely rare, as a vector of size 1000 is used. |
2033 |
|
|
2034 |
|
Last updated: 16 May 2005 |
2035 |
|
Copyright (c) 1997-2005 University of Cambridge. |
2036 |
|
----------------------------------------------------------------------------- |
2037 |
|
|
2038 |
|
|
2039 |
|
|
2040 |
NAME |
NAME |
2041 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
2042 |
|
|
2043 |
|
|
2044 |
PCRE CALLOUTS |
PCRE CALLOUTS |
2045 |
|
|
2046 |
int (*pcre_callout)(pcre_callout_block *); |
int (*pcre_callout)(pcre_callout_block *); |
2095 |
THE CALLOUT INTERFACE |
THE CALLOUT INTERFACE |
2096 |
|
|
2097 |
During matching, when PCRE reaches a callout point, the external func- |
During matching, when PCRE reaches a callout point, the external func- |
2098 |
tion defined by pcre_callout is called (if it is set). The only argu- |
tion defined by pcre_callout is called (if it is set). This applies to |
2099 |
ment is a pointer to a pcre_callout block. This structure contains the |
both the pcre_exec() and the pcre_dfa_exec() matching functions. The |
2100 |
following fields: |
only argument to the callout function is a pointer to a pcre_callout |
2101 |
|
block. This structure contains the following fields: |
2102 |
|
|
2103 |
int version; |
int version; |
2104 |
int callout_number; |
int callout_number; |
2113 |
int pattern_position; |
int pattern_position; |
2114 |
int next_item_length; |
int next_item_length; |
2115 |
|
|
2116 |
The version field is an integer containing the version number of the |
The version field is an integer containing the version number of the |
2117 |
block format. The initial version was 0; the current version is 1. The |
block format. The initial version was 0; the current version is 1. The |
2118 |
version number will change again in future if additional fields are |
version number will change again in future if additional fields are |
2119 |
added, but the intention is never to remove any of the existing fields. |
added, but the intention is never to remove any of the existing fields. |
2120 |
|
|
2121 |
The callout_number field contains the number of the callout, as com- |
The callout_number field contains the number of the callout, as com- |
2122 |
piled into the pattern (that is, the number after ?C for manual call- |
piled into the pattern (that is, the number after ?C for manual call- |
2123 |
outs, and 255 for automatically generated callouts). |
outs, and 255 for automatically generated callouts). |
2124 |
|
|
2125 |
The offset_vector field is a pointer to the vector of offsets that was |
The offset_vector field is a pointer to the vector of offsets that was |
2126 |
passed by the caller to pcre_exec(). The contents can be inspected in |
passed by the caller to pcre_exec() or pcre_dfa_exec(). When |
2127 |
order to extract substrings that have been matched so far, in the same |
pcre_exec() is used, the contents can be inspected in order to extract |
2128 |
way as for extracting substrings after a match has completed. |
substrings that have been matched so far, in the same way as for |
2129 |
|
extracting substrings after a match has completed. For pcre_dfa_exec() |
2130 |
|
this field is not useful. |
2131 |
|
|
2132 |
The subject and subject_length fields contain copies of the values that |
The subject and subject_length fields contain copies of the values that |
2133 |
were passed to pcre_exec(). |
were passed to pcre_exec(). |
2134 |
|
|
2135 |
The start_match field contains the offset within the subject at which |
The start_match field contains the offset within the subject at which |
2136 |
the current match attempt started. If the pattern is not anchored, the |
the current match attempt started. If the pattern is not anchored, the |
2137 |
callout function may be called several times from the same point in the |
callout function may be called several times from the same point in the |
2138 |
pattern for different starting points in the subject. |
pattern for different starting points in the subject. |
2139 |
|
|
2140 |
The current_position field contains the offset within the subject of |
The current_position field contains the offset within the subject of |
2141 |
the current match pointer. |
the current match pointer. |
2142 |
|
|
2143 |
The capture_top field contains one more than the number of the highest |
When the pcre_exec() function is used, the capture_top field contains |
2144 |
numbered captured substring so far. If no substrings have been cap- |
one more than the number of the highest numbered captured substring so |
2145 |
tured, the value of capture_top is one. |
far. If no substrings have been captured, the value of capture_top is |
2146 |
|
one. This is always the case when pcre_dfa_exec() is used, because it |
2147 |
The capture_last field contains the number of the most recently cap- |
does not support captured substrings. |
2148 |
tured substring. If no substrings have been captured, its value is -1. |
|
2149 |
|
The capture_last field contains the number of the most recently cap- |
2150 |
The callout_data field contains a value that is passed to pcre_exec() |
tured substring. If no substrings have been captured, its value is -1. |
2151 |
by the caller specifically so that it can be passed back in callouts. |
This is always the case when pcre_dfa_exec() is used. |
2152 |
It is passed in the pcre_callout field of the pcre_extra data struc- |
|
2153 |
ture. If no such data was passed, the value of callout_data in a |
The callout_data field contains a value that is passed to pcre_exec() |
2154 |
pcre_callout block is NULL. There is a description of the pcre_extra |
or pcre_dfa_exec() specifically so that it can be passed back in call- |
2155 |
|
outs. It is passed in the pcre_callout field of the pcre_extra data |
2156 |
|
structure. If no such data was passed, the value of callout_data in a |
2157 |
|
pcre_callout block is NULL. There is a description of the pcre_extra |
2158 |
structure in the pcreapi documentation. |
structure in the pcreapi documentation. |
2159 |
|
|
2160 |
The pattern_position field is present from version 1 of the pcre_call- |
The pattern_position field is present from version 1 of the pcre_call- |
2161 |
out structure. It contains the offset to the next item to be matched in |
out structure. It contains the offset to the next item to be matched in |
2162 |
the pattern string. |
the pattern string. |
2163 |
|
|
2164 |
The next_item_length field is present from version 1 of the pcre_call- |
The next_item_length field is present from version 1 of the pcre_call- |
2165 |
out structure. It contains the length of the next item to be matched in |
out structure. It contains the length of the next item to be matched in |
2166 |
the pattern string. When the callout immediately precedes an alterna- |
the pattern string. When the callout immediately precedes an alterna- |
2167 |
tion bar, a closing parenthesis, or the end of the pattern, the length |
tion bar, a closing parenthesis, or the end of the pattern, the length |
2168 |
is zero. When the callout precedes an opening parenthesis, the length |
is zero. When the callout precedes an opening parenthesis, the length |
2169 |
is that of the entire subpattern. |
is that of the entire subpattern. |
2170 |
|
|
2171 |
The pattern_position and next_item_length fields are intended to help |
The pattern_position and next_item_length fields are intended to help |
2172 |
in distinguishing between different automatic callouts, which all have |
in distinguishing between different automatic callouts, which all have |
2173 |
the same callout number. However, they are set for all callouts. |
the same callout number. However, they are set for all callouts. |
2174 |
|
|
2175 |
|
|
2176 |
RETURN VALUES |
RETURN VALUES |
2177 |
|
|
2178 |
The external callout function returns an integer to PCRE. If the value |
The external callout function returns an integer to PCRE. If the value |
2179 |
is zero, matching proceeds as normal. If the value is greater than |
is zero, matching proceeds as normal. If the value is greater than |
2180 |
zero, matching fails at the current point, but backtracking to test |
zero, matching fails at the current point, but the testing of other |
2181 |
other matching possibilities goes ahead, just as if a lookahead asser- |
matching possibilities goes ahead, just as if a lookahead assertion had |
2182 |
tion had failed. If the value is less than zero, the match is aban- |
failed. If the value is less than zero, the match is abandoned, and |
2183 |
doned, and pcre_exec() returns the negative value. |
pcre_exec() (or pcre_dfa_exec()) returns the negative value. |
2184 |
|
|
2185 |
Negative values should normally be chosen from the set of |
Negative values should normally be chosen from the set of |
2186 |
PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan- |
PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan- |
2187 |
dard "no match" failure. The error number PCRE_ERROR_CALLOUT is |
dard "no match" failure. The error number PCRE_ERROR_CALLOUT is |
2188 |
reserved for use by callout functions; it will never be used by PCRE |
reserved for use by callout functions; it will never be used by PCRE |
2189 |
itself. |
itself. |
2190 |
|
|
2191 |
Last updated: 09 September 2004 |
Last updated: 28 February 2005 |
2192 |
Copyright (c) 1997-2004 University of Cambridge. |
Copyright (c) 1997-2005 University of Cambridge. |
2193 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
2194 |
|
|
|
PCRE(3) PCRE(3) |
|
|
|
|
2195 |
|
|
2196 |
|
|
2197 |
NAME |
NAME |
2198 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
2199 |
|
|
2200 |
|
|
2201 |
DIFFERENCES BETWEEN PCRE AND PERL |
DIFFERENCES BETWEEN PCRE AND PERL |
2202 |
|
|
2203 |
This document describes the differences in the ways that PCRE and Perl |
This document describes the differences in the ways that PCRE and Perl |
2302 |
(m) Patterns compiled by PCRE can be saved and re-used at a later time, |
(m) Patterns compiled by PCRE can be saved and re-used at a later time, |
2303 |
even on different hosts that have the other endianness. |
even on different hosts that have the other endianness. |
2304 |
|
|
2305 |
Last updated: 09 September 2004 |
(n) The alternative matching function (pcre_dfa_exec()) matches in a |
2306 |
Copyright (c) 1997-2004 University of Cambridge. |
different way and is not Perl-compatible. |
|
----------------------------------------------------------------------------- |
|
2307 |
|
|
2308 |
PCRE(3) PCRE(3) |
Last updated: 28 February 2005 |
2309 |
|
Copyright (c) 1997-2005 University of Cambridge. |
2310 |
|
----------------------------------------------------------------------------- |
2311 |
|
|
2312 |
|
|
2313 |
|
|
2314 |
NAME |
NAME |
2315 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
2316 |
|
|
2317 |
|
|
2318 |
PCRE REGULAR EXPRESSION DETAILS |
PCRE REGULAR EXPRESSION DETAILS |
2319 |
|
|
2320 |
The syntax and semantics of the regular expressions supported by PCRE |
The syntax and semantics of the regular expressions supported by PCRE |
2332 |
of UTF-8 features in the section on UTF-8 support in the main pcre |
of UTF-8 features in the section on UTF-8 support in the main pcre |
2333 |
page. |
page. |
2334 |
|
|
2335 |
|
The remainder of this document discusses the patterns that are sup- |
2336 |
|
ported by PCRE when its main matching function, pcre_exec(), is used. |
2337 |
|
From release 6.0, PCRE offers a second matching function, |
2338 |
|
pcre_dfa_exec(), which matches using a different algorithm that is not |
2339 |
|
Perl-compatible. The advantages and disadvantages of the alternative |
2340 |
|
function, and how it differs from the normal function, are discussed in |
2341 |
|
the pcrematching page. |
2342 |
|
|
2343 |
A regular expression is a pattern that is matched against a subject |
A regular expression is a pattern that is matched against a subject |
2344 |
string from left to right. Most characters stand for themselves in a |
string from left to right. Most characters stand for themselves in a |
2345 |
pattern, and match the corresponding characters in the subject. As a |
pattern, and match the corresponding characters in the subject. As a |
2347 |
|
|
2348 |
The quick brown fox |
The quick brown fox |
2349 |
|
|
2350 |
matches a portion of a subject string that is identical to itself. The |
matches a portion of a subject string that is identical to itself. When |
2351 |
power of regular expressions comes from the ability to include alterna- |
caseless matching is specified (the PCRE_CASELESS option), letters are |
2352 |
tives and repetitions in the pattern. These are encoded in the pattern |
matched independently of case. In UTF-8 mode, PCRE always understands |
2353 |
by the use of metacharacters, which do not stand for themselves but |
the concept of case for characters whose values are less than 128, so |
2354 |
instead are interpreted in some special way. |
caseless matching is always possible. For characters with higher val- |
2355 |
|
ues, the concept of case is supported if PCRE is compiled with Unicode |
2356 |
There are two different sets of metacharacters: those that are recog- |
property support, but not otherwise. If you want to use caseless |
2357 |
nized anywhere in the pattern except within square brackets, and those |
matching for characters 128 and above, you must ensure that PCRE is |
2358 |
that are recognized in square brackets. Outside square brackets, the |
compiled with Unicode property support as well as with UTF-8 support. |
2359 |
|
|
2360 |
|
The power of regular expressions comes from the ability to include |
2361 |
|
alternatives and repetitions in the pattern. These are encoded in the |
2362 |
|
pattern by the use of metacharacters, which do not stand for themselves |
2363 |
|
but instead are interpreted in some special way. |
2364 |
|
|
2365 |
|
There are two different sets of metacharacters: those that are recog- |
2366 |
|
nized anywhere in the pattern except within square brackets, and those |
2367 |
|
that are recognized in square brackets. Outside square brackets, the |
2368 |
metacharacters are as follows: |
metacharacters are as follows: |
2369 |
|
|
2370 |
\ general escape character with several uses |
\ general escape character with several uses |
2383 |
also "possessive quantifier" |
also "possessive quantifier" |
2384 |
{ start min/max quantifier |
{ start min/max quantifier |
2385 |
|
|
2386 |
Part of a pattern that is in square brackets is called a "character |
Part of a pattern that is in square brackets is called a "character |
2387 |
class". In a character class the only metacharacters are: |
class". In a character class the only metacharacters are: |
2388 |
|
|
2389 |
\ general escape character |
\ general escape character |
2393 |
syntax) |
syntax) |
2394 |
] terminates the character class |
] terminates the character class |
2395 |
|
|
2396 |
The following sections describe the use of each of the metacharacters. |
The following sections describe the use of each of the metacharacters. |
2397 |
|
|
2398 |
|
|
2399 |
BACKSLASH |
BACKSLASH |
2400 |
|
|
2401 |
The backslash character has several uses. Firstly, if it is followed by |
The backslash character has several uses. Firstly, if it is followed by |
2402 |
a non-alphanumeric character, it takes away any special meaning that |
a non-alphanumeric character, it takes away any special meaning that |
2403 |
character may have. This use of backslash as an escape character |
character may have. This use of backslash as an escape character |
2404 |
applies both inside and outside character classes. |
applies both inside and outside character classes. |
2405 |
|
|
2406 |
For example, if you want to match a * character, you write \* in the |
For example, if you want to match a * character, you write \* in the |
2407 |
pattern. This escaping action applies whether or not the following |
pattern. This escaping action applies whether or not the following |
2408 |
character would otherwise be interpreted as a metacharacter, so it is |
character would otherwise be interpreted as a metacharacter, so it is |
2409 |
always safe to precede a non-alphanumeric with backslash to specify |
always safe to precede a non-alphanumeric with backslash to specify |
2410 |
that it stands for itself. In particular, if you want to match a back- |
that it stands for itself. In particular, if you want to match a back- |
2411 |
slash, you write \\. |
slash, you write \\. |
2412 |
|
|
2413 |
If a pattern is compiled with the PCRE_EXTENDED option, whitespace in |
If a pattern is compiled with the PCRE_EXTENDED option, whitespace in |
2414 |
the pattern (other than in a character class) and characters between a |
the pattern (other than in a character class) and characters between a |
2415 |
# outside a character class and the next newline character are ignored. |
# outside a character class and the next newline character are ignored. |
2416 |
An escaping backslash can be used to include a whitespace or # charac- |
An escaping backslash can be used to include a whitespace or # charac- |
2417 |
ter as part of the pattern. |
ter as part of the pattern. |
2418 |
|
|
2419 |
If you want to remove the special meaning from a sequence of charac- |
If you want to remove the special meaning from a sequence of charac- |
2420 |
ters, you can do so by putting them between \Q and \E. This is differ- |
ters, you can do so by putting them between \Q and \E. This is differ- |
2421 |
ent from Perl in that $ and @ are handled as literals in \Q...\E |
ent from Perl in that $ and @ are handled as literals in \Q...\E |
2422 |
sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- |
sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- |
2423 |
tion. Note the following examples: |
tion. Note the following examples: |
2424 |
|
|
2425 |
Pattern PCRE matches Perl matches |
Pattern PCRE matches Perl matches |
2429 |
\Qabc\$xyz\E abc\$xyz abc\$xyz |
\Qabc\$xyz\E abc\$xyz abc\$xyz |
2430 |
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
2431 |
|
|
2432 |
The \Q...\E sequence is recognized both inside and outside character |
The \Q...\E sequence is recognized both inside and outside character |
2433 |
classes. |
classes. |
2434 |
|
|
2435 |
Non-printing characters |
Non-printing characters |
2436 |
|
|
2437 |
A second use of backslash provides a way of encoding non-printing char- |
A second use of backslash provides a way of encoding non-printing char- |
2438 |
acters in patterns in a visible manner. There is no restriction on the |
acters in patterns in a visible manner. There is no restriction on the |
2439 |
appearance of non-printing characters, apart from the binary zero that |
appearance of non-printing characters, apart from the binary zero that |
2440 |
terminates a pattern, but when a pattern is being prepared by text |
terminates a pattern, but when a pattern is being prepared by text |
2441 |
editing, it is usually easier to use one of the following escape |
editing, it is usually easier to use one of the following escape |
2442 |
sequences than the binary character it represents: |
sequences than the binary character it represents: |
2443 |
|
|
2444 |
\a alarm, that is, the BEL character (hex 07) |
\a alarm, that is, the BEL character (hex 07) |
2452 |
\xhh character with hex code hh |
\xhh character with hex code hh |
2453 |
\x{hhh..} character with hex code hhh... (UTF-8 mode only) |
\x{hhh..} character with hex code hhh... (UTF-8 mode only) |
2454 |
|
|
2455 |
The precise effect of \cx is as follows: if x is a lower case letter, |
The precise effect of \cx is as follows: if x is a lower case letter, |
2456 |
it is converted to upper case. Then bit 6 of the character (hex 40) is |
it is converted to upper case. Then bit 6 of the character (hex 40) is |
2457 |
inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; |
inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; |
2458 |
becomes hex 7B. |
becomes hex 7B. |
2459 |
|
|
2460 |
After \x, from zero to two hexadecimal digits are read (letters can be |
After \x, from zero to two hexadecimal digits are read (letters can be |
2461 |
in upper or lower case). In UTF-8 mode, any number of hexadecimal dig- |
in upper or lower case). In UTF-8 mode, any number of hexadecimal dig- |
2462 |
its may appear between \x{ and }, but the value of the character code |
its may appear between \x{ and }, but the value of the character code |
2463 |
must be less than 2**31 (that is, the maximum hexadecimal value is |
must be less than 2**31 (that is, the maximum hexadecimal value is |
2464 |
7FFFFFFF). If characters other than hexadecimal digits appear between |
7FFFFFFF). If characters other than hexadecimal digits appear between |
2465 |
\x{ and }, or if there is no terminating }, this form of escape is not |
\x{ and }, or if there is no terminating }, this form of escape is not |
2466 |
recognized. Instead, the initial \x will be interpreted as a basic hex- |
recognized. Instead, the initial \x will be interpreted as a basic |
2467 |
adecimal escape, with no following digits, giving a character whose |
hexadecimal escape, with no following digits, giving a character whose |
2468 |
value is zero. |
value is zero. |
2469 |
|
|
2470 |
Characters whose value is less than 256 can be defined by either of the |
Characters whose value is less than 256 can be defined by either of the |
2471 |
two syntaxes for \x when PCRE is in UTF-8 mode. There is no difference |
two syntaxes for \x when PCRE is in UTF-8 mode. There is no difference |
2472 |
in the way they are handled. For example, \xdc is exactly the same as |
in the way they are handled. For example, \xdc is exactly the same as |
2473 |
\x{dc}. |
\x{dc}. |
2474 |
|
|
2475 |
After \0 up to two further octal digits are read. In both cases, if |
After \0 up to two further octal digits are read. In both cases, if |
2476 |
there are fewer than two digits, just those that are present are used. |
there are fewer than two digits, just those that are present are used. |
2477 |
Thus the sequence \0\x\07 specifies two binary zeros followed by a BEL |
Thus the sequence \0\x\07 specifies two binary zeros followed by a BEL |
2478 |
character (code value 7). Make sure you supply two digits after the |
character (code value 7). Make sure you supply two digits after the |
2479 |
initial zero if the pattern character that follows is itself an octal |
initial zero if the pattern character that follows is itself an octal |
2480 |
digit. |
digit. |
2481 |
|
|
2482 |
The handling of a backslash followed by a digit other than 0 is compli- |
The handling of a backslash followed by a digit other than 0 is compli- |
2483 |
cated. Outside a character class, PCRE reads it and any following dig- |
cated. Outside a character class, PCRE reads it and any following dig- |
2484 |
its as a decimal number. If the number is less than 10, or if there |
its as a decimal number. If the number is less than 10, or if there |
2485 |
have been at least that many previous capturing left parentheses in the |
have been at least that many previous capturing left parentheses in the |
2486 |
expression, the entire sequence is taken as a back reference. A |
expression, the entire sequence is taken as a back reference. A |
2487 |
description of how this works is given later, following the discussion |
description of how this works is given later, following the discussion |
2488 |
of parenthesized subpatterns. |
of parenthesized subpatterns. |
2489 |
|
|
2490 |
Inside a character class, or if the decimal number is greater than 9 |
Inside a character class, or if the decimal number is greater than 9 |
2491 |
and there have not been that many capturing subpatterns, PCRE re-reads |
and there have not been that many capturing subpatterns, PCRE re-reads |
2492 |
up to three octal digits following the backslash, and generates a sin- |
up to three octal digits following the backslash, and generates a sin- |
2493 |
gle byte from the least significant 8 bits of the value. Any subsequent |
gle byte from the least significant 8 bits of the value. Any subsequent |
2494 |
digits stand for themselves. For example: |
digits stand for themselves. For example: |
2495 |
|
|
2508 |
\81 is either a back reference, or a binary zero |
\81 is either a back reference, or a binary zero |
2509 |
followed by the two characters "8" and "1" |
followed by the two characters "8" and "1" |
2510 |
|
|
2511 |
Note that octal values of 100 or greater must not be introduced by a |
Note that octal values of 100 or greater must not be introduced by a |
2512 |
leading zero, because no more than three octal digits are ever read. |
leading zero, because no more than three octal digits are ever read. |
2513 |
|
|
2514 |
All the sequences that define a single byte value or a single UTF-8 |
All the sequences that define a single byte value or a single UTF-8 |
2515 |
character (in UTF-8 mode) can be used both inside and outside character |
character (in UTF-8 mode) can be used both inside and outside character |
2516 |
classes. In addition, inside a character class, the sequence \b is |
classes. In addition, inside a character class, the sequence \b is |
2517 |
interpreted as the backspace character (hex 08), and the sequence \X is |
interpreted as the backspace character (hex 08), and the sequence \X is |
2518 |
interpreted as the character "X". Outside a character class, these |
interpreted as the character "X". Outside a character class, these |
2519 |
sequences have different meanings (see below). |
sequences have different meanings (see below). |
2520 |
|
|
2521 |
Generic character types |
Generic character types |
2522 |
|
|
2523 |
The third use of backslash is for specifying generic character types. |
The third use of backslash is for specifying generic character types. |
2524 |
The following are always recognized: |
The following are always recognized: |
2525 |
|
|
2526 |
\d any decimal digit |
\d any decimal digit |
2531 |
\W any "non-word" character |
\W any "non-word" character |
2532 |
|
|
2533 |
Each pair of escape sequences partitions the complete set of characters |
Each pair of escape sequences partitions the complete set of characters |
2534 |
into two disjoint sets. Any given character matches one, and only one, |
into two disjoint sets. Any given character matches one, and only one, |
2535 |
of each pair. |
of each pair. |
2536 |
|
|
2537 |
These character type sequences can appear both inside and outside char- |
These character type sequences can appear both inside and outside char- |
2538 |
acter classes. They each match one character of the appropriate type. |
acter classes. They each match one character of the appropriate type. |
2539 |
If the current matching point is at the end of the subject string, all |
If the current matching point is at the end of the subject string, all |
2540 |
of them fail, since there is no character to match. |
of them fail, since there is no character to match. |
2541 |
|
|
2542 |
For compatibility with Perl, \s does not match the VT character (code |
For compatibility with Perl, \s does not match the VT character (code |
2543 |
11). This makes it different from the the POSIX "space" class. The \s |
11). This makes it different from the the POSIX "space" class. The \s |
2544 |
characters are HT (9), LF (10), FF (12), CR (13), and space (32). |
characters are HT (9), LF (10), FF (12), CR (13), and space (32). |
2545 |
|
|
2546 |
A "word" character is an underscore or any character less than 256 that |
A "word" character is an underscore or any character less than 256 that |
2547 |
is a letter or digit. The definition of letters and digits is con- |
is a letter or digit. The definition of letters and digits is con- |
2548 |
trolled by PCRE's low-valued character tables, and may vary if locale- |
trolled by PCRE's low-valued character tables, and may vary if locale- |
2549 |
specific matching is taking place (see "Locale support" in the pcreapi |
specific matching is taking place (see "Locale support" in the pcreapi |
2550 |
page). For example, in the "fr_FR" (French) locale, some character |
page). For example, in the "fr_FR" (French) locale, some character |
2551 |
codes greater than 128 are used for accented letters, and these are |
codes greater than 128 are used for accented letters, and these are |
2552 |
matched by \w. |
matched by \w. |
2553 |
|
|
2554 |
In UTF-8 mode, characters with values greater than 128 never match \d, |
In UTF-8 mode, characters with values greater than 128 never match \d, |
2555 |
\s, or \w, and always match \D, \S, and \W. This is true even when Uni- |
\s, or \w, and always match \D, \S, and \W. This is true even when Uni- |
2556 |
code character property support is available. |
code character property support is available. |
2557 |
|
|
2558 |
Unicode character properties |
Unicode character properties |
2559 |
|
|
2560 |
When PCRE is built with Unicode character property support, three addi- |
When PCRE is built with Unicode character property support, three addi- |
2561 |
tional escape sequences to match generic character types are available |
tional escape sequences to match generic character types are available |
2562 |
when UTF-8 mode is selected. They are: |
when UTF-8 mode is selected. They are: |
2563 |
|
|
2564 |
\p{xx} a character with the xx property |
\p{xx} a character with the xx property |
2565 |
\P{xx} a character without the xx property |
\P{xx} a character without the xx property |
2566 |
\X an extended Unicode sequence |
\X an extended Unicode sequence |
2567 |
|
|
2568 |
The property names represented by xx above are limited to the Unicode |
The property names represented by xx above are limited to the Unicode |
2569 |
general category properties. Each character has exactly one such prop- |
general category properties. Each character has exactly one such prop- |
2570 |
erty, specified by a two-letter abbreviation. For compatibility with |
erty, specified by a two-letter abbreviation. For compatibility with |
2571 |
Perl, negation can be specified by including a circumflex between the |
Perl, negation can be specified by including a circumflex between the |
2572 |
opening brace and the property name. For example, \p{^Lu} is the same |
opening brace and the property name. For example, \p{^Lu} is the same |
2573 |
as \P{Lu}. |
as \P{Lu}. |
2574 |
|
|
2575 |
If only one letter is specified with \p or \P, it includes all the |
If only one letter is specified with \p or \P, it includes all the |
2576 |
properties that start with that letter. In this case, in the absence of |
properties that start with that letter. In this case, in the absence of |
2577 |
negation, the curly brackets in the escape sequence are optional; these |
negation, the curly brackets in the escape sequence are optional; these |
2578 |
two examples have the same effect: |
two examples have the same effect: |
2626 |
Zp Paragraph separator |
Zp Paragraph separator |
2627 |
Zs Space separator |
Zs Space separator |
2628 |
|
|
2629 |
Extended properties such as "Greek" or "InMusicalSymbols" are not sup- |
Extended properties such as "Greek" or "InMusicalSymbols" are not sup- |
2630 |
ported by PCRE. |
ported by PCRE. |
2631 |
|
|
2632 |
Specifying caseless matching does not affect these escape sequences. |
Specifying caseless matching does not affect these escape sequences. |
2633 |
For example, \p{Lu} always matches only upper case letters. |
For example, \p{Lu} always matches only upper case letters. |
2634 |
|
|
2635 |
The \X escape matches any number of Unicode characters that form an |
The \X escape matches any number of Unicode characters that form an |
2636 |
extended Unicode sequence. \X is equivalent to |
extended Unicode sequence. \X is equivalent to |
2637 |
|
|
2638 |
(?>\PM\pM*) |
(?>\PM\pM*) |
2639 |
|
|
2640 |
That is, it matches a character without the "mark" property, followed |
That is, it matches a character without the "mark" property, followed |
2641 |
by zero or more characters with the "mark" property, and treats the |
by zero or more characters with the "mark" property, and treats the |
2642 |
sequence as an atomic group (see below). Characters with the "mark" |
sequence as an atomic group (see below). Characters with the "mark" |
2643 |
property are typically accents that affect the preceding character. |
property are typically accents that affect the preceding character. |
2644 |
|
|
2645 |
Matching characters by Unicode property is not fast, because PCRE has |
Matching characters by Unicode property is not fast, because PCRE has |
2646 |
to search a structure that contains data for over fifteen thousand |
to search a structure that contains data for over fifteen thousand |
2647 |
characters. That is why the traditional escape sequences such as \d and |
characters. That is why the traditional escape sequences such as \d and |
2648 |
\w do not use Unicode properties in PCRE. |
\w do not use Unicode properties in PCRE. |
2649 |
|
|
2650 |
Simple assertions |
Simple assertions |
2651 |
|
|
2652 |
The fourth use of backslash is for certain simple assertions. An asser- |
The fourth use of backslash is for certain simple assertions. An asser- |
2653 |
tion specifies a condition that has to be met at a particular point in |
tion specifies a condition that has to be met at a particular point in |
2654 |
a match, without consuming any characters from the subject string. The |
a match, without consuming any characters from the subject string. The |
2655 |
use of subpatterns for more complicated assertions is described below. |
use of subpatterns for more complicated assertions is described below. |
2656 |
The backslashed assertions are: |
The backslashed assertions are: |
2657 |
|
|
2658 |
\b matches at a word boundary |
\b matches at a word boundary |
2662 |
\z matches at end of subject |
\z matches at end of subject |
2663 |
\G matches at first matching position in subject |
\G matches at first matching position in subject |
2664 |
|
|
2665 |
These assertions may not appear in character classes (but note that \b |
These assertions may not appear in character classes (but note that \b |
2666 |
has a different meaning, namely the backspace character, inside a char- |
has a different meaning, namely the backspace character, inside a char- |
2667 |
acter class). |
acter class). |
2668 |
|
|
2669 |
A word boundary is a position in the subject string where the current |
A word boundary is a position in the subject string where the current |
2670 |
character and the previous character do not both match \w or \W (i.e. |
character and the previous character do not both match \w or \W (i.e. |
2671 |
one matches \w and the other matches \W), or the start or end of the |
one matches \w and the other matches \W), or the start or end of the |
2672 |
string if the first or last character matches \w, respectively. |
string if the first or last character matches \w, respectively. |
2673 |
|
|
2674 |
The \A, \Z, and \z assertions differ from the traditional circumflex |
The \A, \Z, and \z assertions differ from the traditional circumflex |
2675 |
and dollar (described in the next section) in that they only ever match |
and dollar (described in the next section) in that they only ever match |
2676 |
at the very start and end of the subject string, whatever options are |
at the very start and end of the subject string, whatever options are |
2677 |
set. Thus, they are independent of multiline mode. These three asser- |
set. Thus, they are independent of multiline mode. These three asser- |
2678 |
tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which |
tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which |
2679 |
affect only the behaviour of the circumflex and dollar metacharacters. |
affect only the behaviour of the circumflex and dollar metacharacters. |
2680 |
However, if the startoffset argument of pcre_exec() is non-zero, indi- |
However, if the startoffset argument of pcre_exec() is non-zero, indi- |
2681 |
cating that matching is to start at a point other than the beginning of |
cating that matching is to start at a point other than the beginning of |
2682 |
the subject, \A can never match. The difference between \Z and \z is |
the subject, \A can never match. The difference between \Z and \z is |
2683 |
that \Z matches before a newline that is the last character of the |
that \Z matches before a newline that is the last character of the |
2684 |
string as well as at the end of the string, whereas \z matches only at |
string as well as at the end of the string, whereas \z matches only at |
2685 |
the end. |
the end. |
2686 |
|
|
2687 |
The \G assertion is true only when the current matching position is at |
The \G assertion is true only when the current matching position is at |
2688 |
the start point of the match, as specified by the startoffset argument |
the start point of the match, as specified by the startoffset argument |
2689 |
of pcre_exec(). It differs from \A when the value of startoffset is |
of pcre_exec(). It differs from \A when the value of startoffset is |
2690 |
non-zero. By calling pcre_exec() multiple times with appropriate argu- |
non-zero. By calling pcre_exec() multiple times with appropriate argu- |
2691 |
ments, you can mimic Perl's /g option, and it is in this kind of imple- |
ments, you can mimic Perl's /g option, and it is in this kind of imple- |
2692 |
mentation where \G can be useful. |
mentation where \G can be useful. |
2693 |
|
|
2694 |
Note, however, that PCRE's interpretation of \G, as the start of the |
Note, however, that PCRE's interpretation of \G, as the start of the |
2695 |
current match, is subtly different from Perl's, which defines it as the |
current match, is subtly different from Perl's, which defines it as the |
2696 |
end of the previous match. In Perl, these can be different when the |
end of the previous match. In Perl, these can be different when the |
2697 |
previously matched string was empty. Because PCRE does just one match |
previously matched string was empty. Because PCRE does just one match |
2698 |
at a time, it cannot reproduce this behaviour. |
at a time, it cannot reproduce this behaviour. |
2699 |
|
|
2700 |
If all the alternatives of a pattern begin with \G, the expression is |
If all the alternatives of a pattern begin with \G, the expression is |
2701 |
anchored to the starting match position, and the "anchored" flag is set |
anchored to the starting match position, and the "anchored" flag is set |
2702 |
in the compiled regular expression. |
in the compiled regular expression. |
2703 |
|
|
2705 |
CIRCUMFLEX AND DOLLAR |
CIRCUMFLEX AND DOLLAR |
2706 |
|
|
2707 |
Outside a character class, in the default matching mode, the circumflex |
Outside a character class, in the default matching mode, the circumflex |
2708 |
character is an assertion that is true only if the current matching |
character is an assertion that is true only if the current matching |
2709 |
point is at the start of the subject string. If the startoffset argu- |
point is at the start of the subject string. If the startoffset argu- |
2710 |
ment of pcre_exec() is non-zero, circumflex can never match if the |
ment of pcre_exec() is non-zero, circumflex can never match if the |
2711 |
PCRE_MULTILINE option is unset. Inside a character class, circumflex |
PCRE_MULTILINE option is unset. Inside a character class, circumflex |
2712 |
has an entirely different meaning (see below). |
has an entirely different meaning (see below). |
2713 |
|
|
2714 |
Circumflex need not be the first character of the pattern if a number |
Circumflex need not be the first character of the pattern if a number |
2715 |
of alternatives are involved, but it should be the first thing in each |
of alternatives are involved, but it should be the first thing in each |
2716 |
alternative in which it appears if the pattern is ever to match that |
alternative in which it appears if the pattern is ever to match that |
2717 |
branch. If all possible alternatives start with a circumflex, that is, |
branch. If all possible alternatives start with a circumflex, that is, |
2718 |
if the pattern is constrained to match only at the start of the sub- |
if the pattern is constrained to match only at the start of the sub- |
2719 |
ject, it is said to be an "anchored" pattern. (There are also other |
ject, it is said to be an "anchored" pattern. (There are also other |
2720 |
constructs that can cause a pattern to be anchored.) |
constructs that can cause a pattern to be anchored.) |
2721 |
|
|
2722 |
A dollar character is an assertion that is true only if the current |
A dollar character is an assertion that is true only if the current |
2723 |
matching point is at the end of the subject string, or immediately |
matching point is at the end of the subject string, or immediately |
2724 |
before a newline character that is the last character in the string (by |
before a newline character that is the last character in the string (by |
2725 |
default). Dollar need not be the last character of the pattern if a |
default). Dollar need not be the last character of the pattern if a |
2726 |
number of alternatives are involved, but it should be the last item in |
number of alternatives are involved, but it should be the last item in |
2727 |
any branch in which it appears. Dollar has no special meaning in a |
any branch in which it appears. Dollar has no special meaning in a |
2728 |
character class. |
character class. |
2729 |
|
|
2730 |
The meaning of dollar can be changed so that it matches only at the |
The meaning of dollar can be changed so that it matches only at the |
2731 |
very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at |
very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at |
2732 |
compile time. This does not affect the \Z assertion. |
compile time. This does not affect the \Z assertion. |
2733 |
|
|
2734 |
The meanings of the circumflex and dollar characters are changed if the |
The meanings of the circumflex and dollar characters are changed if the |
2735 |
PCRE_MULTILINE option is set. When this is the case, they match immedi- |
PCRE_MULTILINE option is set. When this is the case, they match immedi- |
2736 |
ately after and immediately before an internal newline character, |
ately after and immediately before an internal newline character, |
2737 |
respectively, in addition to matching at the start and end of the sub- |
respectively, in addition to matching at the start and end of the sub- |
2738 |
ject string. For example, the pattern /^abc$/ matches the subject |
ject string. For example, the pattern /^abc$/ matches the subject |
2739 |
string "def\nabc" (where \n represents a newline character) in multi- |
string "def\nabc" (where \n represents a newline character) in multi- |
2740 |
line mode, but not otherwise. Consequently, patterns that are anchored |
line mode, but not otherwise. Consequently, patterns that are anchored |
2741 |
in single line mode because all branches start with ^ are not anchored |
in single line mode because all branches start with ^ are not anchored |
2742 |
in multiline mode, and a match for circumflex is possible when the |
in multiline mode, and a match for circumflex is possible when the |
2743 |
startoffset argument of pcre_exec() is non-zero. The PCRE_DOL- |
startoffset argument of pcre_exec() is non-zero. The PCRE_DOL- |
2744 |
LAR_ENDONLY option is ignored if PCRE_MULTILINE is set. |
LAR_ENDONLY option is ignored if PCRE_MULTILINE is set. |
2745 |
|
|
2746 |
Note that the sequences \A, \Z, and \z can be used to match the start |
Note that the sequences \A, \Z, and \z can be used to match the start |
2747 |
and end of the subject in both modes, and if all branches of a pattern |
and end of the subject in both modes, and if all branches of a pattern |
2748 |
start with \A it is always anchored, whether PCRE_MULTILINE is set or |
start with \A it is always anchored, whether PCRE_MULTILINE is set or |
2749 |
not. |
not. |
2750 |
|
|
2751 |
|
|
2752 |
FULL STOP (PERIOD, DOT) |
FULL STOP (PERIOD, DOT) |
2753 |
|
|
2754 |
Outside a character class, a dot in the pattern matches any one charac- |
Outside a character class, a dot in the pattern matches any one charac- |
2755 |
ter in the subject, including a non-printing character, but not (by |
ter in the subject, including a non-printing character, but not (by |
2756 |
default) newline. In UTF-8 mode, a dot matches any UTF-8 character, |
default) newline. In UTF-8 mode, a dot matches any UTF-8 character, |
2757 |
which might be more than one byte long, except (by default) newline. If |
which might be more than one byte long, except (by default) newline. If |
2758 |
the PCRE_DOTALL option is set, dots match newlines as well. The han- |
the PCRE_DOTALL option is set, dots match newlines as well. The han- |
2759 |
dling of dot is entirely independent of the handling of circumflex and |
dling of dot is entirely independent of the handling of circumflex and |
2760 |
dollar, the only relationship being that they both involve newline |
dollar, the only relationship being that they both involve newline |
2761 |
characters. Dot has no special meaning in a character class. |
characters. Dot has no special meaning in a character class. |
2762 |
|
|
2763 |
|
|
2764 |
MATCHING A SINGLE BYTE |
MATCHING A SINGLE BYTE |
2765 |
|
|
2766 |
Outside a character class, the escape sequence \C matches any one byte, |
Outside a character class, the escape sequence \C matches any one byte, |
2767 |
both in and out of UTF-8 mode. Unlike a dot, it can match a newline. |
both in and out of UTF-8 mode. Unlike a dot, it can match a newline. |
2768 |
The feature is provided in Perl in order to match individual bytes in |
The feature is provided in Perl in order to match individual bytes in |
2769 |
UTF-8 mode. Because it breaks up UTF-8 characters into individual |
UTF-8 mode. Because it breaks up UTF-8 characters into individual |
2770 |
bytes, what remains in the string may be a malformed UTF-8 string. For |
bytes, what remains in the string may be a malformed UTF-8 string. For |
2771 |
this reason, the \C escape sequence is best avoided. |
this reason, the \C escape sequence is best avoided. |
2772 |
|
|
2773 |
PCRE does not allow \C to appear in lookbehind assertions (described |
PCRE does not allow \C to appear in lookbehind assertions (described |
2774 |
below), because in UTF-8 mode this would make it impossible to calcu- |
below), because in UTF-8 mode this would make it impossible to calcu- |
2775 |
late the length of the lookbehind. |
late the length of the lookbehind. |
2776 |
|
|
2777 |
|
|
2780 |
An opening square bracket introduces a character class, terminated by a |
An opening square bracket introduces a character class, terminated by a |
2781 |
closing square bracket. A closing square bracket on its own is not spe- |
closing square bracket. A closing square bracket on its own is not spe- |
2782 |
cial. If a closing square bracket is required as a member of the class, |
cial. If a closing square bracket is required as a member of the class, |
2783 |
it should be the first data character in the class (after an initial |
it should be the first data character in the class (after an initial |
2784 |
circumflex, if present) or escaped with a backslash. |
circumflex, if present) or escaped with a backslash. |
2785 |
|
|
2786 |
A character class matches a single character in the subject. In UTF-8 |
A character class matches a single character in the subject. In UTF-8 |
2787 |
mode, the character may occupy more than one byte. A matched character |
mode, the character may occupy more than one byte. A matched character |
2788 |
must be in the set of characters defined by the class, unless the first |
must be in the set of characters defined by the class, unless the first |
2789 |
character in the class definition is a circumflex, in which case the |
character in the class definition is a circumflex, in which case the |
2790 |
subject character must not be in the set defined by the class. If a |
subject character must not be in the set defined by the class. If a |
2791 |
circumflex is actually required as a member of the class, ensure it is |
circumflex is actually required as a member of the class, ensure it is |
2792 |
not the first character, or escape it with a backslash. |
not the first character, or escape it with a backslash. |
2793 |
|
|
2794 |
For example, the character class [aeiou] matches any lower case vowel, |
For example, the character class [aeiou] matches any lower case vowel, |
2795 |
while [^aeiou] matches any character that is not a lower case vowel. |
while [^aeiou] matches any character that is not a lower case vowel. |
2796 |
Note that a circumflex is just a convenient notation for specifying the |
Note that a circumflex is just a convenient notation for specifying the |
2797 |
characters that are in the class by enumerating those that are not. A |
characters that are in the class by enumerating those that are not. A |
2798 |
class that starts with a circumflex is not an assertion: it still con- |
class that starts with a circumflex is not an assertion: it still con- |
2799 |
sumes a character from the subject string, and therefore it fails if |
sumes a character from the subject string, and therefore it fails if |
2800 |
the current pointer is at the end of the string. |
the current pointer is at the end of the string. |
2801 |
|
|
2802 |
In UTF-8 mode, characters with values greater than 255 can be included |
In UTF-8 mode, characters with values greater than 255 can be included |
2803 |
in a class as a literal string of bytes, or by using the \x{ escaping |
in a class as a literal string of bytes, or by using the \x{ escaping |
2804 |
mechanism. |
mechanism. |
2805 |
|
|
2806 |
When caseless matching is set, any letters in a class represent both |
When caseless matching is set, any letters in a class represent both |
2807 |
their upper case and lower case versions, so for example, a caseless |
their upper case and lower case versions, so for example, a caseless |
2808 |
[aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not |
[aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not |
2809 |
match "A", whereas a caseful version would. When running in UTF-8 mode, |
match "A", whereas a caseful version would. In UTF-8 mode, PCRE always |
2810 |
PCRE supports the concept of case for characters with values greater |
understands the concept of case for characters whose values are less |
2811 |
than 128 only when it is compiled with Unicode property support. |
than 128, so caseless matching is always possible. For characters with |
2812 |
|
higher values, the concept of case is supported if PCRE is compiled |
2813 |
|
with Unicode property support, but not otherwise. If you want to use |
2814 |
|
caseless matching for characters 128 and above, you must ensure that |
2815 |
|
PCRE is compiled with Unicode property support as well as with UTF-8 |
2816 |
|
support. |
2817 |
|
|
2818 |
The newline character is never treated in any special way in character |
The newline character is never treated in any special way in character |
2819 |
classes, whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE |
classes, whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE |
3733 |
gether. A complete description of the interface to the callout function |
gether. A complete description of the interface to the callout function |
3734 |
is given in the pcrecallout documentation. |
is given in the pcrecallout documentation. |
3735 |
|
|
3736 |
Last updated: 09 September 2004 |
Last updated: 28 February 2005 |
3737 |
Copyright (c) 1997-2004 University of Cambridge. |
Copyright (c) 1997-2005 University of Cambridge. |
3738 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
3739 |
|
|
|
PCRE(3) PCRE(3) |
|
|
|
|
3740 |
|
|
3741 |
|
|
3742 |
NAME |
NAME |
3743 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
3744 |
|
|
3745 |
|
|
3746 |
PARTIAL MATCHING IN PCRE |
PARTIAL MATCHING IN PCRE |
3747 |
|
|
3748 |
In normal use of PCRE, if the subject string that is passed to |
In normal use of PCRE, if the subject string that is passed to |
3749 |
pcre_exec() matches as far as it goes, but is too short to match the |
pcre_exec() or pcre_dfa_exec() matches as far as it goes, but is too |
3750 |
entire pattern, PCRE_ERROR_NOMATCH is returned. There are circumstances |
short to match the entire pattern, PCRE_ERROR_NOMATCH is returned. |
3751 |
where it might be helpful to distinguish this case from other cases in |
There are circumstances where it might be helpful to distinguish this |
3752 |
which there is no match. |
case from other cases in which there is no match. |
3753 |
|
|
3754 |
Consider, for example, an application where a human is required to type |
Consider, for example, an application where a human is required to type |
3755 |
in data for a field with specific formatting requirements. An example |
in data for a field with specific formatting requirements. An example |
3765 |
until the entire string has been entered. |
until the entire string has been entered. |
3766 |
|
|
3767 |
PCRE supports the concept of partial matching by means of the PCRE_PAR- |
PCRE supports the concept of partial matching by means of the PCRE_PAR- |
3768 |
TIAL option, which can be set when calling pcre_exec(). When this is |
TIAL option, which can be set when calling pcre_exec() or |
3769 |
done, the return code PCRE_ERROR_NOMATCH is converted into |
pcre_dfa_exec(). When this flag is set for pcre_exec(), the return code |
3770 |
PCRE_ERROR_PARTIAL if at any time during the matching process the |
PCRE_ERROR_NOMATCH is converted into PCRE_ERROR_PARTIAL if at any time |
3771 |
entire subject string matched part of the pattern. No captured data is |
during the matching process the last part of the subject string matched |
3772 |
set when this occurs. |
part of the pattern. Unfortunately, for non-anchored matching, it is |
3773 |
|
not possible to obtain the position of the start of the partial match. |
3774 |
|
No captured data is set when PCRE_ERROR_PARTIAL is returned. |
3775 |
|
|
3776 |
|
When PCRE_PARTIAL is set for pcre_dfa_exec(), the return code |
3777 |
|
PCRE_ERROR_NOMATCH is converted into PCRE_ERROR_PARTIAL if the end of |
3778 |
|
the subject is reached, there have been no complete matches, but there |
3779 |
|
is still at least one matching possibility. The portion of the string |
3780 |
|
that provided the partial match is set as the first matching string. |
3781 |
|
|
3782 |
Using PCRE_PARTIAL disables one of PCRE's optimizations. PCRE remembers |
Using PCRE_PARTIAL disables one of PCRE's optimizations. PCRE remembers |
3783 |
the last literal byte in a pattern, and abandons matching immediately |
the last literal byte in a pattern, and abandons matching immediately |
3788 |
RESTRICTED PATTERNS FOR PCRE_PARTIAL |
RESTRICTED PATTERNS FOR PCRE_PARTIAL |
3789 |
|
|
3790 |
Because of the way certain internal optimizations are implemented in |
Because of the way certain internal optimizations are implemented in |
3791 |
PCRE, the PCRE_PARTIAL option cannot be used with all patterns. |
the pcre_exec() function, the PCRE_PARTIAL option cannot be used with |
3792 |
Repeated single characters such as |
all patterns. These restrictions do not apply when pcre_dfa_exec() is |
3793 |
|
used. For pcre_exec(), repeated single characters such as |
3794 |
|
|
3795 |
a{2,4} |
a{2,4} |
3796 |
|
|
3798 |
|
|
3799 |
\d+ |
\d+ |
3800 |
|
|
3801 |
are not permitted if the maximum number of occurrences is greater than |
are not permitted if the maximum number of occurrences is greater than |
3802 |
one. Optional items such as \d? (where the maximum is one) are permit- |
one. Optional items such as \d? (where the maximum is one) are permit- |
3803 |
ted. Quantifiers with any values are permitted after parentheses, so |
ted. Quantifiers with any values are permitted after parentheses, so |
3804 |
the invalid examples above can be coded thus: |
the invalid examples above can be coded thus: |
3805 |
|
|
3806 |
(a){2,4} |
(a){2,4} |
3807 |
(\d)+ |
(\d)+ |
3808 |
|
|
3809 |
These constructions run more slowly, but for the kinds of application |
These constructions run more slowly, but for the kinds of application |
3810 |
that are envisaged for this facility, this is not felt to be a major |
that are envisaged for this facility, this is not felt to be a major |
3811 |
restriction. |
restriction. |
3812 |
|
|
3813 |
If PCRE_PARTIAL is set for a pattern that does not conform to the |
If PCRE_PARTIAL is set for a pattern that does not conform to the |
3814 |
restrictions, pcre_exec() returns the error code PCRE_ERROR_BADPARTIAL |
restrictions, pcre_exec() returns the error code PCRE_ERROR_BADPARTIAL |
3815 |
(-13). |
(-13). |
3816 |
|
|
3817 |
|
|
3818 |
EXAMPLE OF PARTIAL MATCHING USING PCRETEST |
EXAMPLE OF PARTIAL MATCHING USING PCRETEST |
3819 |
|
|
3820 |
If the escape sequence \P is present in a pcretest data line, the |
If the escape sequence \P is present in a pcretest data line, the |
3821 |
PCRE_PARTIAL flag is used for the match. Here is a run of pcretest that |
PCRE_PARTIAL flag is used for the match. Here is a run of pcretest that |
3822 |
uses the date example quoted above: |
uses the date example quoted above: |
3823 |
|
|
3834 |
data> jP |
data> jP |
3835 |
No match |
No match |
3836 |
|
|
3837 |
The first data string is matched completely, so pcretest shows the |
The first data string is matched completely, so pcretest shows the |
3838 |
matched substrings. The remaining four strings do not match the com- |
matched substrings. The remaining four strings do not match the com- |
3839 |
plete pattern, but the first two are partial matches. |
plete pattern, but the first two are partial matches. The same test, |
3840 |
|
using DFA matching (by means of the \D escape sequence), produces the |
3841 |
|
following output: |
3842 |
|
|
3843 |
Last updated: 08 September 2004 |
re> /^?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)$/ |
3844 |
Copyright (c) 1997-2004 University of Cambridge. |
data> 25jun04\P\D |
3845 |
----------------------------------------------------------------------------- |
0: 25jun04 |
3846 |
|
data> 23dec3\P\D |
3847 |
|
Partial match: 23dec3 |
3848 |
|
data> 3ju\P\D |
3849 |
|
Partial match: 3ju |
3850 |
|
data> 3juj\P\D |
3851 |
|
No match |
3852 |
|
data> j\P\D |
3853 |
|
No match |
3854 |
|
|
3855 |
PCRE(3) PCRE(3) |
Notice that in this case the portion of the string that was matched is |
3856 |
|
made available. |
3857 |
|
|
3858 |
|
|
3859 |
|
MULTI-SEGMENT MATCHING WITH pcre_dfa_exec() |
3860 |
|
|
3861 |
|
When a partial match has been found using pcre_dfa_exec(), it is possi- |
3862 |
|
ble to continue the match by providing additional subject data and |
3863 |
|
calling pcre_dfa_exec() again with the PCRE_DFA_RESTART option and the |
3864 |
|
same working space (where details of the previous partial match are |
3865 |
|
stored). Here is an example using pcretest, where the \R escape |
3866 |
|
sequence sets the PCRE_DFA_RESTART option and the \D escape sequence |
3867 |
|
requests the use of pcre_dfa_exec(): |
3868 |
|
|
3869 |
|
re> /^?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)$/ |
3870 |
|
data> 23ja\P\D |
3871 |
|
Partial match: 23ja |
3872 |
|
data> n05\R\D |
3873 |
|
0: n05 |
3874 |
|
|
3875 |
|
The first call has "23ja" as the subject, and requests partial match- |
3876 |
|
ing; the second call has "n05" as the subject for the continued |
3877 |
|
(restarted) match. Notice that when the match is complete, only the |
3878 |
|
last part is shown; PCRE does not retain the previously partially- |
3879 |
|
matched string. It is up to the calling program to do that if it needs |
3880 |
|
to. |
3881 |
|
|
3882 |
|
This facility can be used to pass very long subject strings to |
3883 |
|
pcre_dfa_exec(). However, some care is needed for certain types of pat- |
3884 |
|
tern. |
3885 |
|
|
3886 |
|
1. If the pattern contains tests for the beginning or end of a line, |
3887 |
|
you need to pass the PCRE_NOTBOL or PCRE_NOTEOL options, as appropri- |
3888 |
|
ate, when the subject string for any call does not contain the begin- |
3889 |
|
ning or end of a line. |
3890 |
|
|
3891 |
|
2. If the pattern contains backward assertions (including \b or \B), |
3892 |
|
you need to arrange for some overlap in the subject strings to allow |
3893 |
|
for this. For example, you could pass the subject in chunks that were |
3894 |
|
500 bytes long, but in a buffer of 700 bytes, with the starting offset |
3895 |
|
set to 200 and the previous 200 bytes at the start of the buffer. |
3896 |
|
|
3897 |
|
3. Matching a subject string that is split into multiple segments does |
3898 |
|
not always produce exactly the same result as matching over one single |
3899 |
|
long string. The difference arises when there are multiple matching |
3900 |
|
possibilities, because a partial match result is given only when there |
3901 |
|
are no completed matches in a call to fBpcre_dfa_exec(). This means |
3902 |
|
that as soon as the shortest match has been found, continuation to a |
3903 |
|
new subject segment is no longer possible. Consider this pcretest |
3904 |
|
example: |
3905 |
|
|
3906 |
|
re> /dog(sbody)?/ |
3907 |
|
data> do\P\D |
3908 |
|
Partial match: do |
3909 |
|
data> gsb\R\P\D |
3910 |
|
0: g |
3911 |
|
data> dogsbody\D |
3912 |
|
0: dogsbody |
3913 |
|
1: dog |
3914 |
|
|
3915 |
|
The pattern matches the words "dog" or "dogsbody". When the subject is |
3916 |
|
presented in several parts ("do" and "gsb" being the first two) the |
3917 |
|
match stops when "dog" has been found, and it is not possible to con- |
3918 |
|
tinue. On the other hand, if "dogsbody" is presented as a single |
3919 |
|
string, both matches are found. |
3920 |
|
|
3921 |
|
Because of this phenomenon, it does not usually make sense to end a |
3922 |
|
pattern that is going to be matched in this way with a variable repeat. |
3923 |
|
|
3924 |
|
Last updated: 28 February 2005 |
3925 |
|
Copyright (c) 1997-2005 University of Cambridge. |
3926 |
|
----------------------------------------------------------------------------- |
3927 |
|
|
3928 |
|
|
3929 |
|
|
3930 |
NAME |
NAME |
3931 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
3932 |
|
|
3933 |
|
|
3934 |
SAVING AND RE-USING PRECOMPILED PCRE PATTERNS |
SAVING AND RE-USING PRECOMPILED PCRE PATTERNS |
3935 |
|
|
3936 |
If you are running an application that uses a large number of regular |
If you are running an application that uses a large number of regular |
3999 |
RE-USING A PRECOMPILED PATTERN |
RE-USING A PRECOMPILED PATTERN |
4000 |
|
|
4001 |
Re-using a precompiled pattern is straightforward. Having reloaded it |
Re-using a precompiled pattern is straightforward. Having reloaded it |
4002 |
into main memory, you pass its pointer to pcre_exec() in the usual way. |
into main memory, you pass its pointer to pcre_exec() or |
4003 |
This should work even on another host, and even if that host has the |
pcre_dfa_exec() in the usual way. This should work even on another |
4004 |
opposite endianness to the one where the pattern was compiled. |
host, and even if that host has the opposite endianness to the one |
4005 |
|
where the pattern was compiled. |
4006 |
However, if you passed a pointer to custom character tables when the |
|
4007 |
pattern was compiled (the tableptr argument of pcre_compile()), you |
However, if you passed a pointer to custom character tables when the |
4008 |
must now pass a similar pointer to pcre_exec(), because the value saved |
pattern was compiled (the tableptr argument of pcre_compile()), you |
4009 |
with the compiled pattern will obviously be nonsense. A field in a |
must now pass a similar pointer to pcre_exec() or pcre_dfa_exec(), |
4010 |
pcre_extra() block is used to pass this data, as described in the sec- |
because the value saved with the compiled pattern will obviously be |
4011 |
tion on matching a pattern in the pcreapi documentation. |
nonsense. A field in a pcre_extra() block is used to pass this data, as |
4012 |
|
described in the section on matching a pattern in the pcreapi documen- |
4013 |
|
tation. |
4014 |
|
|
4015 |
If you did not provide custom character tables when the pattern was |
If you did not provide custom character tables when the pattern was |
4016 |
compiled, the pointer in the compiled pattern is NULL, which causes |
compiled, the pointer in the compiled pattern is NULL, which causes |
4021 |
your own pcre_extra data block and set the study_data field to point to |
your own pcre_extra data block and set the study_data field to point to |
4022 |
the reloaded study data. You must also set the PCRE_EXTRA_STUDY_DATA |
the reloaded study data. You must also set the PCRE_EXTRA_STUDY_DATA |
4023 |
bit in the flags field to indicate that study data is present. Then |
bit in the flags field to indicate that study data is present. Then |
4024 |
pass the pcre_extra block to pcre_exec() in the usual way. |
pass the pcre_extra block to pcre_exec() or pcre_dfa_exec() in the |
4025 |
|
usual way. |
4026 |
|
|
4027 |
|
|
4028 |
COMPATIBILITY WITH DIFFERENT PCRE RELEASES |
COMPATIBILITY WITH DIFFERENT PCRE RELEASES |
4029 |
|
|
4030 |
The layout of the control block that is at the start of the data that |
The layout of the control block that is at the start of the data that |
4031 |
makes up a compiled pattern was changed for release 5.0. If you have |
makes up a compiled pattern was changed for release 5.0. If you have |
4032 |
any saved patterns that were compiled with previous releases (not a |
any saved patterns that were compiled with previous releases (not a |
4033 |
facility that was previously advertised), you will have to recompile |
facility that was previously advertised), you will have to recompile |
4034 |
them for release 5.0. However, from now on, it should be possible to |
them for release 5.0. However, from now on, it should be possible to |
4035 |
make changes in a compabible manner. |
make changes in a compabible manner. |
4036 |
|
|
4037 |
Last updated: 10 September 2004 |
Last updated: 28 February 2005 |
4038 |
Copyright (c) 1997-2004 University of Cambridge. |
Copyright (c) 1997-2005 University of Cambridge. |
4039 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
4040 |
|
|
|
PCRE(3) PCRE(3) |
|
|
|
|
4041 |
|
|
4042 |
|
|
4043 |
NAME |
NAME |
4044 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
4045 |
|
|
4046 |
|
|
4047 |
PCRE PERFORMANCE |
PCRE PERFORMANCE |
4048 |
|
|
4049 |
Certain items that may appear in regular expression patterns are more |
Certain items that may appear in regular expression patterns are more |
4079 |
|
|
4080 |
If you are using such a pattern with subject strings that do not con- |
If you are using such a pattern with subject strings that do not con- |
4081 |
tain newlines, the best performance is obtained by setting PCRE_DOTALL, |
tain newlines, the best performance is obtained by setting PCRE_DOTALL, |
4082 |
or starting the pattern with ^.* to indicate explicit anchoring. That |
or starting the pattern with ^.* or ^.*? to indicate explicit anchor- |
4083 |
saves PCRE from having to scan along the subject looking for a newline |
ing. That saves PCRE from having to scan along the subject looking for |
4084 |
to restart at. |
a newline to restart at. |
4085 |
|
|
4086 |
Beware of patterns that contain nested indefinite repeats. These can |
Beware of patterns that contain nested indefinite repeats. These can |
4087 |
take a long time to run when applied to a string that does not match. |
take a long time to run when applied to a string that does not match. |
4102 |
(a+)*b |
(a+)*b |
4103 |
|
|
4104 |
where a literal character follows. Before embarking on the standard |
where a literal character follows. Before embarking on the standard |
4105 |
matching procedure, PCRE checks that there is a "b" later in the |
matching procedure, PCRE checks that there is a "b" later in the sub- |
4106 |
subject string, and if there is not, it fails the match immediately. |
ject string, and if there is not, it fails the match immediately. How- |
4107 |
However, when there is no following literal this optimization cannot be |
ever, when there is no following literal this optimization cannot be |
4108 |
used. You can see the difference by comparing the behaviour of |
used. You can see the difference by comparing the behaviour of |
4109 |
|
|
4110 |
(a+)*\d |
(a+)*\d |
4116 |
In many cases, the solution to this kind of performance issue is to use |
In many cases, the solution to this kind of performance issue is to use |
4117 |
an atomic group or a possessive quantifier. |
an atomic group or a possessive quantifier. |
4118 |
|
|
4119 |
Last updated: 09 September 2004 |
Last updated: 28 February 2005 |
4120 |
Copyright (c) 1997-2004 University of Cambridge. |
Copyright (c) 1997-2005 University of Cambridge. |
4121 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
4122 |
|
|
|
PCRE(3) PCRE(3) |
|
|
|
|
4123 |
|
|
4124 |
|
|
4125 |
NAME |
NAME |
4126 |
PCRE - Perl-compatible regular expressions. |
PCRE - Perl-compatible regular expressions. |
4127 |
|
|
4128 |
|
|
4129 |
SYNOPSIS OF POSIX API |
SYNOPSIS OF POSIX API |
4130 |
|
|
4131 |
#include <pcreposix.h> |
#include <pcreposix.h> |
4146 |
|
|
4147 |
This set of functions provides a POSIX-style API to the PCRE regular |
This set of functions provides a POSIX-style API to the PCRE regular |
4148 |
expression package. See the pcreapi documentation for a description of |
expression package. See the pcreapi documentation for a description of |
4149 |
PCRE's native API, which contains additional functionality. |
PCRE's native API, which contains much additional functionality. |
4150 |
|
|
4151 |
The functions described here are just wrapper functions that ultimately |
The functions described here are just wrapper functions that ultimately |
4152 |
call the PCRE native API. Their prototypes are defined in the |
call the PCRE native API. Their prototypes are defined in the |
4190 |
The argument cflags is either zero, or contains one or more of the bits |
The argument cflags is either zero, or contains one or more of the bits |
4191 |
defined by the following macros: |
defined by the following macros: |
4192 |
|
|
4193 |
|
REG_DOTALL |
4194 |
|
|
4195 |
|
The PCRE_DOTALL option is set when the expression is passed for compi- |
4196 |
|
lation to the native function. Note that REG_DOTALL is not part of the |
4197 |
|
POSIX standard. |
4198 |
|
|
4199 |
REG_ICASE |
REG_ICASE |
4200 |
|
|
4201 |
The PCRE_CASELESS option is set when the expression is passed for com- |
The PCRE_CASELESS option is set when the expression is passed for com- |
4307 |
|
|
4308 |
AUTHOR |
AUTHOR |
4309 |
|
|
4310 |
Philip Hazel <ph10@cam.ac.uk> |
Philip Hazel |
4311 |
University Computing Service, |
University Computing Service, |
4312 |
Cambridge CB2 3QG, England. |
Cambridge CB2 3QG, England. |
4313 |
|
|
4314 |
Last updated: 07 September 2004 |
Last updated: 28 February 2005 |
4315 |
Copyright (c) 1997-2004 University of Cambridge. |
Copyright (c) 1997-2005 University of Cambridge. |
4316 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
4317 |
|
|
4318 |
PCRE(3) PCRE(3) |
|
4319 |
|
|
4320 |
|
NAME |
4321 |
|
PCRE - Perl-compatible regular expressions. |
4322 |
|
|
4323 |
|
|
4324 |
|
SYNOPSIS OF C++ WRAPPER |
4325 |
|
|
4326 |
|
#include <pcrecpp.h> |
4327 |
|
|
4328 |
|
|
4329 |
|
DESCRIPTION |
4330 |
|
|
4331 |
|
The C++ wrapper for PCRE was provided by Google Inc. This brief man |
4332 |
|
page was constructed from the notes in the pcrecpp.h file, which should |
4333 |
|
be consulted for further details. |
4334 |
|
|
4335 |
|
|
4336 |
|
MATCHING INTERFACE |
4337 |
|
|
4338 |
|
The "FullMatch" operation checks that supplied text matches a supplied |
4339 |
|
pattern exactly. If pointer arguments are supplied, it copies matched |
4340 |
|
sub-strings that match sub-patterns into them. |
4341 |
|
|
4342 |
|
Example: successful match |
4343 |
|
pcrecpp::RE re("h.*o"); |
4344 |
|
re.FullMatch("hello"); |
4345 |
|
|
4346 |
|
Example: unsuccessful match (requires full match): |
4347 |
|
pcrecpp::RE re("e"); |
4348 |
|
!re.FullMatch("hello"); |
4349 |
|
|
4350 |
|
Example: creating a temporary RE object: |
4351 |
|
pcrecpp::RE("h.*o").FullMatch("hello"); |
4352 |
|
|
4353 |
|
You can pass in a "const char*" or a "string" for "text". The examples |
4354 |
|
below tend to use a const char*. You can, as in the different examples |
4355 |
|
above, store the RE object explicitly in a variable or use a temporary |
4356 |
|
RE object. The examples below use one mode or the other arbitrarily. |
4357 |
|
Either could correctly be used for any of these examples. |
4358 |
|
|
4359 |
|
You must supply extra pointer arguments to extract matched subpieces. |
4360 |
|
|
4361 |
|
Example: extracts "ruby" into "s" and 1234 into "i" |
4362 |
|
int i; |
4363 |
|
string s; |
4364 |
|
pcrecpp::RE re("(\\w+):(\\d+)"); |
4365 |
|
re.FullMatch("ruby:1234", &s, &i); |
4366 |
|
|
4367 |
|
Example: does not try to extract any extra sub-patterns |
4368 |
|
re.FullMatch("ruby:1234", &s); |
4369 |
|
|
4370 |
|
Example: does not try to extract into NULL |
4371 |
|
re.FullMatch("ruby:1234", NULL, &i); |
4372 |
|
|
4373 |
|
Example: integer overflow causes failure |
4374 |
|
!re.FullMatch("ruby:1234567891234", NULL, &i); |
4375 |
|
|
4376 |
|
Example: fails because there aren't enough sub-patterns: |
4377 |
|
!pcrecpp::RE("\\w+:\\d+").FullMatch("ruby:1234", &s); |
4378 |
|
|
4379 |
|
Example: fails because string cannot be stored in integer |
4380 |
|
!pcrecpp::RE("(.*)").FullMatch("ruby", &i); |
4381 |
|
|
4382 |
|
The provided pointer arguments can be pointers to any scalar numeric |
4383 |
|
type, or one of: |
4384 |
|
|
4385 |
|
string (matched piece is copied to string) |
4386 |
|
StringPiece (StringPiece is mutated to point to matched piece) |
4387 |
|
T (where "bool T::ParseFrom(const char*, int)" exists) |
4388 |
|
NULL (the corresponding matched sub-pattern is not copied) |
4389 |
|
|
4390 |
|
The function returns true iff all of the following conditions are sat- |
4391 |
|
isfied: |
4392 |
|
|
4393 |
|
a. "text" matches "pattern" exactly; |
4394 |
|
|
4395 |
|
b. The number of matched sub-patterns is >= number of supplied |
4396 |
|
pointers; |
4397 |
|
|
4398 |
|
c. The "i"th argument has a suitable type for holding the |
4399 |
|
string captured as the "i"th sub-pattern. If you pass in |
4400 |
|
NULL for the "i"th argument, or pass fewer arguments than |
4401 |
|
number of sub-patterns, "i"th captured sub-pattern is |
4402 |
|
ignored. |
4403 |
|
|
4404 |
|
The matching interface supports at most 16 arguments per call. If you |
4405 |
|
need more, consider using the more general interface |
4406 |
|
pcrecpp::RE::DoMatch. See pcrecpp.h for the signature for DoMatch. |
4407 |
|
|
4408 |
|
|
4409 |
|
PARTIAL MATCHES |
4410 |
|
|
4411 |
|
You can use the "PartialMatch" operation when you want the pattern to |
4412 |
|
match any substring of the text. |
4413 |
|
|
4414 |
|
Example: simple search for a string: |
4415 |
|
pcrecpp::RE("ell").PartialMatch("hello"); |
4416 |
|
|
4417 |
|
Example: find first number in a string: |
4418 |
|
int number; |
4419 |
|
pcrecpp::RE re("(\\d+)"); |
4420 |
|
re.PartialMatch("x*100 + 20", &number); |
4421 |
|
assert(number == 100); |
4422 |
|
|
4423 |
|
|
4424 |
|
UTF-8 AND THE MATCHING INTERFACE |
4425 |
|
|
4426 |
|
By default, pattern and text are plain text, one byte per character. |
4427 |
|
The UTF8 flag, passed to the constructor, causes both pattern and |
4428 |
|
string to be treated as UTF-8 text, still a byte stream but potentially |
4429 |
|
multiple bytes per character. In practice, the text is likelier to be |
4430 |
|
UTF-8 than the pattern, but the match returned may depend on the UTF8 |
4431 |
|
flag, so always use it when matching UTF8 text. For example, "." will |
4432 |
|
match one byte normally but with UTF8 set may match up to three bytes |
4433 |
|
of a multi-byte character. |
4434 |
|
|
4435 |
|
Example: |
4436 |
|
pcrecpp::RE_Options options; |
4437 |
|
options.set_utf8(); |
4438 |
|
pcrecpp::RE re(utf8_pattern, options); |
4439 |
|
re.FullMatch(utf8_string); |
4440 |
|
|
4441 |
|
Example: using the convenience function UTF8(): |
4442 |
|
pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8()); |
4443 |
|
re.FullMatch(utf8_string); |
4444 |
|
|
4445 |
|
NOTE: The UTF8 flag is ignored if pcre was not configured with the |
4446 |
|
--enable-utf8 flag. |
4447 |
|
|
4448 |
|
|
4449 |
|
SCANNING TEXT INCREMENTALLY |
4450 |
|
|
4451 |
|
The "Consume" operation may be useful if you want to repeatedly match |
4452 |
|
regular expressions at the front of a string and skip over them as they |
4453 |
|
match. This requires use of the "StringPiece" type, which represents a |
4454 |
|
sub-range of a real string. Like RE, StringPiece is defined in the |
4455 |
|
pcrecpp namespace. |
4456 |
|
|
4457 |
|
Example: read lines of the form "var = value" from a string. |
4458 |
|
string contents = ...; // Fill string somehow |
4459 |
|
pcrecpp::StringPiece input(contents); // Wrap in a StringPiece |
4460 |
|
|
4461 |
|
string var; |
4462 |
|
int value; |
4463 |
|
pcrecpp::RE re("(\\w+) = (\\d+)\n"); |
4464 |
|
while (re.Consume(&input, &var, &value)) { |
4465 |
|
...; |
4466 |
|
} |
4467 |
|
|
4468 |
|
Each successful call to "Consume" will set "var/value", and also |
4469 |
|
advance "input" so it points past the matched text. |
4470 |
|
|
4471 |
|
The "FindAndConsume" operation is similar to "Consume" but does not |
4472 |
|
anchor your match at the beginning of the string. For example, you |
4473 |
|
could extract all words from a string by repeatedly calling |
4474 |
|
|
4475 |
|
pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word) |
4476 |
|
|
4477 |
|
|
4478 |
|
PARSING HEX/OCTAL/C-RADIX NUMBERS |
4479 |
|
|
4480 |
|
By default, if you pass a pointer to a numeric value, the corresponding |
4481 |
|
text is interpreted as a base-10 number. You can instead wrap the |
4482 |
|
pointer with a call to one of the operators Hex(), Octal(), or CRadix() |
4483 |
|
to interpret the text in another base. The CRadix operator interprets |
4484 |
|
C-style "0" (base-8) and "0x" (base-16) prefixes, but defaults to |
4485 |
|
base-10. |
4486 |
|
|
4487 |
|
Example: |
4488 |
|
int a, b, c, d; |
4489 |
|
pcrecpp::RE re("(.*) (.*) (.*) (.*)"); |
4490 |
|
re.FullMatch("100 40 0100 0x40", |
4491 |
|
pcrecpp::Octal(&a), pcrecpp::Hex(&b), |
4492 |
|
pcrecpp::CRadix(&c), pcrecpp::CRadix(&d)); |
4493 |
|
|
4494 |
|
will leave 64 in a, b, c, and d. |
4495 |
|
|
4496 |
|
|
4497 |
|
REPLACING PARTS OF STRINGS |
4498 |
|
|
4499 |
|
You can replace the first match of "pattern" in "str" with "rewrite". |
4500 |
|
Within "rewrite", backslash-escaped digits (\1 to \9) can be used to |
4501 |
|
insert text matching corresponding parenthesized group from the pat- |
4502 |
|
tern. \0 in "rewrite" refers to the entire matching text. For example: |
4503 |
|
|
4504 |
|
string s = "yabba dabba doo"; |
4505 |
|
pcrecpp::RE("b+").Replace("d", &s); |
4506 |
|
|
4507 |
|
will leave "s" containing "yada dabba doo". The result is true if the |
4508 |
|
pattern matches and a replacement occurs, false otherwise. |
4509 |
|
|
4510 |
|
GlobalReplace is like Replace except that it replaces all occurrences |
4511 |
|
of the pattern in the string with the rewrite. Replacements are not |
4512 |
|
subject to re-matching. For example: |
4513 |
|
|
4514 |
|
string s = "yabba dabba doo"; |
4515 |
|
pcrecpp::RE("b+").GlobalReplace("d", &s); |
4516 |
|
|
4517 |
|
will leave "s" containing "yada dada doo". It returns the number of |
4518 |
|
replacements made. |
4519 |
|
|
4520 |
|
Extract is like Replace, except that if the pattern matches, "rewrite" |
4521 |
|
is copied into "out" (an additional argument) with substitutions. The |
4522 |
|
non-matching portions of "text" are ignored. Returns true iff a match |
4523 |
|
occurred and the extraction happened successfully; if no match occurs, |
4524 |
|
the string is left unaffected. |
4525 |
|
|
4526 |
|
|
4527 |
|
AUTHOR |
4528 |
|
|
4529 |
|
The C++ wrapper was contributed by Google Inc. |
4530 |
|
Copyright (c) 2005 Google Inc. |
4531 |
|
----------------------------------------------------------------------------- |
4532 |
|
|
4533 |
|
|
4534 |
|
|
4535 |
NAME |
NAME |
4536 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
4537 |
|
|
4538 |
|
|
4539 |
PCRE SAMPLE PROGRAM |
PCRE SAMPLE PROGRAM |
4540 |
|
|
4541 |
A simple, complete demonstration program, to get you started with using |
A simple, complete demonstration program, to get you started with using |