1PCREAPI(3) Library Functions Manual PCREAPI(3)
2
3
4
6 PCRE - Perl-compatible regular expressions
7
8 #include <pcre.h>
9
11
12 pcre *pcre_compile(const char *pattern, int options,
13 const char **errptr, int *erroffset,
14 const unsigned char *tableptr);
15
16 pcre *pcre_compile2(const char *pattern, int options,
17 int *errorcodeptr,
18 const char **errptr, int *erroffset,
19 const unsigned char *tableptr);
20
21 pcre_extra *pcre_study(const pcre *code, int options,
22 const char **errptr);
23
24 void pcre_free_study(pcre_extra *extra);
25
26 int pcre_exec(const pcre *code, const pcre_extra *extra,
27 const char *subject, int length, int startoffset,
28 int options, int *ovector, int ovecsize);
29
30 int pcre_dfa_exec(const pcre *code, const pcre_extra *extra,
31 const char *subject, int length, int startoffset,
32 int options, int *ovector, int ovecsize,
33 int *workspace, int wscount);
34
36
37 int pcre_copy_named_substring(const pcre *code,
38 const char *subject, int *ovector,
39 int stringcount, const char *stringname,
40 char *buffer, int buffersize);
41
42 int pcre_copy_substring(const char *subject, int *ovector,
43 int stringcount, int stringnumber, char *buffer,
44 int buffersize);
45
46 int pcre_get_named_substring(const pcre *code,
47 const char *subject, int *ovector,
48 int stringcount, const char *stringname,
49 const char **stringptr);
50
51 int pcre_get_stringnumber(const pcre *code,
52 const char *name);
53
54 int pcre_get_stringtable_entries(const pcre *code,
55 const char *name, char **first, char **last);
56
57 int pcre_get_substring(const char *subject, int *ovector,
58 int stringcount, int stringnumber,
59 const char **stringptr);
60
61 int pcre_get_substring_list(const char *subject,
62 int *ovector, int stringcount, const char ***listptr);
63
64 void pcre_free_substring(const char *stringptr);
65
66 void pcre_free_substring_list(const char **stringptr);
67
69
70 int pcre_jit_exec(const pcre *code, const pcre_extra *extra,
71 const char *subject, int length, int startoffset,
72 int options, int *ovector, int ovecsize,
73 pcre_jit_stack *jstack);
74
75 pcre_jit_stack *pcre_jit_stack_alloc(int startsize, int maxsize);
76
77 void pcre_jit_stack_free(pcre_jit_stack *stack);
78
79 void pcre_assign_jit_stack(pcre_extra *extra,
80 pcre_jit_callback callback, void *data);
81
82 const unsigned char *pcre_maketables(void);
83
84 int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
85 int what, void *where);
86
87 int pcre_refcount(pcre *code, int adjust);
88
89 int pcre_config(int what, void *where);
90
91 const char *pcre_version(void);
92
93 int pcre_pattern_to_host_byte_order(pcre *code,
94 pcre_extra *extra, const unsigned char *tables);
95
97
98 void *(*pcre_malloc)(size_t);
99
100 void (*pcre_free)(void *);
101
102 void *(*pcre_stack_malloc)(size_t);
103
104 void (*pcre_stack_free)(void *);
105
106 int (*pcre_callout)(pcre_callout_block *);
107
108 int (*pcre_stack_guard)(void);
109
111
112 As well as support for 8-bit character strings, PCRE also supports
113 16-bit strings (from release 8.30) and 32-bit strings (from release
114 8.32), by means of two additional libraries. They can be built as well
115 as, or instead of, the 8-bit library. To avoid too much complication,
116 this document describes the 8-bit versions of the functions, with only
117 occasional references to the 16-bit and 32-bit libraries.
118
119 The 16-bit and 32-bit functions operate in the same way as their 8-bit
120 counterparts; they just use different data types for their arguments
121 and results, and their names start with pcre16_ or pcre32_ instead of
122 pcre_. For every option that has UTF8 in its name (for example,
123 PCRE_UTF8), there are corresponding 16-bit and 32-bit names with UTF8
124 replaced by UTF16 or UTF32, respectively. This facility is in fact just
125 cosmetic; the 16-bit and 32-bit option names define the same bit val‐
126 ues.
127
128 References to bytes and UTF-8 in this document should be read as refer‐
129 ences to 16-bit data units and UTF-16 when using the 16-bit library, or
130 32-bit data units and UTF-32 when using the 32-bit library, unless
131 specified otherwise. More details of the specific differences for the
132 16-bit and 32-bit libraries are given in the pcre16 and pcre32 pages.
133
135
136 PCRE has its own native API, which is described in this document. There
137 are also some wrapper functions (for the 8-bit library only) that cor‐
138 respond to the POSIX regular expression API, but they do not give
139 access to all the functionality. They are described in the pcreposix
140 documentation. Both of these APIs define a set of C function calls. A
141 C++ wrapper (again for the 8-bit library only) is also distributed with
142 PCRE. It is documented in the pcrecpp page.
143
144 The native API C function prototypes are defined in the header file
145 pcre.h, and on Unix-like systems the (8-bit) library itself is called
146 libpcre. It can normally be accessed by adding -lpcre to the command
147 for linking an application that uses PCRE. The header file defines the
148 macros PCRE_MAJOR and PCRE_MINOR to contain the major and minor release
149 numbers for the library. Applications can use these to include support
150 for different releases of PCRE.
151
152 In a Windows environment, if you want to statically link an application
153 program against a non-dll pcre.a file, you must define PCRE_STATIC
154 before including pcre.h or pcrecpp.h, because otherwise the pcre_mal‐
155 loc() and pcre_free() exported functions will be declared
156 __declspec(dllimport), with unwanted results.
157
158 The functions pcre_compile(), pcre_compile2(), pcre_study(), and
159 pcre_exec() are used for compiling and matching regular expressions in
160 a Perl-compatible manner. A sample program that demonstrates the sim‐
161 plest way of using them is provided in the file called pcredemo.c in
162 the PCRE source distribution. A listing of this program is given in the
163 pcredemo documentation, and the pcresample documentation describes how
164 to compile and run it.
165
166 Just-in-time compiler support is an optional feature of PCRE that can
167 be built in appropriate hardware environments. It greatly speeds up the
168 matching performance of many patterns. Simple programs can easily
169 request that it be used if available, by setting an option that is
170 ignored when it is not relevant. More complicated programs might need
171 to make use of the functions pcre_jit_stack_alloc(),
172 pcre_jit_stack_free(), and pcre_assign_jit_stack() in order to control
173 the JIT code's memory usage.
174
175 From release 8.32 there is also a direct interface for JIT execution,
176 which gives improved performance. The JIT-specific functions are dis‐
177 cussed in the pcrejit documentation.
178
179 A second matching function, pcre_dfa_exec(), which is not Perl-compati‐
180 ble, is also provided. This uses a different algorithm for the match‐
181 ing. The alternative algorithm finds all possible matches (at a given
182 point in the subject), and scans the subject just once (unless there
183 are lookbehind assertions). However, this algorithm does not return
184 captured substrings. A description of the two matching algorithms and
185 their advantages and disadvantages is given in the pcrematching docu‐
186 mentation.
187
188 In addition to the main compiling and matching functions, there are
189 convenience functions for extracting captured substrings from a subject
190 string that is matched by pcre_exec(). They are:
191
192 pcre_copy_substring()
193 pcre_copy_named_substring()
194 pcre_get_substring()
195 pcre_get_named_substring()
196 pcre_get_substring_list()
197 pcre_get_stringnumber()
198 pcre_get_stringtable_entries()
199
200 pcre_free_substring() and pcre_free_substring_list() are also provided,
201 to free the memory used for extracted strings.
202
203 The function pcre_maketables() is used to build a set of character
204 tables in the current locale for passing to pcre_compile(),
205 pcre_exec(), or pcre_dfa_exec(). This is an optional facility that is
206 provided for specialist use. Most commonly, no special tables are
207 passed, in which case internal tables that are generated when PCRE is
208 built are used.
209
210 The function pcre_fullinfo() is used to find out information about a
211 compiled pattern. The function pcre_version() returns a pointer to a
212 string containing the version of PCRE and its date of release.
213
214 The function pcre_refcount() maintains a reference count in a data
215 block containing a compiled pattern. This is provided for the benefit
216 of object-oriented applications.
217
218 The global variables pcre_malloc and pcre_free initially contain the
219 entry points of the standard malloc() and free() functions, respec‐
220 tively. PCRE calls the memory management functions via these variables,
221 so a calling program can replace them if it wishes to intercept the
222 calls. This should be done before calling any PCRE functions.
223
224 The global variables pcre_stack_malloc and pcre_stack_free are also
225 indirections to memory management functions. These special functions
226 are used only when PCRE is compiled to use the heap for remembering
227 data, instead of recursive function calls, when running the pcre_exec()
228 function. See the pcrebuild documentation for details of how to do
229 this. It is a non-standard way of building PCRE, for use in environ‐
230 ments that have limited stacks. Because of the greater use of memory
231 management, it runs more slowly. Separate functions are provided so
232 that special-purpose external code can be used for this case. When
233 used, these functions always allocate memory blocks of the same size.
234 There is a discussion about PCRE's stack usage in the pcrestack docu‐
235 mentation.
236
237 The global variable pcre_callout initially contains NULL. It can be set
238 by the caller to a "callout" function, which PCRE will then call at
239 specified points during a matching operation. Details are given in the
240 pcrecallout documentation.
241
242 The global variable pcre_stack_guard initially contains NULL. It can be
243 set by the caller to a function that is called by PCRE whenever it
244 starts to compile a parenthesized part of a pattern. When parentheses
245 are nested, PCRE uses recursive function calls, which use up the system
246 stack. This function is provided so that applications with restricted
247 stacks can force a compilation error if the stack runs out. The func‐
248 tion should return zero if all is well, or non-zero to force an error.
249
251
252 PCRE supports five different conventions for indicating line breaks in
253 strings: a single CR (carriage return) character, a single LF (line‐
254 feed) character, the two-character sequence CRLF, any of the three pre‐
255 ceding, or any Unicode newline sequence. The Unicode newline sequences
256 are the three just mentioned, plus the single characters VT (vertical
257 tab, U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line
258 separator, U+2028), and PS (paragraph separator, U+2029).
259
260 Each of the first three conventions is used by at least one operating
261 system as its standard newline sequence. When PCRE is built, a default
262 can be specified. The default default is LF, which is the Unix stan‐
263 dard. When PCRE is run, the default can be overridden, either when a
264 pattern is compiled, or when it is matched.
265
266 At compile time, the newline convention can be specified by the options
267 argument of pcre_compile(), or it can be specified by special text at
268 the start of the pattern itself; this overrides any other settings. See
269 the pcrepattern page for details of the special character sequences.
270
271 In the PCRE documentation the word "newline" is used to mean "the char‐
272 acter or pair of characters that indicate a line break". The choice of
273 newline convention affects the handling of the dot, circumflex, and
274 dollar metacharacters, the handling of #-comments in /x mode, and, when
275 CRLF is a recognized line ending sequence, the match position advance‐
276 ment for a non-anchored pattern. There is more detail about this in the
277 section on pcre_exec() options below.
278
279 The choice of newline convention does not affect the interpretation of
280 the \n or \r escape sequences, nor does it affect what \R matches,
281 which is controlled in a similar way, but by separate options.
282
284
285 The PCRE functions can be used in multi-threading applications, with
286 the proviso that the memory management functions pointed to by
287 pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the
288 callout and stack-checking functions pointed to by pcre_callout and
289 pcre_stack_guard, are shared by all threads.
290
291 The compiled form of a regular expression is not altered during match‐
292 ing, so the same compiled pattern can safely be used by several threads
293 at once.
294
295 If the just-in-time optimization feature is being used, it needs sepa‐
296 rate memory stack areas for each thread. See the pcrejit documentation
297 for more details.
298
300
301 The compiled form of a regular expression can be saved and re-used at a
302 later time, possibly by a different program, and even on a host other
303 than the one on which it was compiled. Details are given in the
304 pcreprecompile documentation, which includes a description of the
305 pcre_pattern_to_host_byte_order() function. However, compiling a regu‐
306 lar expression with one version of PCRE for use with a different ver‐
307 sion is not guaranteed to work and may cause crashes.
308
310
311 int pcre_config(int what, void *where);
312
313 The function pcre_config() makes it possible for a PCRE client to dis‐
314 cover which optional features have been compiled into the PCRE library.
315 The pcrebuild documentation has more details about these optional fea‐
316 tures.
317
318 The first argument for pcre_config() is an integer, specifying which
319 information is required; the second argument is a pointer to a variable
320 into which the information is placed. The returned value is zero on
321 success, or the negative error code PCRE_ERROR_BADOPTION if the value
322 in the first argument is not recognized. The following information is
323 available:
324
325 PCRE_CONFIG_UTF8
326
327 The output is an integer that is set to one if UTF-8 support is avail‐
328 able; otherwise it is set to zero. This value should normally be given
329 to the 8-bit version of this function, pcre_config(). If it is given to
330 the 16-bit or 32-bit version of this function, the result is
331 PCRE_ERROR_BADOPTION.
332
333 PCRE_CONFIG_UTF16
334
335 The output is an integer that is set to one if UTF-16 support is avail‐
336 able; otherwise it is set to zero. This value should normally be given
337 to the 16-bit version of this function, pcre16_config(). If it is given
338 to the 8-bit or 32-bit version of this function, the result is
339 PCRE_ERROR_BADOPTION.
340
341 PCRE_CONFIG_UTF32
342
343 The output is an integer that is set to one if UTF-32 support is avail‐
344 able; otherwise it is set to zero. This value should normally be given
345 to the 32-bit version of this function, pcre32_config(). If it is given
346 to the 8-bit or 16-bit version of this function, the result is
347 PCRE_ERROR_BADOPTION.
348
349 PCRE_CONFIG_UNICODE_PROPERTIES
350
351 The output is an integer that is set to one if support for Unicode
352 character properties is available; otherwise it is set to zero.
353
354 PCRE_CONFIG_JIT
355
356 The output is an integer that is set to one if support for just-in-time
357 compiling is available; otherwise it is set to zero.
358
359 PCRE_CONFIG_JITTARGET
360
361 The output is a pointer to a zero-terminated "const char *" string. If
362 JIT support is available, the string contains the name of the architec‐
363 ture for which the JIT compiler is configured, for example "x86 32bit
364 (little endian + unaligned)". If JIT support is not available, the
365 result is NULL.
366
367 PCRE_CONFIG_NEWLINE
368
369 The output is an integer whose value specifies the default character
370 sequence that is recognized as meaning "newline". The values that are
371 supported in ASCII/Unicode environments are: 10 for LF, 13 for CR, 3338
372 for CRLF, -2 for ANYCRLF, and -1 for ANY. In EBCDIC environments, CR,
373 ANYCRLF, and ANY yield the same values. However, the value for LF is
374 normally 21, though some EBCDIC environments use 37. The corresponding
375 values for CRLF are 3349 and 3365. The default should normally corre‐
376 spond to the standard sequence for your operating system.
377
378 PCRE_CONFIG_BSR
379
380 The output is an integer whose value indicates what character sequences
381 the \R escape sequence matches by default. A value of 0 means that \R
382 matches any Unicode line ending sequence; a value of 1 means that \R
383 matches only CR, LF, or CRLF. The default can be overridden when a pat‐
384 tern is compiled or matched.
385
386 PCRE_CONFIG_LINK_SIZE
387
388 The output is an integer that contains the number of bytes used for
389 internal linkage in compiled regular expressions. For the 8-bit
390 library, the value can be 2, 3, or 4. For the 16-bit library, the value
391 is either 2 or 4 and is still a number of bytes. For the 32-bit
392 library, the value is either 2 or 4 and is still a number of bytes. The
393 default value of 2 is sufficient for all but the most massive patterns,
394 since it allows the compiled pattern to be up to 64K in size. Larger
395 values allow larger regular expressions to be compiled, at the expense
396 of slower matching.
397
398 PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
399
400 The output is an integer that contains the threshold above which the
401 POSIX interface uses malloc() for output vectors. Further details are
402 given in the pcreposix documentation.
403
404 PCRE_CONFIG_PARENS_LIMIT
405
406 The output is a long integer that gives the maximum depth of nesting of
407 parentheses (of any kind) in a pattern. This limit is imposed to cap
408 the amount of system stack used when a pattern is compiled. It is spec‐
409 ified when PCRE is built; the default is 250. This limit does not take
410 into account the stack that may already be used by the calling applica‐
411 tion. For finer control over compilation stack usage, you can set a
412 pointer to an external checking function in pcre_stack_guard.
413
414 PCRE_CONFIG_MATCH_LIMIT
415
416 The output is a long integer that gives the default limit for the num‐
417 ber of internal matching function calls in a pcre_exec() execution.
418 Further details are given with pcre_exec() below.
419
420 PCRE_CONFIG_MATCH_LIMIT_RECURSION
421
422 The output is a long integer that gives the default limit for the depth
423 of recursion when calling the internal matching function in a
424 pcre_exec() execution. Further details are given with pcre_exec()
425 below.
426
427 PCRE_CONFIG_STACKRECURSE
428
429 The output is an integer that is set to one if internal recursion when
430 running pcre_exec() is implemented by recursive function calls that use
431 the stack to remember their state. This is the usual way that PCRE is
432 compiled. The output is zero if PCRE was compiled to use blocks of data
433 on the heap instead of recursive function calls. In this case,
434 pcre_stack_malloc and pcre_stack_free are called to manage memory
435 blocks on the heap, thus avoiding the use of the stack.
436
438
439 pcre *pcre_compile(const char *pattern, int options,
440 const char **errptr, int *erroffset,
441 const unsigned char *tableptr);
442
443 pcre *pcre_compile2(const char *pattern, int options,
444 int *errorcodeptr,
445 const char **errptr, int *erroffset,
446 const unsigned char *tableptr);
447
448 Either of the functions pcre_compile() or pcre_compile2() can be called
449 to compile a pattern into an internal form. The only difference between
450 the two interfaces is that pcre_compile2() has an additional argument,
451 errorcodeptr, via which a numerical error code can be returned. To
452 avoid too much repetition, we refer just to pcre_compile() below, but
453 the information applies equally to pcre_compile2().
454
455 The pattern is a C string terminated by a binary zero, and is passed in
456 the pattern argument. A pointer to a single block of memory that is
457 obtained via pcre_malloc is returned. This contains the compiled code
458 and related data. The pcre type is defined for the returned block; this
459 is a typedef for a structure whose contents are not externally defined.
460 It is up to the caller to free the memory (via pcre_free) when it is no
461 longer required.
462
463 Although the compiled code of a PCRE regex is relocatable, that is, it
464 does not depend on memory location, the complete pcre data block is not
465 fully relocatable, because it may contain a copy of the tableptr argu‐
466 ment, which is an address (see below).
467
468 The options argument contains various bit settings that affect the com‐
469 pilation. It should be zero if no options are required. The available
470 options are described below. Some of them (in particular, those that
471 are compatible with Perl, but some others as well) can also be set and
472 unset from within the pattern (see the detailed description in the
473 pcrepattern documentation). For those options that can be different in
474 different parts of the pattern, the contents of the options argument
475 specifies their settings at the start of compilation and execution. The
476 PCRE_ANCHORED, PCRE_BSR_xxx, PCRE_NEWLINE_xxx, PCRE_NO_UTF8_CHECK, and
477 PCRE_NO_START_OPTIMIZE options can be set at the time of matching as
478 well as at compile time.
479
480 If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise,
481 if compilation of a pattern fails, pcre_compile() returns NULL, and
482 sets the variable pointed to by errptr to point to a textual error mes‐
483 sage. This is a static string that is part of the library. You must not
484 try to free it. Normally, the offset from the start of the pattern to
485 the data unit that was being processed when the error was discovered is
486 placed in the variable pointed to by erroffset, which must not be NULL
487 (if it is, an immediate error is given). However, for an invalid UTF-8
488 or UTF-16 string, the offset is that of the first data unit of the
489 failing character.
490
491 Some errors are not detected until the whole pattern has been scanned;
492 in these cases, the offset passed back is the length of the pattern.
493 Note that the offset is in data units, not characters, even in a UTF
494 mode. It may sometimes point into the middle of a UTF-8 or UTF-16 char‐
495 acter.
496
497 If pcre_compile2() is used instead of pcre_compile(), and the error‐
498 codeptr argument is not NULL, a non-zero error code number is returned
499 via this argument in the event of an error. This is in addition to the
500 textual error message. Error codes and messages are listed below.
501
502 If the final argument, tableptr, is NULL, PCRE uses a default set of
503 character tables that are built when PCRE is compiled, using the
504 default C locale. Otherwise, tableptr must be an address that is the
505 result of a call to pcre_maketables(). This value is stored with the
506 compiled pattern, and used again by pcre_exec() and pcre_dfa_exec()
507 when the pattern is matched. For more discussion, see the section on
508 locale support below.
509
510 This code fragment shows a typical straightforward call to pcre_com‐
511 pile():
512
513 pcre *re;
514 const char *error;
515 int erroffset;
516 re = pcre_compile(
517 "^A.*Z", /* the pattern */
518 0, /* default options */
519 &error, /* for error message */
520 &erroffset, /* for error offset */
521 NULL); /* use default character tables */
522
523 The following names for option bits are defined in the pcre.h header
524 file:
525
526 PCRE_ANCHORED
527
528 If this bit is set, the pattern is forced to be "anchored", that is, it
529 is constrained to match only at the first matching point in the string
530 that is being searched (the "subject string"). This effect can also be
531 achieved by appropriate constructs in the pattern itself, which is the
532 only way to do it in Perl.
533
534 PCRE_AUTO_CALLOUT
535
536 If this bit is set, pcre_compile() automatically inserts callout items,
537 all with number 255, before each pattern item. For discussion of the
538 callout facility, see the pcrecallout documentation.
539
540 PCRE_BSR_ANYCRLF
541 PCRE_BSR_UNICODE
542
543 These options (which are mutually exclusive) control what the \R escape
544 sequence matches. The choice is either to match only CR, LF, or CRLF,
545 or to match any Unicode newline sequence. The default is specified when
546 PCRE is built. It can be overridden from within the pattern, or by set‐
547 ting an option when a compiled pattern is matched.
548
549 PCRE_CASELESS
550
551 If this bit is set, letters in the pattern match both upper and lower
552 case letters. It is equivalent to Perl's /i option, and it can be
553 changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE
554 always understands the concept of case for characters whose values are
555 less than 128, so caseless matching is always possible. For characters
556 with higher values, the concept of case is supported if PCRE is com‐
557 piled with Unicode property support, but not otherwise. If you want to
558 use caseless matching for characters 128 and above, you must ensure
559 that PCRE is compiled with Unicode property support as well as with
560 UTF-8 support.
561
562 PCRE_DOLLAR_ENDONLY
563
564 If this bit is set, a dollar metacharacter in the pattern matches only
565 at the end of the subject string. Without this option, a dollar also
566 matches immediately before a newline at the end of the string (but not
567 before any other newlines). The PCRE_DOLLAR_ENDONLY option is ignored
568 if PCRE_MULTILINE is set. There is no equivalent to this option in
569 Perl, and no way to set it within a pattern.
570
571 PCRE_DOTALL
572
573 If this bit is set, a dot metacharacter in the pattern matches a char‐
574 acter of any value, including one that indicates a newline. However, it
575 only ever matches one character, even if newlines are coded as CRLF.
576 Without this option, a dot does not match when the current position is
577 at a newline. This option is equivalent to Perl's /s option, and it can
578 be changed within a pattern by a (?s) option setting. A negative class
579 such as [^a] always matches newline characters, independent of the set‐
580 ting of this option.
581
582 PCRE_DUPNAMES
583
584 If this bit is set, names used to identify capturing subpatterns need
585 not be unique. This can be helpful for certain types of pattern when it
586 is known that only one instance of the named subpattern can ever be
587 matched. There are more details of named subpatterns below; see also
588 the pcrepattern documentation.
589
590 PCRE_EXTENDED
591
592 If this bit is set, most white space characters in the pattern are
593 totally ignored except when escaped or inside a character class. How‐
594 ever, white space is not allowed within sequences such as (?> that
595 introduce various parenthesized subpatterns, nor within a numerical
596 quantifier such as {1,3}. However, ignorable white space is permitted
597 between an item and a following quantifier and between a quantifier and
598 a following + that indicates possessiveness.
599
600 White space did not used to include the VT character (code 11), because
601 Perl did not treat this character as white space. However, Perl changed
602 at release 5.18, so PCRE followed at release 8.34, and VT is now
603 treated as white space.
604
605 PCRE_EXTENDED also causes characters between an unescaped # outside a
606 character class and the next newline, inclusive, to be ignored.
607 PCRE_EXTENDED is equivalent to Perl's /x option, and it can be changed
608 within a pattern by a (?x) option setting.
609
610 Which characters are interpreted as newlines is controlled by the
611 options passed to pcre_compile() or by a special sequence at the start
612 of the pattern, as described in the section entitled "Newline conven‐
613 tions" in the pcrepattern documentation. Note that the end of this type
614 of comment is a literal newline sequence in the pattern; escape
615 sequences that happen to represent a newline do not count.
616
617 This option makes it possible to include comments inside complicated
618 patterns. Note, however, that this applies only to data characters.
619 White space characters may never appear within special character
620 sequences in a pattern, for example within the sequence (?( that intro‐
621 duces a conditional subpattern.
622
623 PCRE_EXTRA
624
625 This option was invented in order to turn on additional functionality
626 of PCRE that is incompatible with Perl, but it is currently of very
627 little use. When set, any backslash in a pattern that is followed by a
628 letter that has no special meaning causes an error, thus reserving
629 these combinations for future expansion. By default, as in Perl, a
630 backslash followed by a letter with no special meaning is treated as a
631 literal. (Perl can, however, be persuaded to give an error for this, by
632 running it with the -w option.) There are at present no other features
633 controlled by this option. It can also be set by a (?X) option setting
634 within a pattern.
635
636 PCRE_FIRSTLINE
637
638 If this option is set, an unanchored pattern is required to match
639 before or at the first newline in the subject string, though the
640 matched text may continue over the newline.
641
642 PCRE_JAVASCRIPT_COMPAT
643
644 If this option is set, PCRE's behaviour is changed in some ways so that
645 it is compatible with JavaScript rather than Perl. The changes are as
646 follows:
647
648 (1) A lone closing square bracket in a pattern causes a compile-time
649 error, because this is illegal in JavaScript (by default it is treated
650 as a data character). Thus, the pattern AB]CD becomes illegal when this
651 option is set.
652
653 (2) At run time, a back reference to an unset subpattern group matches
654 an empty string (by default this causes the current matching alterna‐
655 tive to fail). A pattern such as (\1)(a) succeeds when this option is
656 set (assuming it can find an "a" in the subject), whereas it fails by
657 default, for Perl compatibility.
658
659 (3) \U matches an upper case "U" character; by default \U causes a com‐
660 pile time error (Perl uses \U to upper case subsequent characters).
661
662 (4) \u matches a lower case "u" character unless it is followed by four
663 hexadecimal digits, in which case the hexadecimal number defines the
664 code point to match. By default, \u causes a compile time error (Perl
665 uses it to upper case the following character).
666
667 (5) \x matches a lower case "x" character unless it is followed by two
668 hexadecimal digits, in which case the hexadecimal number defines the
669 code point to match. By default, as in Perl, a hexadecimal number is
670 always expected after \x, but it may have zero, one, or two digits (so,
671 for example, \xz matches a binary zero character followed by z).
672
673 PCRE_MULTILINE
674
675 By default, for the purposes of matching "start of line" and "end of
676 line", PCRE treats the subject string as consisting of a single line of
677 characters, even if it actually contains newlines. The "start of line"
678 metacharacter (^) matches only at the start of the string, and the "end
679 of line" metacharacter ($) matches only at the end of the string, or
680 before a terminating newline (except when PCRE_DOLLAR_ENDONLY is set).
681 Note, however, that unless PCRE_DOTALL is set, the "any character"
682 metacharacter (.) does not match at a newline. This behaviour (for ^,
683 $, and dot) is the same as Perl.
684
685 When PCRE_MULTILINE it is set, the "start of line" and "end of line"
686 constructs match immediately following or immediately before internal
687 newlines in the subject string, respectively, as well as at the very
688 start and end. This is equivalent to Perl's /m option, and it can be
689 changed within a pattern by a (?m) option setting. If there are no new‐
690 lines in a subject string, or no occurrences of ^ or $ in a pattern,
691 setting PCRE_MULTILINE has no effect.
692
693 PCRE_NEVER_UTF
694
695 This option locks out interpretation of the pattern as UTF-8 (or UTF-16
696 or UTF-32 in the 16-bit and 32-bit libraries). In particular, it pre‐
697 vents the creator of the pattern from switching to UTF interpretation
698 by starting the pattern with (*UTF). This may be useful in applications
699 that process patterns from external sources. The combination of
700 PCRE_UTF8 and PCRE_NEVER_UTF also causes an error.
701
702 PCRE_NEWLINE_CR
703 PCRE_NEWLINE_LF
704 PCRE_NEWLINE_CRLF
705 PCRE_NEWLINE_ANYCRLF
706 PCRE_NEWLINE_ANY
707
708 These options override the default newline definition that was chosen
709 when PCRE was built. Setting the first or the second specifies that a
710 newline is indicated by a single character (CR or LF, respectively).
711 Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by the
712 two-character CRLF sequence. Setting PCRE_NEWLINE_ANYCRLF specifies
713 that any of the three preceding sequences should be recognized. Setting
714 PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should be
715 recognized.
716
717 In an ASCII/Unicode environment, the Unicode newline sequences are the
718 three just mentioned, plus the single characters VT (vertical tab,
719 U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line sep‐
720 arator, U+2028), and PS (paragraph separator, U+2029). For the 8-bit
721 library, the last two are recognized only in UTF-8 mode.
722
723 When PCRE is compiled to run in an EBCDIC (mainframe) environment, the
724 code for CR is 0x0d, the same as ASCII. However, the character code for
725 LF is normally 0x15, though in some EBCDIC environments 0x25 is used.
726 Whichever of these is not LF is made to correspond to Unicode's NEL
727 character. EBCDIC codes are all less than 256. For more details, see
728 the pcrebuild documentation.
729
730 The newline setting in the options word uses three bits that are
731 treated as a number, giving eight possibilities. Currently only six are
732 used (default plus the five values above). This means that if you set
733 more than one newline option, the combination may or may not be sensi‐
734 ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to
735 PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers and
736 cause an error.
737
738 The only time that a line break in a pattern is specially recognized
739 when compiling is when PCRE_EXTENDED is set. CR and LF are white space
740 characters, and so are ignored in this mode. Also, an unescaped # out‐
741 side a character class indicates a comment that lasts until after the
742 next line break sequence. In other circumstances, line break sequences
743 in patterns are treated as literal data.
744
745 The newline option that is set at compile time becomes the default that
746 is used for pcre_exec() and pcre_dfa_exec(), but it can be overridden.
747
748 PCRE_NO_AUTO_CAPTURE
749
750 If this option is set, it disables the use of numbered capturing paren‐
751 theses in the pattern. Any opening parenthesis that is not followed by
752 ? behaves as if it were followed by ?: but named parentheses can still
753 be used for capturing (and they acquire numbers in the usual way).
754 There is no equivalent of this option in Perl.
755
756 PCRE_NO_AUTO_POSSESS
757
758 If this option is set, it disables "auto-possessification". This is an
759 optimization that, for example, turns a+b into a++b in order to avoid
760 backtracks into a+ that can never be successful. However, if callouts
761 are in use, auto-possessification means that some of them are never
762 taken. You can set this option if you want the matching functions to do
763 a full unoptimized search and run all the callouts, but it is mainly
764 provided for testing purposes.
765
766 PCRE_NO_START_OPTIMIZE
767
768 This is an option that acts at matching time; that is, it is really an
769 option for pcre_exec() or pcre_dfa_exec(). If it is set at compile
770 time, it is remembered with the compiled pattern and assumed at match‐
771 ing time. This is necessary if you want to use JIT execution, because
772 the JIT compiler needs to know whether or not this option is set. For
773 details see the discussion of PCRE_NO_START_OPTIMIZE below.
774
775 PCRE_UCP
776
777 This option changes the way PCRE processes \B, \b, \D, \d, \S, \s, \W,
778 \w, and some of the POSIX character classes. By default, only ASCII
779 characters are recognized, but if PCRE_UCP is set, Unicode properties
780 are used instead to classify characters. More details are given in the
781 section on generic character types in the pcrepattern page. If you set
782 PCRE_UCP, matching one of the items it affects takes much longer. The
783 option is available only if PCRE has been compiled with Unicode prop‐
784 erty support.
785
786 PCRE_UNGREEDY
787
788 This option inverts the "greediness" of the quantifiers so that they
789 are not greedy by default, but become greedy if followed by "?". It is
790 not compatible with Perl. It can also be set by a (?U) option setting
791 within the pattern.
792
793 PCRE_UTF8
794
795 This option causes PCRE to regard both the pattern and the subject as
796 strings of UTF-8 characters instead of single-byte strings. However, it
797 is available only when PCRE is built to include UTF support. If not,
798 the use of this option provokes an error. Details of how this option
799 changes the behaviour of PCRE are given in the pcreunicode page.
800
801 PCRE_NO_UTF8_CHECK
802
803 When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
804 automatically checked. There is a discussion about the validity of
805 UTF-8 strings in the pcreunicode page. If an invalid UTF-8 sequence is
806 found, pcre_compile() returns an error. If you already know that your
807 pattern is valid, and you want to skip this check for performance rea‐
808 sons, you can set the PCRE_NO_UTF8_CHECK option. When it is set, the
809 effect of passing an invalid UTF-8 string as a pattern is undefined. It
810 may cause your program to crash or loop. Note that this option can also
811 be passed to pcre_exec() and pcre_dfa_exec(), to suppress the validity
812 checking of subject strings only. If the same string is being matched
813 many times, the option can be safely set for the second and subsequent
814 matchings to improve performance.
815
817
818 The following table lists the error codes than may be returned by
819 pcre_compile2(), along with the error messages that may be returned by
820 both compiling functions. Note that error messages are always 8-bit
821 ASCII strings, even in 16-bit or 32-bit mode. As PCRE has developed,
822 some error codes have fallen out of use. To avoid confusion, they have
823 not been re-used.
824
825 0 no error
826 1 \ at end of pattern
827 2 \c at end of pattern
828 3 unrecognized character follows \
829 4 numbers out of order in {} quantifier
830 5 number too big in {} quantifier
831 6 missing terminating ] for character class
832 7 invalid escape sequence in character class
833 8 range out of order in character class
834 9 nothing to repeat
835 10 [this code is not in use]
836 11 internal error: unexpected repeat
837 12 unrecognized character after (? or (?-
838 13 POSIX named classes are supported only within a class
839 14 missing )
840 15 reference to non-existent subpattern
841 16 erroffset passed as NULL
842 17 unknown option bit(s) set
843 18 missing ) after comment
844 19 [this code is not in use]
845 20 regular expression is too large
846 21 failed to get memory
847 22 unmatched parentheses
848 23 internal error: code overflow
849 24 unrecognized character after (?<
850 25 lookbehind assertion is not fixed length
851 26 malformed number or name after (?(
852 27 conditional group contains more than two branches
853 28 assertion expected after (?(
854 29 (?R or (?[+-]digits must be followed by )
855 30 unknown POSIX class name
856 31 POSIX collating elements are not supported
857 32 this version of PCRE is compiled without UTF support
858 33 [this code is not in use]
859 34 character value in \x{} or \o{} is too large
860 35 invalid condition (?(0)
861 36 \C not allowed in lookbehind assertion
862 37 PCRE does not support \L, \l, \N{name}, \U, or \u
863 38 number after (?C is > 255
864 39 closing ) for (?C expected
865 40 recursive call could loop indefinitely
866 41 unrecognized character after (?P
867 42 syntax error in subpattern name (missing terminator)
868 43 two named subpatterns have the same name
869 44 invalid UTF-8 string (specifically UTF-8)
870 45 support for \P, \p, and \X has not been compiled
871 46 malformed \P or \p sequence
872 47 unknown property name after \P or \p
873 48 subpattern name is too long (maximum 32 characters)
874 49 too many named subpatterns (maximum 10000)
875 50 [this code is not in use]
876 51 octal value is greater than \377 in 8-bit non-UTF-8 mode
877 52 internal error: overran compiling workspace
878 53 internal error: previously-checked referenced subpattern
879 not found
880 54 DEFINE group contains more than one branch
881 55 repeating a DEFINE group is not allowed
882 56 inconsistent NEWLINE options
883 57 \g is not followed by a braced, angle-bracketed, or quoted
884 name/number or by a plain number
885 58 a numbered reference must not be zero
886 59 an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)
887 60 (*VERB) not recognized or malformed
888 61 number is too big
889 62 subpattern name expected
890 63 digit expected after (?+
891 64 ] is an invalid data character in JavaScript compatibility mode
892 65 different names for subpatterns of the same number are
893 not allowed
894 66 (*MARK) must have an argument
895 67 this version of PCRE is not compiled with Unicode property
896 support
897 68 \c must be followed by an ASCII character
898 69 \k is not followed by a braced, angle-bracketed, or quoted name
899 70 internal error: unknown opcode in find_fixedlength()
900 71 \N is not supported in a class
901 72 too many forward references
902 73 disallowed Unicode code point (>= 0xd800 && <= 0xdfff)
903 74 invalid UTF-16 string (specifically UTF-16)
904 75 name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)
905 76 character value in \u.... sequence is too large
906 77 invalid UTF-32 string (specifically UTF-32)
907 78 setting UTF is disabled by the application
908 79 non-hex character in \x{} (closing brace missing?)
909 80 non-octal character in \o{} (closing brace missing?)
910 81 missing opening brace after \o
911 82 parentheses are too deeply nested
912 83 invalid range in character class
913 84 group name must start with a non-digit
914 85 parentheses are too deeply nested (stack check)
915
916 The numbers 32 and 10000 in errors 48 and 49 are defaults; different
917 values may be used if the limits were changed when PCRE was built.
918
920
921 pcre_extra *pcre_study(const pcre *code, int options,
922 const char **errptr);
923
924 If a compiled pattern is going to be used several times, it is worth
925 spending more time analyzing it in order to speed up the time taken for
926 matching. The function pcre_study() takes a pointer to a compiled pat‐
927 tern as its first argument. If studying the pattern produces additional
928 information that will help speed up matching, pcre_study() returns a
929 pointer to a pcre_extra block, in which the study_data field points to
930 the results of the study.
931
932 The returned value from pcre_study() can be passed directly to
933 pcre_exec() or pcre_dfa_exec(). However, a pcre_extra block also con‐
934 tains other fields that can be set by the caller before the block is
935 passed; these are described below in the section on matching a pattern.
936
937 If studying the pattern does not produce any useful information,
938 pcre_study() returns NULL by default. In that circumstance, if the
939 calling program wants to pass any of the other fields to pcre_exec() or
940 pcre_dfa_exec(), it must set up its own pcre_extra block. However, if
941 pcre_study() is called with the PCRE_STUDY_EXTRA_NEEDED option, it
942 returns a pcre_extra block even if studying did not find any additional
943 information. It may still return NULL, however, if an error occurs in
944 pcre_study().
945
946 The second argument of pcre_study() contains option bits. There are
947 three further options in addition to PCRE_STUDY_EXTRA_NEEDED:
948
949 PCRE_STUDY_JIT_COMPILE
950 PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
951 PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
952
953 If any of these are set, and the just-in-time compiler is available,
954 the pattern is further compiled into machine code that executes much
955 faster than the pcre_exec() interpretive matching function. If the
956 just-in-time compiler is not available, these options are ignored. All
957 undefined bits in the options argument must be zero.
958
959 JIT compilation is a heavyweight optimization. It can take some time
960 for patterns to be analyzed, and for one-off matches and simple pat‐
961 terns the benefit of faster execution might be offset by a much slower
962 study time. Not all patterns can be optimized by the JIT compiler. For
963 those that cannot be handled, matching automatically falls back to the
964 pcre_exec() interpreter. For more details, see the pcrejit documenta‐
965 tion.
966
967 The third argument for pcre_study() is a pointer for an error message.
968 If studying succeeds (even if no data is returned), the variable it
969 points to is set to NULL. Otherwise it is set to point to a textual
970 error message. This is a static string that is part of the library. You
971 must not try to free it. You should test the error pointer for NULL
972 after calling pcre_study(), to be sure that it has run successfully.
973
974 When you are finished with a pattern, you can free the memory used for
975 the study data by calling pcre_free_study(). This function was added to
976 the API for release 8.20. For earlier versions, the memory could be
977 freed with pcre_free(), just like the pattern itself. This will still
978 work in cases where JIT optimization is not used, but it is advisable
979 to change to the new function when convenient.
980
981 This is a typical way in which pcre_study() is used (except that in a
982 real application there should be tests for errors):
983
984 int rc;
985 pcre *re;
986 pcre_extra *sd;
987 re = pcre_compile("pattern", 0, &error, &erroroffset, NULL);
988 sd = pcre_study(
989 re, /* result of pcre_compile() */
990 0, /* no options */
991 &error); /* set to NULL or points to a message */
992 rc = pcre_exec( /* see below for details of pcre_exec() options */
993 re, sd, "subject", 7, 0, 0, ovector, 30);
994 ...
995 pcre_free_study(sd);
996 pcre_free(re);
997
998 Studying a pattern does two things: first, a lower bound for the length
999 of subject string that is needed to match the pattern is computed. This
1000 does not mean that there are any strings of that length that match, but
1001 it does guarantee that no shorter strings match. The value is used to
1002 avoid wasting time by trying to match strings that are shorter than the
1003 lower bound. You can find out the value in a calling program via the
1004 pcre_fullinfo() function.
1005
1006 Studying a pattern is also useful for non-anchored patterns that do not
1007 have a single fixed starting character. A bitmap of possible starting
1008 bytes is created. This speeds up finding a position in the subject at
1009 which to start matching. (In 16-bit mode, the bitmap is used for 16-bit
1010 values less than 256. In 32-bit mode, the bitmap is used for 32-bit
1011 values less than 256.)
1012
1013 These two optimizations apply to both pcre_exec() and pcre_dfa_exec(),
1014 and the information is also used by the JIT compiler. The optimiza‐
1015 tions can be disabled by setting the PCRE_NO_START_OPTIMIZE option.
1016 You might want to do this if your pattern contains callouts or (*MARK)
1017 and you want to make use of these facilities in cases where matching
1018 fails.
1019
1020 PCRE_NO_START_OPTIMIZE can be specified at either compile time or exe‐
1021 cution time. However, if PCRE_NO_START_OPTIMIZE is passed to
1022 pcre_exec(), (that is, after any JIT compilation has happened) JIT exe‐
1023 cution is disabled. For JIT execution to work with PCRE_NO_START_OPTI‐
1024 MIZE, the option must be set at compile time.
1025
1026 There is a longer discussion of PCRE_NO_START_OPTIMIZE below.
1027
1029
1030 PCRE handles caseless matching, and determines whether characters are
1031 letters, digits, or whatever, by reference to a set of tables, indexed
1032 by character code point. When running in UTF-8 mode, or in the 16- or
1033 32-bit libraries, this applies only to characters with code points less
1034 than 256. By default, higher-valued code points never match escapes
1035 such as \w or \d. However, if PCRE is built with Unicode property sup‐
1036 port, all characters can be tested with \p and \P, or, alternatively,
1037 the PCRE_UCP option can be set when a pattern is compiled; this causes
1038 \w and friends to use Unicode property support instead of the built-in
1039 tables.
1040
1041 The use of locales with Unicode is discouraged. If you are handling
1042 characters with code points greater than 128, you should either use
1043 Unicode support, or use locales, but not try to mix the two.
1044
1045 PCRE contains an internal set of tables that are used when the final
1046 argument of pcre_compile() is NULL. These are sufficient for many
1047 applications. Normally, the internal tables recognize only ASCII char‐
1048 acters. However, when PCRE is built, it is possible to cause the inter‐
1049 nal tables to be rebuilt in the default "C" locale of the local system,
1050 which may cause them to be different.
1051
1052 The internal tables can always be overridden by tables supplied by the
1053 application that calls PCRE. These may be created in a different locale
1054 from the default. As more and more applications change to using Uni‐
1055 code, the need for this locale support is expected to die away.
1056
1057 External tables are built by calling the pcre_maketables() function,
1058 which has no arguments, in the relevant locale. The result can then be
1059 passed to pcre_compile() as often as necessary. For example, to build
1060 and use tables that are appropriate for the French locale (where
1061 accented characters with values greater than 128 are treated as let‐
1062 ters), the following code could be used:
1063
1064 setlocale(LC_CTYPE, "fr_FR");
1065 tables = pcre_maketables();
1066 re = pcre_compile(..., tables);
1067
1068 The locale name "fr_FR" is used on Linux and other Unix-like systems;
1069 if you are using Windows, the name for the French locale is "french".
1070
1071 When pcre_maketables() runs, the tables are built in memory that is
1072 obtained via pcre_malloc. It is the caller's responsibility to ensure
1073 that the memory containing the tables remains available for as long as
1074 it is needed.
1075
1076 The pointer that is passed to pcre_compile() is saved with the compiled
1077 pattern, and the same tables are used via this pointer by pcre_study()
1078 and also by pcre_exec() and pcre_dfa_exec(). Thus, for any single pat‐
1079 tern, compilation, studying and matching all happen in the same locale,
1080 but different patterns can be processed in different locales.
1081
1082 It is possible to pass a table pointer or NULL (indicating the use of
1083 the internal tables) to pcre_exec() or pcre_dfa_exec() (see the discus‐
1084 sion below in the section on matching a pattern). This facility is pro‐
1085 vided for use with pre-compiled patterns that have been saved and
1086 reloaded. Character tables are not saved with patterns, so if a non-
1087 standard table was used at compile time, it must be provided again when
1088 the reloaded pattern is matched. Attempting to use this facility to
1089 match a pattern in a different locale from the one in which it was com‐
1090 piled is likely to lead to anomalous (usually incorrect) results.
1091
1093
1094 int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
1095 int what, void *where);
1096
1097 The pcre_fullinfo() function returns information about a compiled pat‐
1098 tern. It replaces the pcre_info() function, which was removed from the
1099 library at version 8.30, after more than 10 years of obsolescence.
1100
1101 The first argument for pcre_fullinfo() is a pointer to the compiled
1102 pattern. The second argument is the result of pcre_study(), or NULL if
1103 the pattern was not studied. The third argument specifies which piece
1104 of information is required, and the fourth argument is a pointer to a
1105 variable to receive the data. The yield of the function is zero for
1106 success, or one of the following negative numbers:
1107
1108 PCRE_ERROR_NULL the argument code was NULL
1109 the argument where was NULL
1110 PCRE_ERROR_BADMAGIC the "magic number" was not found
1111 PCRE_ERROR_BADENDIANNESS the pattern was compiled with different
1112 endianness
1113 PCRE_ERROR_BADOPTION the value of what was invalid
1114 PCRE_ERROR_UNSET the requested field is not set
1115
1116 The "magic number" is placed at the start of each compiled pattern as
1117 an simple check against passing an arbitrary memory pointer. The endi‐
1118 anness error can occur if a compiled pattern is saved and reloaded on a
1119 different host. Here is a typical call of pcre_fullinfo(), to obtain
1120 the length of the compiled pattern:
1121
1122 int rc;
1123 size_t length;
1124 rc = pcre_fullinfo(
1125 re, /* result of pcre_compile() */
1126 sd, /* result of pcre_study(), or NULL */
1127 PCRE_INFO_SIZE, /* what is required */
1128 &length); /* where to put the data */
1129
1130 The possible values for the third argument are defined in pcre.h, and
1131 are as follows:
1132
1133 PCRE_INFO_BACKREFMAX
1134
1135 Return the number of the highest back reference in the pattern. The
1136 fourth argument should point to an int variable. Zero is returned if
1137 there are no back references.
1138
1139 PCRE_INFO_CAPTURECOUNT
1140
1141 Return the number of capturing subpatterns in the pattern. The fourth
1142 argument should point to an int variable.
1143
1144 PCRE_INFO_DEFAULT_TABLES
1145
1146 Return a pointer to the internal default character tables within PCRE.
1147 The fourth argument should point to an unsigned char * variable. This
1148 information call is provided for internal use by the pcre_study() func‐
1149 tion. External callers can cause PCRE to use its internal tables by
1150 passing a NULL table pointer.
1151
1152 PCRE_INFO_FIRSTBYTE (deprecated)
1153
1154 Return information about the first data unit of any matched string, for
1155 a non-anchored pattern. The name of this option refers to the 8-bit
1156 library, where data units are bytes. The fourth argument should point
1157 to an int variable. Negative values are used for special cases. How‐
1158 ever, this means that when the 32-bit library is in non-UTF-32 mode,
1159 the full 32-bit range of characters cannot be returned. For this rea‐
1160 son, this value is deprecated; use PCRE_INFO_FIRSTCHARACTERFLAGS and
1161 PCRE_INFO_FIRSTCHARACTER instead.
1162
1163 If there is a fixed first value, for example, the letter "c" from a
1164 pattern such as (cat|cow|coyote), its value is returned. In the 8-bit
1165 library, the value is always less than 256. In the 16-bit library the
1166 value can be up to 0xffff. In the 32-bit library the value can be up to
1167 0x10ffff.
1168
1169 If there is no fixed first value, and if either
1170
1171 (a) the pattern was compiled with the PCRE_MULTILINE option, and every
1172 branch starts with "^", or
1173
1174 (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not
1175 set (if it were set, the pattern would be anchored),
1176
1177 -1 is returned, indicating that the pattern matches only at the start
1178 of a subject string or after any newline within the string. Otherwise
1179 -2 is returned. For anchored patterns, -2 is returned.
1180
1181 PCRE_INFO_FIRSTCHARACTER
1182
1183 Return the value of the first data unit (non-UTF character) of any
1184 matched string in the situation where PCRE_INFO_FIRSTCHARACTERFLAGS
1185 returns 1; otherwise return 0. The fourth argument should point to an
1186 uint_t variable.
1187
1188 In the 8-bit library, the value is always less than 256. In the 16-bit
1189 library the value can be up to 0xffff. In the 32-bit library in UTF-32
1190 mode the value can be up to 0x10ffff, and up to 0xffffffff when not
1191 using UTF-32 mode.
1192
1193 PCRE_INFO_FIRSTCHARACTERFLAGS
1194
1195 Return information about the first data unit of any matched string, for
1196 a non-anchored pattern. The fourth argument should point to an int
1197 variable.
1198
1199 If there is a fixed first value, for example, the letter "c" from a
1200 pattern such as (cat|cow|coyote), 1 is returned, and the character
1201 value can be retrieved using PCRE_INFO_FIRSTCHARACTER. If there is no
1202 fixed first value, and if either
1203
1204 (a) the pattern was compiled with the PCRE_MULTILINE option, and every
1205 branch starts with "^", or
1206
1207 (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not
1208 set (if it were set, the pattern would be anchored),
1209
1210 2 is returned, indicating that the pattern matches only at the start of
1211 a subject string or after any newline within the string. Otherwise 0 is
1212 returned. For anchored patterns, 0 is returned.
1213
1214 PCRE_INFO_FIRSTTABLE
1215
1216 If the pattern was studied, and this resulted in the construction of a
1217 256-bit table indicating a fixed set of values for the first data unit
1218 in any matching string, a pointer to the table is returned. Otherwise
1219 NULL is returned. The fourth argument should point to an unsigned char
1220 * variable.
1221
1222 PCRE_INFO_HASCRORLF
1223
1224 Return 1 if the pattern contains any explicit matches for CR or LF
1225 characters, otherwise 0. The fourth argument should point to an int
1226 variable. An explicit match is either a literal CR or LF character, or
1227 \r or \n.
1228
1229 PCRE_INFO_JCHANGED
1230
1231 Return 1 if the (?J) or (?-J) option setting is used in the pattern,
1232 otherwise 0. The fourth argument should point to an int variable. (?J)
1233 and (?-J) set and unset the local PCRE_DUPNAMES option, respectively.
1234
1235 PCRE_INFO_JIT
1236
1237 Return 1 if the pattern was studied with one of the JIT options, and
1238 just-in-time compiling was successful. The fourth argument should point
1239 to an int variable. A return value of 0 means that JIT support is not
1240 available in this version of PCRE, or that the pattern was not studied
1241 with a JIT option, or that the JIT compiler could not handle this par‐
1242 ticular pattern. See the pcrejit documentation for details of what can
1243 and cannot be handled.
1244
1245 PCRE_INFO_JITSIZE
1246
1247 If the pattern was successfully studied with a JIT option, return the
1248 size of the JIT compiled code, otherwise return zero. The fourth argu‐
1249 ment should point to a size_t variable.
1250
1251 PCRE_INFO_LASTLITERAL
1252
1253 Return the value of the rightmost literal data unit that must exist in
1254 any matched string, other than at its start, if such a value has been
1255 recorded. The fourth argument should point to an int variable. If there
1256 is no such value, -1 is returned. For anchored patterns, a last literal
1257 value is recorded only if it follows something of variable length. For
1258 example, for the pattern /^a\d+z\d+/ the returned value is "z", but for
1259 /^a\dz\d/ the returned value is -1.
1260
1261 Since for the 32-bit library using the non-UTF-32 mode, this function
1262 is unable to return the full 32-bit range of characters, this value is
1263 deprecated; instead the PCRE_INFO_REQUIREDCHARFLAGS and
1264 PCRE_INFO_REQUIREDCHAR values should be used.
1265
1266 PCRE_INFO_MATCH_EMPTY
1267
1268 Return 1 if the pattern can match an empty string, otherwise 0. The
1269 fourth argument should point to an int variable.
1270
1271 PCRE_INFO_MATCHLIMIT
1272
1273 If the pattern set a match limit by including an item of the form
1274 (*LIMIT_MATCH=nnnn) at the start, the value is returned. The fourth
1275 argument should point to an unsigned 32-bit integer. If no such value
1276 has been set, the call to pcre_fullinfo() returns the error
1277 PCRE_ERROR_UNSET.
1278
1279 PCRE_INFO_MAXLOOKBEHIND
1280
1281 Return the number of characters (NB not data units) in the longest
1282 lookbehind assertion in the pattern. This information is useful when
1283 doing multi-segment matching using the partial matching facilities.
1284 Note that the simple assertions \b and \B require a one-character look‐
1285 behind. \A also registers a one-character lookbehind, though it does
1286 not actually inspect the previous character. This is to ensure that at
1287 least one character from the old segment is retained when a new segment
1288 is processed. Otherwise, if there are no lookbehinds in the pattern, \A
1289 might match incorrectly at the start of a new segment.
1290
1291 PCRE_INFO_MINLENGTH
1292
1293 If the pattern was studied and a minimum length for matching subject
1294 strings was computed, its value is returned. Otherwise the returned
1295 value is -1. The value is a number of characters, which in UTF mode may
1296 be different from the number of data units. The fourth argument should
1297 point to an int variable. A non-negative value is a lower bound to the
1298 length of any matching string. There may not be any strings of that
1299 length that do actually match, but every string that does match is at
1300 least that long.
1301
1302 PCRE_INFO_NAMECOUNT
1303 PCRE_INFO_NAMEENTRYSIZE
1304 PCRE_INFO_NAMETABLE
1305
1306 PCRE supports the use of named as well as numbered capturing parenthe‐
1307 ses. The names are just an additional way of identifying the parenthe‐
1308 ses, which still acquire numbers. Several convenience functions such as
1309 pcre_get_named_substring() are provided for extracting captured sub‐
1310 strings by name. It is also possible to extract the data directly, by
1311 first converting the name to a number in order to access the correct
1312 pointers in the output vector (described with pcre_exec() below). To do
1313 the conversion, you need to use the name-to-number map, which is
1314 described by these three values.
1315
1316 The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT
1317 gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size
1318 of each entry; both of these return an int value. The entry size
1319 depends on the length of the longest name. PCRE_INFO_NAMETABLE returns
1320 a pointer to the first entry of the table. This is a pointer to char in
1321 the 8-bit library, where the first two bytes of each entry are the num‐
1322 ber of the capturing parenthesis, most significant byte first. In the
1323 16-bit library, the pointer points to 16-bit data units, the first of
1324 which contains the parenthesis number. In the 32-bit library, the
1325 pointer points to 32-bit data units, the first of which contains the
1326 parenthesis number. The rest of the entry is the corresponding name,
1327 zero terminated.
1328
1329 The names are in alphabetical order. If (?| is used to create multiple
1330 groups with the same number, as described in the section on duplicate
1331 subpattern numbers in the pcrepattern page, the groups may be given the
1332 same name, but there is only one entry in the table. Different names
1333 for groups of the same number are not permitted. Duplicate names for
1334 subpatterns with different numbers are permitted, but only if PCRE_DUP‐
1335 NAMES is set. They appear in the table in the order in which they were
1336 found in the pattern. In the absence of (?| this is the order of
1337 increasing number; when (?| is used this is not necessarily the case
1338 because later subpatterns may have lower numbers.
1339
1340 As a simple example of the name/number table, consider the following
1341 pattern after compilation by the 8-bit library (assume PCRE_EXTENDED is
1342 set, so white space - including newlines - is ignored):
1343
1344 (?<date> (?<year>(\d\d)?\d\d) -
1345 (?<month>\d\d) - (?<day>\d\d) )
1346
1347 There are four named subpatterns, so the table has four entries, and
1348 each entry in the table is eight bytes long. The table is as follows,
1349 with non-printing bytes shows in hexadecimal, and undefined bytes shown
1350 as ??:
1351
1352 00 01 d a t e 00 ??
1353 00 05 d a y 00 ?? ??
1354 00 04 m o n t h 00
1355 00 02 y e a r 00 ??
1356
1357 When writing code to extract data from named subpatterns using the
1358 name-to-number map, remember that the length of the entries is likely
1359 to be different for each compiled pattern.
1360
1361 PCRE_INFO_OKPARTIAL
1362
1363 Return 1 if the pattern can be used for partial matching with
1364 pcre_exec(), otherwise 0. The fourth argument should point to an int
1365 variable. From release 8.00, this always returns 1, because the
1366 restrictions that previously applied to partial matching have been
1367 lifted. The pcrepartial documentation gives details of partial match‐
1368 ing.
1369
1370 PCRE_INFO_OPTIONS
1371
1372 Return a copy of the options with which the pattern was compiled. The
1373 fourth argument should point to an unsigned long int variable. These
1374 option bits are those specified in the call to pcre_compile(), modified
1375 by any top-level option settings at the start of the pattern itself. In
1376 other words, they are the options that will be in force when matching
1377 starts. For example, if the pattern /(?im)abc(?-i)d/ is compiled with
1378 the PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE,
1379 and PCRE_EXTENDED.
1380
1381 A pattern is automatically anchored by PCRE if all of its top-level
1382 alternatives begin with one of the following:
1383
1384 ^ unless PCRE_MULTILINE is set
1385 \A always
1386 \G always
1387 .* if PCRE_DOTALL is set and there are no back
1388 references to the subpattern in which .* appears
1389
1390 For such patterns, the PCRE_ANCHORED bit is set in the options returned
1391 by pcre_fullinfo().
1392
1393 PCRE_INFO_RECURSIONLIMIT
1394
1395 If the pattern set a recursion limit by including an item of the form
1396 (*LIMIT_RECURSION=nnnn) at the start, the value is returned. The fourth
1397 argument should point to an unsigned 32-bit integer. If no such value
1398 has been set, the call to pcre_fullinfo() returns the error
1399 PCRE_ERROR_UNSET.
1400
1401 PCRE_INFO_SIZE
1402
1403 Return the size of the compiled pattern in bytes (for all three
1404 libraries). The fourth argument should point to a size_t variable. This
1405 value does not include the size of the pcre structure that is returned
1406 by pcre_compile(). The value that is passed as the argument to
1407 pcre_malloc() when pcre_compile() is getting memory in which to place
1408 the compiled data is the value returned by this option plus the size of
1409 the pcre structure. Studying a compiled pattern, with or without JIT,
1410 does not alter the value returned by this option.
1411
1412 PCRE_INFO_STUDYSIZE
1413
1414 Return the size in bytes (for all three libraries) of the data block
1415 pointed to by the study_data field in a pcre_extra block. If pcre_extra
1416 is NULL, or there is no study data, zero is returned. The fourth argu‐
1417 ment should point to a size_t variable. The study_data field is set by
1418 pcre_study() to record information that will speed up matching (see the
1419 section entitled "Studying a pattern" above). The format of the
1420 study_data block is private, but its length is made available via this
1421 option so that it can be saved and restored (see the pcreprecompile
1422 documentation for details).
1423
1424 PCRE_INFO_REQUIREDCHARFLAGS
1425
1426 Returns 1 if there is a rightmost literal data unit that must exist in
1427 any matched string, other than at its start. The fourth argument should
1428 point to an int variable. If there is no such value, 0 is returned. If
1429 returning 1, the character value itself can be retrieved using
1430 PCRE_INFO_REQUIREDCHAR.
1431
1432 For anchored patterns, a last literal value is recorded only if it fol‐
1433 lows something of variable length. For example, for the pattern
1434 /^a\d+z\d+/ the returned value 1 (with "z" returned from
1435 PCRE_INFO_REQUIREDCHAR), but for /^a\dz\d/ the returned value is 0.
1436
1437 PCRE_INFO_REQUIREDCHAR
1438
1439 Return the value of the rightmost literal data unit that must exist in
1440 any matched string, other than at its start, if such a value has been
1441 recorded. The fourth argument should point to an uint32_t variable. If
1442 there is no such value, 0 is returned.
1443
1445
1446 int pcre_refcount(pcre *code, int adjust);
1447
1448 The pcre_refcount() function is used to maintain a reference count in
1449 the data block that contains a compiled pattern. It is provided for the
1450 benefit of applications that operate in an object-oriented manner,
1451 where different parts of the application may be using the same compiled
1452 pattern, but you want to free the block when they are all done.
1453
1454 When a pattern is compiled, the reference count field is initialized to
1455 zero. It is changed only by calling this function, whose action is to
1456 add the adjust value (which may be positive or negative) to it. The
1457 yield of the function is the new value. However, the value of the count
1458 is constrained to lie between 0 and 65535, inclusive. If the new value
1459 is outside these limits, it is forced to the appropriate limit value.
1460
1461 Except when it is zero, the reference count is not correctly preserved
1462 if a pattern is compiled on one host and then transferred to a host
1463 whose byte-order is different. (This seems a highly unlikely scenario.)
1464
1466
1467 int pcre_exec(const pcre *code, const pcre_extra *extra,
1468 const char *subject, int length, int startoffset,
1469 int options, int *ovector, int ovecsize);
1470
1471 The function pcre_exec() is called to match a subject string against a
1472 compiled pattern, which is passed in the code argument. If the pattern
1473 was studied, the result of the study should be passed in the extra
1474 argument. You can call pcre_exec() with the same code and extra argu‐
1475 ments as many times as you like, in order to match different subject
1476 strings with the same pattern.
1477
1478 This function is the main matching facility of the library, and it
1479 operates in a Perl-like manner. For specialist use there is also an
1480 alternative matching function, which is described below in the section
1481 about the pcre_dfa_exec() function.
1482
1483 In most applications, the pattern will have been compiled (and option‐
1484 ally studied) in the same process that calls pcre_exec(). However, it
1485 is possible to save compiled patterns and study data, and then use them
1486 later in different processes, possibly even on different hosts. For a
1487 discussion about this, see the pcreprecompile documentation.
1488
1489 Here is an example of a simple call to pcre_exec():
1490
1491 int rc;
1492 int ovector[30];
1493 rc = pcre_exec(
1494 re, /* result of pcre_compile() */
1495 NULL, /* we didn't study the pattern */
1496 "some string", /* the subject string */
1497 11, /* the length of the subject string */
1498 0, /* start at offset 0 in the subject */
1499 0, /* default options */
1500 ovector, /* vector of integers for substring information */
1501 30); /* number of elements (NOT size in bytes) */
1502
1503 Extra data for pcre_exec()
1504
1505 If the extra argument is not NULL, it must point to a pcre_extra data
1506 block. The pcre_study() function returns such a block (when it doesn't
1507 return NULL), but you can also create one for yourself, and pass addi‐
1508 tional information in it. The pcre_extra block contains the following
1509 fields (not necessarily in this order):
1510
1511 unsigned long int flags;
1512 void *study_data;
1513 void *executable_jit;
1514 unsigned long int match_limit;
1515 unsigned long int match_limit_recursion;
1516 void *callout_data;
1517 const unsigned char *tables;
1518 unsigned char **mark;
1519
1520 In the 16-bit version of this structure, the mark field has type
1521 "PCRE_UCHAR16 **".
1522
1523 In the 32-bit version of this structure, the mark field has type
1524 "PCRE_UCHAR32 **".
1525
1526 The flags field is used to specify which of the other fields are set.
1527 The flag bits are:
1528
1529 PCRE_EXTRA_CALLOUT_DATA
1530 PCRE_EXTRA_EXECUTABLE_JIT
1531 PCRE_EXTRA_MARK
1532 PCRE_EXTRA_MATCH_LIMIT
1533 PCRE_EXTRA_MATCH_LIMIT_RECURSION
1534 PCRE_EXTRA_STUDY_DATA
1535 PCRE_EXTRA_TABLES
1536
1537 Other flag bits should be set to zero. The study_data field and some‐
1538 times the executable_jit field are set in the pcre_extra block that is
1539 returned by pcre_study(), together with the appropriate flag bits. You
1540 should not set these yourself, but you may add to the block by setting
1541 other fields and their corresponding flag bits.
1542
1543 The match_limit field provides a means of preventing PCRE from using up
1544 a vast amount of resources when running patterns that are not going to
1545 match, but which have a very large number of possibilities in their
1546 search trees. The classic example is a pattern that uses nested unlim‐
1547 ited repeats.
1548
1549 Internally, pcre_exec() uses a function called match(), which it calls
1550 repeatedly (sometimes recursively). The limit set by match_limit is
1551 imposed on the number of times this function is called during a match,
1552 which has the effect of limiting the amount of backtracking that can
1553 take place. For patterns that are not anchored, the count restarts from
1554 zero for each position in the subject string.
1555
1556 When pcre_exec() is called with a pattern that was successfully studied
1557 with a JIT option, the way that the matching is executed is entirely
1558 different. However, there is still the possibility of runaway matching
1559 that goes on for a very long time, and so the match_limit value is also
1560 used in this case (but in a different way) to limit how long the match‐
1561 ing can continue.
1562
1563 The default value for the limit can be set when PCRE is built; the
1564 default default is 10 million, which handles all but the most extreme
1565 cases. You can override the default by suppling pcre_exec() with a
1566 pcre_extra block in which match_limit is set, and
1567 PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is
1568 exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.
1569
1570 A value for the match limit may also be supplied by an item at the
1571 start of a pattern of the form
1572
1573 (*LIMIT_MATCH=d)
1574
1575 where d is a decimal number. However, such a setting is ignored unless
1576 d is less than the limit set by the caller of pcre_exec() or, if no
1577 such limit is set, less than the default.
1578
1579 The match_limit_recursion field is similar to match_limit, but instead
1580 of limiting the total number of times that match() is called, it limits
1581 the depth of recursion. The recursion depth is a smaller number than
1582 the total number of calls, because not all calls to match() are recur‐
1583 sive. This limit is of use only if it is set smaller than match_limit.
1584
1585 Limiting the recursion depth limits the amount of machine stack that
1586 can be used, or, when PCRE has been compiled to use memory on the heap
1587 instead of the stack, the amount of heap memory that can be used. This
1588 limit is not relevant, and is ignored, when matching is done using JIT
1589 compiled code.
1590
1591 The default value for match_limit_recursion can be set when PCRE is
1592 built; the default default is the same value as the default for
1593 match_limit. You can override the default by suppling pcre_exec() with
1594 a pcre_extra block in which match_limit_recursion is set, and
1595 PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the
1596 limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.
1597
1598 A value for the recursion limit may also be supplied by an item at the
1599 start of a pattern of the form
1600
1601 (*LIMIT_RECURSION=d)
1602
1603 where d is a decimal number. However, such a setting is ignored unless
1604 d is less than the limit set by the caller of pcre_exec() or, if no
1605 such limit is set, less than the default.
1606
1607 The callout_data field is used in conjunction with the "callout" fea‐
1608 ture, and is described in the pcrecallout documentation.
1609
1610 The tables field is provided for use with patterns that have been pre-
1611 compiled using custom character tables, saved to disc or elsewhere, and
1612 then reloaded, because the tables that were used to compile a pattern
1613 are not saved with it. See the pcreprecompile documentation for a dis‐
1614 cussion of saving compiled patterns for later use. If NULL is passed
1615 using this mechanism, it forces PCRE's internal tables to be used.
1616
1617 Warning: The tables that pcre_exec() uses must be the same as those
1618 that were used when the pattern was compiled. If this is not the case,
1619 the behaviour of pcre_exec() is undefined. Therefore, when a pattern is
1620 compiled and matched in the same process, this field should never be
1621 set. In this (the most common) case, the correct table pointer is auto‐
1622 matically passed with the compiled pattern from pcre_compile() to
1623 pcre_exec().
1624
1625 If PCRE_EXTRA_MARK is set in the flags field, the mark field must be
1626 set to point to a suitable variable. If the pattern contains any back‐
1627 tracking control verbs such as (*MARK:NAME), and the execution ends up
1628 with a name to pass back, a pointer to the name string (zero termi‐
1629 nated) is placed in the variable pointed to by the mark field. The
1630 names are within the compiled pattern; if you wish to retain such a
1631 name you must copy it before freeing the memory of a compiled pattern.
1632 If there is no name to pass back, the variable pointed to by the mark
1633 field is set to NULL. For details of the backtracking control verbs,
1634 see the section entitled "Backtracking control" in the pcrepattern doc‐
1635 umentation.
1636
1637 Option bits for pcre_exec()
1638
1639 The unused bits of the options argument for pcre_exec() must be zero.
1640 The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,
1641 PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART,
1642 PCRE_NO_START_OPTIMIZE, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_HARD, and
1643 PCRE_PARTIAL_SOFT.
1644
1645 If the pattern was successfully studied with one of the just-in-time
1646 (JIT) compile options, the only supported options for JIT execution are
1647 PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,
1648 PCRE_NOTEMPTY_ATSTART, PCRE_PARTIAL_HARD, and PCRE_PARTIAL_SOFT. If an
1649 unsupported option is used, JIT execution is disabled and the normal
1650 interpretive code in pcre_exec() is run.
1651
1652 PCRE_ANCHORED
1653
1654 The PCRE_ANCHORED option limits pcre_exec() to matching at the first
1655 matching position. If a pattern was compiled with PCRE_ANCHORED, or
1656 turned out to be anchored by virtue of its contents, it cannot be made
1657 unachored at matching time.
1658
1659 PCRE_BSR_ANYCRLF
1660 PCRE_BSR_UNICODE
1661
1662 These options (which are mutually exclusive) control what the \R escape
1663 sequence matches. The choice is either to match only CR, LF, or CRLF,
1664 or to match any Unicode newline sequence. These options override the
1665 choice that was made or defaulted when the pattern was compiled.
1666
1667 PCRE_NEWLINE_CR
1668 PCRE_NEWLINE_LF
1669 PCRE_NEWLINE_CRLF
1670 PCRE_NEWLINE_ANYCRLF
1671 PCRE_NEWLINE_ANY
1672
1673 These options override the newline definition that was chosen or
1674 defaulted when the pattern was compiled. For details, see the descrip‐
1675 tion of pcre_compile() above. During matching, the newline choice
1676 affects the behaviour of the dot, circumflex, and dollar metacharac‐
1677 ters. It may also alter the way the match position is advanced after a
1678 match failure for an unanchored pattern.
1679
1680 When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is
1681 set, and a match attempt for an unanchored pattern fails when the cur‐
1682 rent position is at a CRLF sequence, and the pattern contains no
1683 explicit matches for CR or LF characters, the match position is
1684 advanced by two characters instead of one, in other words, to after the
1685 CRLF.
1686
1687 The above rule is a compromise that makes the most common cases work as
1688 expected. For example, if the pattern is .+A (and the PCRE_DOTALL
1689 option is not set), it does not match the string "\r\nA" because, after
1690 failing at the start, it skips both the CR and the LF before retrying.
1691 However, the pattern [\r\n]A does match that string, because it con‐
1692 tains an explicit CR or LF reference, and so advances only by one char‐
1693 acter after the first failure.
1694
1695 An explicit match for CR of LF is either a literal appearance of one of
1696 those characters, or one of the \r or \n escape sequences. Implicit
1697 matches such as [^X] do not count, nor does \s (which includes CR and
1698 LF in the characters that it matches).
1699
1700 Notwithstanding the above, anomalous effects may still occur when CRLF
1701 is a valid newline sequence and explicit \r or \n escapes appear in the
1702 pattern.
1703
1704 PCRE_NOTBOL
1705
1706 This option specifies that first character of the subject string is not
1707 the beginning of a line, so the circumflex metacharacter should not
1708 match before it. Setting this without PCRE_MULTILINE (at compile time)
1709 causes circumflex never to match. This option affects only the behav‐
1710 iour of the circumflex metacharacter. It does not affect \A.
1711
1712 PCRE_NOTEOL
1713
1714 This option specifies that the end of the subject string is not the end
1715 of a line, so the dollar metacharacter should not match it nor (except
1716 in multiline mode) a newline immediately before it. Setting this with‐
1717 out PCRE_MULTILINE (at compile time) causes dollar never to match. This
1718 option affects only the behaviour of the dollar metacharacter. It does
1719 not affect \Z or \z.
1720
1721 PCRE_NOTEMPTY
1722
1723 An empty string is not considered to be a valid match if this option is
1724 set. If there are alternatives in the pattern, they are tried. If all
1725 the alternatives match the empty string, the entire match fails. For
1726 example, if the pattern
1727
1728 a?b?
1729
1730 is applied to a string not beginning with "a" or "b", it matches an
1731 empty string at the start of the subject. With PCRE_NOTEMPTY set, this
1732 match is not valid, so PCRE searches further into the string for occur‐
1733 rences of "a" or "b".
1734
1735 PCRE_NOTEMPTY_ATSTART
1736
1737 This is like PCRE_NOTEMPTY, except that an empty string match that is
1738 not at the start of the subject is permitted. If the pattern is
1739 anchored, such a match can occur only if the pattern contains \K.
1740
1741 Perl has no direct equivalent of PCRE_NOTEMPTY or
1742 PCRE_NOTEMPTY_ATSTART, but it does make a special case of a pattern
1743 match of the empty string within its split() function, and when using
1744 the /g modifier. It is possible to emulate Perl's behaviour after
1745 matching a null string by first trying the match again at the same off‐
1746 set with PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED, and then if that
1747 fails, by advancing the starting offset (see below) and trying an ordi‐
1748 nary match again. There is some code that demonstrates how to do this
1749 in the pcredemo sample program. In the most general case, you have to
1750 check to see if the newline convention recognizes CRLF as a newline,
1751 and if so, and the current character is CR followed by LF, advance the
1752 starting offset by two characters instead of one.
1753
1754 PCRE_NO_START_OPTIMIZE
1755
1756 There are a number of optimizations that pcre_exec() uses at the start
1757 of a match, in order to speed up the process. For example, if it is
1758 known that an unanchored match must start with a specific character, it
1759 searches the subject for that character, and fails immediately if it
1760 cannot find it, without actually running the main matching function.
1761 This means that a special item such as (*COMMIT) at the start of a pat‐
1762 tern is not considered until after a suitable starting point for the
1763 match has been found. Also, when callouts or (*MARK) items are in use,
1764 these "start-up" optimizations can cause them to be skipped if the pat‐
1765 tern is never actually used. The start-up optimizations are in effect a
1766 pre-scan of the subject that takes place before the pattern is run.
1767
1768 The PCRE_NO_START_OPTIMIZE option disables the start-up optimizations,
1769 possibly causing performance to suffer, but ensuring that in cases
1770 where the result is "no match", the callouts do occur, and that items
1771 such as (*COMMIT) and (*MARK) are considered at every possible starting
1772 position in the subject string. If PCRE_NO_START_OPTIMIZE is set at
1773 compile time, it cannot be unset at matching time. The use of
1774 PCRE_NO_START_OPTIMIZE at matching time (that is, passing it to
1775 pcre_exec()) disables JIT execution; in this situation, matching is
1776 always done using interpretively.
1777
1778 Setting PCRE_NO_START_OPTIMIZE can change the outcome of a matching
1779 operation. Consider the pattern
1780
1781 (*COMMIT)ABC
1782
1783 When this is compiled, PCRE records the fact that a match must start
1784 with the character "A". Suppose the subject string is "DEFABC". The
1785 start-up optimization scans along the subject, finds "A" and runs the
1786 first match attempt from there. The (*COMMIT) item means that the pat‐
1787 tern must match the current starting position, which in this case, it
1788 does. However, if the same match is run with PCRE_NO_START_OPTIMIZE
1789 set, the initial scan along the subject string does not happen. The
1790 first match attempt is run starting from "D" and when this fails,
1791 (*COMMIT) prevents any further matches being tried, so the overall
1792 result is "no match". If the pattern is studied, more start-up opti‐
1793 mizations may be used. For example, a minimum length for the subject
1794 may be recorded. Consider the pattern
1795
1796 (*MARK:A)(X|Y)
1797
1798 The minimum length for a match is one character. If the subject is
1799 "ABC", there will be attempts to match "ABC", "BC", "C", and then
1800 finally an empty string. If the pattern is studied, the final attempt
1801 does not take place, because PCRE knows that the subject is too short,
1802 and so the (*MARK) is never encountered. In this case, studying the
1803 pattern does not affect the overall match result, which is still "no
1804 match", but it does affect the auxiliary information that is returned.
1805
1806 PCRE_NO_UTF8_CHECK
1807
1808 When PCRE_UTF8 is set at compile time, the validity of the subject as a
1809 UTF-8 string is automatically checked when pcre_exec() is subsequently
1810 called. The entire string is checked before any other processing takes
1811 place. The value of startoffset is also checked to ensure that it
1812 points to the start of a UTF-8 character. There is a discussion about
1813 the validity of UTF-8 strings in the pcreunicode page. If an invalid
1814 sequence of bytes is found, pcre_exec() returns the error
1815 PCRE_ERROR_BADUTF8 or, if PCRE_PARTIAL_HARD is set and the problem is a
1816 truncated character at the end of the subject, PCRE_ERROR_SHORTUTF8. In
1817 both cases, information about the precise nature of the error may also
1818 be returned (see the descriptions of these errors in the section enti‐
1819 tled Error return values from pcre_exec() below). If startoffset con‐
1820 tains a value that does not point to the start of a UTF-8 character (or
1821 to the end of the subject), PCRE_ERROR_BADUTF8_OFFSET is returned.
1822
1823 If you already know that your subject is valid, and you want to skip
1824 these checks for performance reasons, you can set the
1825 PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to
1826 do this for the second and subsequent calls to pcre_exec() if you are
1827 making repeated calls to find all the matches in a single subject
1828 string. However, you should be sure that the value of startoffset
1829 points to the start of a character (or the end of the subject). When
1830 PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid string as a
1831 subject or an invalid value of startoffset is undefined. Your program
1832 may crash or loop.
1833
1834 PCRE_PARTIAL_HARD
1835 PCRE_PARTIAL_SOFT
1836
1837 These options turn on the partial matching feature. For backwards com‐
1838 patibility, PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. A partial
1839 match occurs if the end of the subject string is reached successfully,
1840 but there are not enough subject characters to complete the match. If
1841 this happens when PCRE_PARTIAL_SOFT (but not PCRE_PARTIAL_HARD) is set,
1842 matching continues by testing any remaining alternatives. Only if no
1843 complete match can be found is PCRE_ERROR_PARTIAL returned instead of
1844 PCRE_ERROR_NOMATCH. In other words, PCRE_PARTIAL_SOFT says that the
1845 caller is prepared to handle a partial match, but only if no complete
1846 match can be found.
1847
1848 If PCRE_PARTIAL_HARD is set, it overrides PCRE_PARTIAL_SOFT. In this
1849 case, if a partial match is found, pcre_exec() immediately returns
1850 PCRE_ERROR_PARTIAL, without considering any other alternatives. In
1851 other words, when PCRE_PARTIAL_HARD is set, a partial match is consid‐
1852 ered to be more important that an alternative complete match.
1853
1854 In both cases, the portion of the string that was inspected when the
1855 partial match was found is set as the first matching string. There is a
1856 more detailed discussion of partial and multi-segment matching, with
1857 examples, in the pcrepartial documentation.
1858
1859 The string to be matched by pcre_exec()
1860
1861 The subject string is passed to pcre_exec() as a pointer in subject, a
1862 length in length, and a starting offset in startoffset. The units for
1863 length and startoffset are bytes for the 8-bit library, 16-bit data
1864 items for the 16-bit library, and 32-bit data items for the 32-bit
1865 library.
1866
1867 If startoffset is negative or greater than the length of the subject,
1868 pcre_exec() returns PCRE_ERROR_BADOFFSET. When the starting offset is
1869 zero, the search for a match starts at the beginning of the subject,
1870 and this is by far the most common case. In UTF-8 or UTF-16 mode, the
1871 offset must point to the start of a character, or the end of the sub‐
1872 ject (in UTF-32 mode, one data unit equals one character, so all off‐
1873 sets are valid). Unlike the pattern string, the subject may contain
1874 binary zeroes.
1875
1876 A non-zero starting offset is useful when searching for another match
1877 in the same subject by calling pcre_exec() again after a previous suc‐
1878 cess. Setting startoffset differs from just passing over a shortened
1879 string and setting PCRE_NOTBOL in the case of a pattern that begins
1880 with any kind of lookbehind. For example, consider the pattern
1881
1882 \Biss\B
1883
1884 which finds occurrences of "iss" in the middle of words. (\B matches
1885 only if the current position in the subject is not a word boundary.)
1886 When applied to the string "Mississipi" the first call to pcre_exec()
1887 finds the first occurrence. If pcre_exec() is called again with just
1888 the remainder of the subject, namely "issipi", it does not match,
1889 because \B is always false at the start of the subject, which is deemed
1890 to be a word boundary. However, if pcre_exec() is passed the entire
1891 string again, but with startoffset set to 4, it finds the second occur‐
1892 rence of "iss" because it is able to look behind the starting point to
1893 discover that it is preceded by a letter.
1894
1895 Finding all the matches in a subject is tricky when the pattern can
1896 match an empty string. It is possible to emulate Perl's /g behaviour by
1897 first trying the match again at the same offset, with the
1898 PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED options, and then if that
1899 fails, advancing the starting offset and trying an ordinary match
1900 again. There is some code that demonstrates how to do this in the pcre‐
1901 demo sample program. In the most general case, you have to check to see
1902 if the newline convention recognizes CRLF as a newline, and if so, and
1903 the current character is CR followed by LF, advance the starting offset
1904 by two characters instead of one.
1905
1906 If a non-zero starting offset is passed when the pattern is anchored,
1907 one attempt to match at the given offset is made. This can only succeed
1908 if the pattern does not require the match to be at the start of the
1909 subject.
1910
1911 How pcre_exec() returns captured substrings
1912
1913 In general, a pattern matches a certain portion of the subject, and in
1914 addition, further substrings from the subject may be picked out by
1915 parts of the pattern. Following the usage in Jeffrey Friedl's book,
1916 this is called "capturing" in what follows, and the phrase "capturing
1917 subpattern" is used for a fragment of a pattern that picks out a sub‐
1918 string. PCRE supports several other kinds of parenthesized subpattern
1919 that do not cause substrings to be captured.
1920
1921 Captured substrings are returned to the caller via a vector of integers
1922 whose address is passed in ovector. The number of elements in the vec‐
1923 tor is passed in ovecsize, which must be a non-negative number. Note:
1924 this argument is NOT the size of ovector in bytes.
1925
1926 The first two-thirds of the vector is used to pass back captured sub‐
1927 strings, each substring using a pair of integers. The remaining third
1928 of the vector is used as workspace by pcre_exec() while matching cap‐
1929 turing subpatterns, and is not available for passing back information.
1930 The number passed in ovecsize should always be a multiple of three. If
1931 it is not, it is rounded down.
1932
1933 When a match is successful, information about captured substrings is
1934 returned in pairs of integers, starting at the beginning of ovector,
1935 and continuing up to two-thirds of its length at the most. The first
1936 element of each pair is set to the offset of the first character in a
1937 substring, and the second is set to the offset of the first character
1938 after the end of a substring. These values are always data unit off‐
1939 sets, even in UTF mode. They are byte offsets in the 8-bit library,
1940 16-bit data item offsets in the 16-bit library, and 32-bit data item
1941 offsets in the 32-bit library. Note: they are not character counts.
1942
1943 The first pair of integers, ovector[0] and ovector[1], identify the
1944 portion of the subject string matched by the entire pattern. The next
1945 pair is used for the first capturing subpattern, and so on. The value
1946 returned by pcre_exec() is one more than the highest numbered pair that
1947 has been set. For example, if two substrings have been captured, the
1948 returned value is 3. If there are no capturing subpatterns, the return
1949 value from a successful match is 1, indicating that just the first pair
1950 of offsets has been set.
1951
1952 If a capturing subpattern is matched repeatedly, it is the last portion
1953 of the string that it matched that is returned.
1954
1955 If the vector is too small to hold all the captured substring offsets,
1956 it is used as far as possible (up to two-thirds of its length), and the
1957 function returns a value of zero. If neither the actual string matched
1958 nor any captured substrings are of interest, pcre_exec() may be called
1959 with ovector passed as NULL and ovecsize as zero. However, if the pat‐
1960 tern contains back references and the ovector is not big enough to
1961 remember the related substrings, PCRE has to get additional memory for
1962 use during matching. Thus it is usually advisable to supply an ovector
1963 of reasonable size.
1964
1965 There are some cases where zero is returned (indicating vector over‐
1966 flow) when in fact the vector is exactly the right size for the final
1967 match. For example, consider the pattern
1968
1969 (a)(?:(b)c|bd)
1970
1971 If a vector of 6 elements (allowing for only 1 captured substring) is
1972 given with subject string "abd", pcre_exec() will try to set the second
1973 captured string, thereby recording a vector overflow, before failing to
1974 match "c" and backing up to try the second alternative. The zero
1975 return, however, does correctly indicate that the maximum number of
1976 slots (namely 2) have been filled. In similar cases where there is tem‐
1977 porary overflow, but the final number of used slots is actually less
1978 than the maximum, a non-zero value is returned.
1979
1980 The pcre_fullinfo() function can be used to find out how many capturing
1981 subpatterns there are in a compiled pattern. The smallest size for
1982 ovector that will allow for n captured substrings, in addition to the
1983 offsets of the substring matched by the whole pattern, is (n+1)*3.
1984
1985 It is possible for capturing subpattern number n+1 to match some part
1986 of the subject when subpattern n has not been used at all. For example,
1987 if the string "abc" is matched against the pattern (a|(z))(bc) the
1988 return from the function is 4, and subpatterns 1 and 3 are matched, but
1989 2 is not. When this happens, both values in the offset pairs corre‐
1990 sponding to unused subpatterns are set to -1.
1991
1992 Offset values that correspond to unused subpatterns at the end of the
1993 expression are also set to -1. For example, if the string "abc" is
1994 matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not
1995 matched. The return from the function is 2, because the highest used
1996 capturing subpattern number is 1, and the offsets for for the second
1997 and third capturing subpatterns (assuming the vector is large enough,
1998 of course) are set to -1.
1999
2000 Note: Elements in the first two-thirds of ovector that do not corre‐
2001 spond to capturing parentheses in the pattern are never changed. That
2002 is, if a pattern contains n capturing parentheses, no more than ovec‐
2003 tor[0] to ovector[2n+1] are set by pcre_exec(). The other elements (in
2004 the first two-thirds) retain whatever values they previously had.
2005
2006 Some convenience functions are provided for extracting the captured
2007 substrings as separate strings. These are described below.
2008
2009 Error return values from pcre_exec()
2010
2011 If pcre_exec() fails, it returns a negative number. The following are
2012 defined in the header file:
2013
2014 PCRE_ERROR_NOMATCH (-1)
2015
2016 The subject string did not match the pattern.
2017
2018 PCRE_ERROR_NULL (-2)
2019
2020 Either code or subject was passed as NULL, or ovector was NULL and
2021 ovecsize was not zero.
2022
2023 PCRE_ERROR_BADOPTION (-3)
2024
2025 An unrecognized bit was set in the options argument.
2026
2027 PCRE_ERROR_BADMAGIC (-4)
2028
2029 PCRE stores a 4-byte "magic number" at the start of the compiled code,
2030 to catch the case when it is passed a junk pointer and to detect when a
2031 pattern that was compiled in an environment of one endianness is run in
2032 an environment with the other endianness. This is the error that PCRE
2033 gives when the magic number is not present.
2034
2035 PCRE_ERROR_UNKNOWN_OPCODE (-5)
2036
2037 While running the pattern match, an unknown item was encountered in the
2038 compiled pattern. This error could be caused by a bug in PCRE or by
2039 overwriting of the compiled pattern.
2040
2041 PCRE_ERROR_NOMEMORY (-6)
2042
2043 If a pattern contains back references, but the ovector that is passed
2044 to pcre_exec() is not big enough to remember the referenced substrings,
2045 PCRE gets a block of memory at the start of matching to use for this
2046 purpose. If the call via pcre_malloc() fails, this error is given. The
2047 memory is automatically freed at the end of matching.
2048
2049 This error is also given if pcre_stack_malloc() fails in pcre_exec().
2050 This can happen only when PCRE has been compiled with --disable-stack-
2051 for-recursion.
2052
2053 PCRE_ERROR_NOSUBSTRING (-7)
2054
2055 This error is used by the pcre_copy_substring(), pcre_get_substring(),
2056 and pcre_get_substring_list() functions (see below). It is never
2057 returned by pcre_exec().
2058
2059 PCRE_ERROR_MATCHLIMIT (-8)
2060
2061 The backtracking limit, as specified by the match_limit field in a
2062 pcre_extra structure (or defaulted) was reached. See the description
2063 above.
2064
2065 PCRE_ERROR_CALLOUT (-9)
2066
2067 This error is never generated by pcre_exec() itself. It is provided for
2068 use by callout functions that want to yield a distinctive error code.
2069 See the pcrecallout documentation for details.
2070
2071 PCRE_ERROR_BADUTF8 (-10)
2072
2073 A string that contains an invalid UTF-8 byte sequence was passed as a
2074 subject, and the PCRE_NO_UTF8_CHECK option was not set. If the size of
2075 the output vector (ovecsize) is at least 2, the byte offset to the
2076 start of the the invalid UTF-8 character is placed in the first ele‐
2077 ment, and a reason code is placed in the second element. The reason
2078 codes are listed in the following section. For backward compatibility,
2079 if PCRE_PARTIAL_HARD is set and the problem is a truncated UTF-8 char‐
2080 acter at the end of the subject (reason codes 1 to 5),
2081 PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8.
2082
2083 PCRE_ERROR_BADUTF8_OFFSET (-11)
2084
2085 The UTF-8 byte sequence that was passed as a subject was checked and
2086 found to be valid (the PCRE_NO_UTF8_CHECK option was not set), but the
2087 value of startoffset did not point to the beginning of a UTF-8 charac‐
2088 ter or the end of the subject.
2089
2090 PCRE_ERROR_PARTIAL (-12)
2091
2092 The subject string did not match, but it did match partially. See the
2093 pcrepartial documentation for details of partial matching.
2094
2095 PCRE_ERROR_BADPARTIAL (-13)
2096
2097 This code is no longer in use. It was formerly returned when the
2098 PCRE_PARTIAL option was used with a compiled pattern containing items
2099 that were not supported for partial matching. From release 8.00
2100 onwards, there are no restrictions on partial matching.
2101
2102 PCRE_ERROR_INTERNAL (-14)
2103
2104 An unexpected internal error has occurred. This error could be caused
2105 by a bug in PCRE or by overwriting of the compiled pattern.
2106
2107 PCRE_ERROR_BADCOUNT (-15)
2108
2109 This error is given if the value of the ovecsize argument is negative.
2110
2111 PCRE_ERROR_RECURSIONLIMIT (-21)
2112
2113 The internal recursion limit, as specified by the match_limit_recursion
2114 field in a pcre_extra structure (or defaulted) was reached. See the
2115 description above.
2116
2117 PCRE_ERROR_BADNEWLINE (-23)
2118
2119 An invalid combination of PCRE_NEWLINE_xxx options was given.
2120
2121 PCRE_ERROR_BADOFFSET (-24)
2122
2123 The value of startoffset was negative or greater than the length of the
2124 subject, that is, the value in length.
2125
2126 PCRE_ERROR_SHORTUTF8 (-25)
2127
2128 This error is returned instead of PCRE_ERROR_BADUTF8 when the subject
2129 string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD
2130 option is set. Information about the failure is returned as for
2131 PCRE_ERROR_BADUTF8. It is in fact sufficient to detect this case, but
2132 this special error code for PCRE_PARTIAL_HARD precedes the implementa‐
2133 tion of returned information; it is retained for backwards compatibil‐
2134 ity.
2135
2136 PCRE_ERROR_RECURSELOOP (-26)
2137
2138 This error is returned when pcre_exec() detects a recursion loop within
2139 the pattern. Specifically, it means that either the whole pattern or a
2140 subpattern has been called recursively for the second time at the same
2141 position in the subject string. Some simple patterns that might do this
2142 are detected and faulted at compile time, but more complicated cases,
2143 in particular mutual recursions between two different subpatterns, can‐
2144 not be detected until run time.
2145
2146 PCRE_ERROR_JIT_STACKLIMIT (-27)
2147
2148 This error is returned when a pattern that was successfully studied
2149 using a JIT compile option is being matched, but the memory available
2150 for the just-in-time processing stack is not large enough. See the
2151 pcrejit documentation for more details.
2152
2153 PCRE_ERROR_BADMODE (-28)
2154
2155 This error is given if a pattern that was compiled by the 8-bit library
2156 is passed to a 16-bit or 32-bit library function, or vice versa.
2157
2158 PCRE_ERROR_BADENDIANNESS (-29)
2159
2160 This error is given if a pattern that was compiled and saved is
2161 reloaded on a host with different endianness. The utility function
2162 pcre_pattern_to_host_byte_order() can be used to convert such a pattern
2163 so that it runs on the new host.
2164
2165 PCRE_ERROR_JIT_BADOPTION
2166
2167 This error is returned when a pattern that was successfully studied
2168 using a JIT compile option is being matched, but the matching mode
2169 (partial or complete match) does not correspond to any JIT compilation
2170 mode. When the JIT fast path function is used, this error may be also
2171 given for invalid options. See the pcrejit documentation for more
2172 details.
2173
2174 PCRE_ERROR_BADLENGTH (-32)
2175
2176 This error is given if pcre_exec() is called with a negative value for
2177 the length argument.
2178
2179 Error numbers -16 to -20, -22, and 30 are not used by pcre_exec().
2180
2181 Reason codes for invalid UTF-8 strings
2182
2183 This section applies only to the 8-bit library. The corresponding
2184 information for the 16-bit and 32-bit libraries is given in the pcre16
2185 and pcre32 pages.
2186
2187 When pcre_exec() returns either PCRE_ERROR_BADUTF8 or PCRE_ERROR_SHORT‐
2188 UTF8, and the size of the output vector (ovecsize) is at least 2, the
2189 offset of the start of the invalid UTF-8 character is placed in the
2190 first output vector element (ovector[0]) and a reason code is placed in
2191 the second element (ovector[1]). The reason codes are given names in
2192 the pcre.h header file:
2193
2194 PCRE_UTF8_ERR1
2195 PCRE_UTF8_ERR2
2196 PCRE_UTF8_ERR3
2197 PCRE_UTF8_ERR4
2198 PCRE_UTF8_ERR5
2199
2200 The string ends with a truncated UTF-8 character; the code specifies
2201 how many bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8
2202 characters to be no longer than 4 bytes, the encoding scheme (origi‐
2203 nally defined by RFC 2279) allows for up to 6 bytes, and this is
2204 checked first; hence the possibility of 4 or 5 missing bytes.
2205
2206 PCRE_UTF8_ERR6
2207 PCRE_UTF8_ERR7
2208 PCRE_UTF8_ERR8
2209 PCRE_UTF8_ERR9
2210 PCRE_UTF8_ERR10
2211
2212 The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of
2213 the character do not have the binary value 0b10 (that is, either the
2214 most significant bit is 0, or the next bit is 1).
2215
2216 PCRE_UTF8_ERR11
2217 PCRE_UTF8_ERR12
2218
2219 A character that is valid by the RFC 2279 rules is either 5 or 6 bytes
2220 long; these code points are excluded by RFC 3629.
2221
2222 PCRE_UTF8_ERR13
2223
2224 A 4-byte character has a value greater than 0x10fff; these code points
2225 are excluded by RFC 3629.
2226
2227 PCRE_UTF8_ERR14
2228
2229 A 3-byte character has a value in the range 0xd800 to 0xdfff; this
2230 range of code points are reserved by RFC 3629 for use with UTF-16, and
2231 so are excluded from UTF-8.
2232
2233 PCRE_UTF8_ERR15
2234 PCRE_UTF8_ERR16
2235 PCRE_UTF8_ERR17
2236 PCRE_UTF8_ERR18
2237 PCRE_UTF8_ERR19
2238
2239 A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes
2240 for a value that can be represented by fewer bytes, which is invalid.
2241 For example, the two bytes 0xc0, 0xae give the value 0x2e, whose cor‐
2242 rect coding uses just one byte.
2243
2244 PCRE_UTF8_ERR20
2245
2246 The two most significant bits of the first byte of a character have the
2247 binary value 0b10 (that is, the most significant bit is 1 and the sec‐
2248 ond is 0). Such a byte can only validly occur as the second or subse‐
2249 quent byte of a multi-byte character.
2250
2251 PCRE_UTF8_ERR21
2252
2253 The first byte of a character has the value 0xfe or 0xff. These values
2254 can never occur in a valid UTF-8 string.
2255
2256 PCRE_UTF8_ERR22
2257
2258 This error code was formerly used when the presence of a so-called
2259 "non-character" caused an error. Unicode corrigendum #9 makes it clear
2260 that such characters should not cause a string to be rejected, and so
2261 this code is no longer in use and is never returned.
2262
2264
2265 int pcre_copy_substring(const char *subject, int *ovector,
2266 int stringcount, int stringnumber, char *buffer,
2267 int buffersize);
2268
2269 int pcre_get_substring(const char *subject, int *ovector,
2270 int stringcount, int stringnumber,
2271 const char **stringptr);
2272
2273 int pcre_get_substring_list(const char *subject,
2274 int *ovector, int stringcount, const char ***listptr);
2275
2276 Captured substrings can be accessed directly by using the offsets
2277 returned by pcre_exec() in ovector. For convenience, the functions
2278 pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub‐
2279 string_list() are provided for extracting captured substrings as new,
2280 separate, zero-terminated strings. These functions identify substrings
2281 by number. The next section describes functions for extracting named
2282 substrings.
2283
2284 A substring that contains a binary zero is correctly extracted and has
2285 a further zero added on the end, but the result is not, of course, a C
2286 string. However, you can process such a string by referring to the
2287 length that is returned by pcre_copy_substring() and pcre_get_sub‐
2288 string(). Unfortunately, the interface to pcre_get_substring_list() is
2289 not adequate for handling strings containing binary zeros, because the
2290 end of the final string is not independently indicated.
2291
2292 The first three arguments are the same for all three of these func‐
2293 tions: subject is the subject string that has just been successfully
2294 matched, ovector is a pointer to the vector of integer offsets that was
2295 passed to pcre_exec(), and stringcount is the number of substrings that
2296 were captured by the match, including the substring that matched the
2297 entire regular expression. This is the value returned by pcre_exec() if
2298 it is greater than zero. If pcre_exec() returned zero, indicating that
2299 it ran out of space in ovector, the value passed as stringcount should
2300 be the number of elements in the vector divided by three.
2301
2302 The functions pcre_copy_substring() and pcre_get_substring() extract a
2303 single substring, whose number is given as stringnumber. A value of
2304 zero extracts the substring that matched the entire pattern, whereas
2305 higher values extract the captured substrings. For pcre_copy_sub‐
2306 string(), the string is placed in buffer, whose length is given by
2307 buffersize, while for pcre_get_substring() a new block of memory is
2308 obtained via pcre_malloc, and its address is returned via stringptr.
2309 The yield of the function is the length of the string, not including
2310 the terminating zero, or one of these error codes:
2311
2312 PCRE_ERROR_NOMEMORY (-6)
2313
2314 The buffer was too small for pcre_copy_substring(), or the attempt to
2315 get memory failed for pcre_get_substring().
2316
2317 PCRE_ERROR_NOSUBSTRING (-7)
2318
2319 There is no substring whose number is stringnumber.
2320
2321 The pcre_get_substring_list() function extracts all available sub‐
2322 strings and builds a list of pointers to them. All this is done in a
2323 single block of memory that is obtained via pcre_malloc. The address of
2324 the memory block is returned via listptr, which is also the start of
2325 the list of string pointers. The end of the list is marked by a NULL
2326 pointer. The yield of the function is zero if all went well, or the
2327 error code
2328
2329 PCRE_ERROR_NOMEMORY (-6)
2330
2331 if the attempt to get the memory block failed.
2332
2333 When any of these functions encounter a substring that is unset, which
2334 can happen when capturing subpattern number n+1 matches some part of
2335 the subject, but subpattern n has not been used at all, they return an
2336 empty string. This can be distinguished from a genuine zero-length sub‐
2337 string by inspecting the appropriate offset in ovector, which is nega‐
2338 tive for unset substrings.
2339
2340 The two convenience functions pcre_free_substring() and pcre_free_sub‐
2341 string_list() can be used to free the memory returned by a previous
2342 call of pcre_get_substring() or pcre_get_substring_list(), respec‐
2343 tively. They do nothing more than call the function pointed to by
2344 pcre_free, which of course could be called directly from a C program.
2345 However, PCRE is used in some situations where it is linked via a spe‐
2346 cial interface to another programming language that cannot use
2347 pcre_free directly; it is for these cases that the functions are pro‐
2348 vided.
2349
2351
2352 int pcre_get_stringnumber(const pcre *code,
2353 const char *name);
2354
2355 int pcre_copy_named_substring(const pcre *code,
2356 const char *subject, int *ovector,
2357 int stringcount, const char *stringname,
2358 char *buffer, int buffersize);
2359
2360 int pcre_get_named_substring(const pcre *code,
2361 const char *subject, int *ovector,
2362 int stringcount, const char *stringname,
2363 const char **stringptr);
2364
2365 To extract a substring by name, you first have to find associated num‐
2366 ber. For example, for this pattern
2367
2368 (a+)b(?<xxx>\d+)...
2369
2370 the number of the subpattern called "xxx" is 2. If the name is known to
2371 be unique (PCRE_DUPNAMES was not set), you can find the number from the
2372 name by calling pcre_get_stringnumber(). The first argument is the com‐
2373 piled pattern, and the second is the name. The yield of the function is
2374 the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no
2375 subpattern of that name.
2376
2377 Given the number, you can extract the substring directly, or use one of
2378 the functions described in the previous section. For convenience, there
2379 are also two functions that do the whole job.
2380
2381 Most of the arguments of pcre_copy_named_substring() and
2382 pcre_get_named_substring() are the same as those for the similarly
2383 named functions that extract by number. As these are described in the
2384 previous section, they are not re-described here. There are just two
2385 differences:
2386
2387 First, instead of a substring number, a substring name is given. Sec‐
2388 ond, there is an extra argument, given at the start, which is a pointer
2389 to the compiled pattern. This is needed in order to gain access to the
2390 name-to-number translation table.
2391
2392 These functions call pcre_get_stringnumber(), and if it succeeds, they
2393 then call pcre_copy_substring() or pcre_get_substring(), as appropri‐
2394 ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate names, the
2395 behaviour may not be what you want (see the next section).
2396
2397 Warning: If the pattern uses the (?| feature to set up multiple subpat‐
2398 terns with the same number, as described in the section on duplicate
2399 subpattern numbers in the pcrepattern page, you cannot use names to
2400 distinguish the different subpatterns, because names are not included
2401 in the compiled code. The matching process uses only numbers. For this
2402 reason, the use of different names for subpatterns of the same number
2403 causes an error at compile time.
2404
2406
2407 int pcre_get_stringtable_entries(const pcre *code,
2408 const char *name, char **first, char **last);
2409
2410 When a pattern is compiled with the PCRE_DUPNAMES option, names for
2411 subpatterns are not required to be unique. (Duplicate names are always
2412 allowed for subpatterns with the same number, created by using the (?|
2413 feature. Indeed, if such subpatterns are named, they are required to
2414 use the same names.)
2415
2416 Normally, patterns with duplicate names are such that in any one match,
2417 only one of the named subpatterns participates. An example is shown in
2418 the pcrepattern documentation.
2419
2420 When duplicates are present, pcre_copy_named_substring() and
2421 pcre_get_named_substring() return the first substring corresponding to
2422 the given name that is set. If none are set, PCRE_ERROR_NOSUBSTRING
2423 (-7) is returned; no data is returned. The pcre_get_stringnumber()
2424 function returns one of the numbers that are associated with the name,
2425 but it is not defined which it is.
2426
2427 If you want to get full details of all captured substrings for a given
2428 name, you must use the pcre_get_stringtable_entries() function. The
2429 first argument is the compiled pattern, and the second is the name. The
2430 third and fourth are pointers to variables which are updated by the
2431 function. After it has run, they point to the first and last entries in
2432 the name-to-number table for the given name. The function itself
2433 returns the length of each entry, or PCRE_ERROR_NOSUBSTRING (-7) if
2434 there are none. The format of the table is described above in the sec‐
2435 tion entitled Information about a pattern above. Given all the rele‐
2436 vant entries for the name, you can extract each of their numbers, and
2437 hence the captured data, if any.
2438
2440
2441 The traditional matching function uses a similar algorithm to Perl,
2442 which stops when it finds the first match, starting at a given point in
2443 the subject. If you want to find all possible matches, or the longest
2444 possible match, consider using the alternative matching function (see
2445 below) instead. If you cannot use the alternative function, but still
2446 need to find all possible matches, you can kludge it up by making use
2447 of the callout facility, which is described in the pcrecallout documen‐
2448 tation.
2449
2450 What you have to do is to insert a callout right at the end of the pat‐
2451 tern. When your callout function is called, extract and save the cur‐
2452 rent matched substring. Then return 1, which forces pcre_exec() to
2453 backtrack and try other alternatives. Ultimately, when it runs out of
2454 matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.
2455
2457
2458 Matching certain patterns using pcre_exec() can use a lot of process
2459 stack, which in certain environments can be rather limited in size.
2460 Some users find it helpful to have an estimate of the amount of stack
2461 that is used by pcre_exec(), to help them set recursion limits, as
2462 described in the pcrestack documentation. The estimate that is output
2463 by pcretest when called with the -m and -C options is obtained by call‐
2464 ing pcre_exec with the values NULL, NULL, NULL, -999, and -999 for its
2465 first five arguments.
2466
2467 Normally, if its first argument is NULL, pcre_exec() immediately
2468 returns the negative error code PCRE_ERROR_NULL, but with this special
2469 combination of arguments, it returns instead a negative number whose
2470 absolute value is the approximate stack frame size in bytes. (A nega‐
2471 tive number is used so that it is clear that no match has happened.)
2472 The value is approximate because in some cases, recursive calls to
2473 pcre_exec() occur when there are one or two additional variables on the
2474 stack.
2475
2476 If PCRE has been compiled to use the heap instead of the stack for
2477 recursion, the value returned is the size of each block that is
2478 obtained from the heap.
2479
2481
2482 int pcre_dfa_exec(const pcre *code, const pcre_extra *extra,
2483 const char *subject, int length, int startoffset,
2484 int options, int *ovector, int ovecsize,
2485 int *workspace, int wscount);
2486
2487 The function pcre_dfa_exec() is called to match a subject string
2488 against a compiled pattern, using a matching algorithm that scans the
2489 subject string just once, and does not backtrack. This has different
2490 characteristics to the normal algorithm, and is not compatible with
2491 Perl. Some of the features of PCRE patterns are not supported. Never‐
2492 theless, there are times when this kind of matching can be useful. For
2493 a discussion of the two matching algorithms, and a list of features
2494 that pcre_dfa_exec() does not support, see the pcrematching documenta‐
2495 tion.
2496
2497 The arguments for the pcre_dfa_exec() function are the same as for
2498 pcre_exec(), plus two extras. The ovector argument is used in a differ‐
2499 ent way, and this is described below. The other common arguments are
2500 used in the same way as for pcre_exec(), so their description is not
2501 repeated here.
2502
2503 The two additional arguments provide workspace for the function. The
2504 workspace vector should contain at least 20 elements. It is used for
2505 keeping track of multiple paths through the pattern tree. More
2506 workspace will be needed for patterns and subjects where there are a
2507 lot of potential matches.
2508
2509 Here is an example of a simple call to pcre_dfa_exec():
2510
2511 int rc;
2512 int ovector[10];
2513 int wspace[20];
2514 rc = pcre_dfa_exec(
2515 re, /* result of pcre_compile() */
2516 NULL, /* we didn't study the pattern */
2517 "some string", /* the subject string */
2518 11, /* the length of the subject string */
2519 0, /* start at offset 0 in the subject */
2520 0, /* default options */
2521 ovector, /* vector of integers for substring information */
2522 10, /* number of elements (NOT size in bytes) */
2523 wspace, /* working space vector */
2524 20); /* number of elements (NOT size in bytes) */
2525
2526 Option bits for pcre_dfa_exec()
2527
2528 The unused bits of the options argument for pcre_dfa_exec() must be
2529 zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEW‐
2530 LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,
2531 PCRE_NOTEMPTY_ATSTART, PCRE_NO_UTF8_CHECK, PCRE_BSR_ANYCRLF,
2532 PCRE_BSR_UNICODE, PCRE_NO_START_OPTIMIZE, PCRE_PARTIAL_HARD, PCRE_PAR‐
2533 TIAL_SOFT, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last
2534 four of these are exactly the same as for pcre_exec(), so their
2535 description is not repeated here.
2536
2537 PCRE_PARTIAL_HARD
2538 PCRE_PARTIAL_SOFT
2539
2540 These have the same general effect as they do for pcre_exec(), but the
2541 details are slightly different. When PCRE_PARTIAL_HARD is set for
2542 pcre_dfa_exec(), it returns PCRE_ERROR_PARTIAL if the end of the sub‐
2543 ject is reached and there is still at least one matching possibility
2544 that requires additional characters. This happens even if some complete
2545 matches have also been found. When PCRE_PARTIAL_SOFT is set, the return
2546 code PCRE_ERROR_NOMATCH is converted into PCRE_ERROR_PARTIAL if the end
2547 of the subject is reached, there have been no complete matches, but
2548 there is still at least one matching possibility. The portion of the
2549 string that was inspected when the longest partial match was found is
2550 set as the first matching string in both cases. There is a more
2551 detailed discussion of partial and multi-segment matching, with exam‐
2552 ples, in the pcrepartial documentation.
2553
2554 PCRE_DFA_SHORTEST
2555
2556 Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to
2557 stop as soon as it has found one match. Because of the way the alterna‐
2558 tive algorithm works, this is necessarily the shortest possible match
2559 at the first possible matching point in the subject string.
2560
2561 PCRE_DFA_RESTART
2562
2563 When pcre_dfa_exec() returns a partial match, it is possible to call it
2564 again, with additional subject characters, and have it continue with
2565 the same match. The PCRE_DFA_RESTART option requests this action; when
2566 it is set, the workspace and wscount options must reference the same
2567 vector as before because data about the match so far is left in them
2568 after a partial match. There is more discussion of this facility in the
2569 pcrepartial documentation.
2570
2571 Successful returns from pcre_dfa_exec()
2572
2573 When pcre_dfa_exec() succeeds, it may have matched more than one sub‐
2574 string in the subject. Note, however, that all the matches from one run
2575 of the function start at the same point in the subject. The shorter
2576 matches are all initial substrings of the longer matches. For example,
2577 if the pattern
2578
2579 <.*>
2580
2581 is matched against the string
2582
2583 This is <something> <something else> <something further> no more
2584
2585 the three matched strings are
2586
2587 <something>
2588 <something> <something else>
2589 <something> <something else> <something further>
2590
2591 On success, the yield of the function is a number greater than zero,
2592 which is the number of matched substrings. The substrings themselves
2593 are returned in ovector. Each string uses two elements; the first is
2594 the offset to the start, and the second is the offset to the end. In
2595 fact, all the strings have the same start offset. (Space could have
2596 been saved by giving this only once, but it was decided to retain some
2597 compatibility with the way pcre_exec() returns data, even though the
2598 meaning of the strings is different.)
2599
2600 The strings are returned in reverse order of length; that is, the long‐
2601 est matching string is given first. If there were too many matches to
2602 fit into ovector, the yield of the function is zero, and the vector is
2603 filled with the longest matches. Unlike pcre_exec(), pcre_dfa_exec()
2604 can use the entire ovector for returning matched strings.
2605
2606 NOTE: PCRE's "auto-possessification" optimization usually applies to
2607 character repeats at the end of a pattern (as well as internally). For
2608 example, the pattern "a\d+" is compiled as if it were "a\d++" because
2609 there is no point even considering the possibility of backtracking into
2610 the repeated digits. For DFA matching, this means that only one possi‐
2611 ble match is found. If you really do want multiple matches in such
2612 cases, either use an ungreedy repeat ("a\d+?") or set the
2613 PCRE_NO_AUTO_POSSESS option when compiling.
2614
2615 Error returns from pcre_dfa_exec()
2616
2617 The pcre_dfa_exec() function returns a negative number when it fails.
2618 Many of the errors are the same as for pcre_exec(), and these are
2619 described above. There are in addition the following errors that are
2620 specific to pcre_dfa_exec():
2621
2622 PCRE_ERROR_DFA_UITEM (-16)
2623
2624 This return is given if pcre_dfa_exec() encounters an item in the pat‐
2625 tern that it does not support, for instance, the use of \C or a back
2626 reference.
2627
2628 PCRE_ERROR_DFA_UCOND (-17)
2629
2630 This return is given if pcre_dfa_exec() encounters a condition item
2631 that uses a back reference for the condition, or a test for recursion
2632 in a specific group. These are not supported.
2633
2634 PCRE_ERROR_DFA_UMLIMIT (-18)
2635
2636 This return is given if pcre_dfa_exec() is called with an extra block
2637 that contains a setting of the match_limit or match_limit_recursion
2638 fields. This is not supported (these fields are meaningless for DFA
2639 matching).
2640
2641 PCRE_ERROR_DFA_WSSIZE (-19)
2642
2643 This return is given if pcre_dfa_exec() runs out of space in the
2644 workspace vector.
2645
2646 PCRE_ERROR_DFA_RECURSE (-20)
2647
2648 When a recursive subpattern is processed, the matching function calls
2649 itself recursively, using private vectors for ovector and workspace.
2650 This error is given if the output vector is not large enough. This
2651 should be extremely rare, as a vector of size 1000 is used.
2652
2653 PCRE_ERROR_DFA_BADRESTART (-30)
2654
2655 When pcre_dfa_exec() is called with the PCRE_DFA_RESTART option, some
2656 plausibility checks are made on the contents of the workspace, which
2657 should contain data about the previous partial match. If any of these
2658 checks fail, this error is given.
2659
2661
2662 pcre16(3), pcre32(3), pcrebuild(3), pcrecallout(3), pcrecpp(3)[22m(3),
2663 pcrematching(3), pcrepartial(3), pcreposix(3), pcreprecompile(3), pcre‐
2664 sample(3), pcrestack(3).
2665
2667
2668 Philip Hazel
2669 University Computing Service
2670 Cambridge CB2 3QH, England.
2671
2673
2674 Last updated: 18 December 2015
2675 Copyright (c) 1997-2015 University of Cambridge.
2676
2677
2678
2679PCRE 8.39 18 December 2015 PCREAPI(3)