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