1PCRE2(3) Library Functions Manual PCRE2(3)
2
3
4
6 PCRE2 - Perl-compatible regular expressions (revised API)
7
9
10 PCRE2 is the name used for a revised API for the PCRE library, which is
11 a set of functions, written in C, that implement regular expression
12 pattern matching using the same syntax and semantics as Perl, with just
13 a few differences. After nearly two decades, the limitations of the
14 original API were making development increasingly difficult. The new
15 API is more extensible, and it was simplified by abolishing the sepa‐
16 rate "study" optimizing function; in PCRE2, patterns are automatically
17 optimized where possible. Since forking from PCRE1, the code has been
18 extensively refactored and new features introduced.
19
20 As well as Perl-style regular expression patterns, some features that
21 appeared in Python and the original PCRE before they appeared in Perl
22 are available using the Python syntax. There is also some support for
23 one or two .NET and Oniguruma syntax items, and there are options for
24 requesting some minor changes that give better ECMAScript (aka Java‐
25 Script) compatibility.
26
27 The source code for PCRE2 can be compiled to support strings of 8-bit,
28 16-bit, or 32-bit code units, which means that up to three separate li‐
29 braries may be installed, one for each code unit size. The size of code
30 unit is not related to the bit size of the underlying hardware. In a
31 64-bit environment that also supports 32-bit applications, versions of
32 PCRE2 that are compiled in both 64-bit and 32-bit modes may be needed.
33
34 The original work to extend PCRE to 16-bit and 32-bit code units was
35 done by Zoltan Herczeg and Christian Persch, respectively. In all three
36 cases, strings can be interpreted either as one character per code
37 unit, or as UTF-encoded Unicode, with support for Unicode general cate‐
38 gory properties. Unicode support is optional at build time (but is the
39 default). However, processing strings as UTF code units must be enabled
40 explicitly at run time. The version of Unicode in use can be discovered
41 by running
42
43 pcre2test -C
44
45 The three libraries contain identical sets of functions, with names
46 ending in _8, _16, or _32, respectively (for example, pcre2_com‐
47 pile_8()). However, by defining PCRE2_CODE_UNIT_WIDTH to be 8, 16, or
48 32, a program that uses just one code unit width can be written using
49 generic names such as pcre2_compile(), and the documentation is written
50 assuming that this is the case.
51
52 In addition to the Perl-compatible matching function, PCRE2 contains an
53 alternative function that matches the same compiled patterns in a dif‐
54 ferent way. In certain circumstances, the alternative function has some
55 advantages. For a discussion of the two matching algorithms, see the
56 pcre2matching page.
57
58 Details of exactly which Perl regular expression features are and are
59 not supported by PCRE2 are given in separate documents. See the
60 pcre2pattern and pcre2compat pages. There is a syntax summary in the
61 pcre2syntax page.
62
63 Some features of PCRE2 can be included, excluded, or changed when the
64 library is built. The pcre2_config() function makes it possible for a
65 client to discover which features are available. The features them‐
66 selves are described in the pcre2build page. Documentation about build‐
67 ing PCRE2 for various operating systems can be found in the README and
68 NON-AUTOTOOLS_BUILD files in the source distribution.
69
70 The libraries contains a number of undocumented internal functions and
71 data tables that are used by more than one of the exported external
72 functions, but which are not intended for use by external callers.
73 Their names all begin with "_pcre2", which hopefully will not provoke
74 any name clashes. In some environments, it is possible to control which
75 external symbols are exported when a shared library is built, and in
76 these cases the undocumented symbols are not exported.
77
79
80 If you are using PCRE2 in a non-UTF application that permits users to
81 supply arbitrary patterns for compilation, you should be aware of a
82 feature that allows users to turn on UTF support from within a pattern.
83 For example, an 8-bit pattern that begins with "(*UTF)" turns on UTF-8
84 mode, which interprets patterns and subjects as strings of UTF-8 code
85 units instead of individual 8-bit characters. This causes both the pat‐
86 tern and any data against which it is matched to be checked for UTF-8
87 validity. If the data string is very long, such a check might use suf‐
88 ficiently many resources as to cause your application to lose perfor‐
89 mance.
90
91 One way of guarding against this possibility is to use the pcre2_pat‐
92 tern_info() function to check the compiled pattern's options for
93 PCRE2_UTF. Alternatively, you can set the PCRE2_NEVER_UTF option when
94 calling pcre2_compile(). This causes a compile time error if the pat‐
95 tern contains a UTF-setting sequence.
96
97 The use of Unicode properties for character types such as \d can also
98 be enabled from within the pattern, by specifying "(*UCP)". This fea‐
99 ture can be disallowed by setting the PCRE2_NEVER_UCP option.
100
101 If your application is one that supports UTF, be aware that validity
102 checking can take time. If the same data string is to be matched many
103 times, you can use the PCRE2_NO_UTF_CHECK option for the second and
104 subsequent matches to avoid running redundant checks.
105
106 The use of the \C escape sequence in a UTF-8 or UTF-16 pattern can lead
107 to problems, because it may leave the current matching point in the
108 middle of a multi-code-unit character. The PCRE2_NEVER_BACKSLASH_C op‐
109 tion can be used by an application to lock out the use of \C, causing a
110 compile-time error if it is encountered. It is also possible to build
111 PCRE2 with the use of \C permanently disabled.
112
113 Another way that performance can be hit is by running a pattern that
114 has a very large search tree against a string that will never match.
115 Nested unlimited repeats in a pattern are a common example. PCRE2 pro‐
116 vides some protection against this: see the pcre2_set_match_limit()
117 function in the pcre2api page. There is a similar function called
118 pcre2_set_depth_limit() that can be used to restrict the amount of mem‐
119 ory that is used.
120
122
123 The user documentation for PCRE2 comprises a number of different sec‐
124 tions. In the "man" format, each of these is a separate "man page". In
125 the HTML format, each is a separate page, linked from the index page.
126 In the plain text format, the descriptions of the pcre2grep and
127 pcre2test programs are in files called pcre2grep.txt and pcre2test.txt,
128 respectively. The remaining sections, except for the pcre2demo section
129 (which is a program listing), and the short pages for individual func‐
130 tions, are concatenated in pcre2.txt, for ease of searching. The sec‐
131 tions are as follows:
132
133 pcre2 this document
134 pcre2-config show PCRE2 installation configuration information
135 pcre2api details of PCRE2's native C API
136 pcre2build building PCRE2
137 pcre2callout details of the pattern callout feature
138 pcre2compat discussion of Perl compatibility
139 pcre2convert details of pattern conversion functions
140 pcre2demo a demonstration C program that uses PCRE2
141 pcre2grep description of the pcre2grep command (8-bit only)
142 pcre2jit discussion of just-in-time optimization support
143 pcre2limits details of size and other limits
144 pcre2matching discussion of the two matching algorithms
145 pcre2partial details of the partial matching facility
146 pcre2pattern syntax and semantics of supported regular
147 expression patterns
148 pcre2perform discussion of performance issues
149 pcre2posix the POSIX-compatible C API for the 8-bit library
150 pcre2sample discussion of the pcre2demo program
151 pcre2serialize details of pattern serialization
152 pcre2syntax quick syntax reference
153 pcre2test description of the pcre2test command
154 pcre2unicode discussion of Unicode and UTF support
155
156 In the "man" and HTML formats, there is also a short page for each C
157 library function, listing its arguments and results.
158
160
161 Philip Hazel
162 University Computing Service
163 Cambridge, England.
164
165 Putting an actual email address here is a spam magnet. If you want to
166 email me, use my two initials, followed by the two digits 10, at the
167 domain cam.ac.uk.
168
170
171 Last updated: 28 April 2021
172 Copyright (c) 1997-2021 University of Cambridge.
173
174
175
176PCRE2 10.37 28 April 2021 PCRE2(3)