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