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