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