new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / external / pcre2-10.32 / doc / pcre2api.3
1 .TH PCRE2API 3 "07 September 2018" "PCRE2 10.32"
2 .SH NAME
3 PCRE2 - Perl-compatible regular expressions (revised API)
4 .sp
5 .B #include <pcre2.h>
6 .sp
7 PCRE2 is a new API for PCRE, starting at release 10.0. This document contains a
8 description of all its native functions. See the
9 .\" HREF
10 \fBpcre2\fP
11 .\"
12 document for an overview of all the PCRE2 documentation.
13 .
14 .
15 .SH "PCRE2 NATIVE API BASIC FUNCTIONS"
16 .rs
17 .sp
18 .nf
19 .B pcre2_code *pcre2_compile(PCRE2_SPTR \fIpattern\fP, PCRE2_SIZE \fIlength\fP,
20 .B "  uint32_t \fIoptions\fP, int *\fIerrorcode\fP, PCRE2_SIZE *\fIerroroffset,\fP"
21 .B "  pcre2_compile_context *\fIccontext\fP);"
22 .sp
23 .B void pcre2_code_free(pcre2_code *\fIcode\fP);
24 .sp
25 .B pcre2_match_data *pcre2_match_data_create(uint32_t \fIovecsize\fP,
26 .B "  pcre2_general_context *\fIgcontext\fP);"
27 .sp
28 .B pcre2_match_data *pcre2_match_data_create_from_pattern(
29 .B "  const pcre2_code *\fIcode\fP, pcre2_general_context *\fIgcontext\fP);"
30 .sp
31 .B int pcre2_match(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
32 .B "  PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
33 .B "  uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
34 .B "  pcre2_match_context *\fImcontext\fP);"
35 .sp
36 .B int pcre2_dfa_match(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
37 .B "  PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
38 .B "  uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
39 .B "  pcre2_match_context *\fImcontext\fP,"
40 .B "  int *\fIworkspace\fP, PCRE2_SIZE \fIwscount\fP);"
41 .sp
42 .B void pcre2_match_data_free(pcre2_match_data *\fImatch_data\fP);
43 .fi
44 .
45 .
46 .SH "PCRE2 NATIVE API AUXILIARY MATCH FUNCTIONS"
47 .rs
48 .sp
49 .nf
50 .B PCRE2_SPTR pcre2_get_mark(pcre2_match_data *\fImatch_data\fP);
51 .sp
52 .B uint32_t pcre2_get_ovector_count(pcre2_match_data *\fImatch_data\fP);
53 .sp
54 .B PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *\fImatch_data\fP);
55 .sp
56 .B PCRE2_SIZE pcre2_get_startchar(pcre2_match_data *\fImatch_data\fP);
57 .fi
58 .
59 .
60 .SH "PCRE2 NATIVE API GENERAL CONTEXT FUNCTIONS"
61 .rs
62 .sp
63 .nf
64 .B pcre2_general_context *pcre2_general_context_create(
65 .B "  void *(*\fIprivate_malloc\fP)(PCRE2_SIZE, void *),"
66 .B "  void (*\fIprivate_free\fP)(void *, void *), void *\fImemory_data\fP);"
67 .sp
68 .B pcre2_general_context *pcre2_general_context_copy(
69 .B "  pcre2_general_context *\fIgcontext\fP);"
70 .sp
71 .B void pcre2_general_context_free(pcre2_general_context *\fIgcontext\fP);
72 .fi
73 .
74 .
75 .SH "PCRE2 NATIVE API COMPILE CONTEXT FUNCTIONS"
76 .rs
77 .sp
78 .nf
79 .B pcre2_compile_context *pcre2_compile_context_create(
80 .B "  pcre2_general_context *\fIgcontext\fP);"
81 .sp
82 .B pcre2_compile_context *pcre2_compile_context_copy(
83 .B "  pcre2_compile_context *\fIccontext\fP);"
84 .sp
85 .B void pcre2_compile_context_free(pcre2_compile_context *\fIccontext\fP);
86 .sp
87 .B int pcre2_set_bsr(pcre2_compile_context *\fIccontext\fP,
88 .B "  uint32_t \fIvalue\fP);"
89 .sp
90 .B int pcre2_set_character_tables(pcre2_compile_context *\fIccontext\fP,
91 .B "  const unsigned char *\fItables\fP);"
92 .sp
93 .B int pcre2_set_compile_extra_options(pcre2_compile_context *\fIccontext\fP,
94 .B "  uint32_t \fIextra_options\fP);"
95 .sp
96 .B int pcre2_set_max_pattern_length(pcre2_compile_context *\fIccontext\fP,
97 .B "  PCRE2_SIZE \fIvalue\fP);"
98 .sp
99 .B int pcre2_set_newline(pcre2_compile_context *\fIccontext\fP,
100 .B "  uint32_t \fIvalue\fP);"
101 .sp
102 .B int pcre2_set_parens_nest_limit(pcre2_compile_context *\fIccontext\fP,
103 .B "  uint32_t \fIvalue\fP);"
104 .sp
105 .B int pcre2_set_compile_recursion_guard(pcre2_compile_context *\fIccontext\fP,
106 .B "  int (*\fIguard_function\fP)(uint32_t, void *), void *\fIuser_data\fP);"
107 .fi
108 .
109 .
110 .SH "PCRE2 NATIVE API MATCH CONTEXT FUNCTIONS"
111 .rs
112 .sp
113 .nf
114 .B pcre2_match_context *pcre2_match_context_create(
115 .B "  pcre2_general_context *\fIgcontext\fP);"
116 .sp
117 .B pcre2_match_context *pcre2_match_context_copy(
118 .B "  pcre2_match_context *\fImcontext\fP);"
119 .sp
120 .B void pcre2_match_context_free(pcre2_match_context *\fImcontext\fP);
121 .sp
122 .B int pcre2_set_callout(pcre2_match_context *\fImcontext\fP,
123 .B "  int (*\fIcallout_function\fP)(pcre2_callout_block *, void *),"
124 .B "  void *\fIcallout_data\fP);"
125 .sp
126 .B int pcre2_set_offset_limit(pcre2_match_context *\fImcontext\fP,
127 .B "  PCRE2_SIZE \fIvalue\fP);"
128 .sp
129 .B int pcre2_set_heap_limit(pcre2_match_context *\fImcontext\fP,
130 .B "  uint32_t \fIvalue\fP);"
131 .sp
132 .B int pcre2_set_match_limit(pcre2_match_context *\fImcontext\fP,
133 .B "  uint32_t \fIvalue\fP);"
134 .sp
135 .B int pcre2_set_depth_limit(pcre2_match_context *\fImcontext\fP,
136 .B "  uint32_t \fIvalue\fP);"
137 .fi
138 .
139 .
140 .SH "PCRE2 NATIVE API STRING EXTRACTION FUNCTIONS"
141 .rs
142 .sp
143 .nf
144 .B int pcre2_substring_copy_byname(pcre2_match_data *\fImatch_data\fP,
145 .B "  PCRE2_SPTR \fIname\fP, PCRE2_UCHAR *\fIbuffer\fP, PCRE2_SIZE *\fIbufflen\fP);"
146 .sp
147 .B int pcre2_substring_copy_bynumber(pcre2_match_data *\fImatch_data\fP,
148 .B "  uint32_t \fInumber\fP, PCRE2_UCHAR *\fIbuffer\fP,"
149 .B "  PCRE2_SIZE *\fIbufflen\fP);"
150 .sp
151 .B void pcre2_substring_free(PCRE2_UCHAR *\fIbuffer\fP);
152 .sp
153 .B int pcre2_substring_get_byname(pcre2_match_data *\fImatch_data\fP,
154 .B "  PCRE2_SPTR \fIname\fP, PCRE2_UCHAR **\fIbufferptr\fP, PCRE2_SIZE *\fIbufflen\fP);"
155 .sp
156 .B int pcre2_substring_get_bynumber(pcre2_match_data *\fImatch_data\fP,
157 .B "  uint32_t \fInumber\fP, PCRE2_UCHAR **\fIbufferptr\fP,"
158 .B "  PCRE2_SIZE *\fIbufflen\fP);"
159 .sp
160 .B int pcre2_substring_length_byname(pcre2_match_data *\fImatch_data\fP,
161 .B "  PCRE2_SPTR \fIname\fP, PCRE2_SIZE *\fIlength\fP);"
162 .sp
163 .B int pcre2_substring_length_bynumber(pcre2_match_data *\fImatch_data\fP,
164 .B "  uint32_t \fInumber\fP, PCRE2_SIZE *\fIlength\fP);"
165 .sp
166 .B int pcre2_substring_nametable_scan(const pcre2_code *\fIcode\fP,
167 .B "  PCRE2_SPTR \fIname\fP, PCRE2_SPTR *\fIfirst\fP, PCRE2_SPTR *\fIlast\fP);"
168 .sp
169 .B int pcre2_substring_number_from_name(const pcre2_code *\fIcode\fP,
170 .B "  PCRE2_SPTR \fIname\fP);"
171 .sp
172 .B void pcre2_substring_list_free(PCRE2_SPTR *\fIlist\fP);
173 .sp
174 .B int pcre2_substring_list_get(pcre2_match_data *\fImatch_data\fP,
175 .B "  PCRE2_UCHAR ***\fIlistptr\fP, PCRE2_SIZE **\fIlengthsptr\fP);
176 .fi
177 .
178 .
179 .SH "PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION"
180 .rs
181 .sp
182 .nf
183 .B int pcre2_substitute(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
184 .B "  PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
185 .B "  uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
186 .B "  pcre2_match_context *\fImcontext\fP, PCRE2_SPTR \fIreplacementzfP,"
187 .B "  PCRE2_SIZE \fIrlength\fP, PCRE2_UCHAR *\fIoutputbuffer\fP,"
188 .B "  PCRE2_SIZE *\fIoutlengthptr\fP);"
189 .fi
190 .
191 .
192 .SH "PCRE2 NATIVE API JIT FUNCTIONS"
193 .rs
194 .sp
195 .nf
196 .B int pcre2_jit_compile(pcre2_code *\fIcode\fP, uint32_t \fIoptions\fP);
197 .sp
198 .B int pcre2_jit_match(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
199 .B "  PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
200 .B "  uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
201 .B "  pcre2_match_context *\fImcontext\fP);"
202 .sp
203 .B void pcre2_jit_free_unused_memory(pcre2_general_context *\fIgcontext\fP);
204 .sp
205 .B pcre2_jit_stack *pcre2_jit_stack_create(PCRE2_SIZE \fIstartsize\fP,
206 .B "  PCRE2_SIZE \fImaxsize\fP, pcre2_general_context *\fIgcontext\fP);"
207 .sp
208 .B void pcre2_jit_stack_assign(pcre2_match_context *\fImcontext\fP,
209 .B "  pcre2_jit_callback \fIcallback_function\fP, void *\fIcallback_data\fP);"
210 .sp
211 .B void pcre2_jit_stack_free(pcre2_jit_stack *\fIjit_stack\fP);
212 .fi
213 .
214 .
215 .SH "PCRE2 NATIVE API SERIALIZATION FUNCTIONS"
216 .rs
217 .sp
218 .nf
219 .B int32_t pcre2_serialize_decode(pcre2_code **\fIcodes\fP,
220 .B "  int32_t \fInumber_of_codes\fP, const uint8_t *\fIbytes\fP,"
221 .B "  pcre2_general_context *\fIgcontext\fP);"
222 .sp
223 .B int32_t pcre2_serialize_encode(const pcre2_code **\fIcodes\fP,
224 .B "  int32_t \fInumber_of_codes\fP, uint8_t **\fIserialized_bytes\fP,"
225 .B "  PCRE2_SIZE *\fIserialized_size\fP, pcre2_general_context *\fIgcontext\fP);"
226 .sp
227 .B void pcre2_serialize_free(uint8_t *\fIbytes\fP);
228 .sp
229 .B int32_t pcre2_serialize_get_number_of_codes(const uint8_t *\fIbytes\fP);
230 .fi
231 .
232 .
233 .SH "PCRE2 NATIVE API AUXILIARY FUNCTIONS"
234 .rs
235 .sp
236 .nf
237 .B pcre2_code *pcre2_code_copy(const pcre2_code *\fIcode\fP);
238 .sp
239 .B pcre2_code *pcre2_code_copy_with_tables(const pcre2_code *\fIcode\fP);
240 .sp
241 .B int pcre2_get_error_message(int \fIerrorcode\fP, PCRE2_UCHAR *\fIbuffer\fP,
242 .B "  PCRE2_SIZE \fIbufflen\fP);"
243 .sp
244 .B const unsigned char *pcre2_maketables(pcre2_general_context *\fIgcontext\fP);
245 .sp
246 .B int pcre2_pattern_info(const pcre2 *\fIcode\fP, uint32_t \fIwhat\fP, void *\fIwhere\fP);
247 .sp
248 .B int pcre2_callout_enumerate(const pcre2_code *\fIcode\fP,
249 .B "  int (*\fIcallback\fP)(pcre2_callout_enumerate_block *, void *),"
250 .B "  void *\fIuser_data\fP);"
251 .sp
252 .B int pcre2_config(uint32_t \fIwhat\fP, void *\fIwhere\fP);
253 .fi
254 .
255 .
256 .SH "PCRE2 NATIVE API OBSOLETE FUNCTIONS"
257 .rs
258 .sp
259 .nf
260 .B int pcre2_set_recursion_limit(pcre2_match_context *\fImcontext\fP,
261 .B "  uint32_t \fIvalue\fP);"
262 .sp
263 .B int pcre2_set_recursion_memory_management(
264 .B "  pcre2_match_context *\fImcontext\fP,"
265 .B "  void *(*\fIprivate_malloc\fP)(PCRE2_SIZE, void *),"
266 .B "  void (*\fIprivate_free\fP)(void *, void *), void *\fImemory_data\fP);"
267 .fi
268 .sp
269 These functions became obsolete at release 10.30 and are retained only for
270 backward compatibility. They should not be used in new code. The first is
271 replaced by \fBpcre2_set_depth_limit()\fP; the second is no longer needed and
272 has no effect (it always returns zero).
273 .
274 .
275 .SH "PCRE2 EXPERIMENTAL PATTERN CONVERSION FUNCTIONS"
276 .rs
277 .sp
278 .nf
279 .B pcre2_convert_context *pcre2_convert_context_create(
280 .B "  pcre2_general_context *\fIgcontext\fP);"
281 .sp
282 .B pcre2_convert_context *pcre2_convert_context_copy(
283 .B "  pcre2_convert_context *\fIcvcontext\fP);"
284 .sp
285 .B void pcre2_convert_context_free(pcre2_convert_context *\fIcvcontext\fP);
286 .sp
287 .B int pcre2_set_glob_escape(pcre2_convert_context *\fIcvcontext\fP,
288 .B "  uint32_t \fIescape_char\fP);"
289 .sp
290 .B int pcre2_set_glob_separator(pcre2_convert_context *\fIcvcontext\fP,
291 .B "  uint32_t \fIseparator_char\fP);"
292 .sp
293 .B int pcre2_pattern_convert(PCRE2_SPTR \fIpattern\fP, PCRE2_SIZE \fIlength\fP,
294 .B "  uint32_t \fIoptions\fP, PCRE2_UCHAR **\fIbuffer\fP,"
295 .B "  PCRE2_SIZE *\fIblength\fP, pcre2_convert_context *\fIcvcontext\fP);"
296 .sp
297 .B void pcre2_converted_pattern_free(PCRE2_UCHAR *\fIconverted_pattern\fP);
298 .fi
299 .sp
300 These functions provide a way of converting non-PCRE2 patterns into
301 patterns that can be processed by \fBpcre2_compile()\fP. This facility is
302 experimental and may be changed in future releases. At present, "globs" and
303 POSIX basic and extended patterns can be converted. Details are given in the
304 .\" HREF
305 \fBpcre2convert\fP
306 .\"
307 documentation.
308 .
309 .
310 .SH "PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES"
311 .rs
312 .sp
313 There are three PCRE2 libraries, supporting 8-bit, 16-bit, and 32-bit code
314 units, respectively. However, there is just one header file, \fBpcre2.h\fP.
315 This contains the function prototypes and other definitions for all three
316 libraries. One, two, or all three can be installed simultaneously. On Unix-like
317 systems the libraries are called \fBlibpcre2-8\fP, \fBlibpcre2-16\fP, and
318 \fBlibpcre2-32\fP, and they can also co-exist with the original PCRE libraries.
319 .P
320 Character strings are passed to and from a PCRE2 library as a sequence of
321 unsigned integers in code units of the appropriate width. Every PCRE2 function
322 comes in three different forms, one for each library, for example:
323 .sp
324   \fBpcre2_compile_8()\fP
325   \fBpcre2_compile_16()\fP
326   \fBpcre2_compile_32()\fP
327 .sp
328 There are also three different sets of data types:
329 .sp
330   \fBPCRE2_UCHAR8, PCRE2_UCHAR16, PCRE2_UCHAR32\fP
331   \fBPCRE2_SPTR8,  PCRE2_SPTR16,  PCRE2_SPTR32\fP
332 .sp
333 The UCHAR types define unsigned code units of the appropriate widths. For
334 example, PCRE2_UCHAR16 is usually defined as `uint16_t'. The SPTR types are
335 constant pointers to the equivalent UCHAR types, that is, they are pointers to
336 vectors of unsigned code units.
337 .P
338 Many applications use only one code unit width. For their convenience, macros
339 are defined whose names are the generic forms such as \fBpcre2_compile()\fP and
340 PCRE2_SPTR. These macros use the value of the macro PCRE2_CODE_UNIT_WIDTH to
341 generate the appropriate width-specific function and macro names.
342 PCRE2_CODE_UNIT_WIDTH is not defined by default. An application must define it
343 to be 8, 16, or 32 before including \fBpcre2.h\fP in order to make use of the
344 generic names.
345 .P
346 Applications that use more than one code unit width can be linked with more
347 than one PCRE2 library, but must define PCRE2_CODE_UNIT_WIDTH to be 0 before
348 including \fBpcre2.h\fP, and then use the real function names. Any code that is
349 to be included in an environment where the value of PCRE2_CODE_UNIT_WIDTH is
350 unknown should also use the real function names. (Unfortunately, it is not
351 possible in C code to save and restore the value of a macro.)
352 .P
353 If PCRE2_CODE_UNIT_WIDTH is not defined before including \fBpcre2.h\fP, a
354 compiler error occurs.
355 .P
356 When using multiple libraries in an application, you must take care when
357 processing any particular pattern to use only functions from a single library.
358 For example, if you want to run a match using a pattern that was compiled with
359 \fBpcre2_compile_16()\fP, you must do so with \fBpcre2_match_16()\fP, not
360 \fBpcre2_match_8()\fP or \fBpcre2_match_32()\fP.
361 .P
362 In the function summaries above, and in the rest of this document and other
363 PCRE2 documents, functions and data types are described using their generic
364 names, without the _8, _16, or _32 suffix.
365 .
366 .
367 .SH "PCRE2 API OVERVIEW"
368 .rs
369 .sp
370 PCRE2 has its own native API, which is described in this document. There are
371 also some wrapper functions for the 8-bit library that correspond to the
372 POSIX regular expression API, but they do not give access to all the
373 functionality of PCRE2. They are described in the
374 .\" HREF
375 \fBpcre2posix\fP
376 .\"
377 documentation. Both these APIs define a set of C function calls.
378 .P
379 The native API C data types, function prototypes, option values, and error
380 codes are defined in the header file \fBpcre2.h\fP, which also contains
381 definitions of PCRE2_MAJOR and PCRE2_MINOR, the major and minor release numbers
382 for the library. Applications can use these to include support for different
383 releases of PCRE2.
384 .P
385 In a Windows environment, if you want to statically link an application program
386 against a non-dll PCRE2 library, you must define PCRE2_STATIC before including
387 \fBpcre2.h\fP.
388 .P
389 The functions \fBpcre2_compile()\fP and \fBpcre2_match()\fP are used for
390 compiling and matching regular expressions in a Perl-compatible manner. A
391 sample program that demonstrates the simplest way of using them is provided in
392 the file called \fIpcre2demo.c\fP in the PCRE2 source distribution. A listing
393 of this program is given in the
394 .\" HREF
395 \fBpcre2demo\fP
396 .\"
397 documentation, and the
398 .\" HREF
399 \fBpcre2sample\fP
400 .\"
401 documentation describes how to compile and run it.
402 .P
403 The compiling and matching functions recognize various options that are passed
404 as bits in an options argument. There are also some more complicated parameters
405 such as custom memory management functions and resource limits that are passed
406 in "contexts" (which are just memory blocks, described below). Simple
407 applications do not need to make use of contexts.
408 .P
409 Just-in-time (JIT) compiler support is an optional feature of PCRE2 that can be
410 built in appropriate hardware environments. It greatly speeds up the matching
411 performance of many patterns. Programs can request that it be used if
412 available by calling \fBpcre2_jit_compile()\fP after a pattern has been
413 successfully compiled by \fBpcre2_compile()\fP. This does nothing if JIT
414 support is not available.
415 .P
416 More complicated programs might need to make use of the specialist functions
417 \fBpcre2_jit_stack_create()\fP, \fBpcre2_jit_stack_free()\fP, and
418 \fBpcre2_jit_stack_assign()\fP in order to control the JIT code's memory usage.
419 .P
420 JIT matching is automatically used by \fBpcre2_match()\fP if it is available,
421 unless the PCRE2_NO_JIT option is set. There is also a direct interface for JIT
422 matching, which gives improved performance at the expense of less sanity
423 checking. The JIT-specific functions are discussed in the
424 .\" HREF
425 \fBpcre2jit\fP
426 .\"
427 documentation.
428 .P
429 A second matching function, \fBpcre2_dfa_match()\fP, which is not
430 Perl-compatible, is also provided. This uses a different algorithm for the
431 matching. The alternative algorithm finds all possible matches (at a given
432 point in the subject), and scans the subject just once (unless there are
433 lookaround assertions). However, this algorithm does not return captured
434 substrings. A description of the two matching algorithms and their advantages
435 and disadvantages is given in the
436 .\" HREF
437 \fBpcre2matching\fP
438 .\"
439 documentation. There is no JIT support for \fBpcre2_dfa_match()\fP.
440 .P
441 In addition to the main compiling and matching functions, there are convenience
442 functions for extracting captured substrings from a subject string that has
443 been matched by \fBpcre2_match()\fP. They are:
444 .sp
445   \fBpcre2_substring_copy_byname()\fP
446   \fBpcre2_substring_copy_bynumber()\fP
447   \fBpcre2_substring_get_byname()\fP
448   \fBpcre2_substring_get_bynumber()\fP
449   \fBpcre2_substring_list_get()\fP
450   \fBpcre2_substring_length_byname()\fP
451   \fBpcre2_substring_length_bynumber()\fP
452   \fBpcre2_substring_nametable_scan()\fP
453   \fBpcre2_substring_number_from_name()\fP
454 .sp
455 \fBpcre2_substring_free()\fP and \fBpcre2_substring_list_free()\fP are also
456 provided, to free memory used for extracted strings. If either of these
457 functions is called with a NULL argument, the function returns immediately
458 without doing anything.
459 .P
460 The function \fBpcre2_substitute()\fP can be called to match a pattern and
461 return a copy of the subject string with substitutions for parts that were
462 matched.
463 .P
464 Functions whose names begin with \fBpcre2_serialize_\fP are used for saving
465 compiled patterns on disc or elsewhere, and reloading them later.
466 .P
467 Finally, there are functions for finding out information about a compiled
468 pattern (\fBpcre2_pattern_info()\fP) and about the configuration with which
469 PCRE2 was built (\fBpcre2_config()\fP).
470 .P
471 Functions with names ending with \fB_free()\fP are used for freeing memory
472 blocks of various sorts. In all cases, if one of these functions is called with
473 a NULL argument, it does nothing.
474 .
475 .
476 .SH "STRING LENGTHS AND OFFSETS"
477 .rs
478 .sp
479 The PCRE2 API uses string lengths and offsets into strings of code units in
480 several places. These values are always of type PCRE2_SIZE, which is an
481 unsigned integer type, currently always defined as \fIsize_t\fP. The largest
482 value that can be stored in such a type (that is ~(PCRE2_SIZE)0) is reserved
483 as a special indicator for zero-terminated strings and unset offsets.
484 Therefore, the longest string that can be handled is one less than this
485 maximum.
486 .
487 .
488 .\" HTML <a name="newlines"></a>
489 .SH NEWLINES
490 .rs
491 .sp
492 PCRE2 supports five different conventions for indicating line breaks in
493 strings: a single CR (carriage return) character, a single LF (linefeed)
494 character, the two-character sequence CRLF, any of the three preceding, or any
495 Unicode newline sequence. The Unicode newline sequences are the three just
496 mentioned, plus the single characters VT (vertical tab, U+000B), FF (form feed,
497 U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and PS
498 (paragraph separator, U+2029).
499 .P
500 Each of the first three conventions is used by at least one operating system as
501 its standard newline sequence. When PCRE2 is built, a default can be specified.
502 If it is not, the default is set to LF, which is the Unix standard. However,
503 the newline convention can be changed by an application when calling
504 \fBpcre2_compile()\fP, or it can be specified by special text at the start of
505 the pattern itself; this overrides any other settings. See the
506 .\" HREF
507 \fBpcre2pattern\fP
508 .\"
509 page for details of the special character sequences.
510 .P
511 In the PCRE2 documentation the word "newline" is used to mean "the character or
512 pair of characters that indicate a line break". The choice of newline
513 convention affects the handling of the dot, circumflex, and dollar
514 metacharacters, the handling of #-comments in /x mode, and, when CRLF is a
515 recognized line ending sequence, the match position advancement for a
516 non-anchored pattern. There is more detail about this in the
517 .\" HTML <a href="#matchoptions">
518 .\" </a>
519 section on \fBpcre2_match()\fP options
520 .\"
521 below.
522 .P
523 The choice of newline convention does not affect the interpretation of
524 the \en or \er escape sequences, nor does it affect what \eR matches; this has
525 its own separate convention.
526 .
527 .
528 .SH MULTITHREADING
529 .rs
530 .sp
531 In a multithreaded application it is important to keep thread-specific data
532 separate from data that can be shared between threads. The PCRE2 library code
533 itself is thread-safe: it contains no static or global variables. The API is
534 designed to be fairly simple for non-threaded applications while at the same
535 time ensuring that multithreaded applications can use it.
536 .P
537 There are several different blocks of data that are used to pass information
538 between the application and the PCRE2 libraries.
539 .
540 .
541 .SS "The compiled pattern"
542 .rs
543 .sp
544 A pointer to the compiled form of a pattern is returned to the user when
545 \fBpcre2_compile()\fP is successful. The data in the compiled pattern is fixed,
546 and does not change when the pattern is matched. Therefore, it is thread-safe,
547 that is, the same compiled pattern can be used by more than one thread
548 simultaneously. For example, an application can compile all its patterns at the
549 start, before forking off multiple threads that use them. However, if the
550 just-in-time (JIT) optimization feature is being used, it needs separate memory
551 stack areas for each thread. See the
552 .\" HREF
553 \fBpcre2jit\fP
554 .\"
555 documentation for more details.
556 .P
557 In a more complicated situation, where patterns are compiled only when they are
558 first needed, but are still shared between threads, pointers to compiled
559 patterns must be protected from simultaneous writing by multiple threads, at
560 least until a pattern has been compiled. The logic can be something like this:
561 .sp
562   Get a read-only (shared) lock (mutex) for pointer
563   if (pointer == NULL)
564     {
565     Get a write (unique) lock for pointer
566     pointer = pcre2_compile(...
567     }
568   Release the lock
569   Use pointer in pcre2_match()
570 .sp
571 Of course, testing for compilation errors should also be included in the code.
572 .P
573 If JIT is being used, but the JIT compilation is not being done immediately,
574 (perhaps waiting to see if the pattern is used often enough) similar logic is
575 required. JIT compilation updates a pointer within the compiled code block, so
576 a thread must gain unique write access to the pointer before calling
577 \fBpcre2_jit_compile()\fP. Alternatively, \fBpcre2_code_copy()\fP or
578 \fBpcre2_code_copy_with_tables()\fP can be used to obtain a private copy of the
579 compiled code before calling the JIT compiler.
580 .
581 .
582 .SS "Context blocks"
583 .rs
584 .sp
585 The next main section below introduces the idea of "contexts" in which PCRE2
586 functions are called. A context is nothing more than a collection of parameters
587 that control the way PCRE2 operates. Grouping a number of parameters together
588 in a context is a convenient way of passing them to a PCRE2 function without
589 using lots of arguments. The parameters that are stored in contexts are in some
590 sense "advanced features" of the API. Many straightforward applications will
591 not need to use contexts.
592 .P
593 In a multithreaded application, if the parameters in a context are values that
594 are never changed, the same context can be used by all the threads. However, if
595 any thread needs to change any value in a context, it must make its own
596 thread-specific copy.
597 .
598 .
599 .SS "Match blocks"
600 .rs
601 .sp
602 The matching functions need a block of memory for storing the results of a
603 match. This includes details of what was matched, as well as additional
604 information such as the name of a (*MARK) setting. Each thread must provide its
605 own copy of this memory.
606 .
607 .
608 .SH "PCRE2 CONTEXTS"
609 .rs
610 .sp
611 Some PCRE2 functions have a lot of parameters, many of which are used only by
612 specialist applications, for example, those that use custom memory management
613 or non-standard character tables. To keep function argument lists at a
614 reasonable size, and at the same time to keep the API extensible, "uncommon"
615 parameters are passed to certain functions in a \fBcontext\fP instead of
616 directly. A context is just a block of memory that holds the parameter values.
617 Applications that do not need to adjust any of the context parameters can pass
618 NULL when a context pointer is required.
619 .P
620 There are three different types of context: a general context that is relevant
621 for several PCRE2 operations, a compile-time context, and a match-time context.
622 .
623 .
624 .SS "The general context"
625 .rs
626 .sp
627 At present, this context just contains pointers to (and data for) external
628 memory management functions that are called from several places in the PCRE2
629 library. The context is named `general' rather than specifically `memory'
630 because in future other fields may be added. If you do not want to supply your
631 own custom memory management functions, you do not need to bother with a
632 general context. A general context is created by:
633 .sp
634 .nf
635 .B pcre2_general_context *pcre2_general_context_create(
636 .B "  void *(*\fIprivate_malloc\fP)(PCRE2_SIZE, void *),"
637 .B "  void (*\fIprivate_free\fP)(void *, void *), void *\fImemory_data\fP);"
638 .fi
639 .sp
640 The two function pointers specify custom memory management functions, whose
641 prototypes are:
642 .sp
643   \fBvoid *private_malloc(PCRE2_SIZE, void *);\fP
644   \fBvoid  private_free(void *, void *);\fP
645 .sp
646 Whenever code in PCRE2 calls these functions, the final argument is the value
647 of \fImemory_data\fP. Either of the first two arguments of the creation
648 function may be NULL, in which case the system memory management functions
649 \fImalloc()\fP and \fIfree()\fP are used. (This is not currently useful, as
650 there are no other fields in a general context, but in future there might be.)
651 The \fIprivate_malloc()\fP function is used (if supplied) to obtain memory for
652 storing the context, and all three values are saved as part of the context.
653 .P
654 Whenever PCRE2 creates a data block of any kind, the block contains a pointer
655 to the \fIfree()\fP function that matches the \fImalloc()\fP function that was
656 used. When the time comes to free the block, this function is called.
657 .P
658 A general context can be copied by calling:
659 .sp
660 .nf
661 .B pcre2_general_context *pcre2_general_context_copy(
662 .B "  pcre2_general_context *\fIgcontext\fP);"
663 .fi
664 .sp
665 The memory used for a general context should be freed by calling:
666 .sp
667 .nf
668 .B void pcre2_general_context_free(pcre2_general_context *\fIgcontext\fP);
669 .fi
670 .sp
671 If this function is passed a NULL argument, it returns immediately without
672 doing anything.
673 .
674 .
675 .\" HTML <a name="compilecontext"></a>
676 .SS "The compile context"
677 .rs
678 .sp
679 A compile context is required if you want to provide an external function for
680 stack checking during compilation or to change the default values of any of the
681 following compile-time parameters:
682 .sp
683   What \eR matches (Unicode newlines or CR, LF, CRLF only)
684   PCRE2's character tables
685   The newline character sequence
686   The compile time nested parentheses limit
687   The maximum length of the pattern string
688   The extra options bits (none set by default)
689 .sp
690 A compile context is also required if you are using custom memory management.
691 If none of these apply, just pass NULL as the context argument of
692 \fIpcre2_compile()\fP.
693 .P
694 A compile context is created, copied, and freed by the following functions:
695 .sp
696 .nf
697 .B pcre2_compile_context *pcre2_compile_context_create(
698 .B "  pcre2_general_context *\fIgcontext\fP);"
699 .sp
700 .B pcre2_compile_context *pcre2_compile_context_copy(
701 .B "  pcre2_compile_context *\fIccontext\fP);"
702 .sp
703 .B void pcre2_compile_context_free(pcre2_compile_context *\fIccontext\fP);
704 .fi
705 .sp
706 A compile context is created with default values for its parameters. These can
707 be changed by calling the following functions, which return 0 on success, or
708 PCRE2_ERROR_BADDATA if invalid data is detected.
709 .sp
710 .nf
711 .B int pcre2_set_bsr(pcre2_compile_context *\fIccontext\fP,
712 .B "  uint32_t \fIvalue\fP);"
713 .fi
714 .sp
715 The value must be PCRE2_BSR_ANYCRLF, to specify that \eR matches only CR, LF,
716 or CRLF, or PCRE2_BSR_UNICODE, to specify that \eR matches any Unicode line
717 ending sequence. The value is used by the JIT compiler and by the two
718 interpreted matching functions, \fIpcre2_match()\fP and
719 \fIpcre2_dfa_match()\fP.
720 .sp
721 .nf
722 .B int pcre2_set_character_tables(pcre2_compile_context *\fIccontext\fP,
723 .B "  const unsigned char *\fItables\fP);"
724 .fi
725 .sp
726 The value must be the result of a call to \fIpcre2_maketables()\fP, whose only
727 argument is a general context. This function builds a set of character tables
728 in the current locale.
729 .sp
730 .nf
731 .B int pcre2_set_compile_extra_options(pcre2_compile_context *\fIccontext\fP,
732 .B "  uint32_t \fIextra_options\fP);"
733 .fi
734 .sp
735 As PCRE2 has developed, almost all the 32 option bits that are available in
736 the \fIoptions\fP argument of \fBpcre2_compile()\fP have been used up. To avoid
737 running out, the compile context contains a set of extra option bits which are
738 used for some newer, assumed rarer, options. This function sets those bits. It
739 always sets all the bits (either on or off). It does not modify any existing
740 setting. The available options are defined in the section entitled "Extra
741 compile options"
742 .\" HTML <a href="#extracompileoptions">
743 .\" </a>
744 below.
745 .\"
746 .sp
747 .nf
748 .B int pcre2_set_max_pattern_length(pcre2_compile_context *\fIccontext\fP,
749 .B "  PCRE2_SIZE \fIvalue\fP);"
750 .fi
751 .sp
752 This sets a maximum length, in code units, for any pattern string that is
753 compiled with this context. If the pattern is longer, an error is generated.
754 This facility is provided so that applications that accept patterns from
755 external sources can limit their size. The default is the largest number that a
756 PCRE2_SIZE variable can hold, which is effectively unlimited.
757 .sp
758 .nf
759 .B int pcre2_set_newline(pcre2_compile_context *\fIccontext\fP,
760 .B "  uint32_t \fIvalue\fP);"
761 .fi
762 .sp
763 This specifies which characters or character sequences are to be recognized as
764 newlines. The value must be one of PCRE2_NEWLINE_CR (carriage return only),
765 PCRE2_NEWLINE_LF (linefeed only), PCRE2_NEWLINE_CRLF (the two-character
766 sequence CR followed by LF), PCRE2_NEWLINE_ANYCRLF (any of the above),
767 PCRE2_NEWLINE_ANY (any Unicode newline sequence), or PCRE2_NEWLINE_NUL (the
768 NUL character, that is a binary zero).
769 .P
770 A pattern can override the value set in the compile context by starting with a
771 sequence such as (*CRLF). See the
772 .\" HREF
773 \fBpcre2pattern\fP
774 .\"
775 page for details.
776 .P
777 When a pattern is compiled with the PCRE2_EXTENDED or PCRE2_EXTENDED_MORE
778 option, the newline convention affects the recognition of the end of internal
779 comments starting with #. The value is saved with the compiled pattern for
780 subsequent use by the JIT compiler and by the two interpreted matching
781 functions, \fIpcre2_match()\fP and \fIpcre2_dfa_match()\fP.
782 .sp
783 .nf
784 .B int pcre2_set_parens_nest_limit(pcre2_compile_context *\fIccontext\fP,
785 .B "  uint32_t \fIvalue\fP);"
786 .fi
787 .sp
788 This parameter ajusts the limit, set when PCRE2 is built (default 250), on the
789 depth of parenthesis nesting in a pattern. This limit stops rogue patterns
790 using up too much system stack when being compiled. The limit applies to
791 parentheses of all kinds, not just capturing parentheses.
792 .sp
793 .nf
794 .B int pcre2_set_compile_recursion_guard(pcre2_compile_context *\fIccontext\fP,
795 .B "  int (*\fIguard_function\fP)(uint32_t, void *), void *\fIuser_data\fP);"
796 .fi
797 .sp
798 There is at least one application that runs PCRE2 in threads with very limited
799 system stack, where running out of stack is to be avoided at all costs. The
800 parenthesis limit above cannot take account of how much stack is actually
801 available during compilation. For a finer control, you can supply a function
802 that is called whenever \fBpcre2_compile()\fP starts to compile a parenthesized
803 part of a pattern. This function can check the actual stack size (or anything
804 else that it wants to, of course).
805 .P
806 The first argument to the callout function gives the current depth of
807 nesting, and the second is user data that is set up by the last argument of
808 \fBpcre2_set_compile_recursion_guard()\fP. The callout function should return
809 zero if all is well, or non-zero to force an error.
810 .
811 .
812 .\" HTML <a name="matchcontext"></a>
813 .SS "The match context"
814 .rs
815 .sp
816 A match context is required if you want to:
817 .sp
818   Set up a callout function
819   Set an offset limit for matching an unanchored pattern
820   Change the limit on the amount of heap used when matching
821   Change the backtracking match limit
822   Change the backtracking depth limit
823   Set custom memory management specifically for the match
824 .sp
825 If none of these apply, just pass NULL as the context argument of
826 \fBpcre2_match()\fP, \fBpcre2_dfa_match()\fP, or \fBpcre2_jit_match()\fP.
827 .P
828 A match context is created, copied, and freed by the following functions:
829 .sp
830 .nf
831 .B pcre2_match_context *pcre2_match_context_create(
832 .B "  pcre2_general_context *\fIgcontext\fP);"
833 .sp
834 .B pcre2_match_context *pcre2_match_context_copy(
835 .B "  pcre2_match_context *\fImcontext\fP);"
836 .sp
837 .B void pcre2_match_context_free(pcre2_match_context *\fImcontext\fP);
838 .fi
839 .sp
840 A match context is created with default values for its parameters. These can
841 be changed by calling the following functions, which return 0 on success, or
842 PCRE2_ERROR_BADDATA if invalid data is detected.
843 .sp
844 .nf
845 .B int pcre2_set_callout(pcre2_match_context *\fImcontext\fP,
846 .B "  int (*\fIcallout_function\fP)(pcre2_callout_block *, void *),"
847 .B "  void *\fIcallout_data\fP);"
848 .fi
849 .sp
850 This sets up a "callout" function for PCRE2 to call at specified points
851 during a matching operation. Details are given in the
852 .\" HREF
853 \fBpcre2callout\fP
854 .\"
855 documentation.
856 .sp
857 .nf
858 .B int pcre2_set_offset_limit(pcre2_match_context *\fImcontext\fP,
859 .B "  PCRE2_SIZE \fIvalue\fP);"
860 .fi
861 .sp
862 The \fIoffset_limit\fP parameter limits how far an unanchored search can
863 advance in the subject string. The default value is PCRE2_UNSET. The
864 \fBpcre2_match()\fP and \fBpcre2_dfa_match()\fP functions return
865 PCRE2_ERROR_NOMATCH if a match with a starting point before or at the given
866 offset is not found. The \fBpcre2_substitute()\fP function makes no more
867 substitutions.
868 .P
869 For example, if the pattern /abc/ is matched against "123abc" with an offset
870 limit less than 3, the result is PCRE2_ERROR_NO_MATCH. A match can never be
871 found if the \fIstartoffset\fP argument of \fBpcre2_match()\fP,
872 \fBpcre2_dfa_match()\fP, or \fBpcre2_substitute()\fP is greater than the offset
873 limit set in the match context.
874 .P
875 When using this facility, you must set the PCRE2_USE_OFFSET_LIMIT option when
876 calling \fBpcre2_compile()\fP so that when JIT is in use, different code can be
877 compiled. If a match is started with a non-default match limit when
878 PCRE2_USE_OFFSET_LIMIT is not set, an error is generated.
879 .P
880 The offset limit facility can be used to track progress when searching large
881 subject strings or to limit the extent of global substitutions. See also the
882 PCRE2_FIRSTLINE option, which requires a match to start before or at the first
883 newline that follows the start of matching in the subject. If this is set with
884 an offset limit, a match must occur in the first line and also within the
885 offset limit. In other words, whichever limit comes first is used.
886 .sp
887 .nf
888 .B int pcre2_set_heap_limit(pcre2_match_context *\fImcontext\fP,
889 .B "  uint32_t \fIvalue\fP);"
890 .fi
891 .sp
892 The \fIheap_limit\fP parameter specifies, in units of kibibytes (1024 bytes),
893 the maximum amount of heap memory that \fBpcre2_match()\fP may use to hold
894 backtracking information when running an interpretive match. This limit also
895 applies to \fBpcre2_dfa_match()\fP, which may use the heap when processing
896 patterns with a lot of nested pattern recursion or lookarounds or atomic
897 groups. This limit does not apply to matching with the JIT optimization, which
898 has its own memory control arrangements (see the
899 .\" HREF
900 \fBpcre2jit\fP
901 .\"
902 documentation for more details). If the limit is reached, the negative error
903 code PCRE2_ERROR_HEAPLIMIT is returned. The default limit can be set when PCRE2
904 is built; if it is not, the default is set very large and is essentially
905 "unlimited".
906 .P
907 A value for the heap limit may also be supplied by an item at the start of a
908 pattern of the form
909 .sp
910   (*LIMIT_HEAP=ddd)
911 .sp
912 where ddd is a decimal number. However, such a setting is ignored unless ddd is
913 less than the limit set by the caller of \fBpcre2_match()\fP or, if no such
914 limit is set, less than the default.
915 .P
916 The \fBpcre2_match()\fP function starts out using a 20KiB vector on the system
917 stack for recording backtracking points. The more nested backtracking points
918 there are (that is, the deeper the search tree), the more memory is needed.
919 Heap memory is used only if the initial vector is too small. If the heap limit
920 is set to a value less than 21 (in particular, zero) no heap memory will be
921 used. In this case, only patterns that do not have a lot of nested backtracking
922 can be successfully processed.
923 .P
924 Similarly, for \fBpcre2_dfa_match()\fP, a vector on the system stack is used
925 when processing pattern recursions, lookarounds, or atomic groups, and only if
926 this is not big enough is heap memory used. In this case, too, setting a value
927 of zero disables the use of the heap.
928 .sp
929 .nf
930 .B int pcre2_set_match_limit(pcre2_match_context *\fImcontext\fP,
931 .B "  uint32_t \fIvalue\fP);"
932 .fi
933 .sp
934 The \fImatch_limit\fP parameter provides a means of preventing PCRE2 from using
935 up too many computing resources when processing patterns that are not going to
936 match, but which have a very large number of possibilities in their search
937 trees. The classic example is a pattern that uses nested unlimited repeats.
938 .P
939 There is an internal counter in \fBpcre2_match()\fP that is incremented each
940 time round its main matching loop. If this value reaches the match limit,
941 \fBpcre2_match()\fP returns the negative value PCRE2_ERROR_MATCHLIMIT. This has
942 the effect of limiting the amount of backtracking that can take place. For
943 patterns that are not anchored, the count restarts from zero for each position
944 in the subject string. This limit also applies to \fBpcre2_dfa_match()\fP,
945 though the counting is done in a different way.
946 .P
947 When \fBpcre2_match()\fP is called with a pattern that was successfully
948 processed by \fBpcre2_jit_compile()\fP, the way in which matching is executed
949 is entirely different. However, there is still the possibility of runaway
950 matching that goes on for a very long time, and so the \fImatch_limit\fP value
951 is also used in this case (but in a different way) to limit how long the
952 matching can continue.
953 .P
954 The default value for the limit can be set when PCRE2 is built; the default
955 default is 10 million, which handles all but the most extreme cases. A value
956 for the match limit may also be supplied by an item at the start of a pattern
957 of the form
958 .sp
959   (*LIMIT_MATCH=ddd)
960 .sp
961 where ddd is a decimal number. However, such a setting is ignored unless ddd is
962 less than the limit set by the caller of \fBpcre2_match()\fP or
963 \fBpcre2_dfa_match()\fP or, if no such limit is set, less than the default.
964 .sp
965 .nf
966 .B int pcre2_set_depth_limit(pcre2_match_context *\fImcontext\fP,
967 .B "  uint32_t \fIvalue\fP);"
968 .fi
969 .sp
970 This parameter limits the depth of nested backtracking in \fBpcre2_match()\fP.
971 Each time a nested backtracking point is passed, a new memory "frame" is used
972 to remember the state of matching at that point. Thus, this parameter
973 indirectly limits the amount of memory that is used in a match. However,
974 because the size of each memory "frame" depends on the number of capturing
975 parentheses, the actual memory limit varies from pattern to pattern. This limit
976 was more useful in versions before 10.30, where function recursion was used for
977 backtracking.
978 .P
979 The depth limit is not relevant, and is ignored, when matching is done using
980 JIT compiled code. However, it is supported by \fBpcre2_dfa_match()\fP, which
981 uses it to limit the depth of nested internal recursive function calls that
982 implement atomic groups, lookaround assertions, and pattern recursions. This
983 limits, indirectly, the amount of system stack that is used. It was more useful
984 in versions before 10.32, when stack memory was used for local workspace
985 vectors for recursive function calls. From version 10.32, only local variables
986 are allocated on the stack and as each call uses only a few hundred bytes, even
987 a small stack can support quite a lot of recursion.
988 .P
989 If the depth of internal recursive function calls is great enough, local
990 workspace vectors are allocated on the heap from version 10.32 onwards, so the
991 depth limit also indirectly limits the amount of heap memory that is used. A
992 recursive pattern such as /(.(?2))((?1)|)/, when matched to a very long string
993 using \fBpcre2_dfa_match()\fP, can use a great deal of memory. However, it is
994 probably better to limit heap usage directly by calling
995 \fBpcre2_set_heap_limit()\fP.
996 .P
997 The default value for the depth limit can be set when PCRE2 is built; if it is
998 not, the default is set to the same value as the default for the match limit.
999 If the limit is exceeded, \fBpcre2_match()\fP or \fBpcre2_dfa_match()\fP
1000 returns PCRE2_ERROR_DEPTHLIMIT. A value for the depth limit may also be
1001 supplied by an item at the start of a pattern of the form
1002 .sp
1003   (*LIMIT_DEPTH=ddd)
1004 .sp
1005 where ddd is a decimal number. However, such a setting is ignored unless ddd is
1006 less than the limit set by the caller of \fBpcre2_match()\fP or
1007 \fBpcre2_dfa_match()\fP or, if no such limit is set, less than the default.
1008 .
1009 .
1010 .SH "CHECKING BUILD-TIME OPTIONS"
1011 .rs
1012 .sp
1013 .B int pcre2_config(uint32_t \fIwhat\fP, void *\fIwhere\fP);
1014 .P
1015 The function \fBpcre2_config()\fP makes it possible for a PCRE2 client to
1016 discover which optional features have been compiled into the PCRE2 library. The
1017 .\" HREF
1018 \fBpcre2build\fP
1019 .\"
1020 documentation has more details about these optional features.
1021 .P
1022 The first argument for \fBpcre2_config()\fP specifies which information is
1023 required. The second argument is a pointer to memory into which the information
1024 is placed. If NULL is passed, the function returns the amount of memory that is
1025 needed for the requested information. For calls that return numerical values,
1026 the value is in bytes; when requesting these values, \fIwhere\fP should point
1027 to appropriately aligned memory. For calls that return strings, the required
1028 length is given in code units, not counting the terminating zero.
1029 .P
1030 When requesting information, the returned value from \fBpcre2_config()\fP is
1031 non-negative on success, or the negative error code PCRE2_ERROR_BADOPTION if
1032 the value in the first argument is not recognized. The following information is
1033 available:
1034 .sp
1035   PCRE2_CONFIG_BSR
1036 .sp
1037 The output is a uint32_t integer whose value indicates what character
1038 sequences the \eR escape sequence matches by default. A value of
1039 PCRE2_BSR_UNICODE means that \eR matches any Unicode line ending sequence; a
1040 value of PCRE2_BSR_ANYCRLF means that \eR matches only CR, LF, or CRLF. The
1041 default can be overridden when a pattern is compiled.
1042 .sp
1043   PCRE2_CONFIG_COMPILED_WIDTHS
1044 .sp
1045 The output is a uint32_t integer whose lower bits indicate which code unit
1046 widths were selected when PCRE2 was built. The 1-bit indicates 8-bit support,
1047 and the 2-bit and 4-bit indicate 16-bit and 32-bit support, respectively.
1048 .sp
1049   PCRE2_CONFIG_DEPTHLIMIT
1050 .sp
1051 The output is a uint32_t integer that gives the default limit for the depth of
1052 nested backtracking in \fBpcre2_match()\fP or the depth of nested recursions,
1053 lookarounds, and atomic groups in \fBpcre2_dfa_match()\fP. Further details are
1054 given with \fBpcre2_set_depth_limit()\fP above.
1055 .sp
1056   PCRE2_CONFIG_HEAPLIMIT
1057 .sp
1058 The output is a uint32_t integer that gives, in kibibytes, the default limit
1059 for the amount of heap memory used by \fBpcre2_match()\fP or
1060 \fBpcre2_dfa_match()\fP. Further details are given with
1061 \fBpcre2_set_heap_limit()\fP above.
1062 .sp
1063   PCRE2_CONFIG_JIT
1064 .sp
1065 The output is a uint32_t integer that is set to one if support for just-in-time
1066 compiling is available; otherwise it is set to zero.
1067 .sp
1068   PCRE2_CONFIG_JITTARGET
1069 .sp
1070 The \fIwhere\fP argument should point to a buffer that is at least 48 code
1071 units long. (The exact length required can be found by calling
1072 \fBpcre2_config()\fP with \fBwhere\fP set to NULL.) The buffer is filled with a
1073 string that contains the name of the architecture for which the JIT compiler is
1074 configured, for example "x86 32bit (little endian + unaligned)". If JIT support
1075 is not available, PCRE2_ERROR_BADOPTION is returned, otherwise the number of
1076 code units used is returned. This is the length of the string, plus one unit
1077 for the terminating zero.
1078 .sp
1079   PCRE2_CONFIG_LINKSIZE
1080 .sp
1081 The output is a uint32_t integer that contains the number of bytes used for
1082 internal linkage in compiled regular expressions. When PCRE2 is configured, the
1083 value can be set to 2, 3, or 4, with the default being 2. This is the value
1084 that is returned by \fBpcre2_config()\fP. However, when the 16-bit library is
1085 compiled, a value of 3 is rounded up to 4, and when the 32-bit library is
1086 compiled, internal linkages always use 4 bytes, so the configured value is not
1087 relevant.
1088 .P
1089 The default value of 2 for the 8-bit and 16-bit libraries is sufficient for all
1090 but the most massive patterns, since it allows the size of the compiled pattern
1091 to be up to 65535 code units. Larger values allow larger regular expressions to
1092 be compiled by those two libraries, but at the expense of slower matching.
1093 .sp
1094   PCRE2_CONFIG_MATCHLIMIT
1095 .sp
1096 The output is a uint32_t integer that gives the default match limit for
1097 \fBpcre2_match()\fP. Further details are given with
1098 \fBpcre2_set_match_limit()\fP above.
1099 .sp
1100   PCRE2_CONFIG_NEWLINE
1101 .sp
1102 The output is a uint32_t integer whose value specifies the default character
1103 sequence that is recognized as meaning "newline". The values are:
1104 .sp
1105   PCRE2_NEWLINE_CR       Carriage return (CR)
1106   PCRE2_NEWLINE_LF       Linefeed (LF)
1107   PCRE2_NEWLINE_CRLF     Carriage return, linefeed (CRLF)
1108   PCRE2_NEWLINE_ANY      Any Unicode line ending
1109   PCRE2_NEWLINE_ANYCRLF  Any of CR, LF, or CRLF
1110   PCRE2_NEWLINE_NUL      The NUL character (binary zero)
1111 .sp
1112 The default should normally correspond to the standard sequence for your
1113 operating system.
1114 .sp
1115   PCRE2_CONFIG_NEVER_BACKSLASH_C
1116 .sp
1117 The output is a uint32_t integer that is set to one if the use of \eC was
1118 permanently disabled when PCRE2 was built; otherwise it is set to zero.
1119 .sp
1120   PCRE2_CONFIG_PARENSLIMIT
1121 .sp
1122 The output is a uint32_t integer that gives the maximum depth of nesting
1123 of parentheses (of any kind) in a pattern. This limit is imposed to cap the
1124 amount of system stack used when a pattern is compiled. It is specified when
1125 PCRE2 is built; the default is 250. This limit does not take into account the
1126 stack that may already be used by the calling application. For finer control
1127 over compilation stack usage, see \fBpcre2_set_compile_recursion_guard()\fP.
1128 .sp
1129   PCRE2_CONFIG_STACKRECURSE
1130 .sp
1131 This parameter is obsolete and should not be used in new code. The output is a
1132 uint32_t integer that is always set to zero.
1133 .sp
1134   PCRE2_CONFIG_UNICODE_VERSION
1135 .sp
1136 The \fIwhere\fP argument should point to a buffer that is at least 24 code
1137 units long. (The exact length required can be found by calling
1138 \fBpcre2_config()\fP with \fBwhere\fP set to NULL.) If PCRE2 has been compiled
1139 without Unicode support, the buffer is filled with the text "Unicode not
1140 supported". Otherwise, the Unicode version string (for example, "8.0.0") is
1141 inserted. The number of code units used is returned. This is the length of the
1142 string plus one unit for the terminating zero.
1143 .sp
1144   PCRE2_CONFIG_UNICODE
1145 .sp
1146 The output is a uint32_t integer that is set to one if Unicode support is
1147 available; otherwise it is set to zero. Unicode support implies UTF support.
1148 .sp
1149   PCRE2_CONFIG_VERSION
1150 .sp
1151 The \fIwhere\fP argument should point to a buffer that is at least 24 code
1152 units long. (The exact length required can be found by calling
1153 \fBpcre2_config()\fP with \fBwhere\fP set to NULL.) The buffer is filled with
1154 the PCRE2 version string, zero-terminated. The number of code units used is
1155 returned. This is the length of the string plus one unit for the terminating
1156 zero.
1157 .
1158 .
1159 .\" HTML <a name="compiling"></a>
1160 .SH "COMPILING A PATTERN"
1161 .rs
1162 .sp
1163 .nf
1164 .B pcre2_code *pcre2_compile(PCRE2_SPTR \fIpattern\fP, PCRE2_SIZE \fIlength\fP,
1165 .B "  uint32_t \fIoptions\fP, int *\fIerrorcode\fP, PCRE2_SIZE *\fIerroroffset,\fP"
1166 .B "  pcre2_compile_context *\fIccontext\fP);"
1167 .sp
1168 .B void pcre2_code_free(pcre2_code *\fIcode\fP);
1169 .sp
1170 .B pcre2_code *pcre2_code_copy(const pcre2_code *\fIcode\fP);
1171 .sp
1172 .B pcre2_code *pcre2_code_copy_with_tables(const pcre2_code *\fIcode\fP);
1173 .fi
1174 .P
1175 The \fBpcre2_compile()\fP function compiles a pattern into an internal form.
1176 The pattern is defined by a pointer to a string of code units and a length (in
1177 code units). If the pattern is zero-terminated, the length can be specified as
1178 PCRE2_ZERO_TERMINATED. The function returns a pointer to a block of memory that
1179 contains the compiled pattern and related data, or NULL if an error occurred.
1180 .P
1181 If the compile context argument \fIccontext\fP is NULL, memory for the compiled
1182 pattern is obtained by calling \fBmalloc()\fP. Otherwise, it is obtained from
1183 the same memory function that was used for the compile context. The caller must
1184 free the memory by calling \fBpcre2_code_free()\fP when it is no longer needed.
1185 If \fBpcre2_code_free()\fP is called with a NULL argument, it returns
1186 immediately, without doing anything.
1187 .P
1188 The function \fBpcre2_code_copy()\fP makes a copy of the compiled code in new
1189 memory, using the same memory allocator as was used for the original. However,
1190 if the code has been processed by the JIT compiler (see
1191 .\" HTML <a href="#jitcompiling">
1192 .\" </a>
1193 below),
1194 .\"
1195 the JIT information cannot be copied (because it is position-dependent).
1196 The new copy can initially be used only for non-JIT matching, though it can be
1197 passed to \fBpcre2_jit_compile()\fP if required. If \fBpcre2_code_copy()\fP is
1198 called with a NULL argument, it returns NULL.
1199 .P
1200 The \fBpcre2_code_copy()\fP function provides a way for individual threads in a
1201 multithreaded application to acquire a private copy of shared compiled code.
1202 However, it does not make a copy of the character tables used by the compiled
1203 pattern; the new pattern code points to the same tables as the original code.
1204 (See
1205 .\" HTML <a href="#jitcompiling">
1206 .\" </a>
1207 "Locale Support"
1208 .\"
1209 below for details of these character tables.) In many applications the same
1210 tables are used throughout, so this behaviour is appropriate. Nevertheless,
1211 there are occasions when a copy of a compiled pattern and the relevant tables
1212 are needed. The \fBpcre2_code_copy_with_tables()\fP provides this facility.
1213 Copies of both the code and the tables are made, with the new code pointing to
1214 the new tables. The memory for the new tables is automatically freed when
1215 \fBpcre2_code_free()\fP is called for the new copy of the compiled code. If
1216 \fBpcre2_code_copy_withy_tables()\fP is called with a NULL argument, it returns
1217 NULL.
1218 .P
1219 NOTE: When one of the matching functions is called, pointers to the compiled
1220 pattern and the subject string are set in the match data block so that they can
1221 be referenced by the substring extraction functions. After running a match, you
1222 must not free a compiled pattern (or a subject string) until after all
1223 operations on the
1224 .\" HTML <a href="#matchdatablock">
1225 .\" </a>
1226 match data block
1227 .\"
1228 have taken place.
1229 .P
1230 The \fIoptions\fP argument for \fBpcre2_compile()\fP contains various bit
1231 settings that affect the compilation. It should be zero if no options are
1232 required. The available options are described below. Some of them (in
1233 particular, those that are compatible with Perl, but some others as well) can
1234 also be set and unset from within the pattern (see the detailed description in
1235 the
1236 .\" HREF
1237 \fBpcre2pattern\fP
1238 .\"
1239 documentation).
1240 .P
1241 For those options that can be different in different parts of the pattern, the
1242 contents of the \fIoptions\fP argument specifies their settings at the start of
1243 compilation. The PCRE2_ANCHORED, PCRE2_ENDANCHORED, and PCRE2_NO_UTF_CHECK
1244 options can be set at the time of matching as well as at compile time.
1245 .P
1246 Other, less frequently required compile-time parameters (for example, the
1247 newline setting) can be provided in a compile context (as described
1248 .\" HTML <a href="#compilecontext">
1249 .\" </a>
1250 above).
1251 .\"
1252 .P
1253 If \fIerrorcode\fP or \fIerroroffset\fP is NULL, \fBpcre2_compile()\fP returns
1254 NULL immediately. Otherwise, the variables to which these point are set to an
1255 error code and an offset (number of code units) within the pattern,
1256 respectively, when \fBpcre2_compile()\fP returns NULL because a compilation
1257 error has occurred. The values are not defined when compilation is successful
1258 and \fBpcre2_compile()\fP returns a non-NULL value.
1259 .P
1260 There are nearly 100 positive error codes that \fBpcre2_compile()\fP may return
1261 if it finds an error in the pattern. There are also some negative error codes
1262 that are used for invalid UTF strings. These are the same as given by
1263 \fBpcre2_match()\fP and \fBpcre2_dfa_match()\fP, and are described in the
1264 .\" HREF
1265 \fBpcre2unicode\fP
1266 .\"
1267 page. There is no separate documentation for the positive error codes, because
1268 the textual error messages that are obtained by calling the
1269 \fBpcre2_get_error_message()\fP function (see "Obtaining a textual error
1270 message"
1271 .\" HTML <a href="#geterrormessage">
1272 .\" </a>
1273 below)
1274 .\"
1275 should be self-explanatory. Macro names starting with PCRE2_ERROR_ are defined
1276 for both positive and negative error codes in \fBpcre2.h\fP.
1277 .P
1278 The value returned in \fIerroroffset\fP is an indication of where in the
1279 pattern the error occurred. It is not necessarily the furthest point in the
1280 pattern that was read. For example, after the error "lookbehind assertion is
1281 not fixed length", the error offset points to the start of the failing
1282 assertion. For an invalid UTF-8 or UTF-16 string, the offset is that of the
1283 first code unit of the failing character.
1284 .P
1285 Some errors are not detected until the whole pattern has been scanned; in these
1286 cases, the offset passed back is the length of the pattern. Note that the
1287 offset is in code units, not characters, even in a UTF mode. It may sometimes
1288 point into the middle of a UTF-8 or UTF-16 character.
1289 .P
1290 This code fragment shows a typical straightforward call to
1291 \fBpcre2_compile()\fP:
1292 .sp
1293   pcre2_code *re;
1294   PCRE2_SIZE erroffset;
1295   int errorcode;
1296   re = pcre2_compile(
1297     "^A.*Z",                /* the pattern */
1298     PCRE2_ZERO_TERMINATED,  /* the pattern is zero-terminated */
1299     0,                      /* default options */
1300     &errorcode,             /* for error code */
1301     &erroffset,             /* for error offset */
1302     NULL);                  /* no compile context */
1303 .sp
1304 The following names for option bits are defined in the \fBpcre2.h\fP header
1305 file:
1306 .sp
1307   PCRE2_ANCHORED
1308 .sp
1309 If this bit is set, the pattern is forced to be "anchored", that is, it is
1310 constrained to match only at the first matching point in the string that is
1311 being searched (the "subject string"). This effect can also be achieved by
1312 appropriate constructs in the pattern itself, which is the only way to do it in
1313 Perl.
1314 .sp
1315   PCRE2_ALLOW_EMPTY_CLASS
1316 .sp
1317 By default, for compatibility with Perl, a closing square bracket that
1318 immediately follows an opening one is treated as a data character for the
1319 class. When PCRE2_ALLOW_EMPTY_CLASS is set, it terminates the class, which
1320 therefore contains no characters and so can never match.
1321 .sp
1322   PCRE2_ALT_BSUX
1323 .sp
1324 This option request alternative handling of three escape sequences, which
1325 makes PCRE2's behaviour more like ECMAscript (aka JavaScript). When it is set:
1326 .P
1327 (1) \eU matches an upper case "U" character; by default \eU causes a compile
1328 time error (Perl uses \eU to upper case subsequent characters).
1329 .P
1330 (2) \eu matches a lower case "u" character unless it is followed by four
1331 hexadecimal digits, in which case the hexadecimal number defines the code point
1332 to match. By default, \eu causes a compile time error (Perl uses it to upper
1333 case the following character).
1334 .P
1335 (3) \ex matches a lower case "x" character unless it is followed by two
1336 hexadecimal digits, in which case the hexadecimal number defines the code point
1337 to match. By default, as in Perl, a hexadecimal number is always expected after
1338 \ex, but it may have zero, one, or two digits (so, for example, \exz matches a
1339 binary zero character followed by z).
1340 .sp
1341   PCRE2_ALT_CIRCUMFLEX
1342 .sp
1343 In multiline mode (when PCRE2_MULTILINE is set), the circumflex metacharacter
1344 matches at the start of the subject (unless PCRE2_NOTBOL is set), and also
1345 after any internal newline. However, it does not match after a newline at the
1346 end of the subject, for compatibility with Perl. If you want a multiline
1347 circumflex also to match after a terminating newline, you must set
1348 PCRE2_ALT_CIRCUMFLEX.
1349 .sp
1350   PCRE2_ALT_VERBNAMES
1351 .sp
1352 By default, for compatibility with Perl, the name in any verb sequence such as
1353 (*MARK:NAME) is any sequence of characters that does not include a closing
1354 parenthesis. The name is not processed in any way, and it is not possible to
1355 include a closing parenthesis in the name. However, if the PCRE2_ALT_VERBNAMES
1356 option is set, normal backslash processing is applied to verb names and only an
1357 unescaped closing parenthesis terminates the name. A closing parenthesis can be
1358 included in a name either as \e) or between \eQ and \eE. If the PCRE2_EXTENDED
1359 or PCRE2_EXTENDED_MORE option is set with PCRE2_ALT_VERBNAMES, unescaped
1360 whitespace in verb names is skipped and #-comments are recognized, exactly as
1361 in the rest of the pattern.
1362 .sp
1363   PCRE2_AUTO_CALLOUT
1364 .sp
1365 If this bit is set, \fBpcre2_compile()\fP automatically inserts callout items,
1366 all with number 255, before each pattern item, except immediately before or
1367 after an explicit callout in the pattern. For discussion of the callout
1368 facility, see the
1369 .\" HREF
1370 \fBpcre2callout\fP
1371 .\"
1372 documentation.
1373 .sp
1374   PCRE2_CASELESS
1375 .sp
1376 If this bit is set, letters in the pattern match both upper and lower case
1377 letters in the subject. It is equivalent to Perl's /i option, and it can be
1378 changed within a pattern by a (?i) option setting. If PCRE2_UTF is set, Unicode
1379 properties are used for all characters with more than one other case, and for
1380 all characters whose code points are greater than U+007F. For lower valued
1381 characters with only one other case, a lookup table is used for speed. When
1382 PCRE2_UTF is not set, a lookup table is used for all code points less than 256,
1383 and higher code points (available only in 16-bit or 32-bit mode) are treated as
1384 not having another case.
1385 .sp
1386   PCRE2_DOLLAR_ENDONLY
1387 .sp
1388 If this bit is set, a dollar metacharacter in the pattern matches only at the
1389 end of the subject string. Without this option, a dollar also matches
1390 immediately before a newline at the end of the string (but not before any other
1391 newlines). The PCRE2_DOLLAR_ENDONLY option is ignored if PCRE2_MULTILINE is
1392 set. There is no equivalent to this option in Perl, and no way to set it within
1393 a pattern.
1394 .sp
1395   PCRE2_DOTALL
1396 .sp
1397 If this bit is set, a dot metacharacter in the pattern matches any character,
1398 including one that indicates a newline. However, it only ever matches one
1399 character, even if newlines are coded as CRLF. Without this option, a dot does
1400 not match when the current position in the subject is at a newline. This option
1401 is equivalent to Perl's /s option, and it can be changed within a pattern by a
1402 (?s) option setting. A negative class such as [^a] always matches newline
1403 characters, and the \eN escape sequence always matches a non-newline character,
1404 independent of the setting of PCRE2_DOTALL.
1405 .sp
1406   PCRE2_DUPNAMES
1407 .sp
1408 If this bit is set, names used to identify capturing subpatterns need not be
1409 unique. This can be helpful for certain types of pattern when it is known that
1410 only one instance of the named subpattern can ever be matched. There are more
1411 details of named subpatterns below; see also the
1412 .\" HREF
1413 \fBpcre2pattern\fP
1414 .\"
1415 documentation.
1416 .sp
1417   PCRE2_ENDANCHORED
1418 .sp
1419 If this bit is set, the end of any pattern match must be right at the end of
1420 the string being searched (the "subject string"). If the pattern match
1421 succeeds by reaching (*ACCEPT), but does not reach the end of the subject, the
1422 match fails at the current starting point. For unanchored patterns, a new match
1423 is then tried at the next starting point. However, if the match succeeds by
1424 reaching the end of the pattern, but not the end of the subject, backtracking
1425 occurs and an alternative match may be found. Consider these two patterns:
1426 .sp
1427   .(*ACCEPT)|..
1428   .|..
1429 .sp
1430 If matched against "abc" with PCRE2_ENDANCHORED set, the first matches "c"
1431 whereas the second matches "bc". The effect of PCRE2_ENDANCHORED can also be
1432 achieved by appropriate constructs in the pattern itself, which is the only way
1433 to do it in Perl.
1434 .P
1435 For DFA matching with \fBpcre2_dfa_match()\fP, PCRE2_ENDANCHORED applies only
1436 to the first (that is, the longest) matched string. Other parallel matches,
1437 which are necessarily substrings of the first one, must obviously end before
1438 the end of the subject.
1439 .sp
1440   PCRE2_EXTENDED
1441 .sp
1442 If this bit is set, most white space characters in the pattern are totally
1443 ignored except when escaped or inside a character class. However, white space
1444 is not allowed within sequences such as (?> that introduce various
1445 parenthesized subpatterns, nor within numerical quantifiers such as {1,3}.
1446 Ignorable white space is permitted between an item and a following quantifier
1447 and between a quantifier and a following + that indicates possessiveness.
1448 PCRE2_EXTENDED is equivalent to Perl's /x option, and it can be changed within
1449 a pattern by a (?x) option setting.
1450 .P
1451 When PCRE2 is compiled without Unicode support, PCRE2_EXTENDED recognizes as
1452 white space only those characters with code points less than 256 that are
1453 flagged as white space in its low-character table. The table is normally
1454 created by
1455 .\" HREF
1456 \fBpcre2_maketables()\fP,
1457 .\"
1458 which uses the \fBisspace()\fP function to identify space characters. In most
1459 ASCII environments, the relevant characters are those with code points 0x0009
1460 (tab), 0x000A (linefeed), 0x000B (vertical tab), 0x000C (formfeed), 0x000D
1461 (carriage return), and 0x0020 (space).
1462 .P
1463 When PCRE2 is compiled with Unicode support, in addition to these characters,
1464 five more Unicode "Pattern White Space" characters are recognized by
1465 PCRE2_EXTENDED. These are U+0085 (next line), U+200E (left-to-right mark),
1466 U+200F (right-to-left mark), U+2028 (line separator), and U+2029 (paragraph
1467 separator). This set of characters is the same as recognized by Perl's /x
1468 option. Note that the horizontal and vertical space characters that are matched
1469 by the \eh and \ev escapes in patterns are a much bigger set.
1470 .P
1471 As well as ignoring most white space, PCRE2_EXTENDED also causes characters
1472 between an unescaped # outside a character class and the next newline,
1473 inclusive, to be ignored, which makes it possible to include comments inside
1474 complicated patterns. Note that the end of this type of comment is a literal
1475 newline sequence in the pattern; escape sequences that happen to represent a
1476 newline do not count.
1477 .P
1478 Which characters are interpreted as newlines can be specified by a setting in
1479 the compile context that is passed to \fBpcre2_compile()\fP or by a special
1480 sequence at the start of the pattern, as described in the section entitled
1481 .\" HTML <a href="pcre2pattern.html#newlines">
1482 .\" </a>
1483 "Newline conventions"
1484 .\"
1485 in the \fBpcre2pattern\fP documentation. A default is defined when PCRE2 is
1486 built.
1487 .sp
1488   PCRE2_EXTENDED_MORE
1489 .sp
1490 This option has the effect of PCRE2_EXTENDED, but, in addition, unescaped space
1491 and horizontal tab characters are ignored inside a character class. Note: only
1492 these two characters are ignored, not the full set of pattern white space
1493 characters that are ignored outside a character class. PCRE2_EXTENDED_MORE is
1494 equivalent to Perl's /xx option, and it can be changed within a pattern by a
1495 (?xx) option setting.
1496 .sp
1497   PCRE2_FIRSTLINE
1498 .sp
1499 If this option is set, the start of an unanchored pattern match must be before
1500 or at the first newline in the subject string following the start of matching,
1501 though the matched text may continue over the newline. If \fIstartoffset\fP is
1502 non-zero, the limiting newline is not necessarily the first newline in the
1503 subject. For example, if the subject string is "abc\enxyz" (where \en
1504 represents a single-character newline) a pattern match for "yz" succeeds with
1505 PCRE2_FIRSTLINE if \fIstartoffset\fP is greater than 3. See also
1506 PCRE2_USE_OFFSET_LIMIT, which provides a more general limiting facility. If
1507 PCRE2_FIRSTLINE is set with an offset limit, a match must occur in the first
1508 line and also within the offset limit. In other words, whichever limit comes
1509 first is used.
1510 .sp
1511   PCRE2_LITERAL
1512 .sp
1513 If this option is set, all meta-characters in the pattern are disabled, and it
1514 is treated as a literal string. Matching literal strings with a regular
1515 expression engine is not the most efficient way of doing it. If you are doing a
1516 lot of literal matching and are worried about efficiency, you should consider
1517 using other approaches. The only other main options that are allowed with
1518 PCRE2_LITERAL are: PCRE2_ANCHORED, PCRE2_ENDANCHORED, PCRE2_AUTO_CALLOUT,
1519 PCRE2_CASELESS, PCRE2_FIRSTLINE, PCRE2_NO_START_OPTIMIZE, PCRE2_NO_UTF_CHECK,
1520 PCRE2_UTF, and PCRE2_USE_OFFSET_LIMIT. The extra options PCRE2_EXTRA_MATCH_LINE
1521 and PCRE2_EXTRA_MATCH_WORD are also supported. Any other options cause an
1522 error.
1523 .sp
1524   PCRE2_MATCH_UNSET_BACKREF
1525 .sp
1526 If this option is set, a backreference to an unset subpattern group matches an
1527 empty string (by default this causes the current matching alternative to fail).
1528 A pattern such as (\e1)(a) succeeds when this option is set (assuming it can
1529 find an "a" in the subject), whereas it fails by default, for Perl
1530 compatibility. Setting this option makes PCRE2 behave more like ECMAscript (aka
1531 JavaScript).
1532 .sp
1533   PCRE2_MULTILINE
1534 .sp
1535 By default, for the purposes of matching "start of line" and "end of line",
1536 PCRE2 treats the subject string as consisting of a single line of characters,
1537 even if it actually contains newlines. The "start of line" metacharacter (^)
1538 matches only at the start of the string, and the "end of line" metacharacter
1539 ($) matches only at the end of the string, or before a terminating newline
1540 (except when PCRE2_DOLLAR_ENDONLY is set). Note, however, that unless
1541 PCRE2_DOTALL is set, the "any character" metacharacter (.) does not match at a
1542 newline. This behaviour (for ^, $, and dot) is the same as Perl.
1543 .P
1544 When PCRE2_MULTILINE it is set, the "start of line" and "end of line"
1545 constructs match immediately following or immediately before internal newlines
1546 in the subject string, respectively, as well as at the very start and end. This
1547 is equivalent to Perl's /m option, and it can be changed within a pattern by a
1548 (?m) option setting. Note that the "start of line" metacharacter does not match
1549 after a newline at the end of the subject, for compatibility with Perl.
1550 However, you can change this by setting the PCRE2_ALT_CIRCUMFLEX option. If
1551 there are no newlines in a subject string, or no occurrences of ^ or $ in a
1552 pattern, setting PCRE2_MULTILINE has no effect.
1553 .sp
1554   PCRE2_NEVER_BACKSLASH_C
1555 .sp
1556 This option locks out the use of \eC in the pattern that is being compiled.
1557 This escape can cause unpredictable behaviour in UTF-8 or UTF-16 modes, because
1558 it may leave the current matching point in the middle of a multi-code-unit
1559 character. This option may be useful in applications that process patterns from
1560 external sources. Note that there is also a build-time option that permanently
1561 locks out the use of \eC.
1562 .sp
1563   PCRE2_NEVER_UCP
1564 .sp
1565 This option locks out the use of Unicode properties for handling \eB, \eb, \eD,
1566 \ed, \eS, \es, \eW, \ew, and some of the POSIX character classes, as described
1567 for the PCRE2_UCP option below. In particular, it prevents the creator of the
1568 pattern from enabling this facility by starting the pattern with (*UCP). This
1569 option may be useful in applications that process patterns from external
1570 sources. The option combination PCRE_UCP and PCRE_NEVER_UCP causes an error.
1571 .sp
1572   PCRE2_NEVER_UTF
1573 .sp
1574 This option locks out interpretation of the pattern as UTF-8, UTF-16, or
1575 UTF-32, depending on which library is in use. In particular, it prevents the
1576 creator of the pattern from switching to UTF interpretation by starting the
1577 pattern with (*UTF). This option may be useful in applications that process
1578 patterns from external sources. The combination of PCRE2_UTF and
1579 PCRE2_NEVER_UTF causes an error.
1580 .sp
1581   PCRE2_NO_AUTO_CAPTURE
1582 .sp
1583 If this option is set, it disables the use of numbered capturing parentheses in
1584 the pattern. Any opening parenthesis that is not followed by ? behaves as if it
1585 were followed by ?: but named parentheses can still be used for capturing (and
1586 they acquire numbers in the usual way). This is the same as Perl's /n option.
1587 Note that, when this option is set, references to capturing groups
1588 (backreferences or recursion/subroutine calls) may only refer to named groups,
1589 though the reference can be by name or by number.
1590 .sp
1591   PCRE2_NO_AUTO_POSSESS
1592 .sp
1593 If this option is set, it disables "auto-possessification", which is an
1594 optimization that, for example, turns a+b into a++b in order to avoid
1595 backtracks into a+ that can never be successful. However, if callouts are in
1596 use, auto-possessification means that some callouts are never taken. You can
1597 set this option if you want the matching functions to do a full unoptimized
1598 search and run all the callouts, but it is mainly provided for testing
1599 purposes.
1600 .sp
1601   PCRE2_NO_DOTSTAR_ANCHOR
1602 .sp
1603 If this option is set, it disables an optimization that is applied when .* is
1604 the first significant item in a top-level branch of a pattern, and all the
1605 other branches also start with .* or with \eA or \eG or ^. The optimization is
1606 automatically disabled for .* if it is inside an atomic group or a capturing
1607 group that is the subject of a backreference, or if the pattern contains
1608 (*PRUNE) or (*SKIP). When the optimization is not disabled, such a pattern is
1609 automatically anchored if PCRE2_DOTALL is set for all the .* items and
1610 PCRE2_MULTILINE is not set for any ^ items. Otherwise, the fact that any match
1611 must start either at the start of the subject or following a newline is
1612 remembered. Like other optimizations, this can cause callouts to be skipped.
1613 .sp
1614   PCRE2_NO_START_OPTIMIZE
1615 .sp
1616 This is an option whose main effect is at matching time. It does not change
1617 what \fBpcre2_compile()\fP generates, but it does affect the output of the JIT
1618 compiler.
1619 .P
1620 There are a number of optimizations that may occur at the start of a match, in
1621 order to speed up the process. For example, if it is known that an unanchored
1622 match must start with a specific code unit value, the matching code searches
1623 the subject for that value, and fails immediately if it cannot find it, without
1624 actually running the main matching function. This means that a special item
1625 such as (*COMMIT) at the start of a pattern is not considered until after a
1626 suitable starting point for the match has been found. Also, when callouts or
1627 (*MARK) items are in use, these "start-up" optimizations can cause them to be
1628 skipped if the pattern is never actually used. The start-up optimizations are
1629 in effect a pre-scan of the subject that takes place before the pattern is run.
1630 .P
1631 The PCRE2_NO_START_OPTIMIZE option disables the start-up optimizations,
1632 possibly causing performance to suffer, but ensuring that in cases where the
1633 result is "no match", the callouts do occur, and that items such as (*COMMIT)
1634 and (*MARK) are considered at every possible starting position in the subject
1635 string.
1636 .P
1637 Setting PCRE2_NO_START_OPTIMIZE may change the outcome of a matching operation.
1638 Consider the pattern
1639 .sp
1640   (*COMMIT)ABC
1641 .sp
1642 When this is compiled, PCRE2 records the fact that a match must start with the
1643 character "A". Suppose the subject string is "DEFABC". The start-up
1644 optimization scans along the subject, finds "A" and runs the first match
1645 attempt from there. The (*COMMIT) item means that the pattern must match the
1646 current starting position, which in this case, it does. However, if the same
1647 match is run with PCRE2_NO_START_OPTIMIZE set, the initial scan along the
1648 subject string does not happen. The first match attempt is run starting from
1649 "D" and when this fails, (*COMMIT) prevents any further matches being tried, so
1650 the overall result is "no match".
1651 .P
1652 There are also other start-up optimizations. For example, a minimum length for
1653 the subject may be recorded. Consider the pattern
1654 .sp
1655   (*MARK:A)(X|Y)
1656 .sp
1657 The minimum length for a match is one character. If the subject is "ABC", there
1658 will be attempts to match "ABC", "BC", and "C". An attempt to match an empty
1659 string at the end of the subject does not take place, because PCRE2 knows that
1660 the subject is now too short, and so the (*MARK) is never encountered. In this
1661 case, the optimization does not affect the overall match result, which is still
1662 "no match", but it does affect the auxiliary information that is returned.
1663 .sp
1664   PCRE2_NO_UTF_CHECK
1665 .sp
1666 When PCRE2_UTF is set, the validity of the pattern as a UTF string is
1667 automatically checked. There are discussions about the validity of
1668 .\" HTML <a href="pcre2unicode.html#utf8strings">
1669 .\" </a>
1670 UTF-8 strings,
1671 .\"
1672 .\" HTML <a href="pcre2unicode.html#utf16strings">
1673 .\" </a>
1674 UTF-16 strings,
1675 .\"
1676 and
1677 .\" HTML <a href="pcre2unicode.html#utf32strings">
1678 .\" </a>
1679 UTF-32 strings
1680 .\"
1681 in the
1682 .\" HREF
1683 \fBpcre2unicode\fP
1684 .\"
1685 document. If an invalid UTF sequence is found, \fBpcre2_compile()\fP returns a
1686 negative error code.
1687 .P
1688 If you know that your pattern is a valid UTF string, and you want to skip this
1689 check for performance reasons, you can set the PCRE2_NO_UTF_CHECK option. When
1690 it is set, the effect of passing an invalid UTF string as a pattern is
1691 undefined. It may cause your program to crash or loop.
1692 .P
1693 Note that this option can also be passed to \fBpcre2_match()\fP and
1694 \fBpcre_dfa_match()\fP, to suppress UTF validity checking of the subject
1695 string.
1696 .P
1697 Note also that setting PCRE2_NO_UTF_CHECK at compile time does not disable the
1698 error that is given if an escape sequence for an invalid Unicode code point is
1699 encountered in the pattern. In particular, the so-called "surrogate" code
1700 points (0xd800 to 0xdfff) are invalid. If you want to allow escape sequences
1701 such as \ex{d800} you can set the PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES extra
1702 option, as described in the section entitled "Extra compile options"
1703 .\" HTML <a href="#extracompileoptions">
1704 .\" </a>
1705 below.
1706 .\"
1707 However, this is possible only in UTF-8 and UTF-32 modes, because these values
1708 are not representable in UTF-16.
1709 .sp
1710   PCRE2_UCP
1711 .sp
1712 This option changes the way PCRE2 processes \eB, \eb, \eD, \ed, \eS, \es, \eW,
1713 \ew, and some of the POSIX character classes. By default, only ASCII characters
1714 are recognized, but if PCRE2_UCP is set, Unicode properties are used instead to
1715 classify characters. More details are given in the section on
1716 .\" HTML <a href="pcre2pattern.html#genericchartypes">
1717 .\" </a>
1718 generic character types
1719 .\"
1720 in the
1721 .\" HREF
1722 \fBpcre2pattern\fP
1723 .\"
1724 page. If you set PCRE2_UCP, matching one of the items it affects takes much
1725 longer. The option is available only if PCRE2 has been compiled with Unicode
1726 support (which is the default).
1727 .sp
1728   PCRE2_UNGREEDY
1729 .sp
1730 This option inverts the "greediness" of the quantifiers so that they are not
1731 greedy by default, but become greedy if followed by "?". It is not compatible
1732 with Perl. It can also be set by a (?U) option setting within the pattern.
1733 .sp
1734   PCRE2_USE_OFFSET_LIMIT
1735 .sp
1736 This option must be set for \fBpcre2_compile()\fP if
1737 \fBpcre2_set_offset_limit()\fP is going to be used to set a non-default offset
1738 limit in a match context for matches that use this pattern. An error is
1739 generated if an offset limit is set without this option. For more details, see
1740 the description of \fBpcre2_set_offset_limit()\fP in the
1741 .\" HTML <a href="#matchcontext">
1742 .\" </a>
1743 section
1744 .\"
1745 that describes match contexts. See also the PCRE2_FIRSTLINE
1746 option above.
1747 .sp
1748   PCRE2_UTF
1749 .sp
1750 This option causes PCRE2 to regard both the pattern and the subject strings
1751 that are subsequently processed as strings of UTF characters instead of
1752 single-code-unit strings. It is available when PCRE2 is built to include
1753 Unicode support (which is the default). If Unicode support is not available,
1754 the use of this option provokes an error. Details of how PCRE2_UTF changes the
1755 behaviour of PCRE2 are given in the
1756 .\" HREF
1757 \fBpcre2unicode\fP
1758 .\"
1759 page. In particular, note that it changes the way PCRE2_CASELESS handles
1760 characters with code points greater than 127.
1761 .
1762 .
1763 .\" HTML <a name="extracompileoptions"></a>
1764 .SS "Extra compile options"
1765 .rs
1766 .sp
1767 Unlike the main compile-time options, the extra options are not saved with the
1768 compiled pattern. The option bits that can be set in a compile context by
1769 calling the \fBpcre2_set_compile_extra_options()\fP function are as follows:
1770 .sp
1771   PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES
1772 .sp
1773 This option applies when compiling a pattern in UTF-8 or UTF-32 mode. It is
1774 forbidden in UTF-16 mode, and ignored in non-UTF modes. Unicode "surrogate"
1775 code points in the range 0xd800 to 0xdfff are used in pairs in UTF-16 to encode
1776 code points with values in the range 0x10000 to 0x10ffff. The surrogates cannot
1777 therefore be represented in UTF-16. They can be represented in UTF-8 and
1778 UTF-32, but are defined as invalid code points, and cause errors if encountered
1779 in a UTF-8 or UTF-32 string that is being checked for validity by PCRE2.
1780 .P
1781 These values also cause errors if encountered in escape sequences such as
1782 \ex{d912} within a pattern. However, it seems that some applications, when
1783 using PCRE2 to check for unwanted characters in UTF-8 strings, explicitly test
1784 for the surrogates using escape sequences. The PCRE2_NO_UTF_CHECK option does
1785 not disable the error that occurs, because it applies only to the testing of
1786 input strings for UTF validity.
1787 .P
1788 If the extra option PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES is set, surrogate code
1789 point values in UTF-8 and UTF-32 patterns no longer provoke errors and are
1790 incorporated in the compiled pattern. However, they can only match subject
1791 characters if the matching function is called with PCRE2_NO_UTF_CHECK set.
1792 .sp
1793   PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL
1794 .sp
1795 This is a dangerous option. Use with care. By default, an unrecognized escape
1796 such as \ej or a malformed one such as \ex{2z} causes a compile-time error when
1797 detected by \fBpcre2_compile()\fP. Perl is somewhat inconsistent in handling
1798 such items: for example, \ej is treated as a literal "j", and non-hexadecimal
1799 digits in \ex{} are just ignored, though warnings are given in both cases if
1800 Perl's warning switch is enabled. However, a malformed octal number after \eo{
1801 always causes an error in Perl.
1802 .P
1803 If the PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL extra option is passed to
1804 \fBpcre2_compile()\fP, all unrecognized or erroneous escape sequences are
1805 treated as single-character escapes. For example, \ej is a literal "j" and
1806 \ex{2z} is treated as the literal string "x{2z}". Setting this option means
1807 that typos in patterns may go undetected and have unexpected results. This is a
1808 dangerous option. Use with care.
1809 .sp
1810   PCRE2_EXTRA_MATCH_LINE
1811 .sp
1812 This option is provided for use by the \fB-x\fP option of \fBpcre2grep\fP. It
1813 causes the pattern only to match complete lines. This is achieved by
1814 automatically inserting the code for "^(?:" at the start of the compiled
1815 pattern and ")$" at the end. Thus, when PCRE2_MULTILINE is set, the matched
1816 line may be in the middle of the subject string. This option can be used with
1817 PCRE2_LITERAL.
1818 .sp
1819   PCRE2_EXTRA_MATCH_WORD
1820 .sp
1821 This option is provided for use by the \fB-w\fP option of \fBpcre2grep\fP. It
1822 causes the pattern only to match strings that have a word boundary at the start
1823 and the end. This is achieved by automatically inserting the code for "\eb(?:"
1824 at the start of the compiled pattern and ")\eb" at the end. The option may be
1825 used with PCRE2_LITERAL. However, it is ignored if PCRE2_EXTRA_MATCH_LINE is
1826 also set.
1827 .
1828 .
1829 .\" HTML <a name="jitcompiling"></a>
1830 .SH "JUST-IN-TIME (JIT) COMPILATION"
1831 .rs
1832 .sp
1833 .nf
1834 .B int pcre2_jit_compile(pcre2_code *\fIcode\fP, uint32_t \fIoptions\fP);
1835 .sp
1836 .B int pcre2_jit_match(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
1837 .B "  PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
1838 .B "  uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
1839 .B "  pcre2_match_context *\fImcontext\fP);"
1840 .sp
1841 .B void pcre2_jit_free_unused_memory(pcre2_general_context *\fIgcontext\fP);
1842 .sp
1843 .B pcre2_jit_stack *pcre2_jit_stack_create(PCRE2_SIZE \fIstartsize\fP,
1844 .B "  PCRE2_SIZE \fImaxsize\fP, pcre2_general_context *\fIgcontext\fP);"
1845 .sp
1846 .B void pcre2_jit_stack_assign(pcre2_match_context *\fImcontext\fP,
1847 .B "  pcre2_jit_callback \fIcallback_function\fP, void *\fIcallback_data\fP);"
1848 .sp
1849 .B void pcre2_jit_stack_free(pcre2_jit_stack *\fIjit_stack\fP);
1850 .fi
1851 .P
1852 These functions provide support for JIT compilation, which, if the just-in-time
1853 compiler is available, further processes a compiled pattern into machine code
1854 that executes much faster than the \fBpcre2_match()\fP interpretive matching
1855 function. Full details are given in the
1856 .\" HREF
1857 \fBpcre2jit\fP
1858 .\"
1859 documentation.
1860 .P
1861 JIT compilation is a heavyweight optimization. It can take some time for
1862 patterns to be analyzed, and for one-off matches and simple patterns the
1863 benefit of faster execution might be offset by a much slower compilation time.
1864 Most (but not all) patterns can be optimized by the JIT compiler.
1865 .
1866 .
1867 .\" HTML <a name="localesupport"></a>
1868 .SH "LOCALE SUPPORT"
1869 .rs
1870 .sp
1871 PCRE2 handles caseless matching, and determines whether characters are letters,
1872 digits, or whatever, by reference to a set of tables, indexed by character code
1873 point. This applies only to characters whose code points are less than 256. By
1874 default, higher-valued code points never match escapes such as \ew or \ed.
1875 However, if PCRE2 is built with Unicode support, all characters can be tested
1876 with \ep and \eP, or, alternatively, the PCRE2_UCP option can be set when a
1877 pattern is compiled; this causes \ew and friends to use Unicode property
1878 support instead of the built-in tables.
1879 .P
1880 The use of locales with Unicode is discouraged. If you are handling characters
1881 with code points greater than 128, you should either use Unicode support, or
1882 use locales, but not try to mix the two.
1883 .P
1884 PCRE2 contains an internal set of character tables that are used by default.
1885 These are sufficient for many applications. Normally, the internal tables
1886 recognize only ASCII characters. However, when PCRE2 is built, it is possible
1887 to cause the internal tables to be rebuilt in the default "C" locale of the
1888 local system, which may cause them to be different.
1889 .P
1890 The internal tables can be overridden by tables supplied by the application
1891 that calls PCRE2. These may be created in a different locale from the default.
1892 As more and more applications change to using Unicode, the need for this locale
1893 support is expected to die away.
1894 .P
1895 External tables are built by calling the \fBpcre2_maketables()\fP function, in
1896 the relevant locale. The result can be passed to \fBpcre2_compile()\fP as often
1897 as necessary, by creating a compile context and calling
1898 \fBpcre2_set_character_tables()\fP to set the tables pointer therein. For
1899 example, to build and use tables that are appropriate for the French locale
1900 (where accented characters with values greater than 128 are treated as
1901 letters), the following code could be used:
1902 .sp
1903   setlocale(LC_CTYPE, "fr_FR");
1904   tables = pcre2_maketables(NULL);
1905   ccontext = pcre2_compile_context_create(NULL);
1906   pcre2_set_character_tables(ccontext, tables);
1907   re = pcre2_compile(..., ccontext);
1908 .sp
1909 The locale name "fr_FR" is used on Linux and other Unix-like systems; if you
1910 are using Windows, the name for the French locale is "french". It is the
1911 caller's responsibility to ensure that the memory containing the tables remains
1912 available for as long as it is needed.
1913 .P
1914 The pointer that is passed (via the compile context) to \fBpcre2_compile()\fP
1915 is saved with the compiled pattern, and the same tables are used by
1916 \fBpcre2_match()\fP and \fBpcre_dfa_match()\fP. Thus, for any single pattern,
1917 compilation and matching both happen in the same locale, but different patterns
1918 can be processed in different locales.
1919 .
1920 .
1921 .\" HTML <a name="infoaboutpattern"></a>
1922 .SH "INFORMATION ABOUT A COMPILED PATTERN"
1923 .rs
1924 .sp
1925 .nf
1926 .B int pcre2_pattern_info(const pcre2 *\fIcode\fP, uint32_t \fIwhat\fP, void *\fIwhere\fP);
1927 .fi
1928 .P
1929 The \fBpcre2_pattern_info()\fP function returns general information about a
1930 compiled pattern. For information about callouts, see the
1931 .\" HTML <a href="pcre2pattern.html#infoaboutcallouts">
1932 .\" </a>
1933 next section.
1934 .\"
1935 The first argument for \fBpcre2_pattern_info()\fP is a pointer to the compiled
1936 pattern. The second argument specifies which piece of information is required,
1937 and the third argument is a pointer to a variable to receive the data. If the
1938 third argument is NULL, the first argument is ignored, and the function returns
1939 the size in bytes of the variable that is required for the information
1940 requested. Otherwise, the yield of the function is zero for success, or one of
1941 the following negative numbers:
1942 .sp
1943   PCRE2_ERROR_NULL           the argument \fIcode\fP was NULL
1944   PCRE2_ERROR_BADMAGIC       the "magic number" was not found
1945   PCRE2_ERROR_BADOPTION      the value of \fIwhat\fP was invalid
1946   PCRE2_ERROR_UNSET          the requested field is not set
1947 .sp
1948 The "magic number" is placed at the start of each compiled pattern as an simple
1949 check against passing an arbitrary memory pointer. Here is a typical call of
1950 \fBpcre2_pattern_info()\fP, to obtain the length of the compiled pattern:
1951 .sp
1952   int rc;
1953   size_t length;
1954   rc = pcre2_pattern_info(
1955     re,               /* result of pcre2_compile() */
1956     PCRE2_INFO_SIZE,  /* what is required */
1957     &length);         /* where to put the data */
1958 .sp
1959 The possible values for the second argument are defined in \fBpcre2.h\fP, and
1960 are as follows:
1961 .sp
1962   PCRE2_INFO_ALLOPTIONS
1963   PCRE2_INFO_ARGOPTIONS
1964   PCRE2_INFO_EXTRAOPTIONS
1965 .sp
1966 Return copies of the pattern's options. The third argument should point to a
1967 \fBuint32_t\fP variable. PCRE2_INFO_ARGOPTIONS returns exactly the options that
1968 were passed to \fBpcre2_compile()\fP, whereas PCRE2_INFO_ALLOPTIONS returns
1969 the compile options as modified by any top-level (*XXX) option settings such as
1970 (*UTF) at the start of the pattern itself. PCRE2_INFO_EXTRAOPTIONS returns the
1971 extra options that were set in the compile context by calling the
1972 pcre2_set_compile_extra_options() function.
1973 .P
1974 For example, if the pattern /(*UTF)abc/ is compiled with the PCRE2_EXTENDED
1975 option, the result for PCRE2_INFO_ALLOPTIONS is PCRE2_EXTENDED and PCRE2_UTF.
1976 Option settings such as (?i) that can change within a pattern do not affect the
1977 result of PCRE2_INFO_ALLOPTIONS, even if they appear right at the start of the
1978 pattern. (This was different in some earlier releases.)
1979 .P
1980 A pattern compiled without PCRE2_ANCHORED is automatically anchored by PCRE2 if
1981 the first significant item in every top-level branch is one of the following:
1982 .sp
1983   ^     unless PCRE2_MULTILINE is set
1984   \eA    always
1985   \eG    always
1986   .*    sometimes - see below
1987 .sp
1988 When .* is the first significant item, anchoring is possible only when all the
1989 following are true:
1990 .sp
1991   .* is not in an atomic group
1992 .\" JOIN
1993   .* is not in a capturing group that is the subject
1994        of a backreference
1995   PCRE2_DOTALL is in force for .*
1996   Neither (*PRUNE) nor (*SKIP) appears in the pattern
1997   PCRE2_NO_DOTSTAR_ANCHOR is not set
1998 .sp
1999 For patterns that are auto-anchored, the PCRE2_ANCHORED bit is set in the
2000 options returned for PCRE2_INFO_ALLOPTIONS.
2001 .sp
2002   PCRE2_INFO_BACKREFMAX
2003 .sp
2004 Return the number of the highest backreference in the pattern. The third
2005 argument should point to an \fBuint32_t\fP variable. Named subpatterns acquire
2006 numbers as well as names, and these count towards the highest backreference.
2007 Backreferences such as \e4 or \eg{12} match the captured characters of the
2008 given group, but in addition, the check that a capturing group is set in a
2009 conditional subpattern such as (?(3)a|b) is also a backreference. Zero is
2010 returned if there are no backreferences.
2011 .sp
2012   PCRE2_INFO_BSR
2013 .sp
2014 The output is a uint32_t integer whose value indicates what character sequences
2015 the \eR escape sequence matches. A value of PCRE2_BSR_UNICODE means that \eR
2016 matches any Unicode line ending sequence; a value of PCRE2_BSR_ANYCRLF means
2017 that \eR matches only CR, LF, or CRLF.
2018 .sp
2019   PCRE2_INFO_CAPTURECOUNT
2020 .sp
2021 Return the highest capturing subpattern number in the pattern. In patterns
2022 where (?| is not used, this is also the total number of capturing subpatterns.
2023 The third argument should point to an \fBuint32_t\fP variable.
2024 .sp
2025   PCRE2_INFO_DEPTHLIMIT
2026 .sp
2027 If the pattern set a backtracking depth limit by including an item of the form
2028 (*LIMIT_DEPTH=nnnn) at the start, the value is returned. The third argument
2029 should point to a uint32_t integer. If no such value has been set, the call to
2030 \fBpcre2_pattern_info()\fP returns the error PCRE2_ERROR_UNSET. Note that this
2031 limit will only be used during matching if it is less than the limit set or
2032 defaulted by the caller of the match function.
2033 .sp
2034   PCRE2_INFO_FIRSTBITMAP
2035 .sp
2036 In the absence of a single first code unit for a non-anchored pattern,
2037 \fBpcre2_compile()\fP may construct a 256-bit table that defines a fixed set of
2038 values for the first code unit in any match. For example, a pattern that starts
2039 with [abc] results in a table with three bits set. When code unit values
2040 greater than 255 are supported, the flag bit for 255 means "any code unit of
2041 value 255 or above". If such a table was constructed, a pointer to it is
2042 returned. Otherwise NULL is returned. The third argument should point to a
2043 \fBconst uint8_t *\fP variable.
2044 .sp
2045   PCRE2_INFO_FIRSTCODETYPE
2046 .sp
2047 Return information about the first code unit of any matched string, for a
2048 non-anchored pattern. The third argument should point to an \fBuint32_t\fP
2049 variable. If there is a fixed first value, for example, the letter "c" from a
2050 pattern such as (cat|cow|coyote), 1 is returned, and the value can be retrieved
2051 using PCRE2_INFO_FIRSTCODEUNIT. If there is no fixed first value, but it is
2052 known that a match can occur only at the start of the subject or following a
2053 newline in the subject, 2 is returned. Otherwise, and for anchored patterns, 0
2054 is returned.
2055 .sp
2056   PCRE2_INFO_FIRSTCODEUNIT
2057 .sp
2058 Return the value of the first code unit of any matched string for a pattern
2059 where PCRE2_INFO_FIRSTCODETYPE returns 1; otherwise return 0. The third
2060 argument should point to an \fBuint32_t\fP variable. In the 8-bit library, the
2061 value is always less than 256. In the 16-bit library the value can be up to
2062 0xffff. In the 32-bit library in UTF-32 mode the value can be up to 0x10ffff,
2063 and up to 0xffffffff when not using UTF-32 mode.
2064 .sp
2065   PCRE2_INFO_FRAMESIZE
2066 .sp
2067 Return the size (in bytes) of the data frames that are used to remember
2068 backtracking positions when the pattern is processed by \fBpcre2_match()\fP
2069 without the use of JIT. The third argument should point to a \fBsize_t\fP
2070 variable. The frame size depends on the number of capturing parentheses in the
2071 pattern. Each additional capturing group adds two PCRE2_SIZE variables.
2072 .sp
2073   PCRE2_INFO_HASBACKSLASHC
2074 .sp
2075 Return 1 if the pattern contains any instances of \eC, otherwise 0. The third
2076 argument should point to an \fBuint32_t\fP variable.
2077 .sp
2078   PCRE2_INFO_HASCRORLF
2079 .sp
2080 Return 1 if the pattern contains any explicit matches for CR or LF characters,
2081 otherwise 0. The third argument should point to an \fBuint32_t\fP variable. An
2082 explicit match is either a literal CR or LF character, or \er or \en or one of
2083 the equivalent hexadecimal or octal escape sequences.
2084 .sp
2085   PCRE2_INFO_HEAPLIMIT
2086 .sp
2087 If the pattern set a heap memory limit by including an item of the form
2088 (*LIMIT_HEAP=nnnn) at the start, the value is returned. The third argument
2089 should point to a uint32_t integer. If no such value has been set, the call to
2090 \fBpcre2_pattern_info()\fP returns the error PCRE2_ERROR_UNSET. Note that this
2091 limit will only be used during matching if it is less than the limit set or
2092 defaulted by the caller of the match function.
2093 .sp
2094   PCRE2_INFO_JCHANGED
2095 .sp
2096 Return 1 if the (?J) or (?-J) option setting is used in the pattern, otherwise
2097 0. The third argument should point to an \fBuint32_t\fP variable. (?J) and
2098 (?-J) set and unset the local PCRE2_DUPNAMES option, respectively.
2099 .sp
2100   PCRE2_INFO_JITSIZE
2101 .sp
2102 If the compiled pattern was successfully processed by
2103 \fBpcre2_jit_compile()\fP, return the size of the JIT compiled code, otherwise
2104 return zero. The third argument should point to a \fBsize_t\fP variable.
2105 .sp
2106   PCRE2_INFO_LASTCODETYPE
2107 .sp
2108 Returns 1 if there is a rightmost literal code unit that must exist in any
2109 matched string, other than at its start. The third argument should  point to an
2110 \fBuint32_t\fP variable. If there is no such value, 0 is returned. When 1 is
2111 returned, the code unit value itself can be retrieved using
2112 PCRE2_INFO_LASTCODEUNIT. For anchored patterns, a last literal value is
2113 recorded only if it follows something of variable length. For example, for the
2114 pattern /^a\ed+z\ed+/ the returned value is 1 (with "z" returned from
2115 PCRE2_INFO_LASTCODEUNIT), but for /^a\edz\ed/ the returned value is 0.
2116 .sp
2117   PCRE2_INFO_LASTCODEUNIT
2118 .sp
2119 Return the value of the rightmost literal code unit that must exist in any
2120 matched string, other than at its start, for a pattern where
2121 PCRE2_INFO_LASTCODETYPE returns 1. Otherwise, return 0. The third argument
2122 should point to an \fBuint32_t\fP variable.
2123 .sp
2124   PCRE2_INFO_MATCHEMPTY
2125 .sp
2126 Return 1 if the pattern might match an empty string, otherwise 0. The third
2127 argument should point to an \fBuint32_t\fP variable. When a pattern contains
2128 recursive subroutine calls it is not always possible to determine whether or
2129 not it can match an empty string. PCRE2 takes a cautious approach and returns 1
2130 in such cases.
2131 .sp
2132   PCRE2_INFO_MATCHLIMIT
2133 .sp
2134 If the pattern set a match limit by including an item of the form
2135 (*LIMIT_MATCH=nnnn) at the start, the value is returned. The third argument
2136 should point to a uint32_t integer. If no such value has been set, the call to
2137 \fBpcre2_pattern_info()\fP returns the error PCRE2_ERROR_UNSET. Note that this
2138 limit will only be used during matching if it is less than the limit set or
2139 defaulted by the caller of the match function.
2140 .sp
2141   PCRE2_INFO_MAXLOOKBEHIND
2142 .sp
2143 Return the number of characters (not code units) in the longest lookbehind
2144 assertion in the pattern. The third argument should point to a uint32_t
2145 integer. This information is useful when doing multi-segment matching using the
2146 partial matching facilities. Note that the simple assertions \eb and \eB
2147 require a one-character lookbehind. \eA also registers a one-character
2148 lookbehind, though it does not actually inspect the previous character. This is
2149 to ensure that at least one character from the old segment is retained when a
2150 new segment is processed. Otherwise, if there are no lookbehinds in the
2151 pattern, \eA might match incorrectly at the start of a second or subsequent
2152 segment.
2153 .sp
2154   PCRE2_INFO_MINLENGTH
2155 .sp
2156 If a minimum length for matching subject strings was computed, its value is
2157 returned. Otherwise the returned value is 0. The value is a number of
2158 characters, which in UTF mode may be different from the number of code units.
2159 The third argument should point to an \fBuint32_t\fP variable. The value is a
2160 lower bound to the length of any matching string. There may not be any strings
2161 of that length that do actually match, but every string that does match is at
2162 least that long.
2163 .sp
2164   PCRE2_INFO_NAMECOUNT
2165   PCRE2_INFO_NAMEENTRYSIZE
2166   PCRE2_INFO_NAMETABLE
2167 .sp
2168 PCRE2 supports the use of named as well as numbered capturing parentheses. The
2169 names are just an additional way of identifying the parentheses, which still
2170 acquire numbers. Several convenience functions such as
2171 \fBpcre2_substring_get_byname()\fP are provided for extracting captured
2172 substrings by name. It is also possible to extract the data directly, by first
2173 converting the name to a number in order to access the correct pointers in the
2174 output vector (described with \fBpcre2_match()\fP below). To do the conversion,
2175 you need to use the name-to-number map, which is described by these three
2176 values.
2177 .P
2178 The map consists of a number of fixed-size entries. PCRE2_INFO_NAMECOUNT gives
2179 the number of entries, and PCRE2_INFO_NAMEENTRYSIZE gives the size of each
2180 entry in code units; both of these return a \fBuint32_t\fP value. The entry
2181 size depends on the length of the longest name.
2182 .P
2183 PCRE2_INFO_NAMETABLE returns a pointer to the first entry of the table. This is
2184 a PCRE2_SPTR pointer to a block of code units. In the 8-bit library, the first
2185 two bytes of each entry are the number of the capturing parenthesis, most
2186 significant byte first. In the 16-bit library, the pointer points to 16-bit
2187 code units, the first of which contains the parenthesis number. In the 32-bit
2188 library, the pointer points to 32-bit code units, the first of which contains
2189 the parenthesis number. The rest of the entry is the corresponding name, zero
2190 terminated.
2191 .P
2192 The names are in alphabetical order. If (?| is used to create multiple groups
2193 with the same number, as described in the
2194 .\" HTML <a href="pcre2pattern.html#dupsubpatternnumber">
2195 .\" </a>
2196 section on duplicate subpattern numbers
2197 .\"
2198 in the
2199 .\" HREF
2200 \fBpcre2pattern\fP
2201 .\"
2202 page, the groups may be given the same name, but there is only one entry in the
2203 table. Different names for groups of the same number are not permitted.
2204 .P
2205 Duplicate names for subpatterns with different numbers are permitted, but only
2206 if PCRE2_DUPNAMES is set. They appear in the table in the order in which they
2207 were found in the pattern. In the absence of (?| this is the order of
2208 increasing number; when (?| is used this is not necessarily the case because
2209 later subpatterns may have lower numbers.
2210 .P
2211 As a simple example of the name/number table, consider the following pattern
2212 after compilation by the 8-bit library (assume PCRE2_EXTENDED is set, so white
2213 space - including newlines - is ignored):
2214 .sp
2215 .\" JOIN
2216   (?<date> (?<year>(\ed\ed)?\ed\ed) -
2217   (?<month>\ed\ed) - (?<day>\ed\ed) )
2218 .sp
2219 There are four named subpatterns, so the table has four entries, and each entry
2220 in the table is eight bytes long. The table is as follows, with non-printing
2221 bytes shows in hexadecimal, and undefined bytes shown as ??:
2222 .sp
2223   00 01 d  a  t  e  00 ??
2224   00 05 d  a  y  00 ?? ??
2225   00 04 m  o  n  t  h  00
2226   00 02 y  e  a  r  00 ??
2227 .sp
2228 When writing code to extract data from named subpatterns using the
2229 name-to-number map, remember that the length of the entries is likely to be
2230 different for each compiled pattern.
2231 .sp
2232   PCRE2_INFO_NEWLINE
2233 .sp
2234 The output is one of the following \fBuint32_t\fP values:
2235 .sp
2236   PCRE2_NEWLINE_CR       Carriage return (CR)
2237   PCRE2_NEWLINE_LF       Linefeed (LF)
2238   PCRE2_NEWLINE_CRLF     Carriage return, linefeed (CRLF)
2239   PCRE2_NEWLINE_ANY      Any Unicode line ending
2240   PCRE2_NEWLINE_ANYCRLF  Any of CR, LF, or CRLF
2241   PCRE2_NEWLINE_NUL      The NUL character (binary zero)
2242 .sp
2243 This identifies the character sequence that will be recognized as meaning
2244 "newline" while matching.
2245 .sp
2246   PCRE2_INFO_SIZE
2247 .sp
2248 Return the size of the compiled pattern in bytes (for all three libraries). The
2249 third argument should point to a \fBsize_t\fP variable. This value includes the
2250 size of the general data block that precedes the code units of the compiled
2251 pattern itself. The value that is used when \fBpcre2_compile()\fP is getting
2252 memory in which to place the compiled pattern may be slightly larger than the
2253 value returned by this option, because there are cases where the code that
2254 calculates the size has to over-estimate. Processing a pattern with the JIT
2255 compiler does not alter the value returned by this option.
2256 .
2257 .
2258 .\" HTML <a name="infoaboutcallouts"></a>
2259 .SH "INFORMATION ABOUT A PATTERN'S CALLOUTS"
2260 .rs
2261 .sp
2262 .nf
2263 .B int pcre2_callout_enumerate(const pcre2_code *\fIcode\fP,
2264 .B "  int (*\fIcallback\fP)(pcre2_callout_enumerate_block *, void *),"
2265 .B "  void *\fIuser_data\fP);"
2266 .fi
2267 .sp
2268 A script language that supports the use of string arguments in callouts might
2269 like to scan all the callouts in a pattern before running the match. This can
2270 be done by calling \fBpcre2_callout_enumerate()\fP. The first argument is a
2271 pointer to a compiled pattern, the second points to a callback function, and
2272 the third is arbitrary user data. The callback function is called for every
2273 callout in the pattern in the order in which they appear. Its first argument is
2274 a pointer to a callout enumeration block, and its second argument is the
2275 \fIuser_data\fP value that was passed to \fBpcre2_callout_enumerate()\fP. The
2276 contents of the callout enumeration block are described in the
2277 .\" HREF
2278 \fBpcre2callout\fP
2279 .\"
2280 documentation, which also gives further details about callouts.
2281 .
2282 .
2283 .SH "SERIALIZATION AND PRECOMPILING"
2284 .rs
2285 .sp
2286 It is possible to save compiled patterns on disc or elsewhere, and reload them
2287 later, subject to a number of restrictions. The host on which the patterns are
2288 reloaded must be running the same version of PCRE2, with the same code unit
2289 width, and must also have the same endianness, pointer width, and PCRE2_SIZE
2290 type. Before compiled patterns can be saved, they must be converted to a
2291 "serialized" form, which in the case of PCRE2 is really just a bytecode dump.
2292 The functions whose names begin with \fBpcre2_serialize_\fP are used for
2293 converting to and from the serialized form. They are described in the
2294 .\" HREF
2295 \fBpcre2serialize\fP
2296 .\"
2297 documentation. Note that PCRE2 serialization does not convert compiled patterns
2298 to an abstract format like Java or .NET serialization.
2299 .
2300 .
2301 .\" HTML <a name="matchdatablock"></a>
2302 .SH "THE MATCH DATA BLOCK"
2303 .rs
2304 .sp
2305 .nf
2306 .B pcre2_match_data *pcre2_match_data_create(uint32_t \fIovecsize\fP,
2307 .B "  pcre2_general_context *\fIgcontext\fP);"
2308 .sp
2309 .B pcre2_match_data *pcre2_match_data_create_from_pattern(
2310 .B "  const pcre2_code *\fIcode\fP, pcre2_general_context *\fIgcontext\fP);"
2311 .sp
2312 .B void pcre2_match_data_free(pcre2_match_data *\fImatch_data\fP);
2313 .fi
2314 .P
2315 Information about a successful or unsuccessful match is placed in a match
2316 data block, which is an opaque structure that is accessed by function calls. In
2317 particular, the match data block contains a vector of offsets into the subject
2318 string that define the matched part of the subject and any substrings that were
2319 captured. This is known as the \fIovector\fP.
2320 .P
2321 Before calling \fBpcre2_match()\fP, \fBpcre2_dfa_match()\fP, or
2322 \fBpcre2_jit_match()\fP you must create a match data block by calling one of
2323 the creation functions above. For \fBpcre2_match_data_create()\fP, the first
2324 argument is the number of pairs of offsets in the \fIovector\fP. One pair of
2325 offsets is required to identify the string that matched the whole pattern, with
2326 an additional pair for each captured substring. For example, a value of 4
2327 creates enough space to record the matched portion of the subject plus three
2328 captured substrings. A minimum of at least 1 pair is imposed by
2329 \fBpcre2_match_data_create()\fP, so it is always possible to return the overall
2330 matched string.
2331 .P
2332 The second argument of \fBpcre2_match_data_create()\fP is a pointer to a
2333 general context, which can specify custom memory management for obtaining the
2334 memory for the match data block. If you are not using custom memory management,
2335 pass NULL, which causes \fBmalloc()\fP to be used.
2336 .P
2337 For \fBpcre2_match_data_create_from_pattern()\fP, the first argument is a
2338 pointer to a compiled pattern. The ovector is created to be exactly the right
2339 size to hold all the substrings a pattern might capture. The second argument is
2340 again a pointer to a general context, but in this case if NULL is passed, the
2341 memory is obtained using the same allocator that was used for the compiled
2342 pattern (custom or default).
2343 .P
2344 A match data block can be used many times, with the same or different compiled
2345 patterns. You can extract information from a match data block after a match
2346 operation has finished, using functions that are described in the sections on
2347 .\" HTML <a href="#matchedstrings">
2348 .\" </a>
2349 matched strings
2350 .\"
2351 and
2352 .\" HTML <a href="#matchotherdata">
2353 .\" </a>
2354 other match data
2355 .\"
2356 below.
2357 .P
2358 When a call of \fBpcre2_match()\fP fails, valid data is available in the match
2359 block only when the error is PCRE2_ERROR_NOMATCH, PCRE2_ERROR_PARTIAL, or one
2360 of the error codes for an invalid UTF string. Exactly what is available depends
2361 on the error, and is detailed below.
2362 .P
2363 When one of the matching functions is called, pointers to the compiled pattern
2364 and the subject string are set in the match data block so that they can be
2365 referenced by the extraction functions. After running a match, you must not
2366 free a compiled pattern or a subject string until after all operations on the
2367 match data block (for that match) have taken place.
2368 .P
2369 When a match data block itself is no longer needed, it should be freed by
2370 calling \fBpcre2_match_data_free()\fP. If this function is called with a NULL
2371 argument, it returns immediately, without doing anything.
2372 .
2373 .
2374 .SH "MATCHING A PATTERN: THE TRADITIONAL FUNCTION"
2375 .rs
2376 .sp
2377 .nf
2378 .B int pcre2_match(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
2379 .B "  PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
2380 .B "  uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
2381 .B "  pcre2_match_context *\fImcontext\fP);"
2382 .fi
2383 .P
2384 The function \fBpcre2_match()\fP is called to match a subject string against a
2385 compiled pattern, which is passed in the \fIcode\fP argument. You can call
2386 \fBpcre2_match()\fP with the same \fIcode\fP argument as many times as you
2387 like, in order to find multiple matches in the subject string or to match
2388 different subject strings with the same pattern.
2389 .P
2390 This function is the main matching facility of the library, and it operates in
2391 a Perl-like manner. For specialist use there is also an alternative matching
2392 function, which is described
2393 .\" HTML <a href="#dfamatch">
2394 .\" </a>
2395 below
2396 .\"
2397 in the section about the \fBpcre2_dfa_match()\fP function.
2398 .P
2399 Here is an example of a simple call to \fBpcre2_match()\fP:
2400 .sp
2401   pcre2_match_data *md = pcre2_match_data_create(4, NULL);
2402   int rc = pcre2_match(
2403     re,             /* result of pcre2_compile() */
2404     "some string",  /* the subject string */
2405     11,             /* the length of the subject string */
2406     0,              /* start at offset 0 in the subject */
2407     0,              /* default options */
2408     md,             /* the match data block */
2409     NULL);          /* a match context; NULL means use defaults */
2410 .sp
2411 If the subject string is zero-terminated, the length can be given as
2412 PCRE2_ZERO_TERMINATED. A match context must be provided if certain less common
2413 matching parameters are to be changed. For details, see the section on
2414 .\" HTML <a href="#matchcontext">
2415 .\" </a>
2416 the match context
2417 .\"
2418 above.
2419 .
2420 .
2421 .SS "The string to be matched by \fBpcre2_match()\fP"
2422 .rs
2423 .sp
2424 The subject string is passed to \fBpcre2_match()\fP as a pointer in
2425 \fIsubject\fP, a length in \fIlength\fP, and a starting offset in
2426 \fIstartoffset\fP. The length and offset are in code units, not characters.
2427 That is, they are in bytes for the 8-bit library, 16-bit code units for the
2428 16-bit library, and 32-bit code units for the 32-bit library, whether or not
2429 UTF processing is enabled.
2430 .P
2431 If \fIstartoffset\fP is greater than the length of the subject,
2432 \fBpcre2_match()\fP returns PCRE2_ERROR_BADOFFSET. When the starting offset is
2433 zero, the search for a match starts at the beginning of the subject, and this
2434 is by far the most common case. In UTF-8 or UTF-16 mode, the starting offset
2435 must point to the start of a character, or to the end of the subject (in UTF-32
2436 mode, one code unit equals one character, so all offsets are valid). Like the
2437 pattern string, the subject may contain binary zeros.
2438 .P
2439 A non-zero starting offset is useful when searching for another match in the
2440 same subject by calling \fBpcre2_match()\fP again after a previous success.
2441 Setting \fIstartoffset\fP differs from passing over a shortened string and
2442 setting PCRE2_NOTBOL in the case of a pattern that begins with any kind of
2443 lookbehind. For example, consider the pattern
2444 .sp
2445   \eBiss\eB
2446 .sp
2447 which finds occurrences of "iss" in the middle of words. (\eB matches only if
2448 the current position in the subject is not a word boundary.) When applied to
2449 the string "Mississipi" the first call to \fBpcre2_match()\fP finds the first
2450 occurrence. If \fBpcre2_match()\fP is called again with just the remainder of
2451 the subject, namely "issipi", it does not match, because \eB is always false at
2452 the start of the subject, which is deemed to be a word boundary. However, if
2453 \fBpcre2_match()\fP is passed the entire string again, but with
2454 \fIstartoffset\fP set to 4, it finds the second occurrence of "iss" because it
2455 is able to look behind the starting point to discover that it is preceded by a
2456 letter.
2457 .P
2458 Finding all the matches in a subject is tricky when the pattern can match an
2459 empty string. It is possible to emulate Perl's /g behaviour by first trying the
2460 match again at the same offset, with the PCRE2_NOTEMPTY_ATSTART and
2461 PCRE2_ANCHORED options, and then if that fails, advancing the starting offset
2462 and trying an ordinary match again. There is some code that demonstrates how to
2463 do this in the
2464 .\" HREF
2465 \fBpcre2demo\fP
2466 .\"
2467 sample program. In the most general case, you have to check to see if the
2468 newline convention recognizes CRLF as a newline, and if so, and the current
2469 character is CR followed by LF, advance the starting offset by two characters
2470 instead of one.
2471 .P
2472 If a non-zero starting offset is passed when the pattern is anchored, a single
2473 attempt to match at the given offset is made. This can only succeed if the
2474 pattern does not require the match to be at the start of the subject. In other
2475 words, the anchoring must be the result of setting the PCRE2_ANCHORED option or
2476 the use of .* with PCRE2_DOTALL, not by starting the pattern with ^ or \eA.
2477 .
2478 .
2479 .\" HTML <a name="matchoptions"></a>
2480 .SS "Option bits for \fBpcre2_match()\fP"
2481 .rs
2482 .sp
2483 The unused bits of the \fIoptions\fP argument for \fBpcre2_match()\fP must be
2484 zero. The only bits that may be set are PCRE2_ANCHORED, PCRE2_ENDANCHORED,
2485 PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY, PCRE2_NOTEMPTY_ATSTART,
2486 PCRE2_NO_JIT, PCRE2_NO_UTF_CHECK, PCRE2_PARTIAL_HARD, and PCRE2_PARTIAL_SOFT.
2487 Their action is described below.
2488 .P
2489 Setting PCRE2_ANCHORED or PCRE2_ENDANCHORED at match time is not supported by
2490 the just-in-time (JIT) compiler. If it is set, JIT matching is disabled and the
2491 interpretive code in \fBpcre2_match()\fP is run. Apart from PCRE2_NO_JIT
2492 (obviously), the remaining options are supported for JIT matching.
2493 .sp
2494   PCRE2_ANCHORED
2495 .sp
2496 The PCRE2_ANCHORED option limits \fBpcre2_match()\fP to matching at the first
2497 matching position. If a pattern was compiled with PCRE2_ANCHORED, or turned out
2498 to be anchored by virtue of its contents, it cannot be made unachored at
2499 matching time. Note that setting the option at match time disables JIT
2500 matching.
2501 .sp
2502   PCRE2_ENDANCHORED
2503 .sp
2504 If the PCRE2_ENDANCHORED option is set, any string that \fBpcre2_match()\fP
2505 matches must be right at the end of the subject string. Note that setting the
2506 option at match time disables JIT matching.
2507 .sp
2508   PCRE2_NOTBOL
2509 .sp
2510 This option specifies that first character of the subject string is not the
2511 beginning of a line, so the circumflex metacharacter should not match before
2512 it. Setting this without having set PCRE2_MULTILINE at compile time causes
2513 circumflex never to match. This option affects only the behaviour of the
2514 circumflex metacharacter. It does not affect \eA.
2515 .sp
2516   PCRE2_NOTEOL
2517 .sp
2518 This option specifies that the end of the subject string is not the end of a
2519 line, so the dollar metacharacter should not match it nor (except in multiline
2520 mode) a newline immediately before it. Setting this without having set
2521 PCRE2_MULTILINE at compile time causes dollar never to match. This option
2522 affects only the behaviour of the dollar metacharacter. It does not affect \eZ
2523 or \ez.
2524 .sp
2525   PCRE2_NOTEMPTY
2526 .sp
2527 An empty string is not considered to be a valid match if this option is set. If
2528 there are alternatives in the pattern, they are tried. If all the alternatives
2529 match the empty string, the entire match fails. For example, if the pattern
2530 .sp
2531   a?b?
2532 .sp
2533 is applied to a string not beginning with "a" or "b", it matches an empty
2534 string at the start of the subject. With PCRE2_NOTEMPTY set, this match is not
2535 valid, so \fBpcre2_match()\fP searches further into the string for occurrences
2536 of "a" or "b".
2537 .sp
2538   PCRE2_NOTEMPTY_ATSTART
2539 .sp
2540 This is like PCRE2_NOTEMPTY, except that it locks out an empty string match
2541 only at the first matching position, that is, at the start of the subject plus
2542 the starting offset. An empty string match later in the subject is permitted.
2543 If the pattern is anchored, such a match can occur only if the pattern contains
2544 \eK.
2545 .sp
2546   PCRE2_NO_JIT
2547 .sp
2548 By default, if a pattern has been successfully processed by
2549 \fBpcre2_jit_compile()\fP, JIT is automatically used when \fBpcre2_match()\fP
2550 is called with options that JIT supports. Setting PCRE2_NO_JIT disables the use
2551 of JIT; it forces matching to be done by the interpreter.
2552 .sp
2553   PCRE2_NO_UTF_CHECK
2554 .sp
2555 When PCRE2_UTF is set at compile time, the validity of the subject as a UTF
2556 string is checked by default when \fBpcre2_match()\fP is subsequently called.
2557 If a non-zero starting offset is given, the check is applied only to that part
2558 of the subject that could be inspected during matching, and there is a check
2559 that the starting offset points to the first code unit of a character or to the
2560 end of the subject. If there are no lookbehind assertions in the pattern, the
2561 check starts at the starting offset. Otherwise, it starts at the length of the
2562 longest lookbehind before the starting offset, or at the start of the subject
2563 if there are not that many characters before the starting offset. Note that the
2564 sequences \eb and \eB are one-character lookbehinds.
2565 .P
2566 The check is carried out before any other processing takes place, and a
2567 negative error code is returned if the check fails. There are several UTF error
2568 codes for each code unit width, corresponding to different problems with the
2569 code unit sequence. There are discussions about the validity of
2570 .\" HTML <a href="pcre2unicode.html#utf8strings">
2571 .\" </a>
2572 UTF-8 strings,
2573 .\"
2574 .\" HTML <a href="pcre2unicode.html#utf16strings">
2575 .\" </a>
2576 UTF-16 strings,
2577 .\"
2578 and
2579 .\" HTML <a href="pcre2unicode.html#utf32strings">
2580 .\" </a>
2581 UTF-32 strings
2582 .\"
2583 in the
2584 .\" HREF
2585 \fBpcre2unicode\fP
2586 .\"
2587 page.
2588 .P
2589 If you know that your subject is valid, and you want to skip these checks for
2590 performance reasons, you can set the PCRE2_NO_UTF_CHECK option when calling
2591 \fBpcre2_match()\fP. You might want to do this for the second and subsequent
2592 calls to \fBpcre2_match()\fP if you are making repeated calls to find other
2593 matches in the same subject string.
2594 .P
2595 \fBWarning:\fP When PCRE2_NO_UTF_CHECK is set, the effect of passing an invalid
2596 string as a subject, or an invalid value of \fIstartoffset\fP, is undefined.
2597 Your program may crash or loop indefinitely.
2598 .sp
2599   PCRE2_PARTIAL_HARD
2600   PCRE2_PARTIAL_SOFT
2601 .sp
2602 These options turn on the partial matching feature. A partial match occurs if
2603 the end of the subject string is reached successfully, but there are not enough
2604 subject characters to complete the match. If this happens when
2605 PCRE2_PARTIAL_SOFT (but not PCRE2_PARTIAL_HARD) is set, matching continues by
2606 testing any remaining alternatives. Only if no complete match can be found is
2607 PCRE2_ERROR_PARTIAL returned instead of PCRE2_ERROR_NOMATCH. In other words,
2608 PCRE2_PARTIAL_SOFT specifies that the caller is prepared to handle a partial
2609 match, but only if no complete match can be found.
2610 .P
2611 If PCRE2_PARTIAL_HARD is set, it overrides PCRE2_PARTIAL_SOFT. In this case, if
2612 a partial match is found, \fBpcre2_match()\fP immediately returns
2613 PCRE2_ERROR_PARTIAL, without considering any other alternatives. In other
2614 words, when PCRE2_PARTIAL_HARD is set, a partial match is considered to be more
2615 important that an alternative complete match.
2616 .P
2617 There is a more detailed discussion of partial and multi-segment matching, with
2618 examples, in the
2619 .\" HREF
2620 \fBpcre2partial\fP
2621 .\"
2622 documentation.
2623 .
2624 .
2625 .
2626 .SH "NEWLINE HANDLING WHEN MATCHING"
2627 .rs
2628 .sp
2629 When PCRE2 is built, a default newline convention is set; this is usually the
2630 standard convention for the operating system. The default can be overridden in
2631 a
2632 .\" HTML <a href="#compilecontext">
2633 .\" </a>
2634 compile context
2635 .\"
2636 by calling \fBpcre2_set_newline()\fP. It can also be overridden by starting a
2637 pattern string with, for example, (*CRLF), as described in the
2638 .\" HTML <a href="pcre2pattern.html#newlines">
2639 .\" </a>
2640 section on newline conventions
2641 .\"
2642 in the
2643 .\" HREF
2644 \fBpcre2pattern\fP
2645 .\"
2646 page. During matching, the newline choice affects the behaviour of the dot,
2647 circumflex, and dollar metacharacters. It may also alter the way the match
2648 starting position is advanced after a match failure for an unanchored pattern.
2649 .P
2650 When PCRE2_NEWLINE_CRLF, PCRE2_NEWLINE_ANYCRLF, or PCRE2_NEWLINE_ANY is set as
2651 the newline convention, and a match attempt for an unanchored pattern fails
2652 when the current starting position is at a CRLF sequence, and the pattern
2653 contains no explicit matches for CR or LF characters, the match position is
2654 advanced by two characters instead of one, in other words, to after the CRLF.
2655 .P
2656 The above rule is a compromise that makes the most common cases work as
2657 expected. For example, if the pattern is .+A (and the PCRE2_DOTALL option is
2658 not set), it does not match the string "\er\enA" because, after failing at the
2659 start, it skips both the CR and the LF before retrying. However, the pattern
2660 [\er\en]A does match that string, because it contains an explicit CR or LF
2661 reference, and so advances only by one character after the first failure.
2662 .P
2663 An explicit match for CR of LF is either a literal appearance of one of those
2664 characters in the pattern, or one of the \er or \en or equivalent octal or
2665 hexadecimal escape sequences. Implicit matches such as [^X] do not count, nor
2666 does \es, even though it includes CR and LF in the characters that it matches.
2667 .P
2668 Notwithstanding the above, anomalous effects may still occur when CRLF is a
2669 valid newline sequence and explicit \er or \en escapes appear in the pattern.
2670 .
2671 .
2672 .\" HTML <a name="matchedstrings"></a>
2673 .SH "HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS"
2674 .rs
2675 .sp
2676 .nf
2677 .B uint32_t pcre2_get_ovector_count(pcre2_match_data *\fImatch_data\fP);
2678 .sp
2679 .B PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *\fImatch_data\fP);
2680 .fi
2681 .P
2682 In general, a pattern matches a certain portion of the subject, and in
2683 addition, further substrings from the subject may be picked out by
2684 parenthesized parts of the pattern. Following the usage in Jeffrey Friedl's
2685 book, this is called "capturing" in what follows, and the phrase "capturing
2686 subpattern" or "capturing group" is used for a fragment of a pattern that picks
2687 out a substring. PCRE2 supports several other kinds of parenthesized subpattern
2688 that do not cause substrings to be captured. The \fBpcre2_pattern_info()\fP
2689 function can be used to find out how many capturing subpatterns there are in a
2690 compiled pattern.
2691 .P
2692 You can use auxiliary functions for accessing captured substrings
2693 .\" HTML <a href="#extractbynumber">
2694 .\" </a>
2695 by number
2696 .\"
2697 or
2698 .\" HTML <a href="#extractbyname">
2699 .\" </a>
2700 by name,
2701 .\"
2702 as described in sections below.
2703 .P
2704 Alternatively, you can make direct use of the vector of PCRE2_SIZE values,
2705 called the \fBovector\fP, which contains the offsets of captured strings. It is
2706 part of the
2707 .\" HTML <a href="#matchdatablock">
2708 .\" </a>
2709 match data block.
2710 .\"
2711 The function \fBpcre2_get_ovector_pointer()\fP returns the address of the
2712 ovector, and \fBpcre2_get_ovector_count()\fP returns the number of pairs of
2713 values it contains.
2714 .P
2715 Within the ovector, the first in each pair of values is set to the offset of
2716 the first code unit of a substring, and the second is set to the offset of the
2717 first code unit after the end of a substring. These values are always code unit
2718 offsets, not character offsets. That is, they are byte offsets in the 8-bit
2719 library, 16-bit offsets in the 16-bit library, and 32-bit offsets in the 32-bit
2720 library.
2721 .P
2722 After a partial match (error return PCRE2_ERROR_PARTIAL), only the first pair
2723 of offsets (that is, \fIovector[0]\fP and \fIovector[1]\fP) are set. They
2724 identify the part of the subject that was partially matched. See the
2725 .\" HREF
2726 \fBpcre2partial\fP
2727 .\"
2728 documentation for details of partial matching.
2729 .P
2730 After a fully successful match, the first pair of offsets identifies the
2731 portion of the subject string that was matched by the entire pattern. The next
2732 pair is used for the first captured substring, and so on. The value returned by
2733 \fBpcre2_match()\fP is one more than the highest numbered pair that has been
2734 set. For example, if two substrings have been captured, the returned value is
2735 3. If there are no captured substrings, the return value from a successful
2736 match is 1, indicating that just the first pair of offsets has been set.
2737 .P
2738 If a pattern uses the \eK escape sequence within a positive assertion, the
2739 reported start of a successful match can be greater than the end of the match.
2740 For example, if the pattern (?=ab\eK) is matched against "ab", the start and
2741 end offset values for the match are 2 and 0.
2742 .P
2743 If a capturing subpattern group is matched repeatedly within a single match
2744 operation, it is the last portion of the subject that it matched that is
2745 returned.
2746 .P
2747 If the ovector is too small to hold all the captured substring offsets, as much
2748 as possible is filled in, and the function returns a value of zero. If captured
2749 substrings are not of interest, \fBpcre2_match()\fP may be called with a match
2750 data block whose ovector is of minimum length (that is, one pair).
2751 .P
2752 It is possible for capturing subpattern number \fIn+1\fP to match some part of
2753 the subject when subpattern \fIn\fP has not been used at all. For example, if
2754 the string "abc" is matched against the pattern (a|(z))(bc) the return from the
2755 function is 4, and subpatterns 1 and 3 are matched, but 2 is not. When this
2756 happens, both values in the offset pairs corresponding to unused subpatterns
2757 are set to PCRE2_UNSET.
2758 .P
2759 Offset values that correspond to unused subpatterns at the end of the
2760 expression are also set to PCRE2_UNSET. For example, if the string "abc" is
2761 matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not matched.
2762 The return from the function is 2, because the highest used capturing
2763 subpattern number is 1. The offsets for for the second and third capturing
2764 subpatterns (assuming the vector is large enough, of course) are set to
2765 PCRE2_UNSET.
2766 .P
2767 Elements in the ovector that do not correspond to capturing parentheses in the
2768 pattern are never changed. That is, if a pattern contains \fIn\fP capturing
2769 parentheses, no more than \fIovector[0]\fP to \fIovector[2n+1]\fP are set by
2770 \fBpcre2_match()\fP. The other elements retain whatever values they previously
2771 had. After a failed match attempt, the contents of the ovector are unchanged.
2772 .
2773 .
2774 .\" HTML <a name="matchotherdata"></a>
2775 .SH "OTHER INFORMATION ABOUT A MATCH"
2776 .rs
2777 .sp
2778 .nf
2779 .B PCRE2_SPTR pcre2_get_mark(pcre2_match_data *\fImatch_data\fP);
2780 .sp
2781 .B PCRE2_SIZE pcre2_get_startchar(pcre2_match_data *\fImatch_data\fP);
2782 .fi
2783 .P
2784 As well as the offsets in the ovector, other information about a match is
2785 retained in the match data block and can be retrieved by the above functions in
2786 appropriate circumstances. If they are called at other times, the result is
2787 undefined.
2788 .P
2789 After a successful match, a partial match (PCRE2_ERROR_PARTIAL), or a failure
2790 to match (PCRE2_ERROR_NOMATCH), a (*MARK), (*PRUNE), or (*THEN) name may be
2791 available. The function \fBpcre2_get_mark()\fP can be called to access this
2792 name. The same function applies to all three verbs. It returns a pointer to the
2793 zero-terminated name, which is within the compiled pattern. If no name is
2794 available, NULL is returned. The length of the name (excluding the terminating
2795 zero) is stored in the code unit that precedes the name. You should use this
2796 length instead of relying on the terminating zero if the name might contain a
2797 binary zero.
2798 .P
2799 After a successful match, the name that is returned is the last (*MARK),
2800 (*PRUNE), or (*THEN) name encountered on the matching path through the pattern.
2801 Instances of (*PRUNE) and (*THEN) without names are ignored. Thus, for example,
2802 if the matching path contains (*MARK:A)(*PRUNE), the name "A" is returned.
2803 After a "no match" or a partial match, the last encountered name is returned.
2804 For example, consider this pattern:
2805 .sp
2806   ^(*MARK:A)((*MARK:B)a|b)c
2807 .sp
2808 When it matches "bc", the returned name is A. The B mark is "seen" in the first
2809 branch of the group, but it is not on the matching path. On the other hand,
2810 when this pattern fails to match "bx", the returned name is B.
2811 .P
2812 \fBWarning:\fP By default, certain start-of-match optimizations are used to
2813 give a fast "no match" result in some situations. For example, if the anchoring
2814 is removed from the pattern above, there is an initial check for the presence
2815 of "c" in the subject before running the matching engine. This check fails for
2816 "bx", causing a match failure without seeing any marks. You can disable the
2817 start-of-match optimizations by setting the PCRE2_NO_START_OPTIMIZE option for
2818 \fBpcre2_compile()\fP or starting the pattern with (*NO_START_OPT).
2819 .P
2820 After a successful match, a partial match, or one of the invalid UTF errors
2821 (for example, PCRE2_ERROR_UTF8_ERR5), \fBpcre2_get_startchar()\fP can be
2822 called. After a successful or partial match it returns the code unit offset of
2823 the character at which the match started. For a non-partial match, this can be
2824 different to the value of \fIovector[0]\fP if the pattern contains the \eK
2825 escape sequence. After a partial match, however, this value is always the same
2826 as \fIovector[0]\fP because \eK does not affect the result of a partial match.
2827 .P
2828 After a UTF check failure, \fBpcre2_get_startchar()\fP can be used to obtain
2829 the code unit offset of the invalid UTF character. Details are given in the
2830 .\" HREF
2831 \fBpcre2unicode\fP
2832 .\"
2833 page.
2834 .
2835 .
2836 .\" HTML <a name="errorlist"></a>
2837 .SH "ERROR RETURNS FROM \fBpcre2_match()\fP"
2838 .rs
2839 .sp
2840 If \fBpcre2_match()\fP fails, it returns a negative number. This can be
2841 converted to a text string by calling the \fBpcre2_get_error_message()\fP
2842 function (see "Obtaining a textual error message"
2843 .\" HTML <a href="#geterrormessage">
2844 .\" </a>
2845 below).
2846 .\"
2847 Negative error codes are also returned by other functions, and are documented
2848 with them. The codes are given names in the header file. If UTF checking is in
2849 force and an invalid UTF subject string is detected, one of a number of
2850 UTF-specific negative error codes is returned. Details are given in the
2851 .\" HREF
2852 \fBpcre2unicode\fP
2853 .\"
2854 page. The following are the other errors that may be returned by
2855 \fBpcre2_match()\fP:
2856 .sp
2857   PCRE2_ERROR_NOMATCH
2858 .sp
2859 The subject string did not match the pattern.
2860 .sp
2861   PCRE2_ERROR_PARTIAL
2862 .sp
2863 The subject string did not match, but it did match partially. See the
2864 .\" HREF
2865 \fBpcre2partial\fP
2866 .\"
2867 documentation for details of partial matching.
2868 .sp
2869   PCRE2_ERROR_BADMAGIC
2870 .sp
2871 PCRE2 stores a 4-byte "magic number" at the start of the compiled code, to
2872 catch the case when it is passed a junk pointer. This is the error that is
2873 returned when the magic number is not present.
2874 .sp
2875   PCRE2_ERROR_BADMODE
2876 .sp
2877 This error is given when a compiled pattern is passed to a function in a
2878 library of a different code unit width, for example, a pattern compiled by
2879 the 8-bit library is passed to a 16-bit or 32-bit library function.
2880 .sp
2881   PCRE2_ERROR_BADOFFSET
2882 .sp
2883 The value of \fIstartoffset\fP was greater than the length of the subject.
2884 .sp
2885   PCRE2_ERROR_BADOPTION
2886 .sp
2887 An unrecognized bit was set in the \fIoptions\fP argument.
2888 .sp
2889   PCRE2_ERROR_BADUTFOFFSET
2890 .sp
2891 The UTF code unit sequence that was passed as a subject was checked and found
2892 to be valid (the PCRE2_NO_UTF_CHECK option was not set), but the value of
2893 \fIstartoffset\fP did not point to the beginning of a UTF character or the end
2894 of the subject.
2895 .sp
2896   PCRE2_ERROR_CALLOUT
2897 .sp
2898 This error is never generated by \fBpcre2_match()\fP itself. It is provided for
2899 use by callout functions that want to cause \fBpcre2_match()\fP or
2900 \fBpcre2_callout_enumerate()\fP to return a distinctive error code. See the
2901 .\" HREF
2902 \fBpcre2callout\fP
2903 .\"
2904 documentation for details.
2905 .sp
2906   PCRE2_ERROR_DEPTHLIMIT
2907 .sp
2908 The nested backtracking depth limit was reached.
2909 .sp
2910   PCRE2_ERROR_HEAPLIMIT
2911 .sp
2912 The heap limit was reached.
2913 .sp
2914   PCRE2_ERROR_INTERNAL
2915 .sp
2916 An unexpected internal error has occurred. This error could be caused by a bug
2917 in PCRE2 or by overwriting of the compiled pattern.
2918 .sp
2919   PCRE2_ERROR_JIT_STACKLIMIT
2920 .sp
2921 This error is returned when a pattern that was successfully studied using JIT
2922 is being matched, but the memory available for the just-in-time processing
2923 stack is not large enough. See the
2924 .\" HREF
2925 \fBpcre2jit\fP
2926 .\"
2927 documentation for more details.
2928 .sp
2929   PCRE2_ERROR_MATCHLIMIT
2930 .sp
2931 The backtracking match limit was reached.
2932 .sp
2933   PCRE2_ERROR_NOMEMORY
2934 .sp
2935 If a pattern contains many nested backtracking points, heap memory is used to
2936 remember them. This error is given when the memory allocation function (default
2937 or custom) fails. Note that a different error, PCRE2_ERROR_HEAPLIMIT, is given
2938 if the amount of memory needed exceeds the heap limit.
2939 .sp
2940   PCRE2_ERROR_NULL
2941 .sp
2942 Either the \fIcode\fP, \fIsubject\fP, or \fImatch_data\fP argument was passed
2943 as NULL.
2944 .sp
2945   PCRE2_ERROR_RECURSELOOP
2946 .sp
2947 This error is returned when \fBpcre2_match()\fP detects a recursion loop within
2948 the pattern. Specifically, it means that either the whole pattern or a
2949 subpattern has been called recursively for the second time at the same position
2950 in the subject string. Some simple patterns that might do this are detected and
2951 faulted at compile time, but more complicated cases, in particular mutual
2952 recursions between two different subpatterns, cannot be detected until matching
2953 is attempted.
2954 .
2955 .
2956 .\" HTML <a name="geterrormessage"></a>
2957 .SH "OBTAINING A TEXTUAL ERROR MESSAGE"
2958 .rs
2959 .sp
2960 .nf
2961 .B int pcre2_get_error_message(int \fIerrorcode\fP, PCRE2_UCHAR *\fIbuffer\fP,
2962 .B "  PCRE2_SIZE \fIbufflen\fP);"
2963 .fi
2964 .P
2965 A text message for an error code from any PCRE2 function (compile, match, or
2966 auxiliary) can be obtained by calling \fBpcre2_get_error_message()\fP. The code
2967 is passed as the first argument, with the remaining two arguments specifying a
2968 code unit buffer and its length in code units, into which the text message is
2969 placed. The message is returned in code units of the appropriate width for the
2970 library that is being used.
2971 .P
2972 The returned message is terminated with a trailing zero, and the function
2973 returns the number of code units used, excluding the trailing zero. If the
2974 error number is unknown, the negative error code PCRE2_ERROR_BADDATA is
2975 returned. If the buffer is too small, the message is truncated (but still with
2976 a trailing zero), and the negative error code PCRE2_ERROR_NOMEMORY is returned.
2977 None of the messages are very long; a buffer size of 120 code units is ample.
2978 .
2979 .
2980 .\" HTML <a name="extractbynumber"></a>
2981 .SH "EXTRACTING CAPTURED SUBSTRINGS BY NUMBER"
2982 .rs
2983 .sp
2984 .nf
2985 .B int pcre2_substring_length_bynumber(pcre2_match_data *\fImatch_data\fP,
2986 .B "  uint32_t \fInumber\fP, PCRE2_SIZE *\fIlength\fP);"
2987 .sp
2988 .B int pcre2_substring_copy_bynumber(pcre2_match_data *\fImatch_data\fP,
2989 .B "  uint32_t \fInumber\fP, PCRE2_UCHAR *\fIbuffer\fP,"
2990 .B "  PCRE2_SIZE *\fIbufflen\fP);"
2991 .sp
2992 .B int pcre2_substring_get_bynumber(pcre2_match_data *\fImatch_data\fP,
2993 .B "  uint32_t \fInumber\fP, PCRE2_UCHAR **\fIbufferptr\fP,"
2994 .B "  PCRE2_SIZE *\fIbufflen\fP);"
2995 .sp
2996 .B void pcre2_substring_free(PCRE2_UCHAR *\fIbuffer\fP);
2997 .fi
2998 .P
2999 Captured substrings can be accessed directly by using the ovector as described
3000 .\" HTML <a href="#matchedstrings">
3001 .\" </a>
3002 above.
3003 .\"
3004 For convenience, auxiliary functions are provided for extracting captured
3005 substrings as new, separate, zero-terminated strings. A substring that contains
3006 a binary zero is correctly extracted and has a further zero added on the end,
3007 but the result is not, of course, a C string.
3008 .P
3009 The functions in this section identify substrings by number. The number zero
3010 refers to the entire matched substring, with higher numbers referring to
3011 substrings captured by parenthesized groups. After a partial match, only
3012 substring zero is available. An attempt to extract any other substring gives
3013 the error PCRE2_ERROR_PARTIAL. The next section describes similar functions for
3014 extracting captured substrings by name.
3015 .P
3016 If a pattern uses the \eK escape sequence within a positive assertion, the
3017 reported start of a successful match can be greater than the end of the match.
3018 For example, if the pattern (?=ab\eK) is matched against "ab", the start and
3019 end offset values for the match are 2 and 0. In this situation, calling these
3020 functions with a zero substring number extracts a zero-length empty string.
3021 .P
3022 You can find the length in code units of a captured substring without
3023 extracting it by calling \fBpcre2_substring_length_bynumber()\fP. The first
3024 argument is a pointer to the match data block, the second is the group number,
3025 and the third is a pointer to a variable into which the length is placed. If
3026 you just want to know whether or not the substring has been captured, you can
3027 pass the third argument as NULL.
3028 .P
3029 The \fBpcre2_substring_copy_bynumber()\fP function copies a captured substring
3030 into a supplied buffer, whereas \fBpcre2_substring_get_bynumber()\fP copies it
3031 into new memory, obtained using the same memory allocation function that was
3032 used for the match data block. The first two arguments of these functions are a
3033 pointer to the match data block and a capturing group number.
3034 .P
3035 The final arguments of \fBpcre2_substring_copy_bynumber()\fP are a pointer to
3036 the buffer and a pointer to a variable that contains its length in code units.
3037 This is updated to contain the actual number of code units used for the
3038 extracted substring, excluding the terminating zero.
3039 .P
3040 For \fBpcre2_substring_get_bynumber()\fP the third and fourth arguments point
3041 to variables that are updated with a pointer to the new memory and the number
3042 of code units that comprise the substring, again excluding the terminating
3043 zero. When the substring is no longer needed, the memory should be freed by
3044 calling \fBpcre2_substring_free()\fP.
3045 .P
3046 The return value from all these functions is zero for success, or a negative
3047 error code. If the pattern match failed, the match failure code is returned.
3048 If a substring number greater than zero is used after a partial match,
3049 PCRE2_ERROR_PARTIAL is returned. Other possible error codes are:
3050 .sp
3051   PCRE2_ERROR_NOMEMORY
3052 .sp
3053 The buffer was too small for \fBpcre2_substring_copy_bynumber()\fP, or the
3054 attempt to get memory failed for \fBpcre2_substring_get_bynumber()\fP.
3055 .sp
3056   PCRE2_ERROR_NOSUBSTRING
3057 .sp
3058 There is no substring with that number in the pattern, that is, the number is
3059 greater than the number of capturing parentheses.
3060 .sp
3061   PCRE2_ERROR_UNAVAILABLE
3062 .sp
3063 The substring number, though not greater than the number of captures in the
3064 pattern, is greater than the number of slots in the ovector, so the substring
3065 could not be captured.
3066 .sp
3067   PCRE2_ERROR_UNSET
3068 .sp
3069 The substring did not participate in the match. For example, if the pattern is
3070 (abc)|(def) and the subject is "def", and the ovector contains at least two
3071 capturing slots, substring number 1 is unset.
3072 .
3073 .
3074 .SH "EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS"
3075 .rs
3076 .sp
3077 .nf
3078 .B int pcre2_substring_list_get(pcre2_match_data *\fImatch_data\fP,
3079 .B "  PCRE2_UCHAR ***\fIlistptr\fP, PCRE2_SIZE **\fIlengthsptr\fP);
3080 .sp
3081 .B void pcre2_substring_list_free(PCRE2_SPTR *\fIlist\fP);
3082 .fi
3083 .P
3084 The \fBpcre2_substring_list_get()\fP function extracts all available substrings
3085 and builds a list of pointers to them. It also (optionally) builds a second
3086 list that contains their lengths (in code units), excluding a terminating zero
3087 that is added to each of them. All this is done in a single block of memory
3088 that is obtained using the same memory allocation function that was used to get
3089 the match data block.
3090 .P
3091 This function must be called only after a successful match. If called after a
3092 partial match, the error code PCRE2_ERROR_PARTIAL is returned.
3093 .P
3094 The address of the memory block is returned via \fIlistptr\fP, which is also
3095 the start of the list of string pointers. The end of the list is marked by a
3096 NULL pointer. The address of the list of lengths is returned via
3097 \fIlengthsptr\fP. If your strings do not contain binary zeros and you do not
3098 therefore need the lengths, you may supply NULL as the \fBlengthsptr\fP
3099 argument to disable the creation of a list of lengths. The yield of the
3100 function is zero if all went well, or PCRE2_ERROR_NOMEMORY if the memory block
3101 could not be obtained. When the list is no longer needed, it should be freed by
3102 calling \fBpcre2_substring_list_free()\fP.
3103 .P
3104 If this function encounters a substring that is unset, which can happen when
3105 capturing subpattern number \fIn+1\fP matches some part of the subject, but
3106 subpattern \fIn\fP has not been used at all, it returns an empty string. This
3107 can be distinguished from a genuine zero-length substring by inspecting the
3108 appropriate offset in the ovector, which contain PCRE2_UNSET for unset
3109 substrings, or by calling \fBpcre2_substring_length_bynumber()\fP.
3110 .
3111 .
3112 .\" HTML <a name="extractbyname"></a>
3113 .SH "EXTRACTING CAPTURED SUBSTRINGS BY NAME"
3114 .rs
3115 .sp
3116 .nf
3117 .B int pcre2_substring_number_from_name(const pcre2_code *\fIcode\fP,
3118 .B "  PCRE2_SPTR \fIname\fP);"
3119 .sp
3120 .B int pcre2_substring_length_byname(pcre2_match_data *\fImatch_data\fP,
3121 .B "  PCRE2_SPTR \fIname\fP, PCRE2_SIZE *\fIlength\fP);"
3122 .sp
3123 .B int pcre2_substring_copy_byname(pcre2_match_data *\fImatch_data\fP,
3124 .B "  PCRE2_SPTR \fIname\fP, PCRE2_UCHAR *\fIbuffer\fP, PCRE2_SIZE *\fIbufflen\fP);"
3125 .sp
3126 .B int pcre2_substring_get_byname(pcre2_match_data *\fImatch_data\fP,
3127 .B "  PCRE2_SPTR \fIname\fP, PCRE2_UCHAR **\fIbufferptr\fP, PCRE2_SIZE *\fIbufflen\fP);"
3128 .sp
3129 .B void pcre2_substring_free(PCRE2_UCHAR *\fIbuffer\fP);
3130 .fi
3131 .P
3132 To extract a substring by name, you first have to find associated number.
3133 For example, for this pattern:
3134 .sp
3135   (a+)b(?<xxx>\ed+)...
3136 .sp
3137 the number of the subpattern called "xxx" is 2. If the name is known to be
3138 unique (PCRE2_DUPNAMES was not set), you can find the number from the name by
3139 calling \fBpcre2_substring_number_from_name()\fP. The first argument is the
3140 compiled pattern, and the second is the name. The yield of the function is the
3141 subpattern number, PCRE2_ERROR_NOSUBSTRING if there is no subpattern of that
3142 name, or PCRE2_ERROR_NOUNIQUESUBSTRING if there is more than one subpattern of
3143 that name. Given the number, you can extract the substring directly from the
3144 ovector, or use one of the "bynumber" functions described above.
3145 .P
3146 For convenience, there are also "byname" functions that correspond to the
3147 "bynumber" functions, the only difference being that the second argument is a
3148 name instead of a number. If PCRE2_DUPNAMES is set and there are duplicate
3149 names, these functions scan all the groups with the given name, and return the
3150 first named string that is set.
3151 .P
3152 If there are no groups with the given name, PCRE2_ERROR_NOSUBSTRING is
3153 returned. If all groups with the name have numbers that are greater than the
3154 number of slots in the ovector, PCRE2_ERROR_UNAVAILABLE is returned. If there
3155 is at least one group with a slot in the ovector, but no group is found to be
3156 set, PCRE2_ERROR_UNSET is returned.
3157 .P
3158 \fBWarning:\fP If the pattern uses the (?| feature to set up multiple
3159 subpatterns with the same number, as described in the
3160 .\" HTML <a href="pcre2pattern.html#dupsubpatternnumber">
3161 .\" </a>
3162 section on duplicate subpattern numbers
3163 .\"
3164 in the
3165 .\" HREF
3166 \fBpcre2pattern\fP
3167 .\"
3168 page, you cannot use names to distinguish the different subpatterns, because
3169 names are not included in the compiled code. The matching process uses only
3170 numbers. For this reason, the use of different names for subpatterns of the
3171 same number causes an error at compile time.
3172 .
3173 .
3174 .SH "CREATING A NEW STRING WITH SUBSTITUTIONS"
3175 .rs
3176 .sp
3177 .nf
3178 .B int pcre2_substitute(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
3179 .B "  PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
3180 .B "  uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
3181 .B "  pcre2_match_context *\fImcontext\fP, PCRE2_SPTR \fIreplacement\fP,"
3182 .B "  PCRE2_SIZE \fIrlength\fP, PCRE2_UCHAR *\fIoutputbuffer\zfP,"
3183 .B "  PCRE2_SIZE *\fIoutlengthptr\fP);"
3184 .fi
3185 .P
3186 This function calls \fBpcre2_match()\fP and then makes a copy of the subject
3187 string in \fIoutputbuffer\fP, replacing the part that was matched with the
3188 \fIreplacement\fP string, whose length is supplied in \fBrlength\fP. This can
3189 be given as PCRE2_ZERO_TERMINATED for a zero-terminated string. Matches in
3190 which a \eK item in a lookahead in the pattern causes the match to end before
3191 it starts are not supported, and give rise to an error return. For global
3192 replacements, matches in which \eK in a lookbehind causes the match to start
3193 earlier than the point that was reached in the previous iteration are also not
3194 supported.
3195 .P
3196 The first seven arguments of \fBpcre2_substitute()\fP are the same as for
3197 \fBpcre2_match()\fP, except that the partial matching options are not
3198 permitted, and \fImatch_data\fP may be passed as NULL, in which case a match
3199 data block is obtained and freed within this function, using memory management
3200 functions from the match context, if provided, or else those that were used to
3201 allocate memory for the compiled code.
3202 .P
3203 If an external \fImatch_data\fP block is provided, its contents afterwards
3204 are those set by the final call to \fBpcre2_match()\fP, which will have
3205 ended in a matching error. The contents of the ovector within the match data
3206 block may or may not have been changed.
3207 .P
3208 The \fIoutlengthptr\fP argument must point to a variable that contains the
3209 length, in code units, of the output buffer. If the function is successful, the
3210 value is updated to contain the length of the new string, excluding the
3211 trailing zero that is automatically added.
3212 .P
3213 If the function is not successful, the value set via \fIoutlengthptr\fP depends
3214 on the type of error. For syntax errors in the replacement string, the value is
3215 the offset in the replacement string where the error was detected. For other
3216 errors, the value is PCRE2_UNSET by default. This includes the case of the
3217 output buffer being too small, unless PCRE2_SUBSTITUTE_OVERFLOW_LENGTH is set
3218 (see below), in which case the value is the minimum length needed, including
3219 space for the trailing zero. Note that in order to compute the required length,
3220 \fBpcre2_substitute()\fP has to simulate all the matching and copying, instead
3221 of giving an error return as soon as the buffer overflows. Note also that the
3222 length is in code units, not bytes.
3223 .P
3224 In the replacement string, which is interpreted as a UTF string in UTF mode,
3225 and is checked for UTF validity unless the PCRE2_NO_UTF_CHECK option is set, a
3226 dollar character is an escape character that can specify the insertion of
3227 characters from capturing groups or (*MARK), (*PRUNE), or (*THEN) items in the
3228 pattern. The following forms are always recognized:
3229 .sp
3230   $$                  insert a dollar character
3231   $<n> or ${<n>}      insert the contents of group <n>
3232   $*MARK or ${*MARK}  insert a (*MARK), (*PRUNE), or (*THEN) name
3233 .sp
3234 Either a group number or a group name can be given for <n>. Curly brackets are
3235 required only if the following character would be interpreted as part of the
3236 number or name. The number may be zero to include the entire matched string.
3237 For example, if the pattern a(b)c is matched with "=abc=" and the replacement
3238 string "+$1$0$1+", the result is "=+babcb+=".
3239 .P
3240 $*MARK inserts the name from the last encountered (*MARK), (*PRUNE), or (*THEN)
3241 on the matching path that has a name. (*MARK) must always include a name, but
3242 (*PRUNE) and (*THEN) need not. For example, in the case of (*MARK:A)(*PRUNE)
3243 the name inserted is "A", but for (*MARK:A)(*PRUNE:B) the relevant name is "B".
3244 This facility can be used to perform simple simultaneous substitutions, as this
3245 \fBpcre2test\fP example shows:
3246 .sp
3247   /(*MARK:pear)apple|(*MARK:orange)lemon/g,replace=${*MARK}
3248       apple lemon
3249    2: pear orange
3250 .sp
3251 As well as the usual options for \fBpcre2_match()\fP, a number of additional
3252 options can be set in the \fIoptions\fP argument of \fBpcre2_substitute()\fP.
3253 .P
3254 PCRE2_SUBSTITUTE_GLOBAL causes the function to iterate over the subject string,
3255 replacing every matching substring. If this option is not set, only the first
3256 matching substring is replaced. The search for matches takes place in the
3257 original subject string (that is, previous replacements do not affect it).
3258 Iteration is implemented by advancing the \fIstartoffset\fP value for each
3259 search, which is always passed the entire subject string. If an offset limit is
3260 set in the match context, searching stops when that limit is reached.
3261 .P
3262 You can restrict the effect of a global substitution to a portion of the
3263 subject string by setting either or both of \fIstartoffset\fP and an offset
3264 limit. Here is a \fPpcre2test\fP example:
3265 .sp
3266   /B/g,replace=!,use_offset_limit
3267   ABC ABC ABC ABC\e=offset=3,offset_limit=12
3268    2: ABC A!C A!C ABC
3269 .sp
3270 When continuing with global substitutions after matching a substring with zero
3271 length, an attempt to find a non-empty match at the same offset is performed.
3272 If this is not successful, the offset is advanced by one character except when
3273 CRLF is a valid newline sequence and the next two characters are CR, LF. In
3274 this case, the offset is advanced by two characters.
3275 .P
3276 PCRE2_SUBSTITUTE_OVERFLOW_LENGTH changes what happens when the output buffer is
3277 too small. The default action is to return PCRE2_ERROR_NOMEMORY immediately. If
3278 this option is set, however, \fBpcre2_substitute()\fP continues to go through
3279 the motions of matching and substituting (without, of course, writing anything)
3280 in order to compute the size of buffer that is needed. This value is passed
3281 back via the \fIoutlengthptr\fP variable, with the result of the function still
3282 being PCRE2_ERROR_NOMEMORY.
3283 .P
3284 Passing a buffer size of zero is a permitted way of finding out how much memory
3285 is needed for given substitution. However, this does mean that the entire
3286 operation is carried out twice. Depending on the application, it may be more
3287 efficient to allocate a large buffer and free the excess afterwards, instead of
3288 using PCRE2_SUBSTITUTE_OVERFLOW_LENGTH.
3289 .P
3290 PCRE2_SUBSTITUTE_UNKNOWN_UNSET causes references to capturing groups that do
3291 not appear in the pattern to be treated as unset groups. This option should be
3292 used with care, because it means that a typo in a group name or number no
3293 longer causes the PCRE2_ERROR_NOSUBSTRING error.
3294 .P
3295 PCRE2_SUBSTITUTE_UNSET_EMPTY causes unset capturing groups (including unknown
3296 groups when PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set) to be treated as empty
3297 strings when inserted as described above. If this option is not set, an attempt
3298 to insert an unset group causes the PCRE2_ERROR_UNSET error. This option does
3299 not influence the extended substitution syntax described below.
3300 .P
3301 PCRE2_SUBSTITUTE_EXTENDED causes extra processing to be applied to the
3302 replacement string. Without this option, only the dollar character is special,
3303 and only the group insertion forms listed above are valid. When
3304 PCRE2_SUBSTITUTE_EXTENDED is set, two things change:
3305 .P
3306 Firstly, backslash in a replacement string is interpreted as an escape
3307 character. The usual forms such as \en or \ex{ddd} can be used to specify
3308 particular character codes, and backslash followed by any non-alphanumeric
3309 character quotes that character. Extended quoting can be coded using \eQ...\eE,
3310 exactly as in pattern strings.
3311 .P
3312 There are also four escape sequences for forcing the case of inserted letters.
3313 The insertion mechanism has three states: no case forcing, force upper case,
3314 and force lower case. The escape sequences change the current state: \eU and
3315 \eL change to upper or lower case forcing, respectively, and \eE (when not
3316 terminating a \eQ quoted sequence) reverts to no case forcing. The sequences
3317 \eu and \el force the next character (if it is a letter) to upper or lower
3318 case, respectively, and then the state automatically reverts to no case
3319 forcing. Case forcing applies to all inserted  characters, including those from
3320 captured groups and letters within \eQ...\eE quoted sequences.
3321 .P
3322 Note that case forcing sequences such as \eU...\eE do not nest. For example,
3323 the result of processing "\eUaa\eLBB\eEcc\eE" is "AAbbcc"; the final \eE has no
3324 effect.
3325 .P
3326 The second effect of setting PCRE2_SUBSTITUTE_EXTENDED is to add more
3327 flexibility to group substitution. The syntax is similar to that used by Bash:
3328 .sp
3329   ${<n>:-<string>}
3330   ${<n>:+<string1>:<string2>}
3331 .sp
3332 As before, <n> may be a group number or a name. The first form specifies a
3333 default value. If group <n> is set, its value is inserted; if not, <string> is
3334 expanded and the result inserted. The second form specifies strings that are
3335 expanded and inserted when group <n> is set or unset, respectively. The first
3336 form is just a convenient shorthand for
3337 .sp
3338   ${<n>:+${<n>}:<string>}
3339 .sp
3340 Backslash can be used to escape colons and closing curly brackets in the
3341 replacement strings. A change of the case forcing state within a replacement
3342 string remains in force afterwards, as shown in this \fBpcre2test\fP example:
3343 .sp
3344   /(some)?(body)/substitute_extended,replace=${1:+\eU:\eL}HeLLo
3345       body
3346    1: hello
3347       somebody
3348    1: HELLO
3349 .sp
3350 The PCRE2_SUBSTITUTE_UNSET_EMPTY option does not affect these extended
3351 substitutions. However, PCRE2_SUBSTITUTE_UNKNOWN_UNSET does cause unknown
3352 groups in the extended syntax forms to be treated as unset.
3353 .P
3354 If successful, \fBpcre2_substitute()\fP returns the number of replacements that
3355 were made. This may be zero if no matches were found, and is never greater than
3356 1 unless PCRE2_SUBSTITUTE_GLOBAL is set.
3357 .P
3358 In the event of an error, a negative error code is returned. Except for
3359 PCRE2_ERROR_NOMATCH (which is never returned), errors from \fBpcre2_match()\fP
3360 are passed straight back.
3361 .P
3362 PCRE2_ERROR_NOSUBSTRING is returned for a non-existent substring insertion,
3363 unless PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set.
3364 .P
3365 PCRE2_ERROR_UNSET is returned for an unset substring insertion (including an
3366 unknown substring when PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set) when the simple
3367 (non-extended) syntax is used and PCRE2_SUBSTITUTE_UNSET_EMPTY is not set.
3368 .P
3369 PCRE2_ERROR_NOMEMORY is returned if the output buffer is not big enough. If the
3370 PCRE2_SUBSTITUTE_OVERFLOW_LENGTH option is set, the size of buffer that is
3371 needed is returned via \fIoutlengthptr\fP. Note that this does not happen by
3372 default.
3373 .P
3374 PCRE2_ERROR_BADREPLACEMENT is used for miscellaneous syntax errors in the
3375 replacement string, with more particular errors being PCRE2_ERROR_BADREPESCAPE
3376 (invalid escape sequence), PCRE2_ERROR_REPMISSINGBRACE (closing curly bracket
3377 not found), PCRE2_ERROR_BADSUBSTITUTION (syntax error in extended group
3378 substitution), and PCRE2_ERROR_BADSUBSPATTERN (the pattern match ended before
3379 it started or the match started earlier than the current position in the
3380 subject, which can happen if \eK is used in an assertion).
3381 .P
3382 As for all PCRE2 errors, a text message that describes the error can be
3383 obtained by calling the \fBpcre2_get_error_message()\fP function (see
3384 "Obtaining a textual error message"
3385 .\" HTML <a href="#geterrormessage">
3386 .\" </a>
3387 above).
3388 .\"
3389 .
3390 .
3391 .SH "DUPLICATE SUBPATTERN NAMES"
3392 .rs
3393 .sp
3394 .nf
3395 .B int pcre2_substring_nametable_scan(const pcre2_code *\fIcode\fP,
3396 .B "  PCRE2_SPTR \fIname\fP, PCRE2_SPTR *\fIfirst\fP, PCRE2_SPTR *\fIlast\fP);"
3397 .fi
3398 .P
3399 When a pattern is compiled with the PCRE2_DUPNAMES option, names for
3400 subpatterns are not required to be unique. Duplicate names are always allowed
3401 for subpatterns with the same number, created by using the (?| feature. Indeed,
3402 if such subpatterns are named, they are required to use the same names.
3403 .P
3404 Normally, patterns with duplicate names are such that in any one match, only
3405 one of the named subpatterns participates. An example is shown in the
3406 .\" HREF
3407 \fBpcre2pattern\fP
3408 .\"
3409 documentation.
3410 .P
3411 When duplicates are present, \fBpcre2_substring_copy_byname()\fP and
3412 \fBpcre2_substring_get_byname()\fP return the first substring corresponding to
3413 the given name that is set. Only if none are set is PCRE2_ERROR_UNSET is
3414 returned. The \fBpcre2_substring_number_from_name()\fP function returns the
3415 error PCRE2_ERROR_NOUNIQUESUBSTRING when there are duplicate names.
3416 .P
3417 If you want to get full details of all captured substrings for a given name,
3418 you must use the \fBpcre2_substring_nametable_scan()\fP function. The first
3419 argument is the compiled pattern, and the second is the name. If the third and
3420 fourth arguments are NULL, the function returns a group number for a unique
3421 name, or PCRE2_ERROR_NOUNIQUESUBSTRING otherwise.
3422 .P
3423 When the third and fourth arguments are not NULL, they must be pointers to
3424 variables that are updated by the function. After it has run, they point to the
3425 first and last entries in the name-to-number table for the given name, and the
3426 function returns the length of each entry in code units. In both cases,
3427 PCRE2_ERROR_NOSUBSTRING is returned if there are no entries for the given name.
3428 .P
3429 The format of the name table is described
3430 .\" HTML <a href="#infoaboutpattern">
3431 .\" </a>
3432 above
3433 .\"
3434 in the section entitled \fIInformation about a pattern\fP. Given all the
3435 relevant entries for the name, you can extract each of their numbers, and hence
3436 the captured data.
3437 .
3438 .
3439 .SH "FINDING ALL POSSIBLE MATCHES AT ONE POSITION"
3440 .rs
3441 .sp
3442 The traditional matching function uses a similar algorithm to Perl, which stops
3443 when it finds the first match at a given point in the subject. If you want to
3444 find all possible matches, or the longest possible match at a given position,
3445 consider using the alternative matching function (see below) instead. If you
3446 cannot use the alternative function, you can kludge it up by making use of the
3447 callout facility, which is described in the
3448 .\" HREF
3449 \fBpcre2callout\fP
3450 .\"
3451 documentation.
3452 .P
3453 What you have to do is to insert a callout right at the end of the pattern.
3454 When your callout function is called, extract and save the current matched
3455 substring. Then return 1, which forces \fBpcre2_match()\fP to backtrack and try
3456 other alternatives. Ultimately, when it runs out of matches,
3457 \fBpcre2_match()\fP will yield PCRE2_ERROR_NOMATCH.
3458 .
3459 .
3460 .\" HTML <a name="dfamatch"></a>
3461 .SH "MATCHING A PATTERN: THE ALTERNATIVE FUNCTION"
3462 .rs
3463 .sp
3464 .nf
3465 .B int pcre2_dfa_match(const pcre2_code *\fIcode\fP, PCRE2_SPTR \fIsubject\fP,
3466 .B "  PCRE2_SIZE \fIlength\fP, PCRE2_SIZE \fIstartoffset\fP,"
3467 .B "  uint32_t \fIoptions\fP, pcre2_match_data *\fImatch_data\fP,"
3468 .B "  pcre2_match_context *\fImcontext\fP,"
3469 .B "  int *\fIworkspace\fP, PCRE2_SIZE \fIwscount\fP);"
3470 .fi
3471 .P
3472 The function \fBpcre2_dfa_match()\fP is called to match a subject string
3473 against a compiled pattern, using a matching algorithm that scans the subject
3474 string just once (not counting lookaround assertions), and does not backtrack.
3475 This has different characteristics to the normal algorithm, and is not
3476 compatible with Perl. Some of the features of PCRE2 patterns are not supported.
3477 Nevertheless, there are times when this kind of matching can be useful. For a
3478 discussion of the two matching algorithms, and a list of features that
3479 \fBpcre2_dfa_match()\fP does not support, see the
3480 .\" HREF
3481 \fBpcre2matching\fP
3482 .\"
3483 documentation.
3484 .P
3485 The arguments for the \fBpcre2_dfa_match()\fP function are the same as for
3486 \fBpcre2_match()\fP, plus two extras. The ovector within the match data block
3487 is used in a different way, and this is described below. The other common
3488 arguments are used in the same way as for \fBpcre2_match()\fP, so their
3489 description is not repeated here.
3490 .P
3491 The two additional arguments provide workspace for the function. The workspace
3492 vector should contain at least 20 elements. It is used for keeping track of
3493 multiple paths through the pattern tree. More workspace is needed for patterns
3494 and subjects where there are a lot of potential matches.
3495 .P
3496 Here is an example of a simple call to \fBpcre2_dfa_match()\fP:
3497 .sp
3498   int wspace[20];
3499   pcre2_match_data *md = pcre2_match_data_create(4, NULL);
3500   int rc = pcre2_dfa_match(
3501     re,             /* result of pcre2_compile() */
3502     "some string",  /* the subject string */
3503     11,             /* the length of the subject string */
3504     0,              /* start at offset 0 in the subject */
3505     0,              /* default options */
3506     md,             /* the match data block */
3507     NULL,           /* a match context; NULL means use defaults */
3508     wspace,         /* working space vector */
3509     20);            /* number of elements (NOT size in bytes) */
3510 .
3511 .SS "Option bits for \fBpcre_dfa_match()\fP"
3512 .rs
3513 .sp
3514 The unused bits of the \fIoptions\fP argument for \fBpcre2_dfa_match()\fP must
3515 be zero. The only bits that may be set are PCRE2_ANCHORED, PCRE2_ENDANCHORED,
3516 PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY, PCRE2_NOTEMPTY_ATSTART,
3517 PCRE2_NO_UTF_CHECK, PCRE2_PARTIAL_HARD, PCRE2_PARTIAL_SOFT, PCRE2_DFA_SHORTEST,
3518 and PCRE2_DFA_RESTART. All but the last four of these are exactly the same as
3519 for \fBpcre2_match()\fP, so their description is not repeated here.
3520 .sp
3521   PCRE2_PARTIAL_HARD
3522   PCRE2_PARTIAL_SOFT
3523 .sp
3524 These have the same general effect as they do for \fBpcre2_match()\fP, but the
3525 details are slightly different. When PCRE2_PARTIAL_HARD is set for
3526 \fBpcre2_dfa_match()\fP, it returns PCRE2_ERROR_PARTIAL if the end of the
3527 subject is reached and there is still at least one matching possibility that
3528 requires additional characters. This happens even if some complete matches have
3529 already been found. When PCRE2_PARTIAL_SOFT is set, the return code
3530 PCRE2_ERROR_NOMATCH is converted into PCRE2_ERROR_PARTIAL if the end of the
3531 subject is reached, there have been no complete matches, but there is still at
3532 least one matching possibility. The portion of the string that was inspected
3533 when the longest partial match was found is set as the first matching string in
3534 both cases. There is a more detailed discussion of partial and multi-segment
3535 matching, with examples, in the
3536 .\" HREF
3537 \fBpcre2partial\fP
3538 .\"
3539 documentation.
3540 .sp
3541   PCRE2_DFA_SHORTEST
3542 .sp
3543 Setting the PCRE2_DFA_SHORTEST option causes the matching algorithm to stop as
3544 soon as it has found one match. Because of the way the alternative algorithm
3545 works, this is necessarily the shortest possible match at the first possible
3546 matching point in the subject string.
3547 .sp
3548   PCRE2_DFA_RESTART
3549 .sp
3550 When \fBpcre2_dfa_match()\fP returns a partial match, it is possible to call it
3551 again, with additional subject characters, and have it continue with the same
3552 match. The PCRE2_DFA_RESTART option requests this action; when it is set, the
3553 \fIworkspace\fP and \fIwscount\fP options must reference the same vector as
3554 before because data about the match so far is left in them after a partial
3555 match. There is more discussion of this facility in the
3556 .\" HREF
3557 \fBpcre2partial\fP
3558 .\"
3559 documentation.
3560 .
3561 .
3562 .SS "Successful returns from \fBpcre2_dfa_match()\fP"
3563 .rs
3564 .sp
3565 When \fBpcre2_dfa_match()\fP succeeds, it may have matched more than one
3566 substring in the subject. Note, however, that all the matches from one run of
3567 the function start at the same point in the subject. The shorter matches are
3568 all initial substrings of the longer matches. For example, if the pattern
3569 .sp
3570   <.*>
3571 .sp
3572 is matched against the string
3573 .sp
3574   This is <something> <something else> <something further> no more
3575 .sp
3576 the three matched strings are
3577 .sp
3578   <something> <something else> <something further>
3579   <something> <something else>
3580   <something>
3581 .sp
3582 On success, the yield of the function is a number greater than zero, which is
3583 the number of matched substrings. The offsets of the substrings are returned in
3584 the ovector, and can be extracted by number in the same way as for
3585 \fBpcre2_match()\fP, but the numbers bear no relation to any capturing groups
3586 that may exist in the pattern, because DFA matching does not support group
3587 capture.
3588 .P
3589 Calls to the convenience functions that extract substrings by name
3590 return the error PCRE2_ERROR_DFA_UFUNC (unsupported function) if used after a
3591 DFA match. The convenience functions that extract substrings by number never
3592 return PCRE2_ERROR_NOSUBSTRING.
3593 .P
3594 The matched strings are stored in the ovector in reverse order of length; that
3595 is, the longest matching string is first. If there were too many matches to fit
3596 into the ovector, the yield of the function is zero, and the vector is filled
3597 with the longest matches.
3598 .P
3599 NOTE: PCRE2's "auto-possessification" optimization usually applies to character
3600 repeats at the end of a pattern (as well as internally). For example, the
3601 pattern "a\ed+" is compiled as if it were "a\ed++". For DFA matching, this
3602 means that only one possible match is found. If you really do want multiple
3603 matches in such cases, either use an ungreedy repeat such as "a\ed+?" or set
3604 the PCRE2_NO_AUTO_POSSESS option when compiling.
3605 .
3606 .
3607 .SS "Error returns from \fBpcre2_dfa_match()\fP"
3608 .rs
3609 .sp
3610 The \fBpcre2_dfa_match()\fP function returns a negative number when it fails.
3611 Many of the errors are the same as for \fBpcre2_match()\fP, as described
3612 .\" HTML <a href="#errorlist">
3613 .\" </a>
3614 above.
3615 .\"
3616 There are in addition the following errors that are specific to
3617 \fBpcre2_dfa_match()\fP:
3618 .sp
3619   PCRE2_ERROR_DFA_UITEM
3620 .sp
3621 This return is given if \fBpcre2_dfa_match()\fP encounters an item in the
3622 pattern that it does not support, for instance, the use of \eC in a UTF mode or
3623 a backreference.
3624 .sp
3625   PCRE2_ERROR_DFA_UCOND
3626 .sp
3627 This return is given if \fBpcre2_dfa_match()\fP encounters a condition item
3628 that uses a backreference for the condition, or a test for recursion in a
3629 specific group. These are not supported.
3630 .sp
3631   PCRE2_ERROR_DFA_WSSIZE
3632 .sp
3633 This return is given if \fBpcre2_dfa_match()\fP runs out of space in the
3634 \fIworkspace\fP vector.
3635 .sp
3636   PCRE2_ERROR_DFA_RECURSE
3637 .sp
3638 When a recursive subpattern is processed, the matching function calls itself
3639 recursively, using private memory for the ovector and \fIworkspace\fP. This
3640 error is given if the internal ovector is not large enough. This should be
3641 extremely rare, as a vector of size 1000 is used.
3642 .sp
3643   PCRE2_ERROR_DFA_BADRESTART
3644 .sp
3645 When \fBpcre2_dfa_match()\fP is called with the \fBPCRE2_DFA_RESTART\fP option,
3646 some plausibility checks are made on the contents of the workspace, which
3647 should contain data about the previous partial match. If any of these checks
3648 fail, this error is given.
3649 .
3650 .
3651 .SH "SEE ALSO"
3652 .rs
3653 .sp
3654 \fBpcre2build\fP(3), \fBpcre2callout\fP(3), \fBpcre2demo(3)\fP,
3655 \fBpcre2matching\fP(3), \fBpcre2partial\fP(3), \fBpcre2posix\fP(3),
3656 \fBpcre2sample\fP(3), \fBpcre2unicode\fP(3).
3657 .
3658 .
3659 .SH AUTHOR
3660 .rs
3661 .sp
3662 .nf
3663 Philip Hazel
3664 University Computing Service
3665 Cambridge, England.
3666 .fi
3667 .
3668 .
3669 .SH REVISION
3670 .rs
3671 .sp
3672 .nf
3673 Last updated: 07 September 2018
3674 Copyright (c) 1997-2018 University of Cambridge.
3675 .fi