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