1PCRE(3) Library Functions Manual PCRE(3)
2
3
4
6 PCRE - Perl-compatible regular expressions
7
9
10 The PCRE library is a set of functions that implement regular expres‐
11 sion pattern matching using the same syntax and semantics as Perl, with
12 just a few differences. (Certain features that appeared in Python and
13 PCRE before they appeared in Perl are also available using the Python
14 syntax.)
15
16 The current implementation of PCRE (release 7.x) corresponds approxi‐
17 mately with Perl 5.10, including support for UTF-8 encoded strings and
18 Unicode general category properties. However, UTF-8 and Unicode support
19 has to be explicitly enabled; it is not the default. The Unicode tables
20 correspond to Unicode release 5.0.0.
21
22 In addition to the Perl-compatible matching function, PCRE contains an
23 alternative matching function that matches the same compiled patterns
24 in a different way. In certain circumstances, the alternative function
25 has some advantages. For a discussion of the two matching algorithms,
26 see the pcrematching page.
27
28 PCRE is written in C and released as a C library. A number of people
29 have written wrappers and interfaces of various kinds. In particular,
30 Google Inc. have provided a comprehensive C++ wrapper. This is now
31 included as part of the PCRE distribution. The pcrecpp page has details
32 of this interface. Other people's contributions can be found in the
33 Contrib directory at the primary FTP site, which is:
34
35 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre
36
37 Details of exactly which Perl regular expression features are and are
38 not supported by PCRE are given in separate documents. See the pcrepat‐
39 tern and pcrecompat pages. There is a syntax summary in the pcresyntax
40 page.
41
42 Some features of PCRE can be included, excluded, or changed when the
43 library is built. The pcre_config() function makes it possible for a
44 client to discover which features are available. The features them‐
45 selves are described in the pcrebuild page. Documentation about build‐
46 ing PCRE for various operating systems can be found in the README file
47 in the source distribution.
48
49 The library contains a number of undocumented internal functions and
50 data tables that are used by more than one of the exported external
51 functions, but which are not intended for use by external callers.
52 Their names all begin with "_pcre_", which hopefully will not provoke
53 any name clashes. In some environments, it is possible to control which
54 external symbols are exported when a shared library is built, and in
55 these cases the undocumented symbols are not exported.
56
58
59 The user documentation for PCRE comprises a number of different sec‐
60 tions. In the "man" format, each of these is a separate "man page". In
61 the HTML format, each is a separate page, linked from the index page.
62 In the plain text format, all the sections are concatenated, for ease
63 of searching. The sections are as follows:
64
65 pcre this document
66 pcre-config show PCRE installation configuration information
67 pcreapi details of PCRE's native C API
68 pcrebuild options for building PCRE
69 pcrecallout details of the callout feature
70 pcrecompat discussion of Perl compatibility
71 pcrecpp details of the C++ wrapper
72 pcregrep description of the pcregrep command
73 pcrematching discussion of the two matching algorithms
74 pcrepartial details of the partial matching facility
75 pcrepattern syntax and semantics of supported
76 regular expressions
77 pcresyntax quick syntax reference
78 pcreperform discussion of performance issues
79 pcreposix the POSIX-compatible C API
80 pcreprecompile details of saving and re-using precompiled patterns
81 pcresample discussion of the sample program
82 pcrestack discussion of stack usage
83 pcretest description of the pcretest testing command
84
85 In addition, in the "man" and HTML formats, there is a short page for
86 each C library function, listing its arguments and results.
87
89
90 There are some size limitations in PCRE but it is hoped that they will
91 never in practice be relevant.
92
93 The maximum length of a compiled pattern is 65539 (sic) bytes if PCRE
94 is compiled with the default internal linkage size of 2. If you want to
95 process regular expressions that are truly enormous, you can compile
96 PCRE with an internal linkage size of 3 or 4 (see the README file in
97 the source distribution and the pcrebuild documentation for details).
98 In these cases the limit is substantially larger. However, the speed
99 of execution is slower.
100
101 All values in repeating quantifiers must be less than 65536.
102
103 There is no limit to the number of parenthesized subpatterns, but there
104 can be no more than 65535 capturing subpatterns.
105
106 The maximum length of name for a named subpattern is 32 characters, and
107 the maximum number of named subpatterns is 10000.
108
109 The maximum length of a subject string is the largest positive number
110 that an integer variable can hold. However, when using the traditional
111 matching function, PCRE uses recursion to handle subpatterns and indef‐
112 inite repetition. This means that the available stack space may limit
113 the size of a subject string that can be processed by certain patterns.
114 For a discussion of stack issues, see the pcrestack documentation.
115
117
118 From release 3.3, PCRE has had some support for character strings
119 encoded in the UTF-8 format. For release 4.0 this was greatly extended
120 to cover most common requirements, and in release 5.0 additional sup‐
121 port for Unicode general category properties was added.
122
123 In order process UTF-8 strings, you must build PCRE to include UTF-8
124 support in the code, and, in addition, you must call pcre_compile()
125 with the PCRE_UTF8 option flag. When you do this, both the pattern and
126 any subject strings that are matched against it are treated as UTF-8
127 strings instead of just strings of bytes.
128
129 If you compile PCRE with UTF-8 support, but do not use it at run time,
130 the library will be a bit bigger, but the additional run time overhead
131 is limited to testing the PCRE_UTF8 flag occasionally, so should not be
132 very big.
133
134 If PCRE is built with Unicode character property support (which implies
135 UTF-8 support), the escape sequences \p{..}, \P{..}, and \X are sup‐
136 ported. The available properties that can be tested are limited to the
137 general category properties such as Lu for an upper case letter or Nd
138 for a decimal number, the Unicode script names such as Arabic or Han,
139 and the derived properties Any and L&. A full list is given in the
140 pcrepattern documentation. Only the short names for properties are sup‐
141 ported. For example, \p{L} matches a letter. Its Perl synonym, \p{Let‐
142 ter}, is not supported. Furthermore, in Perl, many properties may
143 optionally be prefixed by "Is", for compatibility with Perl 5.6. PCRE
144 does not support this.
145
146 Validity of UTF-8 strings
147
148 When you set the PCRE_UTF8 flag, the strings passed as patterns and
149 subjects are (by default) checked for validity on entry to the relevant
150 functions. From release 7.3 of PCRE, the check is according the rules
151 of RFC 3629, which are themselves derived from the Unicode specifica‐
152 tion. Earlier releases of PCRE followed the rules of RFC 2279, which
153 allows the full range of 31-bit values (0 to 0x7FFFFFFF). The current
154 check allows only values in the range U+0 to U+10FFFF, excluding U+D800
155 to U+DFFF.
156
157 The excluded code points are the "Low Surrogate Area" of Unicode, of
158 which the Unicode Standard says this: "The Low Surrogate Area does not
159 contain any character assignments, consequently no character code
160 charts or namelists are provided for this area. Surrogates are reserved
161 for use with UTF-16 and then must be used in pairs." The code points
162 that are encoded by UTF-16 pairs are available as independent code
163 points in the UTF-8 encoding. (In other words, the whole surrogate
164 thing is a fudge for UTF-16 which unfortunately messes up UTF-8.)
165
166 If an invalid UTF-8 string is passed to PCRE, an error return
167 (PCRE_ERROR_BADUTF8) is given. In some situations, you may already know
168 that your strings are valid, and therefore want to skip these checks in
169 order to improve performance. If you set the PCRE_NO_UTF8_CHECK flag at
170 compile time or at run time, PCRE assumes that the pattern or subject
171 it is given (respectively) contains only valid UTF-8 codes. In this
172 case, it does not diagnose an invalid UTF-8 string.
173
174 If you pass an invalid UTF-8 string when PCRE_NO_UTF8_CHECK is set,
175 what happens depends on why the string is invalid. If the string con‐
176 forms to the "old" definition of UTF-8 (RFC 2279), it is processed as a
177 string of characters in the range 0 to 0x7FFFFFFF. In other words,
178 apart from the initial validity test, PCRE (when in UTF-8 mode) handles
179 strings according to the more liberal rules of RFC 2279. However, if
180 the string does not even conform to RFC 2279, the result is undefined.
181 Your program may crash.
182
183 If you want to process strings of values in the full range 0 to
184 0x7FFFFFFF, encoded in a UTF-8-like manner as per the old RFC, you can
185 set PCRE_NO_UTF8_CHECK to bypass the more restrictive test. However, in
186 this situation, you will have to apply your own validity check.
187
188 General comments about UTF-8 mode
189
190 1. An unbraced hexadecimal escape sequence (such as \xb3) matches a
191 two-byte UTF-8 character if the value is greater than 127.
192
193 2. Octal numbers up to \777 are recognized, and match two-byte UTF-8
194 characters for values greater than \177.
195
196 3. Repeat quantifiers apply to complete UTF-8 characters, not to indi‐
197 vidual bytes, for example: \x{100}{3}.
198
199 4. The dot metacharacter matches one UTF-8 character instead of a sin‐
200 gle byte.
201
202 5. The escape sequence \C can be used to match a single byte in UTF-8
203 mode, but its use can lead to some strange effects. This facility is
204 not available in the alternative matching function, pcre_dfa_exec().
205
206 6. The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly
207 test characters of any code value, but the characters that PCRE recog‐
208 nizes as digits, spaces, or word characters remain the same set as
209 before, all with values less than 256. This remains true even when PCRE
210 includes Unicode property support, because to do otherwise would slow
211 down PCRE in many common cases. If you really want to test for a wider
212 sense of, say, "digit", you must use Unicode property tests such as
213 \p{Nd}.
214
215 7. Similarly, characters that match the POSIX named character classes
216 are all low-valued characters.
217
218 8. However, the Perl 5.10 horizontal and vertical whitespace matching
219 escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char‐
220 acters.
221
222 9. Case-insensitive matching applies only to characters whose values
223 are less than 128, unless PCRE is built with Unicode property support.
224 Even when Unicode property support is available, PCRE still uses its
225 own character tables when checking the case of low-valued characters,
226 so as not to degrade performance. The Unicode property information is
227 used only for characters with higher values. Even when Unicode property
228 support is available, PCRE supports case-insensitive matching only when
229 there is a one-to-one mapping between a letter's cases. There are a
230 small number of many-to-one mappings in Unicode; these are not sup‐
231 ported by PCRE.
232
234
235 Philip Hazel
236 University Computing Service
237 Cambridge CB2 3QH, England.
238
239 Putting an actual email address here seems to have been a spam magnet,
240 so I've taken it away. If you want to email me, use my two initials,
241 followed by the two digits 10, at the domain cam.ac.uk.
242
244
245 Last updated: 09 August 2007
246 Copyright (c) 1997-2007 University of Cambridge.
247
248
249
250 PCRE(3)