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