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