Parent Directory
|
Revision Log
|
Patch
| revision 53 by nigel, Sat Feb 24 21:39:42 2007 UTC | revision 87 by nigel, Sat Feb 24 21:41:21 2007 UTC | |
|---|---|---|
| # | Line 1 | Line 1 |
| 1 | ----------------------------------------------------------------------------- | |
| 2 | This file contains a concatenation of the PCRE man pages, converted to plain | |
| 3 | text format for ease of searching with a text editor, or for use on systems | |
| 4 | that do not have a man page processor. The small individual files that give | |
| 5 | synopses of each function in the library have not been included. There are | |
| 6 | separate text files for the pcregrep and pcretest commands. | |
| 7 | ----------------------------------------------------------------------------- | |
| 8 | ||
| 9 | ||
| 10 | PCRE(3) PCRE(3) | |
| 11 | ||
| 12 | ||
| 13 | NAME | NAME |
| 14 | pcre - Perl-compatible regular expressions. | PCRE - Perl-compatible regular expressions |
| 15 | ||
| 16 | ||
| 17 | INTRODUCTION | |
| 18 | ||
| 19 | SYNOPSIS | The PCRE library is a set of functions that implement regular expres- |
| 20 | #include <pcre.h> | sion pattern matching using the same syntax and semantics as Perl, with |
| 21 | just a few differences. The current implementation of PCRE (release | |
| 22 | 6.x) corresponds approximately with Perl 5.8, including support for | |
| 23 | UTF-8 encoded strings and Unicode general category properties. However, | |
| 24 | this support has to be explicitly enabled; it is not the default. | |
| 25 | ||
| 26 | In addition to the Perl-compatible matching function, PCRE also con- | |
| 27 | tains an alternative matching function that matches the same compiled | |
| 28 | patterns in a different way. In certain circumstances, the alternative | |
| 29 | function has some advantages. For a discussion of the two matching | |
| 30 | algorithms, see the pcrematching page. | |
| 31 | ||
| 32 | PCRE is written in C and released as a C library. A number of people | |
| 33 | have written wrappers and interfaces of various kinds. In particular, | |
| 34 | Google Inc. have provided a comprehensive C++ wrapper. This is now | |
| 35 | included as part of the PCRE distribution. The pcrecpp page has details | |
| 36 | of this interface. Other people's contributions can be found in the | |
| 37 | Contrib directory at the primary FTP site, which is: | |
| 38 | ||
| 39 | ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre | |
| 40 | ||
| 41 | Details of exactly which Perl regular expression features are and are | |
| 42 | not supported by PCRE are given in separate documents. See the pcrepat- | |
| 43 | tern and pcrecompat pages. | |
| 44 | ||
| 45 | Some features of PCRE can be included, excluded, or changed when the | |
| 46 | library is built. The pcre_config() function makes it possible for a | |
| 47 | client to discover which features are available. The features them- | |
| 48 | selves are described in the pcrebuild page. Documentation about build- | |
| 49 | ing PCRE for various operating systems can be found in the README file | |
| 50 | in the source distribution. | |
| 51 | ||
| 52 | The library contains a number of undocumented internal functions and | |
| 53 | data tables that are used by more than one of the exported external | |
| 54 | functions, but which are not intended for use by external callers. | |
| 55 | Their names all begin with "_pcre_", which hopefully will not provoke | |
| 56 | any name clashes. In some environments, it is possible to control which | |
| 57 | external symbols are exported when a shared library is built, and in | |
| 58 | these cases the undocumented symbols are not exported. | |
| 59 | ||
| 60 | ||
| 61 | USER DOCUMENTATION | |
| 62 | ||
| 63 | The user documentation for PCRE comprises a number of different sec- | |
| 64 | tions. In the "man" format, each of these is a separate "man page". In | |
| 65 | the HTML format, each is a separate page, linked from the index page. | |
| 66 | In the plain text format, all the sections are concatenated, for ease | |
| 67 | of searching. The sections are as follows: | |
| 68 | ||
| 69 | pcre this document | |
| 70 | pcreapi details of PCRE's native C API | |
| 71 | pcrebuild options for building PCRE | |
| 72 | pcrecallout details of the callout feature | |
| 73 | pcrecompat discussion of Perl compatibility | |
| 74 | pcrecpp details of the C++ wrapper | |
| 75 | pcregrep description of the pcregrep command | |
| 76 | pcrematching discussion of the two matching algorithms | |
| 77 | pcrepartial details of the partial matching facility | |
| 78 | pcrepattern syntax and semantics of supported | |
| 79 | regular expressions | |
| 80 | pcreperform discussion of performance issues | |
| 81 | pcreposix the POSIX-compatible C API | |
| 82 | pcreprecompile details of saving and re-using precompiled patterns | |
| 83 | pcresample discussion of the sample program | |
| 84 | pcretest description of the pcretest testing command | |
| 85 | ||
| 86 | pcre *pcre_compile(const char *pattern, int options, | In addition, in the "man" and HTML formats, there is a short page for |
| 87 | const char **errptr, int *erroffset, | each C library function, listing its arguments and results. |
| const unsigned char *tableptr); | ||
| 88 | ||
| pcre_extra *pcre_study(const pcre *code, int options, | ||
| const char **errptr); | ||
| 89 | ||
| 90 | int pcre_exec(const pcre *code, const pcre_extra *extra, | LIMITATIONS |
| const char *subject, int length, int startoffset, | ||
| int options, int *ovector, int ovecsize); | ||
| 91 | ||
| 92 | int pcre_copy_substring(const char *subject, int *ovector, | There are some size limitations in PCRE but it is hoped that they will |
| 93 | int stringcount, int stringnumber, char *buffer, | never in practice be relevant. |
| int buffersize); | ||
| 94 | ||
| 95 | int pcre_get_substring(const char *subject, int *ovector, | The maximum length of a compiled pattern is 65539 (sic) bytes if PCRE |
| 96 | int stringcount, int stringnumber, | is compiled with the default internal linkage size of 2. If you want to |
| 97 | const char **stringptr); | process regular expressions that are truly enormous, you can compile |
| 98 | PCRE with an internal linkage size of 3 or 4 (see the README file in | |
| 99 | the source distribution and the pcrebuild documentation for details). | |
| 100 | In these cases the limit is substantially larger. However, the speed | |
| 101 | of execution will be slower. | |
| 102 | ||
| 103 | All values in repeating quantifiers must be less than 65536. The maxi- | |
| 104 | mum number of capturing subpatterns is 65535. | |
| 105 | ||
| 106 | There is no limit to the number of non-capturing subpatterns, but the | |
| 107 | maximum depth of nesting of all kinds of parenthesized subpattern, | |
| 108 | including capturing subpatterns, assertions, and other types of subpat- | |
| 109 | tern, is 200. | |
| 110 | ||
| 111 | The maximum length of a subject string is the largest positive number | |
| 112 | that an integer variable can hold. However, when using the traditional | |
| 113 | matching function, PCRE uses recursion to handle subpatterns and indef- | |
| 114 | inite repetition. This means that the available stack space may limit | |
| 115 | the size of a subject string that can be processed by certain patterns. | |
| 116 | ||
| 117 | ||
| 118 | UTF-8 AND UNICODE PROPERTY SUPPORT | |
| 119 | ||
| 120 | From release 3.3, PCRE has had some support for character strings | |
| 121 | encoded in the UTF-8 format. For release 4.0 this was greatly extended | |
| 122 | to cover most common requirements, and in release 5.0 additional sup- | |
| 123 | port for Unicode general category properties was added. | |
| 124 | ||
| 125 | In order process UTF-8 strings, you must build PCRE to include UTF-8 | |
| 126 | support in the code, and, in addition, you must call pcre_compile() | |
| 127 | with the PCRE_UTF8 option flag. When you do this, both the pattern and | |
| 128 | any subject strings that are matched against it are treated as UTF-8 | |
| 129 | strings instead of just strings of bytes. | |
| 130 | ||
| 131 | If you compile PCRE with UTF-8 support, but do not use it at run time, | |
| 132 | the library will be a bit bigger, but the additional run time overhead | |
| 133 | is limited to testing the PCRE_UTF8 flag in several places, so should | |
| 134 | not be very large. | |
| 135 | ||
| 136 | If PCRE is built with Unicode character property support (which implies | |
| 137 | UTF-8 support), the escape sequences \p{..}, \P{..}, and \X are sup- | |
| 138 | ported. The available properties that can be tested are limited to the | |
| 139 | general category properties such as Lu for an upper case letter or Nd | |
| 140 | for a decimal number, the Unicode script names such as Arabic or Han, | |
| 141 | and the derived properties Any and L&. A full list is given in the | |
| 142 | pcrepattern documentation. Only the short names for properties are sup- | |
| 143 | ported. For example, \p{L} matches a letter. Its Perl synonym, \p{Let- | |
| 144 | ter}, is not supported. Furthermore, in Perl, many properties may | |
| 145 | optionally be prefixed by "Is", for compatibility with Perl 5.6. PCRE | |
| 146 | does not support this. | |
| 147 | ||
| 148 | The following comments apply when PCRE is running in UTF-8 mode: | |
| 149 | ||
| 150 | 1. When you set the PCRE_UTF8 flag, the strings passed as patterns and | |
| 151 | subjects are checked for validity on entry to the relevant functions. | |
| 152 | If an invalid UTF-8 string is passed, an error return is given. In some | |
| 153 | situations, you may already know that your strings are valid, and | |
| 154 | therefore want to skip these checks in order to improve performance. If | |
| 155 | you set the PCRE_NO_UTF8_CHECK flag at compile time or at run time, | |
| 156 | PCRE assumes that the pattern or subject it is given (respectively) | |
| 157 | contains only valid UTF-8 codes. In this case, it does not diagnose an | |
| 158 | invalid UTF-8 string. If you pass an invalid UTF-8 string to PCRE when | |
| 159 | PCRE_NO_UTF8_CHECK is set, the results are undefined. Your program may | |
| 160 | crash. | |
| 161 | ||
| 162 | 2. An unbraced hexadecimal escape sequence (such as \xb3) matches a | |
| 163 | two-byte UTF-8 character if the value is greater than 127. | |
| 164 | ||
| 165 | 3. Repeat quantifiers apply to complete UTF-8 characters, not to indi- | |
| 166 | vidual bytes, for example: \x{100}{3}. | |
| 167 | ||
| 168 | 4. The dot metacharacter matches one UTF-8 character instead of a sin- | |
| 169 | gle byte. | |
| 170 | ||
| 171 | 5. The escape sequence \C can be used to match a single byte in UTF-8 | |
| 172 | mode, but its use can lead to some strange effects. This facility is | |
| 173 | not available in the alternative matching function, pcre_dfa_exec(). | |
| 174 | ||
| 175 | 6. The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly | |
| 176 | test characters of any code value, but the characters that PCRE recog- | |
| 177 | nizes as digits, spaces, or word characters remain the same set as | |
| 178 | before, all with values less than 256. This remains true even when PCRE | |
| 179 | includes Unicode property support, because to do otherwise would slow | |
| 180 | down PCRE in many common cases. If you really want to test for a wider | |
| 181 | sense of, say, "digit", you must use Unicode property tests such as | |
| 182 | \p{Nd}. | |
| 183 | ||
| 184 | 7. Similarly, characters that match the POSIX named character classes | |
| 185 | are all low-valued characters. | |
| 186 | ||
| 187 | 8. Case-insensitive matching applies only to characters whose values | |
| 188 | are less than 128, unless PCRE is built with Unicode property support. | |
| 189 | Even when Unicode property support is available, PCRE still uses its | |
| 190 | own character tables when checking the case of low-valued characters, | |
| 191 | so as not to degrade performance. The Unicode property information is | |
| 192 | used only for characters with higher values. Even when Unicode property | |
| 193 | support is available, PCRE supports case-insensitive matching only when | |
| 194 | there is a one-to-one mapping between a letter's cases. There are a | |
| 195 | small number of many-to-one mappings in Unicode; these are not sup- | |
| 196 | ported by PCRE. | |
| 197 | ||
| int pcre_get_substring_list(const char *subject, | ||
| int *ovector, int stringcount, const char ***listptr); | ||
| 198 | ||
| 199 | void pcre_free_substring(const char *stringptr); | AUTHOR |
| 200 | ||
| 201 | void pcre_free_substring_list(const char **stringptr); | Philip Hazel |
| 202 | University Computing Service, | |
| 203 | Cambridge CB2 3QG, England. | |
| 204 | ||
| 205 | const unsigned char *pcre_maketables(void); | Putting an actual email address here seems to have been a spam magnet, |
| 206 | so I've taken it away. If you want to email me, use my initial and sur- | |
| 207 | name, separated by a dot, at the domain ucs.cam.ac.uk. | |
| 208 | ||
| 209 | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, | Last updated: 24 January 2006 |
| 210 | int what, void *where); | Copyright (c) 1997-2006 University of Cambridge. |
| 211 | ------------------------------------------------------------------------------ | |
| 212 | ||
| int pcre_info(const pcre *code, int *optptr, *firstcharptr); | ||
| 213 | ||
| 214 | char *pcre_version(void); | PCREBUILD(3) PCREBUILD(3) |
| 215 | ||
| void *(*pcre_malloc)(size_t); | ||
| 216 | ||
| 217 | void (*pcre_free)(void *); | NAME |
| 218 | PCRE - Perl-compatible regular expressions | |
| 219 | ||
| 220 | ||
| 221 | PCRE BUILD-TIME OPTIONS | |
| 222 | ||
| 223 | This document describes the optional features of PCRE that can be | |
| 224 | selected when the library is compiled. They are all selected, or dese- | |
| 225 | lected, by providing options to the configure script that is run before | |
| 226 | the make command. The complete list of options for configure (which | |
| 227 | includes the standard ones such as the selection of the installation | |
| 228 | directory) can be obtained by running | |
| 229 | ||
| 230 | DESCRIPTION | ./configure --help |
| 231 | The PCRE library is a set of functions that implement regu- | |
| 232 | lar expression pattern matching using the same syntax and | The following sections describe certain options whose names begin with |
| 233 | semantics as Perl 5, with just a few differences (see | --enable or --disable. These settings specify changes to the defaults |
| 234 | for the configure command. Because of the way that configure works, | |
| 235 | below). The current implementation corresponds to Perl | --enable and --disable always come in pairs, so the complementary |
| 236 | 5.005, with some additional features from later versions. | option always exists as well, but as it specifies the default, it is |
| 237 | This includes some experimental, incomplete support for | not described. |
| 238 | UTF-8 encoded strings. Details of exactly what is and what | |
| 239 | is not supported are given below. | |
| 240 | C++ SUPPORT | |
| 241 | PCRE has its own native API, which is described in this | |
| 242 | document. There is also a set of wrapper functions that | By default, the configure script will search for a C++ compiler and C++ |
| 243 | correspond to the POSIX regular expression API. These are | header files. If it finds them, it automatically builds the C++ wrapper |
| 244 | described in the pcreposix documentation. | library for PCRE. You can disable this by adding |
| 245 | ||
| 246 | The native API function prototypes are defined in the header | --disable-cpp |
| 247 | file pcre.h, and on Unix systems the library itself is | |
| 248 | called libpcre.a, so can be accessed by adding -lpcre to the | to the configure command. |
| 249 | command for linking an application which calls it. The | |
| 250 | header file defines the macros PCRE_MAJOR and PCRE_MINOR to | |
| 251 | contain the major and minor release numbers for the library. | UTF-8 SUPPORT |
| 252 | Applications can use these to include support for different | |
| 253 | releases. | To build PCRE with support for UTF-8 character strings, add |
| 254 | ||
| 255 | The functions pcre_compile(), pcre_study(), and pcre_exec() | --enable-utf8 |
| 256 | are used for compiling and matching regular expressions. A | |
| 257 | sample program that demonstrates the simplest way of using | to the configure command. Of itself, this does not make PCRE treat |
| 258 | them is given in the file pcredemo.c. The last section of | strings as UTF-8. As well as compiling PCRE with this option, you also |
| 259 | this man page describes how to run it. | have have to set the PCRE_UTF8 option when you call the pcre_compile() |
| 260 | function. | |
| 261 | The functions pcre_copy_substring(), pcre_get_substring(), | |
| 262 | and pcre_get_substring_list() are convenience functions for | |
| 263 | extracting captured substrings from a matched subject | UNICODE CHARACTER PROPERTY SUPPORT |
| 264 | string; pcre_free_substring() and pcre_free_substring_list() | |
| 265 | are also provided, to free the memory used for extracted | UTF-8 support allows PCRE to process character values greater than 255 |
| 266 | strings. | in the strings that it handles. On its own, however, it does not pro- |
| 267 | vide any facilities for accessing the properties of such characters. If | |
| 268 | The function pcre_maketables() is used (optionally) to build | you want to be able to use the pattern escapes \P, \p, and \X, which |
| 269 | a set of character tables in the current locale for passing | refer to Unicode character properties, you must add |
| 270 | to pcre_compile(). | |
| 271 | --enable-unicode-properties | |
| 272 | The function pcre_fullinfo() is used to find out information | |
| 273 | about a compiled pattern; pcre_info() is an obsolete version | to the configure command. This implies UTF-8 support, even if you have |
| 274 | which returns only some of the available information, but is | not explicitly requested it. |
| 275 | retained for backwards compatibility. The function | |
| 276 | pcre_version() returns a pointer to a string containing the | Including Unicode property support adds around 90K of tables to the |
| 277 | version of PCRE and its date of release. | PCRE library, approximately doubling its size. Only the general cate- |
| 278 | gory properties such as Lu and Nd are supported. Details are given in | |
| 279 | The global variables pcre_malloc and pcre_free initially | the pcrepattern documentation. |
| 280 | contain the entry points of the standard malloc() and free() | |
| 281 | functions respectively. PCRE calls the memory management | |
| 282 | functions via these variables, so a calling program can | CODE VALUE OF NEWLINE |
| 283 | replace them if it wishes to intercept the calls. This | |
| 284 | should be done before calling any PCRE functions. | By default, PCRE treats character 10 (linefeed) as the newline charac- |
| 285 | ter. This is the normal newline character on Unix-like systems. You can | |
| 286 | compile PCRE to use character 13 (carriage return) instead by adding | |
| 287 | ||
| 288 | MULTI-THREADING | --enable-newline-is-cr |
| 289 | The PCRE functions can be used in multi-threading applica- | |
| 290 | tions, with the proviso that the memory management functions | to the configure command. For completeness there is also a --enable- |
| 291 | pointed to by pcre_malloc and pcre_free are shared by all | newline-is-lf option, which explicitly specifies linefeed as the new- |
| 292 | threads. | line character. |
| 293 | ||
| 294 | The compiled form of a regular expression is not altered | |
| 295 | during matching, so the same compiled pattern can safely be | BUILDING SHARED AND STATIC LIBRARIES |
| 296 | used by several threads at once. | |
| 297 | The PCRE building process uses libtool to build both shared and static | |
| 298 | Unix libraries by default. You can suppress one of these by adding one | |
| 299 | of | |
| 300 | ||
| 301 | --disable-shared | |
| 302 | --disable-static | |
| 303 | ||
| 304 | to the configure command, as required. | |
| 305 | ||
| 306 | ||
| 307 | POSIX MALLOC USAGE | |
| 308 | ||
| 309 | When PCRE is called through the POSIX interface (see the pcreposix doc- | |
| 310 | umentation), additional working storage is required for holding the | |
| 311 | pointers to capturing substrings, because PCRE requires three integers | |
| 312 | per substring, whereas the POSIX interface provides only two. If the | |
| 313 | number of expected substrings is small, the wrapper function uses space | |
| 314 | on the stack, because this is faster than using malloc() for each call. | |
| 315 | The default threshold above which the stack is no longer used is 10; it | |
| 316 | can be changed by adding a setting such as | |
| 317 | ||
| 318 | --with-posix-malloc-threshold=20 | |
| 319 | ||
| 320 | to the configure command. | |
| 321 | ||
| 322 | ||
| 323 | LIMITING PCRE RESOURCE USAGE | |
| 324 | ||
| 325 | Internally, PCRE has a function called match(), which it calls repeat- | |
| 326 | edly (possibly recursively) when matching a pattern with the | |
| 327 | pcre_exec() function. By controlling the maximum number of times this | |
| 328 | function may be called during a single matching operation, a limit can | |
| 329 | be placed on the resources used by a single call to pcre_exec(). The | |
| 330 | limit can be changed at run time, as described in the pcreapi documen- | |
| 331 | tation. The default is 10 million, but this can be changed by adding a | |
| 332 | setting such as | |
| 333 | ||
| 334 | --with-match-limit=500000 | |
| 335 | ||
| 336 | to the configure command. This setting has no effect on the | |
| 337 | pcre_dfa_exec() matching function. | |
| 338 | ||
| 339 | ||
| 340 | HANDLING VERY LARGE PATTERNS | |
| 341 | ||
| 342 | Within a compiled pattern, offset values are used to point from one | |
| 343 | part to another (for example, from an opening parenthesis to an alter- | |
| 344 | nation metacharacter). By default, two-byte values are used for these | |
| 345 | offsets, leading to a maximum size for a compiled pattern of around | |
| 346 | 64K. This is sufficient to handle all but the most gigantic patterns. | |
| 347 | Nevertheless, some people do want to process enormous patterns, so it | |
| 348 | is possible to compile PCRE to use three-byte or four-byte offsets by | |
| 349 | adding a setting such as | |
| 350 | ||
| 351 | --with-link-size=3 | |
| 352 | ||
| 353 | to the configure command. The value given must be 2, 3, or 4. Using | |
| 354 | longer offsets slows down the operation of PCRE because it has to load | |
| 355 | additional bytes when handling them. | |
| 356 | ||
| 357 | If you build PCRE with an increased link size, test 2 (and test 5 if | |
| 358 | you are using UTF-8) will fail. Part of the output of these tests is a | |
| 359 | representation of the compiled pattern, and this changes with the link | |
| 360 | size. | |
| 361 | ||
| 362 | ||
| 363 | AVOIDING EXCESSIVE STACK USAGE | |
| 364 | ||
| 365 | When matching with the pcre_exec() function, PCRE implements backtrack- | |
| 366 | ing by making recursive calls to an internal function called match(). | |
| 367 | In environments where the size of the stack is limited, this can se- | |
| 368 | verely limit PCRE's operation. (The Unix environment does not usually | |
| 369 | suffer from this problem.) An alternative approach that uses memory | |
| 370 | from the heap to remember data, instead of using recursive function | |
| 371 | calls, has been implemented to work round this problem. If you want to | |
| 372 | build a version of PCRE that works this way, add | |
| 373 | ||
| 374 | --disable-stack-for-recursion | |
| 375 | ||
| 376 | to the configure command. With this configuration, PCRE will use the | |
| 377 | pcre_stack_malloc and pcre_stack_free variables to call memory manage- | |
| 378 | ment functions. Separate functions are provided because the usage is | |
| 379 | very predictable: the block sizes requested are always the same, and | |
| 380 | the blocks are always freed in reverse order. A calling program might | |
| 381 | be able to implement optimized functions that perform better than the | |
| 382 | standard malloc() and free() functions. PCRE runs noticeably more | |
| 383 | slowly when built in this way. This option affects only the pcre_exec() | |
| 384 | function; it is not relevant for the the pcre_dfa_exec() function. | |
| 385 | ||
| 386 | ||
| 387 | USING EBCDIC CODE | |
| 388 | ||
| 389 | PCRE assumes by default that it will run in an environment where the | |
| 390 | character code is ASCII (or Unicode, which is a superset of ASCII). | |
| 391 | PCRE can, however, be compiled to run in an EBCDIC environment by | |
| 392 | adding | |
| 393 | ||
| 394 | --enable-ebcdic | |
| 395 | ||
| 396 | to the configure command. | |
| 397 | ||
| 398 | Last updated: 15 August 2005 | |
| 399 | Copyright (c) 1997-2005 University of Cambridge. | |
| 400 | ------------------------------------------------------------------------------ | |
| 401 | ||
| 402 | ||
| 403 | PCREMATCHING(3) PCREMATCHING(3) | |
| 404 | ||
| 405 | ||
| 406 | NAME | |
| 407 | PCRE - Perl-compatible regular expressions | |
| 408 | ||
| 409 | ||
| 410 | PCRE MATCHING ALGORITHMS | |
| 411 | ||
| 412 | This document describes the two different algorithms that are available | |
| 413 | in PCRE for matching a compiled regular expression against a given sub- | |
| 414 | ject string. The "standard" algorithm is the one provided by the | |
| 415 | pcre_exec() function. This works in the same was as Perl's matching | |
| 416 | function, and provides a Perl-compatible matching operation. | |
| 417 | ||
| 418 | An alternative algorithm is provided by the pcre_dfa_exec() function; | |
| 419 | this operates in a different way, and is not Perl-compatible. It has | |
| 420 | advantages and disadvantages compared with the standard algorithm, and | |
| 421 | these are described below. | |
| 422 | ||
| 423 | When there is only one possible way in which a given subject string can | |
| 424 | match a pattern, the two algorithms give the same answer. A difference | |
| 425 | arises, however, when there are multiple possibilities. For example, if | |
| 426 | the pattern | |
| 427 | ||
| 428 | ^<.*> | |
| 429 | ||
| 430 | is matched against the string | |
| 431 | ||
| 432 | <something> <something else> <something further> | |
| 433 | ||
| 434 | there are three possible answers. The standard algorithm finds only one | |
| 435 | of them, whereas the DFA algorithm finds all three. | |
| 436 | ||
| 437 | ||
| 438 | REGULAR EXPRESSIONS AS TREES | |
| 439 | ||
| 440 | The set of strings that are matched by a regular expression can be rep- | |
| 441 | resented as a tree structure. An unlimited repetition in the pattern | |
| 442 | makes the tree of infinite size, but it is still a tree. Matching the | |
| 443 | pattern to a given subject string (from a given starting point) can be | |
| 444 | thought of as a search of the tree. There are two standard ways to | |
| 445 | search a tree: depth-first and breadth-first, and these correspond to | |
| 446 | the two matching algorithms provided by PCRE. | |
| 447 | ||
| 448 | ||
| 449 | THE STANDARD MATCHING ALGORITHM | |
| 450 | ||
| 451 | In the terminology of Jeffrey Friedl's book Mastering Regular Expres- | |
| 452 | sions, the standard algorithm is an "NFA algorithm". It conducts a | |
| 453 | depth-first search of the pattern tree. That is, it proceeds along a | |
| 454 | single path through the tree, checking that the subject matches what is | |
| 455 | required. When there is a mismatch, the algorithm tries any alterna- | |
| 456 | tives at the current point, and if they all fail, it backs up to the | |
| 457 | previous branch point in the tree, and tries the next alternative | |
| 458 | branch at that level. This often involves backing up (moving to the | |
| 459 | left) in the subject string as well. The order in which repetition | |
| 460 | branches are tried is controlled by the greedy or ungreedy nature of | |
| 461 | the quantifier. | |
| 462 | ||
| 463 | If a leaf node is reached, a matching string has been found, and at | |
| 464 | that point the algorithm stops. Thus, if there is more than one possi- | |
| 465 | ble match, this algorithm returns the first one that it finds. Whether | |
| 466 | this is the shortest, the longest, or some intermediate length depends | |
| 467 | on the way the greedy and ungreedy repetition quantifiers are specified | |
| 468 | in the pattern. | |
| 469 | ||
| 470 | Because it ends up with a single path through the tree, it is rela- | |
| 471 | tively straightforward for this algorithm to keep track of the sub- | |
| 472 | strings that are matched by portions of the pattern in parentheses. | |
| 473 | This provides support for capturing parentheses and back references. | |
| 474 | ||
| 475 | ||
| 476 | THE DFA MATCHING ALGORITHM | |
| 477 | ||
| 478 | DFA stands for "deterministic finite automaton", but you do not need to | |
| 479 | understand the origins of that name. This algorithm conducts a breadth- | |
| 480 | first search of the tree. Starting from the first matching point in the | |
| 481 | subject, it scans the subject string from left to right, once, charac- | |
| 482 | ter by character, and as it does this, it remembers all the paths | |
| 483 | through the tree that represent valid matches. | |
| 484 | ||
| 485 | The scan continues until either the end of the subject is reached, or | |
| 486 | there are no more unterminated paths. At this point, terminated paths | |
| 487 | represent the different matching possibilities (if there are none, the | |
| 488 | match has failed). Thus, if there is more than one possible match, | |
| 489 | this algorithm finds all of them, and in particular, it finds the long- | |
| 490 | est. In PCRE, there is an option to stop the algorithm after the first | |
| 491 | match (which is necessarily the shortest) has been found. | |
| 492 | ||
| 493 | Note that all the matches that are found start at the same point in the | |
| 494 | subject. If the pattern | |
| 495 | ||
| 496 | cat(er(pillar)?) | |
| 497 | ||
| 498 | is matched against the string "the caterpillar catchment", the result | |
| 499 | will be the three strings "cat", "cater", and "caterpillar" that start | |
| 500 | at the fourth character of the subject. The algorithm does not automat- | |
| 501 | ically move on to find matches that start at later positions. | |
| 502 | ||
| 503 | There are a number of features of PCRE regular expressions that are not | |
| 504 | supported by the DFA matching algorithm. They are as follows: | |
| 505 | ||
| 506 | 1. Because the algorithm finds all possible matches, the greedy or | |
| 507 | ungreedy nature of repetition quantifiers is not relevant. Greedy and | |
| 508 | ungreedy quantifiers are treated in exactly the same way. | |
| 509 | ||
| 510 | 2. When dealing with multiple paths through the tree simultaneously, it | |
| 511 | is not straightforward to keep track of captured substrings for the | |
| 512 | different matching possibilities, and PCRE's implementation of this | |
| 513 | algorithm does not attempt to do this. This means that no captured sub- | |
| 514 | strings are available. | |
| 515 | ||
| 516 | 3. Because no substrings are captured, back references within the pat- | |
| 517 | tern are not supported, and cause errors if encountered. | |
| 518 | ||
| 519 | 4. For the same reason, conditional expressions that use a backrefer- | |
| 520 | ence as the condition are not supported. | |
| 521 | ||
| 522 | 5. Callouts are supported, but the value of the capture_top field is | |
| 523 | always 1, and the value of the capture_last field is always -1. | |
| 524 | ||
| 525 | 6. The \C escape sequence, which (in the standard algorithm) matches a | |
| 526 | single byte, even in UTF-8 mode, is not supported because the DFA algo- | |
| 527 | rithm moves through the subject string one character at a time, for all | |
| 528 | active paths through the tree. | |
| 529 | ||
| 530 | ||
| 531 | ADVANTAGES OF THE DFA ALGORITHM | |
| 532 | ||
| 533 | Using the DFA matching algorithm provides the following advantages: | |
| 534 | ||
| 535 | 1. All possible matches (at a single point in the subject) are automat- | |
| 536 | ically found, and in particular, the longest match is found. To find | |
| 537 | more than one match using the standard algorithm, you have to do kludgy | |
| 538 | things with callouts. | |
| 539 | ||
| 540 | 2. There is much better support for partial matching. The restrictions | |
| 541 | on the content of the pattern that apply when using the standard algo- | |
| 542 | rithm for partial matching do not apply to the DFA algorithm. For non- | |
| 543 | anchored patterns, the starting position of a partial match is avail- | |
| 544 | able. | |
| 545 | ||
| 546 | 3. Because the DFA algorithm scans the subject string just once, and | |
| 547 | never needs to backtrack, it is possible to pass very long subject | |
| 548 | strings to the matching function in several pieces, checking for par- | |
| 549 | tial matching each time. | |
| 550 | ||
| 551 | ||
| 552 | DISADVANTAGES OF THE DFA ALGORITHM | |
| 553 | ||
| 554 | The DFA algorithm suffers from a number of disadvantages: | |
| 555 | ||
| 556 | 1. It is substantially slower than the standard algorithm. This is | |
| 557 | partly because it has to search for all possible matches, but is also | |
| 558 | because it is less susceptible to optimization. | |
| 559 | ||
| 560 | 2. Capturing parentheses and back references are not supported. | |
| 561 | ||
| 562 | 3. The "atomic group" feature of PCRE regular expressions is supported, | |
| 563 | but does not provide the advantage that it does for the standard algo- | |
| 564 | rithm. | |
| 565 | ||
| 566 | Last updated: 28 February 2005 | |
| 567 | Copyright (c) 1997-2005 University of Cambridge. | |
| 568 | ------------------------------------------------------------------------------ | |
| 569 | ||
| 570 | ||
| 571 | PCREAPI(3) PCREAPI(3) | |
| 572 | ||
| 573 | ||
| 574 | NAME | |
| 575 | PCRE - Perl-compatible regular expressions | |
| 576 | ||
| 577 | ||
| 578 | PCRE NATIVE API | |
| 579 | ||
| 580 | #include <pcre.h> | |
| 581 | ||
| 582 | pcre *pcre_compile(const char *pattern, int options, | |
| 583 | const char **errptr, int *erroffset, | |
| 584 | const unsigned char *tableptr); | |
| 585 | ||
| 586 | pcre *pcre_compile2(const char *pattern, int options, | |
| 587 | int *errorcodeptr, | |
| 588 | const char **errptr, int *erroffset, | |
| 589 | const unsigned char *tableptr); | |
| 590 | ||
| 591 | pcre_extra *pcre_study(const pcre *code, int options, | |
| 592 | const char **errptr); | |
| 593 | ||
| 594 | int pcre_exec(const pcre *code, const pcre_extra *extra, | |
| 595 | const char *subject, int length, int startoffset, | |
| 596 | int options, int *ovector, int ovecsize); | |
| 597 | ||
| 598 | int pcre_dfa_exec(const pcre *code, const pcre_extra *extra, | |
| 599 | const char *subject, int length, int startoffset, | |
| 600 | int options, int *ovector, int ovecsize, | |
| 601 | int *workspace, int wscount); | |
| 602 | ||
| 603 | int pcre_copy_named_substring(const pcre *code, | |
| 604 | const char *subject, int *ovector, | |
| 605 | int stringcount, const char *stringname, | |
| 606 | char *buffer, int buffersize); | |
| 607 | ||
| 608 | int pcre_copy_substring(const char *subject, int *ovector, | |
| 609 | int stringcount, int stringnumber, char *buffer, | |
| 610 | int buffersize); | |
| 611 | ||
| 612 | int pcre_get_named_substring(const pcre *code, | |
| 613 | const char *subject, int *ovector, | |
| 614 | int stringcount, const char *stringname, | |
| 615 | const char **stringptr); | |
| 616 | ||
| 617 | int pcre_get_stringnumber(const pcre *code, | |
| 618 | const char *name); | |
| 619 | ||
| 620 | int pcre_get_substring(const char *subject, int *ovector, | |
| 621 | int stringcount, int stringnumber, | |
| 622 | const char **stringptr); | |
| 623 | ||
| 624 | int pcre_get_substring_list(const char *subject, | |
| 625 | int *ovector, int stringcount, const char ***listptr); | |
| 626 | ||
| 627 | void pcre_free_substring(const char *stringptr); | |
| 628 | ||
| 629 | void pcre_free_substring_list(const char **stringptr); | |
| 630 | ||
| 631 | const unsigned char *pcre_maketables(void); | |
| 632 | ||
| 633 | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, | |
| 634 | int what, void *where); | |
| 635 | ||
| 636 | int pcre_info(const pcre *code, int *optptr, int *firstcharptr); | |
| 637 | ||
| 638 | int pcre_refcount(pcre *code, int adjust); | |
| 639 | ||
| 640 | int pcre_config(int what, void *where); | |
| 641 | ||
| 642 | char *pcre_version(void); | |
| 643 | ||
| 644 | void *(*pcre_malloc)(size_t); | |
| 645 | ||
| 646 | void (*pcre_free)(void *); | |
| 647 | ||
| 648 | void *(*pcre_stack_malloc)(size_t); | |
| 649 | ||
| 650 | void (*pcre_stack_free)(void *); | |
| 651 | ||
| 652 | int (*pcre_callout)(pcre_callout_block *); | |
| 653 | ||
| 654 | ||
| 655 | PCRE API OVERVIEW | |
| 656 | ||
| 657 | PCRE has its own native API, which is described in this document. There | |
| 658 | is also a set of wrapper functions that correspond to the POSIX regular | |
| 659 | expression API. These are described in the pcreposix documentation. | |
| 660 | Both of these APIs define a set of C function calls. A C++ wrapper is | |
| 661 | distributed with PCRE. It is documented in the pcrecpp page. | |
| 662 | ||
| 663 | The native API C function prototypes are defined in the header file | |
| 664 | pcre.h, and on Unix systems the library itself is called libpcre. It | |
| 665 | can normally be accessed by adding -lpcre to the command for linking an | |
| 666 | application that uses PCRE. The header file defines the macros | |
| 667 | PCRE_MAJOR and PCRE_MINOR to contain the major and minor release num- | |
| 668 | bers for the library. Applications can use these to include support | |
| 669 | for different releases of PCRE. | |
| 670 | ||
| 671 | The functions pcre_compile(), pcre_compile2(), pcre_study(), and | |
| 672 | pcre_exec() are used for compiling and matching regular expressions in | |
| 673 | a Perl-compatible manner. A sample program that demonstrates the sim- | |
| 674 | plest way of using them is provided in the file called pcredemo.c in | |
| 675 | the source distribution. The pcresample documentation describes how to | |
| 676 | run it. | |
| 677 | ||
| 678 | A second matching function, pcre_dfa_exec(), which is not Perl-compati- | |
| 679 | ble, is also provided. This uses a different algorithm for the match- | |
| 680 | ing. This allows it to find all possible matches (at a given point in | |
| 681 | the subject), not just one. However, this algorithm does not return | |
| 682 | captured substrings. A description of the two matching algorithms and | |
| 683 | their advantages and disadvantages is given in the pcrematching docu- | |
| 684 | mentation. | |
| 685 | ||
| 686 | In addition to the main compiling and matching functions, there are | |
| 687 | convenience functions for extracting captured substrings from a subject | |
| 688 | string that is matched by pcre_exec(). They are: | |
| 689 | ||
| 690 | pcre_copy_substring() | |
| 691 | pcre_copy_named_substring() | |
| 692 | pcre_get_substring() | |
| 693 | pcre_get_named_substring() | |
| 694 | pcre_get_substring_list() | |
| 695 | pcre_get_stringnumber() | |
| 696 | ||
| 697 | pcre_free_substring() and pcre_free_substring_list() are also provided, | |
| 698 | to free the memory used for extracted strings. | |
| 699 | ||
| 700 | The function pcre_maketables() is used to build a set of character | |
| 701 | tables in the current locale for passing to pcre_compile(), | |
| 702 | pcre_exec(), or pcre_dfa_exec(). This is an optional facility that is | |
| 703 | provided for specialist use. Most commonly, no special tables are | |
| 704 | passed, in which case internal tables that are generated when PCRE is | |
| 705 | built are used. | |
| 706 | ||
| 707 | The function pcre_fullinfo() is used to find out information about a | |
| 708 | compiled pattern; pcre_info() is an obsolete version that returns only | |
| 709 | some of the available information, but is retained for backwards com- | |
| 710 | patibility. The function pcre_version() returns a pointer to a string | |
| 711 | containing the version of PCRE and its date of release. | |
| 712 | ||
| 713 | The function pcre_refcount() maintains a reference count in a data | |
| 714 | block containing a compiled pattern. This is provided for the benefit | |
| 715 | of object-oriented applications. | |
| 716 | ||
| 717 | The global variables pcre_malloc and pcre_free initially contain the | |
| 718 | entry points of the standard malloc() and free() functions, respec- | |
| 719 | tively. PCRE calls the memory management functions via these variables, | |
| 720 | so a calling program can replace them if it wishes to intercept the | |
| 721 | calls. This should be done before calling any PCRE functions. | |
| 722 | ||
| 723 | The global variables pcre_stack_malloc and pcre_stack_free are also | |
| 724 | indirections to memory management functions. These special functions | |
| 725 | are used only when PCRE is compiled to use the heap for remembering | |
| 726 | data, instead of recursive function calls, when running the pcre_exec() | |
| 727 | function. This is a non-standard way of building PCRE, for use in envi- | |
| 728 | ronments that have limited stacks. Because of the greater use of memory | |
| 729 | management, it runs more slowly. Separate functions are provided so | |
| 730 | that special-purpose external code can be used for this case. When | |
| 731 | used, these functions are always called in a stack-like manner (last | |
| 732 | obtained, first freed), and always for memory blocks of the same size. | |
| 733 | ||
| 734 | The global variable pcre_callout initially contains NULL. It can be set | |
| 735 | by the caller to a "callout" function, which PCRE will then call at | |
| 736 | specified points during a matching operation. Details are given in the | |
| 737 | pcrecallout documentation. | |
| 738 | ||
| 739 | ||
| 740 | MULTITHREADING | |
| 741 | ||
| 742 | The PCRE functions can be used in multi-threading applications, with | |
| 743 | the proviso that the memory management functions pointed to by | |
| 744 | pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the | |
| 745 | callout function pointed to by pcre_callout, are shared by all threads. | |
| 746 | ||
| 747 | The compiled form of a regular expression is not altered during match- | |
| 748 | ing, so the same compiled pattern can safely be used by several threads | |
| 749 | at once. | |
| 750 | ||
| 751 | ||
| 752 | SAVING PRECOMPILED PATTERNS FOR LATER USE | |
| 753 | ||
| 754 | The compiled form of a regular expression can be saved and re-used at a | |
| 755 | later time, possibly by a different program, and even on a host other | |
| 756 | than the one on which it was compiled. Details are given in the | |
| 757 | pcreprecompile documentation. | |
| 758 | ||
| 759 | ||
| 760 | CHECKING BUILD-TIME OPTIONS | |
| 761 | ||
| 762 | int pcre_config(int what, void *where); | |
| 763 | ||
| 764 | The function pcre_config() makes it possible for a PCRE client to dis- | |
| 765 | cover which optional features have been compiled into the PCRE library. | |
| 766 | The pcrebuild documentation has more details about these optional fea- | |
| 767 | tures. | |
| 768 | ||
| 769 | The first argument for pcre_config() is an integer, specifying which | |
| 770 | information is required; the second argument is a pointer to a variable | |
| 771 | into which the information is placed. The following information is | |
| 772 | available: | |
| 773 | ||
| 774 | PCRE_CONFIG_UTF8 | |
| 775 | ||
| 776 | The output is an integer that is set to one if UTF-8 support is avail- | |
| 777 | able; otherwise it is set to zero. | |
| 778 | ||
| 779 | PCRE_CONFIG_UNICODE_PROPERTIES | |
| 780 | ||
| 781 | The output is an integer that is set to one if support for Unicode | |
| 782 | character properties is available; otherwise it is set to zero. | |
| 783 | ||
| 784 | PCRE_CONFIG_NEWLINE | |
| 785 | ||
| 786 | The output is an integer that is set to the value of the code that is | |
| 787 | used for the newline character. It is either linefeed (10) or carriage | |
| 788 | return (13), and should normally be the standard character for your | |
| 789 | operating system. | |
| 790 | ||
| 791 | PCRE_CONFIG_LINK_SIZE | |
| 792 | ||
| 793 | The output is an integer that contains the number of bytes used for | |
| 794 | internal linkage in compiled regular expressions. The value is 2, 3, or | |
| 795 | 4. Larger values allow larger regular expressions to be compiled, at | |
| 796 | the expense of slower matching. The default value of 2 is sufficient | |
| 797 | for all but the most massive patterns, since it allows the compiled | |
| 798 | pattern to be up to 64K in size. | |
| 799 | ||
| 800 | PCRE_CONFIG_POSIX_MALLOC_THRESHOLD | |
| 801 | ||
| 802 | The output is an integer that contains the threshold above which the | |
| 803 | POSIX interface uses malloc() for output vectors. Further details are | |
| 804 | given in the pcreposix documentation. | |
| 805 | ||
| 806 | PCRE_CONFIG_MATCH_LIMIT | |
| 807 | ||
| 808 | The output is an integer that gives the default limit for the number of | |
| 809 | internal matching function calls in a pcre_exec() execution. Further | |
| 810 | details are given with pcre_exec() below. | |
| 811 | ||
| 812 | PCRE_CONFIG_MATCH_LIMIT_RECURSION | |
| 813 | ||
| 814 | The output is an integer that gives the default limit for the depth of | |
| 815 | recursion when calling the internal matching function in a pcre_exec() | |
| 816 | execution. Further details are given with pcre_exec() below. | |
| 817 | ||
| 818 | PCRE_CONFIG_STACKRECURSE | |
| 819 | ||
| 820 | The output is an integer that is set to one if internal recursion when | |
| 821 | running pcre_exec() is implemented by recursive function calls that use | |
| 822 | the stack to remember their state. This is the usual way that PCRE is | |
| 823 | compiled. The output is zero if PCRE was compiled to use blocks of data | |
| 824 | on the heap instead of recursive function calls. In this case, | |
| 825 | pcre_stack_malloc and pcre_stack_free are called to manage memory | |
| 826 | blocks on the heap, thus avoiding the use of the stack. | |
| 827 | ||
| 828 | ||
| 829 | COMPILING A PATTERN | COMPILING A PATTERN |
| The function pcre_compile() is called to compile a pattern | ||
| into an internal form. The pattern is a C string terminated | ||
| by a binary zero, and is passed in the argument pattern. A | ||
| pointer to a single block of memory that is obtained via | ||
| pcre_malloc is returned. This contains the compiled code and | ||
| related data. The pcre type is defined for the returned | ||
| block; this is a typedef for a structure whose contents are | ||
| not externally defined. It is up to the caller to free the | ||
| memory when it is no longer required. | ||
| Although the compiled code of a PCRE regex is relocatable, | ||
| that is, it does not depend on memory location, the complete | ||
| pcre data block is not fully relocatable, because it con- | ||
| tains a copy of the tableptr argument, which is an address | ||
| (see below). | ||
| The size of a compiled pattern is roughly proportional to | ||
| the length of the pattern string, except that each character | ||
| class (other than those containing just a single character, | ||
| negated or not) requires 33 bytes, and repeat quantifiers | ||
| with a minimum greater than one or a bounded maximum cause | ||
| the relevant portions of the compiled pattern to be repli- | ||
| cated. | ||
| The options argument contains independent bits that affect | ||
| the compilation. It should be zero if no options are | ||
| required. Some of the options, in particular, those that are | ||
| compatible with Perl, can also be set and unset from within | ||
| the pattern (see the detailed description of regular expres- | ||
| sions below). For these options, the contents of the options | ||
| argument specifies their initial settings at the start of | ||
| compilation and execution. The PCRE_ANCHORED option can be | ||
| set at the time of matching as well as at compile time. | ||
| If errptr is NULL, pcre_compile() returns NULL immediately. | ||
| Otherwise, if compilation of a pattern fails, pcre_compile() | ||
| returns NULL, and sets the variable pointed to by errptr to | ||
| point to a textual error message. The offset from the start | ||
| of the pattern to the character where the error was | ||
| discovered is placed in the variable pointed to by | ||
| erroffset, which must not be NULL. If it is, an immediate | ||
| error is given. | ||
| If the final argument, tableptr, is NULL, PCRE uses a | ||
| default set of character tables which are built when it is | ||
| compiled, using the default C locale. Otherwise, tableptr | ||
| must be the result of a call to pcre_maketables(). See the | ||
| section on locale support below. | ||
| This code fragment shows a typical straightforward call to | ||
| pcre_compile(): | ||
| pcre *re; | ||
| const char *error; | ||
| int erroffset; | ||
| re = pcre_compile( | ||
| "^A.*Z", /* the pattern */ | ||
| 0, /* default options */ | ||
| &error, /* for error message */ | ||
| &erroffset, /* for error offset */ | ||
| NULL); /* use default character tables */ | ||
| The following option bits are defined in the header file: | ||
| PCRE_ANCHORED | ||
| If this bit is set, the pattern is forced to be "anchored", | ||
| that is, it is constrained to match only at the start of the | ||
| string which is being searched (the "subject string"). This | ||
| effect can also be achieved by appropriate constructs in the | ||
| pattern itself, which is the only way to do it in Perl. | ||
| PCRE_CASELESS | ||
| If this bit is set, letters in the pattern match both upper | ||
| and lower case letters. It is equivalent to Perl's /i | ||
| option. | ||
| PCRE_DOLLAR_ENDONLY | ||
| If this bit is set, a dollar metacharacter in the pattern | ||
| matches only at the end of the subject string. Without this | ||
| option, a dollar also matches immediately before the final | ||
| character if it is a newline (but not before any other new- | ||
| lines). The PCRE_DOLLAR_ENDONLY option is ignored if | ||
| PCRE_MULTILINE is set. There is no equivalent to this option | ||
| in Perl. | ||
| PCRE_DOTALL | ||
| If this bit is set, a dot metacharater in the pattern | ||
| matches all characters, including newlines. Without it, new- | ||
| lines are excluded. This option is equivalent to Perl's /s | ||
| option. A negative class such as [^a] always matches a new- | ||
| line character, independent of the setting of this option. | ||
| PCRE_EXTENDED | ||
| If this bit is set, whitespace data characters in the pat- | ||
| tern are totally ignored except when escaped or inside a | ||
| character class, and characters between an unescaped # out- | ||
| side a character class and the next newline character, | ||
| inclusive, are also ignored. This is equivalent to Perl's /x | ||
| option, and makes it possible to include comments inside | ||
| complicated patterns. Note, however, that this applies only | ||
| to data characters. Whitespace characters may never appear | ||
| within special character sequences in a pattern, for example | ||
| within the sequence (?( which introduces a conditional sub- | ||
| pattern. | ||
| PCRE_EXTRA | ||
| This option was invented in order to turn on additional | ||
| functionality of PCRE that is incompatible with Perl, but it | ||
| is currently of very little use. When set, any backslash in | ||
| a pattern that is followed by a letter that has no special | ||
| meaning causes an error, thus reserving these combinations | ||
| for future expansion. By default, as in Perl, a backslash | ||
| followed by a letter with no special meaning is treated as a | ||
| literal. There are at present no other features controlled | ||
| by this option. It can also be set by a (?X) option setting | ||
| within a pattern. | ||
| PCRE_MULTILINE | ||
| By default, PCRE treats the subject string as consisting of | ||
| a single "line" of characters (even if it actually contains | ||
| several newlines). The "start of line" metacharacter (^) | ||
| matches only at the start of the string, while the "end of | ||
| line" metacharacter ($) matches only at the end of the | ||
| string, or before a terminating newline (unless | ||
| PCRE_DOLLAR_ENDONLY is set). This is the same as Perl. | ||
| When PCRE_MULTILINE it is set, the "start of line" and "end | ||
| of line" constructs match immediately following or immedi- | ||
| ately before any newline in the subject string, respec- | ||
| tively, as well as at the very start and end. This is | ||
| equivalent to Perl's /m option. If there are no "\n" charac- | ||
| ters in a subject string, or no occurrences of ^ or $ in a | ||
| pattern, setting PCRE_MULTILINE has no effect. | ||
| PCRE_UNGREEDY | ||
| This option inverts the "greediness" of the quantifiers so | ||
| that they are not greedy by default, but become greedy if | ||
| followed by "?". It is not compatible with Perl. It can also | ||
| be set by a (?U) option setting within the pattern. | ||
| PCRE_UTF8 | ||
| This option causes PCRE to regard both the pattern and the | ||
| subject as strings of UTF-8 characters instead of just byte | ||
| strings. However, it is available only if PCRE has been | ||
| built to include UTF-8 support. If not, the use of this | ||
| option provokes an error. Support for UTF-8 is new, experi- | ||
| mental, and incomplete. Details of exactly what it entails | ||
| are given below. | ||
| 830 | ||
| 831 | pcre *pcre_compile(const char *pattern, int options, | |
| 832 | const char **errptr, int *erroffset, | |
| 833 | const unsigned char *tableptr); | |
| 834 | ||
| 835 | pcre *pcre_compile2(const char *pattern, int options, | |
| 836 | int *errorcodeptr, | |
| 837 | const char **errptr, int *erroffset, | |
| 838 | const unsigned char *tableptr); | |
| 839 | ||
| 840 | Either of the functions pcre_compile() or pcre_compile2() can be called | |
| 841 | to compile a pattern into an internal form. The only difference between | |
| 842 | the two interfaces is that pcre_compile2() has an additional argument, | |
| 843 | errorcodeptr, via which a numerical error code can be returned. | |
| 844 | ||
| 845 | The pattern is a C string terminated by a binary zero, and is passed in | |
| 846 | the pattern argument. A pointer to a single block of memory that is | |
| 847 | obtained via pcre_malloc is returned. This contains the compiled code | |
| 848 | and related data. The pcre type is defined for the returned block; this | |
| 849 | is a typedef for a structure whose contents are not externally defined. | |
| 850 | It is up to the caller to free the memory when it is no longer | |
| 851 | required. | |
| 852 | ||
| 853 | Although the compiled code of a PCRE regex is relocatable, that is, it | |
| 854 | does not depend on memory location, the complete pcre data block is not | |
| 855 | fully relocatable, because it may contain a copy of the tableptr argu- | |
| 856 | ment, which is an address (see below). | |
| 857 | ||
| 858 | The options argument contains independent bits that affect the compila- | |
| 859 | tion. It should be zero if no options are required. The available | |
| 860 | options are described below. Some of them, in particular, those that | |
| 861 | are compatible with Perl, can also be set and unset from within the | |
| 862 | pattern (see the detailed description in the pcrepattern documenta- | |
| 863 | tion). For these options, the contents of the options argument speci- | |
| 864 | fies their initial settings at the start of compilation and execution. | |
| 865 | The PCRE_ANCHORED option can be set at the time of matching as well as | |
| 866 | at compile time. | |
| 867 | ||
| 868 | If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise, | |
| 869 | if compilation of a pattern fails, pcre_compile() returns NULL, and | |
| 870 | sets the variable pointed to by errptr to point to a textual error mes- | |
| 871 | sage. This is a static string that is part of the library. You must not | |
| 872 | try to free it. The offset from the start of the pattern to the charac- | |
| 873 | ter where the error was discovered is placed in the variable pointed to | |
| 874 | by erroffset, which must not be NULL. If it is, an immediate error is | |
| 875 | given. | |
| 876 | ||
| 877 | If pcre_compile2() is used instead of pcre_compile(), and the error- | |
| 878 | codeptr argument is not NULL, a non-zero error code number is returned | |
| 879 | via this argument in the event of an error. This is in addition to the | |
| 880 | textual error message. Error codes and messages are listed below. | |
| 881 | ||
| 882 | If the final argument, tableptr, is NULL, PCRE uses a default set of | |
| 883 | character tables that are built when PCRE is compiled, using the | |
| 884 | default C locale. Otherwise, tableptr must be an address that is the | |
| 885 | result of a call to pcre_maketables(). This value is stored with the | |
| 886 | compiled pattern, and used again by pcre_exec(), unless another table | |
| 887 | pointer is passed to it. For more discussion, see the section on locale | |
| 888 | support below. | |
| 889 | ||
| 890 | This code fragment shows a typical straightforward call to pcre_com- | |
| 891 | pile(): | |
| 892 | ||
| 893 | pcre *re; | |
| 894 | const char *error; | |
| 895 | int erroffset; | |
| 896 | re = pcre_compile( | |
| 897 | "^A.*Z", /* the pattern */ | |
| 898 | 0, /* default options */ | |
| 899 | &error, /* for error message */ | |
| 900 | &erroffset, /* for error offset */ | |
| 901 | NULL); /* use default character tables */ | |
| 902 | ||
| 903 | The following names for option bits are defined in the pcre.h header | |
| 904 | file: | |
| 905 | ||
| 906 | PCRE_ANCHORED | |
| 907 | ||
| 908 | If this bit is set, the pattern is forced to be "anchored", that is, it | |
| 909 | is constrained to match only at the first matching point in the string | |
| 910 | that is being searched (the "subject string"). This effect can also be | |
| 911 | achieved by appropriate constructs in the pattern itself, which is the | |
| 912 | only way to do it in Perl. | |
| 913 | ||
| 914 | PCRE_AUTO_CALLOUT | |
| 915 | ||
| 916 | If this bit is set, pcre_compile() automatically inserts callout items, | |
| 917 | all with number 255, before each pattern item. For discussion of the | |
| 918 | callout facility, see the pcrecallout documentation. | |
| 919 | ||
| 920 | PCRE_CASELESS | |
| 921 | ||
| 922 | If this bit is set, letters in the pattern match both upper and lower | |
| 923 | case letters. It is equivalent to Perl's /i option, and it can be | |
| 924 | changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE | |
| 925 | always understands the concept of case for characters whose values are | |
| 926 | less than 128, so caseless matching is always possible. For characters | |
| 927 | with higher values, the concept of case is supported if PCRE is com- | |
| 928 | piled with Unicode property support, but not otherwise. If you want to | |
| 929 | use caseless matching for characters 128 and above, you must ensure | |
| 930 | that PCRE is compiled with Unicode property support as well as with | |
| 931 | UTF-8 support. | |
| 932 | ||
| 933 | PCRE_DOLLAR_ENDONLY | |
| 934 | ||
| 935 | If this bit is set, a dollar metacharacter in the pattern matches only | |
| 936 | at the end of the subject string. Without this option, a dollar also | |
| 937 | matches immediately before the final character if it is a newline (but | |
| 938 | not before any other newlines). The PCRE_DOLLAR_ENDONLY option is | |
| 939 | ignored if PCRE_MULTILINE is set. There is no equivalent to this option | |
| 940 | in Perl, and no way to set it within a pattern. | |
| 941 | ||
| 942 | PCRE_DOTALL | |
| 943 | ||
| 944 | If this bit is set, a dot metacharater in the pattern matches all char- | |
| 945 | acters, including newlines. Without it, newlines are excluded. This | |
| 946 | option is equivalent to Perl's /s option, and it can be changed within | |
| 947 | a pattern by a (?s) option setting. A negative class such as [^a] | |
| 948 | always matches a newline character, independent of the setting of this | |
| 949 | option. | |
| 950 | ||
| 951 | PCRE_EXTENDED | |
| 952 | ||
| 953 | If this bit is set, whitespace data characters in the pattern are | |
| 954 | totally ignored except when escaped or inside a character class. White- | |
| 955 | space does not include the VT character (code 11). In addition, charac- | |
| 956 | ters between an unescaped # outside a character class and the next new- | |
| 957 | line character, inclusive, are also ignored. This is equivalent to | |
| 958 | Perl's /x option, and it can be changed within a pattern by a (?x) | |
| 959 | option setting. | |
| 960 | ||
| 961 | This option makes it possible to include comments inside complicated | |
| 962 | patterns. Note, however, that this applies only to data characters. | |
| 963 | Whitespace characters may never appear within special character | |
| 964 | sequences in a pattern, for example within the sequence (?( which | |
| 965 | introduces a conditional subpattern. | |
| 966 | ||
| 967 | PCRE_EXTRA | |
| 968 | ||
| 969 | This option was invented in order to turn on additional functionality | |
| 970 | of PCRE that is incompatible with Perl, but it is currently of very | |
| 971 | little use. When set, any backslash in a pattern that is followed by a | |
| 972 | letter that has no special meaning causes an error, thus reserving | |
| 973 | these combinations for future expansion. By default, as in Perl, a | |
| 974 | backslash followed by a letter with no special meaning is treated as a | |
| 975 | literal. There are at present no other features controlled by this | |
| 976 | option. It can also be set by a (?X) option setting within a pattern. | |
| 977 | ||
| 978 | PCRE_FIRSTLINE | |
| 979 | ||
| 980 | If this option is set, an unanchored pattern is required to match | |
| 981 | before or at the first newline character in the subject string, though | |
| 982 | the matched text may continue over the newline. | |
| 983 | ||
| 984 | PCRE_MULTILINE | |
| 985 | ||
| 986 | By default, PCRE treats the subject string as consisting of a single | |
| 987 | line of characters (even if it actually contains newlines). The "start | |
| 988 | of line" metacharacter (^) matches only at the start of the string, | |
| 989 | while the "end of line" metacharacter ($) matches only at the end of | |
| 990 | the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY | |
| 991 | is set). This is the same as Perl. | |
| 992 | ||
| 993 | When PCRE_MULTILINE it is set, the "start of line" and "end of line" | |
| 994 | constructs match immediately following or immediately before any new- | |
| 995 | line in the subject string, respectively, as well as at the very start | |
| 996 | and end. This is equivalent to Perl's /m option, and it can be changed | |
| 997 | within a pattern by a (?m) option setting. If there are no "\n" charac- | |
| 998 | ters in a subject string, or no occurrences of ^ or $ in a pattern, | |
| 999 | setting PCRE_MULTILINE has no effect. | |
| 1000 | ||
| 1001 | PCRE_NO_AUTO_CAPTURE | |
| 1002 | ||
| 1003 | If this option is set, it disables the use of numbered capturing paren- | |
| 1004 | theses in the pattern. Any opening parenthesis that is not followed by | |
| 1005 | ? behaves as if it were followed by ?: but named parentheses can still | |
| 1006 | be used for capturing (and they acquire numbers in the usual way). | |
| 1007 | There is no equivalent of this option in Perl. | |
| 1008 | ||
| 1009 | PCRE_UNGREEDY | |
| 1010 | ||
| 1011 | This option inverts the "greediness" of the quantifiers so that they | |
| 1012 | are not greedy by default, but become greedy if followed by "?". It is | |
| 1013 | not compatible with Perl. It can also be set by a (?U) option setting | |
| 1014 | within the pattern. | |
| 1015 | ||
| 1016 | PCRE_UTF8 | |
| 1017 | ||
| 1018 | This option causes PCRE to regard both the pattern and the subject as | |
| 1019 | strings of UTF-8 characters instead of single-byte character strings. | |
| 1020 | However, it is available only when PCRE is built to include UTF-8 sup- | |
| 1021 | port. If not, the use of this option provokes an error. Details of how | |
| 1022 | this option changes the behaviour of PCRE are given in the section on | |
| 1023 | UTF-8 support in the main pcre page. | |
| 1024 | ||
| 1025 | PCRE_NO_UTF8_CHECK | |
| 1026 | ||
| 1027 | When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is | |
| 1028 | automatically checked. If an invalid UTF-8 sequence of bytes is found, | |
| 1029 | pcre_compile() returns an error. If you already know that your pattern | |
| 1030 | is valid, and you want to skip this check for performance reasons, you | |
| 1031 | can set the PCRE_NO_UTF8_CHECK option. When it is set, the effect of | |
| 1032 | passing an invalid UTF-8 string as a pattern is undefined. It may cause | |
| 1033 | your program to crash. Note that this option can also be passed to | |
| 1034 | pcre_exec() and pcre_dfa_exec(), to suppress the UTF-8 validity check- | |
| 1035 | ing of subject strings. | |
| 1036 | ||
| 1037 | ||
| 1038 | COMPILATION ERROR CODES | |
| 1039 | ||
| 1040 | The following table lists the error codes than may be returned by | |
| 1041 | pcre_compile2(), along with the error messages that may be returned by | |
| 1042 | both compiling functions. | |
| 1043 | ||
| 1044 | 0 no error | |
| 1045 | 1 \ at end of pattern | |
| 1046 | 2 \c at end of pattern | |
| 1047 | 3 unrecognized character follows \ | |
| 1048 | 4 numbers out of order in {} quantifier | |
| 1049 | 5 number too big in {} quantifier | |
| 1050 | 6 missing terminating ] for character class | |
| 1051 | 7 invalid escape sequence in character class | |
| 1052 | 8 range out of order in character class | |
| 1053 | 9 nothing to repeat | |
| 1054 | 10 operand of unlimited repeat could match the empty string | |
| 1055 | 11 internal error: unexpected repeat | |
| 1056 | 12 unrecognized character after (? | |
| 1057 | 13 POSIX named classes are supported only within a class | |
| 1058 | 14 missing ) | |
| 1059 | 15 reference to non-existent subpattern | |
| 1060 | 16 erroffset passed as NULL | |
| 1061 | 17 unknown option bit(s) set | |
| 1062 | 18 missing ) after comment | |
| 1063 | 19 parentheses nested too deeply | |
| 1064 | 20 regular expression too large | |
| 1065 | 21 failed to get memory | |
| 1066 | 22 unmatched parentheses | |
| 1067 | 23 internal error: code overflow | |
| 1068 | 24 unrecognized character after (?< | |
| 1069 | 25 lookbehind assertion is not fixed length | |
| 1070 | 26 malformed number after (?( | |
| 1071 | 27 conditional group contains more than two branches | |
| 1072 | 28 assertion expected after (?( | |
| 1073 | 29 (?R or (?digits must be followed by ) | |
| 1074 | 30 unknown POSIX class name | |
| 1075 | 31 POSIX collating elements are not supported | |
| 1076 | 32 this version of PCRE is not compiled with PCRE_UTF8 support | |
| 1077 | 33 spare error | |
| 1078 | 34 character value in \x{...} sequence is too large | |
| 1079 | 35 invalid condition (?(0) | |
| 1080 | 36 \C not allowed in lookbehind assertion | |
| 1081 | 37 PCRE does not support \L, \l, \N, \U, or \u | |
| 1082 | 38 number after (?C is > 255 | |
| 1083 | 39 closing ) for (?C expected | |
| 1084 | 40 recursive call could loop indefinitely | |
| 1085 | 41 unrecognized character after (?P | |
| 1086 | 42 syntax error after (?P | |
| 1087 | 43 two named groups have the same name | |
| 1088 | 44 invalid UTF-8 string | |
| 1089 | 45 support for \P, \p, and \X has not been compiled | |
| 1090 | 46 malformed \P or \p sequence | |
| 1091 | 47 unknown property name after \P or \p | |
| 1092 | ||
| 1093 | ||
| 1094 | STUDYING A PATTERN | STUDYING A PATTERN |
| When a pattern is going to be used several times, it is | ||
| worth spending more time analyzing it in order to speed up | ||
| the time taken for matching. The function pcre_study() takes | ||
| a pointer to a compiled pattern as its first argument, and | ||
| returns a pointer to a pcre_extra block (another typedef for | ||
| a structure with hidden contents) containing additional | ||
| information about the pattern; this can be passed to | ||
| pcre_exec(). If no additional information is available, NULL | ||
| is returned. | ||
| The second argument contains option bits. At present, no | ||
| options are defined for pcre_study(), and this argument | ||
| should always be zero. | ||
| The third argument for pcre_study() is a pointer to an error | ||
| message. If studying succeeds (even if no data is returned), | ||
| the variable it points to is set to NULL. Otherwise it | ||
| points to a textual error message. | ||
| This is a typical call to pcre_study(): | ||
| pcre_extra *pe; | ||
| pe = pcre_study( | ||
| re, /* result of pcre_compile() */ | ||
| 0, /* no options exist */ | ||
| &error); /* set to NULL or points to a message */ | ||
| At present, studying a pattern is useful only for non- | ||
| anchored patterns that do not have a single fixed starting | ||
| character. A bitmap of possible starting characters is | ||
| created. | ||
| 1095 | ||
| 1096 | pcre_extra *pcre_study(const pcre *code, int options | |
| 1097 | const char **errptr); | |
| 1098 | ||
| 1099 | If a compiled pattern is going to be used several times, it is worth | |
| 1100 | spending more time analyzing it in order to speed up the time taken for | |
| 1101 | matching. The function pcre_study() takes a pointer to a compiled pat- | |
| 1102 | tern as its first argument. If studying the pattern produces additional | |
| 1103 | information that will help speed up matching, pcre_study() returns a | |
| 1104 | pointer to a pcre_extra block, in which the study_data field points to | |
| 1105 | the results of the study. | |
| 1106 | ||
| 1107 | The returned value from pcre_study() can be passed directly to | |
| 1108 | pcre_exec(). However, a pcre_extra block also contains other fields | |
| 1109 | that can be set by the caller before the block is passed; these are | |
| 1110 | described below in the section on matching a pattern. | |
| 1111 | ||
| 1112 | If studying the pattern does not produce any additional information | |
| 1113 | pcre_study() returns NULL. In that circumstance, if the calling program | |
| 1114 | wants to pass any of the other fields to pcre_exec(), it must set up | |
| 1115 | its own pcre_extra block. | |
| 1116 | ||
| 1117 | The second argument of pcre_study() contains option bits. At present, | |
| 1118 | no options are defined, and this argument should always be zero. | |
| 1119 | ||
| 1120 | The third argument for pcre_study() is a pointer for an error message. | |
| 1121 | If studying succeeds (even if no data is returned), the variable it | |
| 1122 | points to is set to NULL. Otherwise it is set to point to a textual | |
| 1123 | error message. This is a static string that is part of the library. You | |
| 1124 | must not try to free it. You should test the error pointer for NULL | |
| 1125 | after calling pcre_study(), to be sure that it has run successfully. | |
| 1126 | ||
| 1127 | This is a typical call to pcre_study(): | |
| 1128 | ||
| 1129 | pcre_extra *pe; | |
| 1130 | pe = pcre_study( | |
| 1131 | re, /* result of pcre_compile() */ | |
| 1132 | 0, /* no options exist */ | |
| 1133 | &error); /* set to NULL or points to a message */ | |
| 1134 | ||
| 1135 | At present, studying a pattern is useful only for non-anchored patterns | |
| 1136 | that do not have a single fixed starting character. A bitmap of possi- | |
| 1137 | ble starting bytes is created. | |
| 1138 | ||
| 1139 | ||
| 1140 | LOCALE SUPPORT | LOCALE SUPPORT |
| PCRE handles caseless matching, and determines whether char- | ||
| acters are letters, digits, or whatever, by reference to a | ||
| set of tables. The library contains a default set of tables | ||
| which is created in the default C locale when PCRE is com- | ||
| piled. This is used when the final argument of | ||
| pcre_compile() is NULL, and is sufficient for many applica- | ||
| tions. | ||
| An alternative set of tables can, however, be supplied. Such | ||
| tables are built by calling the pcre_maketables() function, | ||
| which has no arguments, in the relevant locale. The result | ||
| can then be passed to pcre_compile() as often as necessary. | ||
| For example, to build and use tables that are appropriate | ||
| for the French locale (where accented characters with codes | ||
| greater than 128 are treated as letters), the following code | ||
| could be used: | ||
| setlocale(LC_CTYPE, "fr"); | ||
| tables = pcre_maketables(); | ||
| re = pcre_compile(..., tables); | ||
| The tables are built in memory that is obtained via | ||
| pcre_malloc. The pointer that is passed to pcre_compile is | ||
| saved with the compiled pattern, and the same tables are | ||
| used via this pointer by pcre_study() and pcre_exec(). Thus | ||
| for any single pattern, compilation, studying and matching | ||
| all happen in the same locale, but different patterns can be | ||
| compiled in different locales. It is the caller's responsi- | ||
| bility to ensure that the memory containing the tables | ||
| remains available for as long as it is needed. | ||
| 1141 | ||
| 1142 | PCRE handles caseless matching, and determines whether characters are | |
| 1143 | letters digits, or whatever, by reference to a set of tables, indexed | |
| 1144 | by character value. When running in UTF-8 mode, this applies only to | |
| 1145 | characters with codes less than 128. Higher-valued codes never match | |
| 1146 | escapes such as \w or \d, but can be tested with \p if PCRE is built | |
| 1147 | with Unicode character property support. The use of locales with Uni- | |
| 1148 | code is discouraged. | |
| 1149 | ||
| 1150 | An internal set of tables is created in the default C locale when PCRE | |
| 1151 | is built. This is used when the final argument of pcre_compile() is | |
| 1152 | NULL, and is sufficient for many applications. An alternative set of | |
| 1153 | tables can, however, be supplied. These may be created in a different | |
| 1154 | locale from the default. As more and more applications change to using | |
| 1155 | Unicode, the need for this locale support is expected to die away. | |
| 1156 | ||
| 1157 | External tables are built by calling the pcre_maketables() function, | |
| 1158 | which has no arguments, in the relevant locale. The result can then be | |
| 1159 | passed to pcre_compile() or pcre_exec() as often as necessary. For | |
| 1160 | example, to build and use tables that are appropriate for the French | |
| 1161 | locale (where accented characters with values greater than 128 are | |
| 1162 | treated as letters), the following code could be used: | |
| 1163 | ||
| 1164 | setlocale(LC_CTYPE, "fr_FR"); | |
| 1165 | tables = pcre_maketables(); | |
| 1166 | re = pcre_compile(..., tables); | |
| 1167 | ||
| 1168 | When pcre_maketables() runs, the tables are built in memory that is | |
| 1169 | obtained via pcre_malloc. It is the caller's responsibility to ensure | |
| 1170 | that the memory containing the tables remains available for as long as | |
| 1171 | it is needed. | |
| 1172 | ||
| 1173 | The pointer that is passed to pcre_compile() is saved with the compiled | |
| 1174 | pattern, and the same tables are used via this pointer by pcre_study() | |
| 1175 | and normally also by pcre_exec(). Thus, by default, for any single pat- | |
| 1176 | tern, compilation, studying and matching all happen in the same locale, | |
| 1177 | but different patterns can be compiled in different locales. | |
| 1178 | ||
| 1179 | It is possible to pass a table pointer or NULL (indicating the use of | |
| 1180 | the internal tables) to pcre_exec(). Although not intended for this | |
| 1181 | purpose, this facility could be used to match a pattern in a different | |
| 1182 | locale from the one in which it was compiled. Passing table pointers at | |
| 1183 | run time is discussed below in the section on matching a pattern. | |
| 1184 | ||
| 1185 | ||
| 1186 | INFORMATION ABOUT A PATTERN | INFORMATION ABOUT A PATTERN |
| The pcre_fullinfo() function returns information about a | ||
| compiled pattern. It replaces the obsolete pcre_info() func- | ||
| tion, which is nevertheless retained for backwards compabil- | ||
| ity (and is documented below). | ||
| The first argument for pcre_fullinfo() is a pointer to the | ||
| compiled pattern. The second argument is the result of | ||
| pcre_study(), or NULL if the pattern was not studied. The | ||
| third argument specifies which piece of information is | ||
| required, while the fourth argument is a pointer to a vari- | ||
| able to receive the data. The yield of the function is zero | ||
| for success, or one of the following negative numbers: | ||
| PCRE_ERROR_NULL the argument code was NULL | ||
| the argument where was NULL | ||
| PCRE_ERROR_BADMAGIC the "magic number" was not found | ||
| PCRE_ERROR_BADOPTION the value of what was invalid | ||
| Here is a typical call of pcre_fullinfo(), to obtain the | ||
| length of the compiled pattern: | ||
| int rc; | ||
| unsigned long int length; | ||
| rc = pcre_fullinfo( | ||
| re, /* result of pcre_compile() */ | ||
| pe, /* result of pcre_study(), or NULL */ | ||
| PCRE_INFO_SIZE, /* what is required */ | ||
| &length); /* where to put the data */ | ||
| The possible values for the third argument are defined in | ||
| pcre.h, and are as follows: | ||
| PCRE_INFO_OPTIONS | ||
| Return a copy of the options with which the pattern was com- | ||
| piled. The fourth argument should point to an unsigned long | ||
| int variable. These option bits are those specified in the | ||
| call to pcre_compile(), modified by any top-level option | ||
| settings within the pattern itself, and with the | ||
| PCRE_ANCHORED bit forcibly set if the form of the pattern | ||
| implies that it can match only at the start of a subject | ||
| string. | ||
| PCRE_INFO_SIZE | ||
| Return the size of the compiled pattern, that is, the value | ||
| that was passed as the argument to pcre_malloc() when PCRE | ||
| was getting memory in which to place the compiled data. The | ||
| fourth argument should point to a size_t variable. | ||
| PCRE_INFO_CAPTURECOUNT | ||
| Return the number of capturing subpatterns in the pattern. | ||
| The fourth argument should point to an int variable. | ||
| PCRE_INFO_BACKREFMAX | ||
| Return the number of the highest back reference in the pat- | ||
| tern. The fourth argument should point to an int variable. | ||
| Zero is returned if there are no back references. | ||
| PCRE_INFO_FIRSTCHAR | ||
| Return information about the first character of any matched | ||
| string, for a non-anchored pattern. If there is a fixed | ||
| first character, e.g. from a pattern such as | ||
| (cat|cow|coyote), it is returned in the integer pointed to | ||
| by where. Otherwise, if either | ||
| (a) the pattern was compiled with the PCRE_MULTILINE option, | ||
| and every branch starts with "^", or | ||
| (b) every branch of the pattern starts with ".*" and | ||
| PCRE_DOTALL is not set (if it were set, the pattern would be | ||
| anchored), | ||
| -1 is returned, indicating that the pattern matches only at | ||
| the start of a subject string or after any "\n" within the | ||
| string. Otherwise -2 is returned. For anchored patterns, -2 | ||
| is returned. | ||
| PCRE_INFO_FIRSTTABLE | ||
| If the pattern was studied, and this resulted in the con- | ||
| struction of a 256-bit table indicating a fixed set of char- | ||
| acters for the first character in any matching string, a | ||
| pointer to the table is returned. Otherwise NULL is | ||
| returned. The fourth argument should point to an unsigned | ||
| char * variable. | ||
| PCRE_INFO_LASTLITERAL | ||
| For a non-anchored pattern, return the value of the right- | ||
| most literal character which must exist in any matched | ||
| string, other than at its start. The fourth argument should | ||
| point to an int variable. If there is no such character, or | ||
| if the pattern is anchored, -1 is returned. For example, for | ||
| the pattern /a\d+z\d+/ the returned value is 'z'. | ||
| The pcre_info() function is now obsolete because its inter- | ||
| face is too restrictive to return all the available data | ||
| about a compiled pattern. New programs should use | ||
| pcre_fullinfo() instead. The yield of pcre_info() is the | ||
| number of capturing subpatterns, or one of the following | ||
| negative numbers: | ||
| PCRE_ERROR_NULL the argument code was NULL | ||
| PCRE_ERROR_BADMAGIC the "magic number" was not found | ||
| If the optptr argument is not NULL, a copy of the options | ||
| with which the pattern was compiled is placed in the integer | ||
| it points to (see PCRE_INFO_OPTIONS above). | ||
| If the pattern is not anchored and the firstcharptr argument | ||
| is not NULL, it is used to pass back information about the | ||
| first character of any matched string (see | ||
| PCRE_INFO_FIRSTCHAR above). | ||
| 1187 | ||
| 1188 | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, | |
| 1189 | int what, void *where); | |
| 1190 | ||
| 1191 | The pcre_fullinfo() function returns information about a compiled pat- | |
| 1192 | tern. It replaces the obsolete pcre_info() function, which is neverthe- | |
| 1193 | less retained for backwards compability (and is documented below). | |
| 1194 | ||
| 1195 | The first argument for pcre_fullinfo() is a pointer to the compiled | |
| 1196 | pattern. The second argument is the result of pcre_study(), or NULL if | |
| 1197 | the pattern was not studied. The third argument specifies which piece | |
| 1198 | of information is required, and the fourth argument is a pointer to a | |
| 1199 | variable to receive the data. The yield of the function is zero for | |
| 1200 | success, or one of the following negative numbers: | |
| 1201 | ||
| 1202 | PCRE_ERROR_NULL the argument code was NULL | |
| 1203 | the argument where was NULL | |
| 1204 | PCRE_ERROR_BADMAGIC the "magic number" was not found | |
| 1205 | PCRE_ERROR_BADOPTION the value of what was invalid | |
| 1206 | ||
| 1207 | The "magic number" is placed at the start of each compiled pattern as | |
| 1208 | an simple check against passing an arbitrary memory pointer. Here is a | |
| 1209 | typical call of pcre_fullinfo(), to obtain the length of the compiled | |
| 1210 | pattern: | |
| 1211 | ||
| 1212 | int rc; | |
| 1213 | unsigned long int length; | |
| 1214 | rc = pcre_fullinfo( | |
| 1215 | re, /* result of pcre_compile() */ | |
| 1216 | pe, /* result of pcre_study(), or NULL */ | |
| 1217 | PCRE_INFO_SIZE, /* what is required */ | |
| 1218 | &length); /* where to put the data */ | |
| 1219 | ||
| 1220 | The possible values for the third argument are defined in pcre.h, and | |
| 1221 | are as follows: | |
| 1222 | ||
| 1223 | PCRE_INFO_BACKREFMAX | |
| 1224 | ||
| 1225 | Return the number of the highest back reference in the pattern. The | |
| 1226 | fourth argument should point to an int variable. Zero is returned if | |
| 1227 | there are no back references. | |
| 1228 | ||
| 1229 | PCRE_INFO_CAPTURECOUNT | |
| 1230 | ||
| 1231 | Return the number of capturing subpatterns in the pattern. The fourth | |
| 1232 | argument should point to an int variable. | |
| 1233 | ||
| 1234 | PCRE_INFO_DEFAULT_TABLES | |
| 1235 | ||
| 1236 | Return a pointer to the internal default character tables within PCRE. | |
| 1237 | The fourth argument should point to an unsigned char * variable. This | |
| 1238 | information call is provided for internal use by the pcre_study() func- | |
| 1239 | tion. External callers can cause PCRE to use its internal tables by | |
| 1240 | passing a NULL table pointer. | |
| 1241 | ||
| 1242 | PCRE_INFO_FIRSTBYTE | |
| 1243 | ||
| 1244 | Return information about the first byte of any matched string, for a | |
| 1245 | non-anchored pattern. (This option used to be called | |
| 1246 | PCRE_INFO_FIRSTCHAR; the old name is still recognized for backwards | |
| 1247 | compatibility.) | |
| 1248 | ||
| 1249 | If there is a fixed first byte, for example, from a pattern such as | |
| 1250 | (cat|cow|coyote), it is returned in the integer pointed to by where. | |
| 1251 | Otherwise, if either | |
| 1252 | ||
| 1253 | (a) the pattern was compiled with the PCRE_MULTILINE option, and every | |
| 1254 | branch starts with "^", or | |
| 1255 | ||
| 1256 | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not | |
| 1257 | set (if it were set, the pattern would be anchored), | |
| 1258 | ||
| 1259 | -1 is returned, indicating that the pattern matches only at the start | |
| 1260 | of a subject string or after any newline within the string. Otherwise | |
| 1261 | -2 is returned. For anchored patterns, -2 is returned. | |
| 1262 | ||
| 1263 | PCRE_INFO_FIRSTTABLE | |
| 1264 | ||
| 1265 | If the pattern was studied, and this resulted in the construction of a | |
| 1266 | 256-bit table indicating a fixed set of bytes for the first byte in any | |
| 1267 | matching string, a pointer to the table is returned. Otherwise NULL is | |
| 1268 | returned. The fourth argument should point to an unsigned char * vari- | |
| 1269 | able. | |
| 1270 | ||
| 1271 | PCRE_INFO_LASTLITERAL | |
| 1272 | ||
| 1273 | Return the value of the rightmost literal byte that must exist in any | |
| 1274 | matched string, other than at its start, if such a byte has been | |
| 1275 | recorded. The fourth argument should point to an int variable. If there | |
| 1276 | is no such byte, -1 is returned. For anchored patterns, a last literal | |
| 1277 | byte is recorded only if it follows something of variable length. For | |
| 1278 | example, for the pattern /^a\d+z\d+/ the returned value is "z", but for | |
| 1279 | /^a\dz\d/ the returned value is -1. | |
| 1280 | ||
| 1281 | PCRE_INFO_NAMECOUNT | |
| 1282 | PCRE_INFO_NAMEENTRYSIZE | |
| 1283 | PCRE_INFO_NAMETABLE | |
| 1284 | ||
| 1285 | PCRE supports the use of named as well as numbered capturing parenthe- | |
| 1286 | ses. The names are just an additional way of identifying the parenthe- | |
| 1287 | ses, which still acquire numbers. A convenience function called | |
| 1288 | pcre_get_named_substring() is provided for extracting an individual | |
| 1289 | captured substring by name. It is also possible to extract the data | |
| 1290 | directly, by first converting the name to a number in order to access | |
| 1291 | the correct pointers in the output vector (described with pcre_exec() | |
| 1292 | below). To do the conversion, you need to use the name-to-number map, | |
| 1293 | which is described by these three values. | |
| 1294 | ||
| 1295 | The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT | |
| 1296 | gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size | |
| 1297 | of each entry; both of these return an int value. The entry size | |
| 1298 | depends on the length of the longest name. PCRE_INFO_NAMETABLE returns | |
| 1299 | a pointer to the first entry of the table (a pointer to char). The | |
| 1300 | first two bytes of each entry are the number of the capturing parenthe- | |
| 1301 | sis, most significant byte first. The rest of the entry is the corre- | |
| 1302 | sponding name, zero terminated. The names are in alphabetical order. | |
| 1303 | For example, consider the following pattern (assume PCRE_EXTENDED is | |
| 1304 | set, so white space - including newlines - is ignored): | |
| 1305 | ||
| 1306 | (?P<date> (?P<year>(\d\d)?\d\d) - | |
| 1307 | (?P<month>\d\d) - (?P<day>\d\d) ) | |
| 1308 | ||
| 1309 | There are four named subpatterns, so the table has four entries, and | |
| 1310 | each entry in the table is eight bytes long. The table is as follows, | |
| 1311 | with non-printing bytes shows in hexadecimal, and undefined bytes shown | |
| 1312 | as ??: | |
| 1313 | ||
| 1314 | 00 01 d a t e 00 ?? | |
| 1315 | 00 05 d a y 00 ?? ?? | |
| 1316 | 00 04 m o n t h 00 | |
| 1317 | 00 02 y e a r 00 ?? | |
| 1318 | ||
| 1319 | When writing code to extract data from named subpatterns using the | |
| 1320 | name-to-number map, remember that the length of each entry is likely to | |
| 1321 | be different for each compiled pattern. | |
| 1322 | ||
| 1323 | PCRE_INFO_OPTIONS | |
| 1324 | ||
| 1325 | Return a copy of the options with which the pattern was compiled. The | |
| 1326 | fourth argument should point to an unsigned long int variable. These | |
| 1327 | option bits are those specified in the call to pcre_compile(), modified | |
| 1328 | by any top-level option settings within the pattern itself. | |
| 1329 | ||
| 1330 | A pattern is automatically anchored by PCRE if all of its top-level | |
| 1331 | alternatives begin with one of the following: | |
| 1332 | ||
| 1333 | ^ unless PCRE_MULTILINE is set | |
| 1334 | \A always | |
| 1335 | \G always | |
| 1336 | .* if PCRE_DOTALL is set and there are no back | |
| 1337 | references to the subpattern in which .* appears | |
| 1338 | ||
| 1339 | For such patterns, the PCRE_ANCHORED bit is set in the options returned | |
| 1340 | by pcre_fullinfo(). | |
| 1341 | ||
| 1342 | PCRE_INFO_SIZE | |
| 1343 | ||
| 1344 | Return the size of the compiled pattern, that is, the value that was | |
| 1345 | passed as the argument to pcre_malloc() when PCRE was getting memory in | |
| 1346 | which to place the compiled data. The fourth argument should point to a | |
| 1347 | size_t variable. | |
| 1348 | ||
| 1349 | PCRE_INFO_STUDYSIZE | |
| 1350 | ||
| 1351 | Return the size of the data block pointed to by the study_data field in | |
| 1352 | a pcre_extra block. That is, it is the value that was passed to | |
| 1353 | pcre_malloc() when PCRE was getting memory into which to place the data | |
| 1354 | created by pcre_study(). The fourth argument should point to a size_t | |
| 1355 | variable. | |
| 1356 | ||
| 1357 | ||
| 1358 | OBSOLETE INFO FUNCTION | |
| 1359 | ||
| 1360 | int pcre_info(const pcre *code, int *optptr, int *firstcharptr); | |
| 1361 | ||
| 1362 | The pcre_info() function is now obsolete because its interface is too | |
| 1363 | restrictive to return all the available data about a compiled pattern. | |
| 1364 | New programs should use pcre_fullinfo() instead. The yield of | |
| 1365 | pcre_info() is the number of capturing subpatterns, or one of the fol- | |
| 1366 | lowing negative numbers: | |
| 1367 | ||
| 1368 | PCRE_ERROR_NULL the argument code was NULL | |
| 1369 | PCRE_ERROR_BADMAGIC the "magic number" was not found | |
| 1370 | ||
| 1371 | If the optptr argument is not NULL, a copy of the options with which | |
| 1372 | the pattern was compiled is placed in the integer it points to (see | |
| 1373 | PCRE_INFO_OPTIONS above). | |
| 1374 | ||
| 1375 | If the pattern is not anchored and the firstcharptr argument is not | |
| 1376 | NULL, it is used to pass back information about the first character of | |
| 1377 | any matched string (see PCRE_INFO_FIRSTBYTE above). | |
| 1378 | ||
| 1379 | ||
| 1380 | REFERENCE COUNTS | |
| 1381 | ||
| 1382 | int pcre_refcount(pcre *code, int adjust); | |
| 1383 | ||
| 1384 | The pcre_refcount() function is used to maintain a reference count in | |
| 1385 | the data block that contains a compiled pattern. It is provided for the | |
| 1386 | benefit of applications that operate in an object-oriented manner, | |
| 1387 | where different parts of the application may be using the same compiled | |
| 1388 | pattern, but you want to free the block when they are all done. | |
| 1389 | ||
| 1390 | When a pattern is compiled, the reference count field is initialized to | |
| 1391 | zero. It is changed only by calling this function, whose action is to | |
| 1392 | add the adjust value (which may be positive or negative) to it. The | |
| 1393 | yield of the function is the new value. However, the value of the count | |
| 1394 | is constrained to lie between 0 and 65535, inclusive. If the new value | |
| 1395 | is outside these limits, it is forced to the appropriate limit value. | |
| 1396 | ||
| 1397 | Except when it is zero, the reference count is not correctly preserved | |
| 1398 | if a pattern is compiled on one host and then transferred to a host | |
| 1399 | whose byte-order is different. (This seems a highly unlikely scenario.) | |
| 1400 | ||
| 1401 | ||
| 1402 | MATCHING A PATTERN: THE TRADITIONAL FUNCTION | |
| 1403 | ||
| 1404 | int pcre_exec(const pcre *code, const pcre_extra *extra, | |
| 1405 | const char *subject, int length, int startoffset, | |
| 1406 | int options, int *ovector, int ovecsize); | |
| 1407 | ||
| 1408 | The function pcre_exec() is called to match a subject string against a | |
| 1409 | compiled pattern, which is passed in the code argument. If the pattern | |
| 1410 | has been studied, the result of the study should be passed in the extra | |
| 1411 | argument. This function is the main matching facility of the library, | |
| 1412 | and it operates in a Perl-like manner. For specialist use there is also | |
| 1413 | an alternative matching function, which is described below in the sec- | |
| 1414 | tion about the pcre_dfa_exec() function. | |
| 1415 | ||
| 1416 | In most applications, the pattern will have been compiled (and option- | |
| 1417 | ally studied) in the same process that calls pcre_exec(). However, it | |
| 1418 | is possible to save compiled patterns and study data, and then use them | |
| 1419 | later in different processes, possibly even on different hosts. For a | |
| 1420 | discussion about this, see the pcreprecompile documentation. | |
| 1421 | ||
| 1422 | Here is an example of a simple call to pcre_exec(): | |
| 1423 | ||
| 1424 | int rc; | |
| 1425 | int ovector[30]; | |
| 1426 | rc = pcre_exec( | |
| 1427 | re, /* result of pcre_compile() */ | |
| 1428 | NULL, /* we didn't study the pattern */ | |
| 1429 | "some string", /* the subject string */ | |
| 1430 | 11, /* the length of the subject string */ | |
| 1431 | 0, /* start at offset 0 in the subject */ | |
| 1432 | 0, /* default options */ | |
| 1433 | ovector, /* vector of integers for substring information */ | |
| 1434 | 30); /* number of elements (NOT size in bytes) */ | |
| 1435 | ||
| 1436 | Extra data for pcre_exec() | |
| 1437 | ||
| 1438 | If the extra argument is not NULL, it must point to a pcre_extra data | |
| 1439 | block. The pcre_study() function returns such a block (when it doesn't | |
| 1440 | return NULL), but you can also create one for yourself, and pass addi- | |
| 1441 | tional information in it. The pcre_extra block contains the following | |
| 1442 | fields (not necessarily in this order): | |
| 1443 | ||
| 1444 | unsigned long int flags; | |
| 1445 | void *study_data; | |
| 1446 | unsigned long int match_limit; | |
| 1447 | unsigned long int match_limit_recursion; | |
| 1448 | void *callout_data; | |
| 1449 | const unsigned char *tables; | |
| 1450 | ||
| 1451 | The flags field is a bitmap that specifies which of the other fields | |
| 1452 | are set. The flag bits are: | |
| 1453 | ||
| 1454 | PCRE_EXTRA_STUDY_DATA | |
| 1455 | PCRE_EXTRA_MATCH_LIMIT | |
| 1456 | PCRE_EXTRA_MATCH_LIMIT_RECURSION | |
| 1457 | PCRE_EXTRA_CALLOUT_DATA | |
| 1458 | PCRE_EXTRA_TABLES | |
| 1459 | ||
| 1460 | Other flag bits should be set to zero. The study_data field is set in | |
| 1461 | the pcre_extra block that is returned by pcre_study(), together with | |
| 1462 | the appropriate flag bit. You should not set this yourself, but you may | |
| 1463 | add to the block by setting the other fields and their corresponding | |
| 1464 | flag bits. | |
| 1465 | ||
| 1466 | The match_limit field provides a means of preventing PCRE from using up | |
| 1467 | a vast amount of resources when running patterns that are not going to | |
| 1468 | match, but which have a very large number of possibilities in their | |
| 1469 | search trees. The classic example is the use of nested unlimited | |
| 1470 | repeats. | |
| 1471 | ||
| 1472 | Internally, PCRE uses a function called match() which it calls repeat- | |
| 1473 | edly (sometimes recursively). The limit set by match_limit is imposed | |
| 1474 | on the number of times this function is called during a match, which | |
| 1475 | has the effect of limiting the amount of backtracking that can take | |
| 1476 | place. For patterns that are not anchored, the count restarts from zero | |
| 1477 | for each position in the subject string. | |
| 1478 | ||
| 1479 | The default value for the limit can be set when PCRE is built; the | |
| 1480 | default default is 10 million, which handles all but the most extreme | |
| 1481 | cases. You can override the default by suppling pcre_exec() with a | |
| 1482 | pcre_extra block in which match_limit is set, and | |
| 1483 | PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is | |
| 1484 | exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT. | |
| 1485 | ||
| 1486 | The match_limit_recursion field is similar to match_limit, but instead | |
| 1487 | of limiting the total number of times that match() is called, it limits | |
| 1488 | the depth of recursion. The recursion depth is a smaller number than | |
| 1489 | the total number of calls, because not all calls to match() are recur- | |
| 1490 | sive. This limit is of use only if it is set smaller than match_limit. | |
| 1491 | ||
| 1492 | Limiting the recursion depth limits the amount of stack that can be | |
| 1493 | used, or, when PCRE has been compiled to use memory on the heap instead | |
| 1494 | of the stack, the amount of heap memory that can be used. | |
| 1495 | ||
| 1496 | The default value for match_limit_recursion can be set when PCRE is | |
| 1497 | built; the default default is the same value as the default for | |
| 1498 | match_limit. You can override the default by suppling pcre_exec() with | |
| 1499 | a pcre_extra block in which match_limit_recursion is set, and | |
| 1500 | PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the | |
| 1501 | limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT. | |
| 1502 | ||
| 1503 | The pcre_callout field is used in conjunction with the "callout" fea- | |
| 1504 | ture, which is described in the pcrecallout documentation. | |
| 1505 | ||
| 1506 | The tables field is used to pass a character tables pointer to | |
| 1507 | pcre_exec(); this overrides the value that is stored with the compiled | |
| 1508 | pattern. A non-NULL value is stored with the compiled pattern only if | |
| 1509 | custom tables were supplied to pcre_compile() via its tableptr argu- | |
| 1510 | ment. If NULL is passed to pcre_exec() using this mechanism, it forces | |
| 1511 | PCRE's internal tables to be used. This facility is helpful when re- | |
| 1512 | using patterns that have been saved after compiling with an external | |
| 1513 | set of tables, because the external tables might be at a different | |
| 1514 | address when pcre_exec() is called. See the pcreprecompile documenta- | |
| 1515 | tion for a discussion of saving compiled patterns for later use. | |
| 1516 | ||
| 1517 | Option bits for pcre_exec() | |
| 1518 | ||
| 1519 | The unused bits of the options argument for pcre_exec() must be zero. | |
| 1520 | The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL, | |
| 1521 | PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK and PCRE_PARTIAL. | |
| 1522 | ||
| 1523 | PCRE_ANCHORED | |
| 1524 | ||
| 1525 | The PCRE_ANCHORED option limits pcre_exec() to matching at the first | |
| 1526 | matching position. If a pattern was compiled with PCRE_ANCHORED, or | |
| 1527 | turned out to be anchored by virtue of its contents, it cannot be made | |
| 1528 | unachored at matching time. | |
| 1529 | ||
| 1530 | PCRE_NOTBOL | |
| 1531 | ||
| 1532 | This option specifies that first character of the subject string is not | |
| 1533 | the beginning of a line, so the circumflex metacharacter should not | |
| 1534 | match before it. Setting this without PCRE_MULTILINE (at compile time) | |
| 1535 | causes circumflex never to match. This option affects only the behav- | |
| 1536 | iour of the circumflex metacharacter. It does not affect \A. | |
| 1537 | ||
| 1538 | PCRE_NOTEOL | |
| 1539 | ||
| 1540 | This option specifies that the end of the subject string is not the end | |
| 1541 | of a line, so the dollar metacharacter should not match it nor (except | |
| 1542 | in multiline mode) a newline immediately before it. Setting this with- | |
| 1543 | out PCRE_MULTILINE (at compile time) causes dollar never to match. This | |
| 1544 | option affects only the behaviour of the dollar metacharacter. It does | |
| 1545 | not affect \Z or \z. | |
| 1546 | ||
| 1547 | PCRE_NOTEMPTY | |
| 1548 | ||
| 1549 | An empty string is not considered to be a valid match if this option is | |
| 1550 | set. If there are alternatives in the pattern, they are tried. If all | |
| 1551 | the alternatives match the empty string, the entire match fails. For | |
| 1552 | example, if the pattern | |
| 1553 | ||
| 1554 | a?b? | |
| 1555 | ||
| 1556 | is applied to a string not beginning with "a" or "b", it matches the | |
| 1557 | empty string at the start of the subject. With PCRE_NOTEMPTY set, this | |
| 1558 | match is not valid, so PCRE searches further into the string for occur- | |
| 1559 | rences of "a" or "b". | |
| 1560 | ||
| 1561 | Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe- | |
| 1562 | cial case of a pattern match of the empty string within its split() | |
| 1563 | function, and when using the /g modifier. It is possible to emulate | |
| 1564 | Perl's behaviour after matching a null string by first trying the match | |
| 1565 | again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then | |
| 1566 | if that fails by advancing the starting offset (see below) and trying | |
| 1567 | an ordinary match again. There is some code that demonstrates how to do | |
| 1568 | this in the pcredemo.c sample program. | |
| 1569 | ||
| 1570 | PCRE_NO_UTF8_CHECK | |
| 1571 | ||
| 1572 | When PCRE_UTF8 is set at compile time, the validity of the subject as a | |
| 1573 | UTF-8 string is automatically checked when pcre_exec() is subsequently | |
| 1574 | called. The value of startoffset is also checked to ensure that it | |
| 1575 | points to the start of a UTF-8 character. If an invalid UTF-8 sequence | |
| 1576 | of bytes is found, pcre_exec() returns the error PCRE_ERROR_BADUTF8. If | |
| 1577 | startoffset contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is | |
| 1578 | returned. | |
| 1579 | ||
| 1580 | If you already know that your subject is valid, and you want to skip | |
| 1581 | these checks for performance reasons, you can set the | |
| 1582 | PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to | |
| 1583 | do this for the second and subsequent calls to pcre_exec() if you are | |
| 1584 | making repeated calls to find all the matches in a single subject | |
| 1585 | string. However, you should be sure that the value of startoffset | |
| 1586 | points to the start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is | |
| 1587 | set, the effect of passing an invalid UTF-8 string as a subject, or a | |
| 1588 | value of startoffset that does not point to the start of a UTF-8 char- | |
| 1589 | acter, is undefined. Your program may crash. | |
| 1590 | ||
| 1591 | PCRE_PARTIAL | |
| 1592 | ||
| 1593 | This option turns on the partial matching feature. If the subject | |
| 1594 | string fails to match the pattern, but at some point during the match- | |
| 1595 | ing process the end of the subject was reached (that is, the subject | |
| 1596 | partially matches the pattern and the failure to match occurred only | |
| 1597 | because there were not enough subject characters), pcre_exec() returns | |
| 1598 | PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is | |
| 1599 | used, there are restrictions on what may appear in the pattern. These | |
| 1600 | are discussed in the pcrepartial documentation. | |
| 1601 | ||
| 1602 | The string to be matched by pcre_exec() | |
| 1603 | ||
| 1604 | The subject string is passed to pcre_exec() as a pointer in subject, a | |
| 1605 | length in length, and a starting byte offset in startoffset. In UTF-8 | |
| 1606 | mode, the byte offset must point to the start of a UTF-8 character. | |
| 1607 | Unlike the pattern string, the subject may contain binary zero bytes. | |
| 1608 | When the starting offset is zero, the search for a match starts at the | |
| 1609 | beginning of the subject, and this is by far the most common case. | |
| 1610 | ||
| 1611 | A non-zero starting offset is useful when searching for another match | |
| 1612 | in the same subject by calling pcre_exec() again after a previous suc- | |
| 1613 | cess. Setting startoffset differs from just passing over a shortened | |
| 1614 | string and setting PCRE_NOTBOL in the case of a pattern that begins | |
| 1615 | with any kind of lookbehind. For example, consider the pattern | |
| 1616 | ||
| 1617 | \Biss\B | |
| 1618 | ||
| 1619 | which finds occurrences of "iss" in the middle of words. (\B matches | |
| 1620 | only if the current position in the subject is not a word boundary.) | |
| 1621 | When applied to the string "Mississipi" the first call to pcre_exec() | |
| 1622 | finds the first occurrence. If pcre_exec() is called again with just | |
| 1623 | the remainder of the subject, namely "issipi", it does not match, | |
| 1624 | because \B is always false at the start of the subject, which is deemed | |
| 1625 | to be a word boundary. However, if pcre_exec() is passed the entire | |
| 1626 | string again, but with startoffset set to 4, it finds the second occur- | |
| 1627 | rence of "iss" because it is able to look behind the starting point to | |
| 1628 | discover that it is preceded by a letter. | |
| 1629 | ||
| 1630 | If a non-zero starting offset is passed when the pattern is anchored, | |
| 1631 | one attempt to match at the given offset is made. This can only succeed | |
| 1632 | if the pattern does not require the match to be at the start of the | |
| 1633 | subject. | |
| 1634 | ||
| 1635 | How pcre_exec() returns captured substrings | |
| 1636 | ||
| 1637 | In general, a pattern matches a certain portion of the subject, and in | |
| 1638 | addition, further substrings from the subject may be picked out by | |
| 1639 | parts of the pattern. Following the usage in Jeffrey Friedl's book, | |
| 1640 | this is called "capturing" in what follows, and the phrase "capturing | |
| 1641 | subpattern" is used for a fragment of a pattern that picks out a sub- | |
| 1642 | string. PCRE supports several other kinds of parenthesized subpattern | |
| 1643 | that do not cause substrings to be captured. | |
| 1644 | ||
| 1645 | Captured substrings are returned to the caller via a vector of integer | |
| 1646 | offsets whose address is passed in ovector. The number of elements in | |
| 1647 | the vector is passed in ovecsize, which must be a non-negative number. | |
| 1648 | Note: this argument is NOT the size of ovector in bytes. | |
| 1649 | ||
| 1650 | The first two-thirds of the vector is used to pass back captured sub- | |
| 1651 | strings, each substring using a pair of integers. The remaining third | |
| 1652 | of the vector is used as workspace by pcre_exec() while matching cap- | |
| 1653 | turing subpatterns, and is not available for passing back information. | |
| 1654 | The length passed in ovecsize should always be a multiple of three. If | |
| 1655 | it is not, it is rounded down. | |
| 1656 | ||
| 1657 | When a match is successful, information about captured substrings is | |
| 1658 | returned in pairs of integers, starting at the beginning of ovector, | |
| 1659 | and continuing up to two-thirds of its length at the most. The first | |
| 1660 | element of a pair is set to the offset of the first character in a sub- | |
| 1661 | string, and the second is set to the offset of the first character | |
| 1662 | after the end of a substring. The first pair, ovector[0] and ovec- | |
| 1663 | tor[1], identify the portion of the subject string matched by the | |
| 1664 | entire pattern. The next pair is used for the first capturing subpat- | |
| 1665 | tern, and so on. The value returned by pcre_exec() is the number of | |
| 1666 | pairs that have been set. If there are no capturing subpatterns, the | |
| 1667 | return value from a successful match is 1, indicating that just the | |
| 1668 | first pair of offsets has been set. | |
| 1669 | ||
| 1670 | Some convenience functions are provided for extracting the captured | |
| 1671 | substrings as separate strings. These are described in the following | |
| 1672 | section. | |
| 1673 | ||
| 1674 | It is possible for an capturing subpattern number n+1 to match some | |
| 1675 | part of the subject when subpattern n has not been used at all. For | |
| 1676 | example, if the string "abc" is matched against the pattern (a|(z))(bc) | |
| 1677 | subpatterns 1 and 3 are matched, but 2 is not. When this happens, both | |
| 1678 | offset values corresponding to the unused subpattern are set to -1. | |
| 1679 | ||
| 1680 | If a capturing subpattern is matched repeatedly, it is the last portion | |
| 1681 | of the string that it matched that is returned. | |
| 1682 | ||
| 1683 | If the vector is too small to hold all the captured substring offsets, | |
| 1684 | it is used as far as possible (up to two-thirds of its length), and the | |
| 1685 | function returns a value of zero. In particular, if the substring off- | |
| 1686 | sets are not of interest, pcre_exec() may be called with ovector passed | |
| 1687 | as NULL and ovecsize as zero. However, if the pattern contains back | |
| 1688 | references and the ovector is not big enough to remember the related | |
| 1689 | substrings, PCRE has to get additional memory for use during matching. | |
| 1690 | Thus it is usually advisable to supply an ovector. | |
| 1691 | ||
| 1692 | Note that pcre_info() can be used to find out how many capturing sub- | |
| 1693 | patterns there are in a compiled pattern. The smallest size for ovector | |
| 1694 | that will allow for n captured substrings, in addition to the offsets | |
| 1695 | of the substring matched by the whole pattern, is (n+1)*3. | |
| 1696 | ||
| 1697 | Return values from pcre_exec() | |
| 1698 | ||
| 1699 | If pcre_exec() fails, it returns a negative number. The following are | |
| 1700 | defined in the header file: | |
| 1701 | ||
| 1702 | PCRE_ERROR_NOMATCH (-1) | |
| 1703 | ||
| 1704 | The subject string did not match the pattern. | |
| 1705 | ||
| 1706 | PCRE_ERROR_NULL (-2) | |
| 1707 | ||
| 1708 | Either code or subject was passed as NULL, or ovector was NULL and | |
| 1709 | ovecsize was not zero. | |
| 1710 | ||
| 1711 | PCRE_ERROR_BADOPTION (-3) | |
| 1712 | ||
| 1713 | An unrecognized bit was set in the options argument. | |
| 1714 | ||
| 1715 | PCRE_ERROR_BADMAGIC (-4) | |
| 1716 | ||
| 1717 | PCRE stores a 4-byte "magic number" at the start of the compiled code, | |
| 1718 | to catch the case when it is passed a junk pointer and to detect when a | |
| 1719 | pattern that was compiled in an environment of one endianness is run in | |
| 1720 | an environment with the other endianness. This is the error that PCRE | |
| 1721 | gives when the magic number is not present. | |
| 1722 | ||
| 1723 | PCRE_ERROR_UNKNOWN_NODE (-5) | |
| 1724 | ||
| 1725 | While running the pattern match, an unknown item was encountered in the | |
| 1726 | compiled pattern. This error could be caused by a bug in PCRE or by | |
| 1727 | overwriting of the compiled pattern. | |
| 1728 | ||
| 1729 | PCRE_ERROR_NOMEMORY (-6) | |
| 1730 | ||
| 1731 | If a pattern contains back references, but the ovector that is passed | |
| 1732 | to pcre_exec() is not big enough to remember the referenced substrings, | |
| 1733 | PCRE gets a block of memory at the start of matching to use for this | |
| 1734 | purpose. If the call via pcre_malloc() fails, this error is given. The | |
| 1735 | memory is automatically freed at the end of matching. | |
| 1736 | ||
| 1737 | PCRE_ERROR_NOSUBSTRING (-7) | |
| 1738 | ||
| 1739 | This error is used by the pcre_copy_substring(), pcre_get_substring(), | |
| 1740 | and pcre_get_substring_list() functions (see below). It is never | |
| 1741 | returned by pcre_exec(). | |
| 1742 | ||
| 1743 | PCRE_ERROR_MATCHLIMIT (-8) | |
| 1744 | ||
| 1745 | The backtracking limit, as specified by the match_limit field in a | |
| 1746 | pcre_extra structure (or defaulted) was reached. See the description | |
| 1747 | above. | |
| 1748 | ||
| 1749 | PCRE_ERROR_RECURSIONLIMIT (-21) | |
| 1750 | ||
| 1751 | The internal recursion limit, as specified by the match_limit_recursion | |
| 1752 | field in a pcre_extra structure (or defaulted) was reached. See the | |
| 1753 | description above. | |
| 1754 | ||
| 1755 | PCRE_ERROR_CALLOUT (-9) | |
| 1756 | ||
| 1757 | This error is never generated by pcre_exec() itself. It is provided for | |
| 1758 | use by callout functions that want to yield a distinctive error code. | |
| 1759 | See the pcrecallout documentation for details. | |
| 1760 | ||
| 1761 | PCRE_ERROR_BADUTF8 (-10) | |
| 1762 | ||
| 1763 | A string that contains an invalid UTF-8 byte sequence was passed as a | |
| 1764 | subject. | |
| 1765 | ||
| 1766 | PCRE_ERROR_BADUTF8_OFFSET (-11) | |
| 1767 | ||
| 1768 | The UTF-8 byte sequence that was passed as a subject was valid, but the | |
| 1769 | value of startoffset did not point to the beginning of a UTF-8 charac- | |
| 1770 | ter. | |
| 1771 | ||
| 1772 | PCRE_ERROR_PARTIAL (-12) | |
| 1773 | ||
| 1774 | The subject string did not match, but it did match partially. See the | |
| 1775 | pcrepartial documentation for details of partial matching. | |
| 1776 | ||
| 1777 | PCRE_ERROR_BADPARTIAL (-13) | |
| 1778 | ||
| 1779 | The PCRE_PARTIAL option was used with a compiled pattern containing | |
| 1780 | items that are not supported for partial matching. See the pcrepartial | |
| 1781 | documentation for details of partial matching. | |
| 1782 | ||
| 1783 | PCRE_ERROR_INTERNAL (-14) | |
| 1784 | ||
| 1785 | An unexpected internal error has occurred. This error could be caused | |
| 1786 | by a bug in PCRE or by overwriting of the compiled pattern. | |
| 1787 | ||
| 1788 | PCRE_ERROR_BADCOUNT (-15) | |
| 1789 | ||
| 1790 | This error is given if the value of the ovecsize argument is negative. | |
| 1791 | ||
| 1792 | ||
| 1793 | EXTRACTING CAPTURED SUBSTRINGS BY NUMBER | |
| 1794 | ||
| 1795 | int pcre_copy_substring(const char *subject, int *ovector, | |
| 1796 | int stringcount, int stringnumber, char *buffer, | |
| 1797 | int buffersize); | |
| 1798 | ||
| 1799 | int pcre_get_substring(const char *subject, int *ovector, | |
| 1800 | int stringcount, int stringnumber, | |
| 1801 | const char **stringptr); | |
| 1802 | ||
| 1803 | int pcre_get_substring_list(const char *subject, | |
| 1804 | int *ovector, int stringcount, const char ***listptr); | |
| 1805 | ||
| 1806 | Captured substrings can be accessed directly by using the offsets | |
| 1807 | returned by pcre_exec() in ovector. For convenience, the functions | |
| 1808 | pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub- | |
| 1809 | string_list() are provided for extracting captured substrings as new, | |
| 1810 | separate, zero-terminated strings. These functions identify substrings | |
| 1811 | by number. The next section describes functions for extracting named | |
| 1812 | substrings. A substring that contains a binary zero is correctly | |
| 1813 | extracted and has a further zero added on the end, but the result is | |
| 1814 | not, of course, a C string. | |
| 1815 | ||
| 1816 | The first three arguments are the same for all three of these func- | |
| 1817 | tions: subject is the subject string that has just been successfully | |
| 1818 | matched, ovector is a pointer to the vector of integer offsets that was | |
| 1819 | passed to pcre_exec(), and stringcount is the number of substrings that | |
| 1820 | were captured by the match, including the substring that matched the | |
| 1821 | entire regular expression. This is the value returned by pcre_exec() if | |
| 1822 | it is greater than zero. If pcre_exec() returned zero, indicating that | |
| 1823 | it ran out of space in ovector, the value passed as stringcount should | |
| 1824 | be the number of elements in the vector divided by three. | |
| 1825 | ||
| 1826 | The functions pcre_copy_substring() and pcre_get_substring() extract a | |
| 1827 | single substring, whose number is given as stringnumber. A value of | |
| 1828 | zero extracts the substring that matched the entire pattern, whereas | |
| 1829 | higher values extract the captured substrings. For pcre_copy_sub- | |
| 1830 | string(), the string is placed in buffer, whose length is given by | |
| 1831 | buffersize, while for pcre_get_substring() a new block of memory is | |
| 1832 | obtained via pcre_malloc, and its address is returned via stringptr. | |
| 1833 | The yield of the function is the length of the string, not including | |
| 1834 | the terminating zero, or one of | |
| 1835 | ||
| 1836 | PCRE_ERROR_NOMEMORY (-6) | |
| 1837 | ||
| 1838 | The buffer was too small for pcre_copy_substring(), or the attempt to | |
| 1839 | get memory failed for pcre_get_substring(). | |
| 1840 | ||
| 1841 | PCRE_ERROR_NOSUBSTRING (-7) | |
| 1842 | ||
| 1843 | There is no substring whose number is stringnumber. | |
| 1844 | ||
| 1845 | The pcre_get_substring_list() function extracts all available sub- | |
| 1846 | strings and builds a list of pointers to them. All this is done in a | |
| 1847 | single block of memory that is obtained via pcre_malloc. The address of | |
| 1848 | the memory block is returned via listptr, which is also the start of | |
| 1849 | the list of string pointers. The end of the list is marked by a NULL | |
| 1850 | pointer. The yield of the function is zero if all went well, or | |
| 1851 | ||
| 1852 | PCRE_ERROR_NOMEMORY (-6) | |
| 1853 | ||
| 1854 | if the attempt to get the memory block failed. | |
| 1855 | ||
| 1856 | When any of these functions encounter a substring that is unset, which | |
| 1857 | can happen when capturing subpattern number n+1 matches some part of | |
| 1858 | the subject, but subpattern n has not been used at all, they return an | |
| 1859 | empty string. This can be distinguished from a genuine zero-length sub- | |
| 1860 | string by inspecting the appropriate offset in ovector, which is nega- | |
| 1861 | tive for unset substrings. | |
| 1862 | ||
| 1863 | The two convenience functions pcre_free_substring() and pcre_free_sub- | |
| 1864 | string_list() can be used to free the memory returned by a previous | |
| 1865 | call of pcre_get_substring() or pcre_get_substring_list(), respec- | |
| 1866 | tively. They do nothing more than call the function pointed to by | |
| 1867 | pcre_free, which of course could be called directly from a C program. | |
| 1868 | However, PCRE is used in some situations where it is linked via a spe- | |
| 1869 | cial interface to another programming language which cannot use | |
| 1870 | pcre_free directly; it is for these cases that the functions are pro- | |
| 1871 | vided. | |
| 1872 | ||
| 1873 | ||
| 1874 | EXTRACTING CAPTURED SUBSTRINGS BY NAME | |
| 1875 | ||
| 1876 | int pcre_get_stringnumber(const pcre *code, | |
| 1877 | const char *name); | |
| 1878 | ||
| 1879 | int pcre_copy_named_substring(const pcre *code, | |
| 1880 | const char *subject, int *ovector, | |
| 1881 | int stringcount, const char *stringname, | |
| 1882 | char *buffer, int buffersize); | |
| 1883 | ||
| 1884 | int pcre_get_named_substring(const pcre *code, | |
| 1885 | const char *subject, int *ovector, | |
| 1886 | int stringcount, const char *stringname, | |
| 1887 | const char **stringptr); | |
| 1888 | ||
| 1889 | To extract a substring by name, you first have to find associated num- | |
| 1890 | ber. For example, for this pattern | |
| 1891 | ||
| 1892 | (a+)b(?P<xxx>\d+)... | |
| 1893 | ||
| 1894 | the number of the subpattern called "xxx" is 2. You can find the number | |
| 1895 | from the name by calling pcre_get_stringnumber(). The first argument is | |
| 1896 | the compiled pattern, and the second is the name. The yield of the | |
| 1897 | function is the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if | |
| 1898 | there is no subpattern of that name. | |
| 1899 | ||
| 1900 | Given the number, you can extract the substring directly, or use one of | |
| 1901 | the functions described in the previous section. For convenience, there | |
| 1902 | are also two functions that do the whole job. | |
| 1903 | ||
| 1904 | Most of the arguments of pcre_copy_named_substring() and | |
| 1905 | pcre_get_named_substring() are the same as those for the similarly | |
| 1906 | named functions that extract by number. As these are described in the | |
| 1907 | previous section, they are not re-described here. There are just two | |
| 1908 | differences: | |
| 1909 | ||
| 1910 | First, instead of a substring number, a substring name is given. Sec- | |
| 1911 | ond, there is an extra argument, given at the start, which is a pointer | |
| 1912 | to the compiled pattern. This is needed in order to gain access to the | |
| 1913 | name-to-number translation table. | |
| 1914 | ||
| 1915 | These functions call pcre_get_stringnumber(), and if it succeeds, they | |
| 1916 | then call pcre_copy_substring() or pcre_get_substring(), as appropri- | |
| 1917 | ate. | |
| 1918 | ||
| 1919 | ||
| 1920 | FINDING ALL POSSIBLE MATCHES | |
| 1921 | ||
| 1922 | The traditional matching function uses a similar algorithm to Perl, | |
| 1923 | which stops when it finds the first match, starting at a given point in | |
| 1924 | the subject. If you want to find all possible matches, or the longest | |
| 1925 | possible match, consider using the alternative matching function (see | |
| 1926 | below) instead. If you cannot use the alternative function, but still | |
| 1927 | need to find all possible matches, you can kludge it up by making use | |
| 1928 | of the callout facility, which is described in the pcrecallout documen- | |
| 1929 | tation. | |
| 1930 | ||
| 1931 | What you have to do is to insert a callout right at the end of the pat- | |
| 1932 | tern. When your callout function is called, extract and save the cur- | |
| 1933 | rent matched substring. Then return 1, which forces pcre_exec() to | |
| 1934 | backtrack and try other alternatives. Ultimately, when it runs out of | |
| 1935 | matches, pcre_exec() will yield PCRE_ERROR_NOMATCH. | |
| 1936 | ||
| 1937 | ||
| 1938 | MATCHING A PATTERN: THE ALTERNATIVE FUNCTION | |
| 1939 | ||
| 1940 | int pcre_dfa_exec(const pcre *code, const pcre_extra *extra, | |
| 1941 | const char *subject, int length, int startoffset, | |
| 1942 | int options, int *ovector, int ovecsize, | |
| 1943 | int *workspace, int wscount); | |
| 1944 | ||
| 1945 | The function pcre_dfa_exec() is called to match a subject string | |
| 1946 | against a compiled pattern, using a "DFA" matching algorithm. This has | |
| 1947 | different characteristics to the normal algorithm, and is not compati- | |
| 1948 | ble with Perl. Some of the features of PCRE patterns are not supported. | |
| 1949 | Nevertheless, there are times when this kind of matching can be useful. | |
| 1950 | For a discussion of the two matching algorithms, see the pcrematching | |
| 1951 | documentation. | |
| 1952 | ||
| 1953 | The arguments for the pcre_dfa_exec() function are the same as for | |
| 1954 | pcre_exec(), plus two extras. The ovector argument is used in a differ- | |
| 1955 | ent way, and this is described below. The other common arguments are | |
| 1956 | used in the same way as for pcre_exec(), so their description is not | |
| 1957 | repeated here. | |
| 1958 | ||
| 1959 | The two additional arguments provide workspace for the function. The | |
| 1960 | workspace vector should contain at least 20 elements. It is used for | |
| 1961 | keeping track of multiple paths through the pattern tree. More | |
| 1962 | workspace will be needed for patterns and subjects where there are a | |
| 1963 | lot of possible matches. | |
| 1964 | ||
| 1965 | Here is an example of a simple call to pcre_dfa_exec(): | |
| 1966 | ||
| 1967 | int rc; | |
| 1968 | int ovector[10]; | |
| 1969 | int wspace[20]; | |
| 1970 | rc = pcre_dfa_exec( | |
| 1971 | re, /* result of pcre_compile() */ | |
| 1972 | NULL, /* we didn't study the pattern */ | |
| 1973 | "some string", /* the subject string */ | |
| 1974 | 11, /* the length of the subject string */ | |
| 1975 | 0, /* start at offset 0 in the subject */ | |
| 1976 | 0, /* default options */ | |
| 1977 | ovector, /* vector of integers for substring information */ | |
| 1978 | 10, /* number of elements (NOT size in bytes) */ | |
| 1979 | wspace, /* working space vector */ | |
| 1980 | 20); /* number of elements (NOT size in bytes) */ | |
| 1981 | ||
| 1982 | Option bits for pcre_dfa_exec() | |
| 1983 | ||
| 1984 | The unused bits of the options argument for pcre_dfa_exec() must be | |
| 1985 | zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NOTBOL, | |
| 1986 | PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL, | |
| 1987 | PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last three of | |
| 1988 | these are the same as for pcre_exec(), so their description is not | |
| 1989 | repeated here. | |
| 1990 | ||
| 1991 | PCRE_PARTIAL | |
| 1992 | ||
| 1993 | This has the same general effect as it does for pcre_exec(), but the | |
| 1994 | details are slightly different. When PCRE_PARTIAL is set for | |
| 1995 | pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is converted into | |
| 1996 | PCRE_ERROR_PARTIAL if the end of the subject is reached, there have | |
| 1997 | been no complete matches, but there is still at least one matching pos- | |
| 1998 | sibility. The portion of the string that provided the partial match is | |
| 1999 | set as the first matching string. | |
| 2000 | ||
| 2001 | PCRE_DFA_SHORTEST | |
| 2002 | ||
| 2003 | Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to | |
| 2004 | stop as soon as it has found one match. Because of the way the DFA | |
| 2005 | algorithm works, this is necessarily the shortest possible match at the | |
| 2006 | first possible matching point in the subject string. | |
| 2007 | ||
| 2008 | PCRE_DFA_RESTART | |
| 2009 | ||
| 2010 | When pcre_dfa_exec() is called with the PCRE_PARTIAL option, and | |
| 2011 | returns a partial match, it is possible to call it again, with addi- | |
| 2012 | tional subject characters, and have it continue with the same match. | |
| 2013 | The PCRE_DFA_RESTART option requests this action; when it is set, the | |
| 2014 | workspace and wscount options must reference the same vector as before | |
| 2015 | because data about the match so far is left in them after a partial | |
| 2016 | match. There is more discussion of this facility in the pcrepartial | |
| 2017 | documentation. | |
| 2018 | ||
| 2019 | Successful returns from pcre_dfa_exec() | |
| 2020 | ||
| 2021 | When pcre_dfa_exec() succeeds, it may have matched more than one sub- | |
| 2022 | string in the subject. Note, however, that all the matches from one run | |
| 2023 | of the function start at the same point in the subject. The shorter | |
| 2024 | matches are all initial substrings of the longer matches. For example, | |
| 2025 | if the pattern | |
| 2026 | ||
| 2027 | <.*> | |
| 2028 | ||
| 2029 | is matched against the string | |
| 2030 | ||
| 2031 | This is <something> <something else> <something further> no more | |
| 2032 | ||
| 2033 | the three matched strings are | |
| 2034 | ||
| 2035 | <something> | |
| 2036 | <something> <something else> | |
| 2037 | <something> <something else> <something further> | |
| 2038 | ||
| 2039 | On success, the yield of the function is a number greater than zero, | |
| 2040 | which is the number of matched substrings. The substrings themselves | |
| 2041 | are returned in ovector. Each string uses two elements; the first is | |
| 2042 | the offset to the start, and the second is the offset to the end. All | |
| 2043 | the strings have the same start offset. (Space could have been saved by | |
| 2044 | giving this only once, but it was decided to retain some compatibility | |
| 2045 | with the way pcre_exec() returns data, even though the meaning of the | |
| 2046 | strings is different.) | |
| 2047 | ||
| 2048 | The strings are returned in reverse order of length; that is, the long- | |
| 2049 | est matching string is given first. If there were too many matches to | |
| 2050 | fit into ovector, the yield of the function is zero, and the vector is | |
| 2051 | filled with the longest matches. | |
| 2052 | ||
| 2053 | Error returns from pcre_dfa_exec() | |
| 2054 | ||
| 2055 | The pcre_dfa_exec() function returns a negative number when it fails. | |
| 2056 | Many of the errors are the same as for pcre_exec(), and these are | |
| 2057 | described above. There are in addition the following errors that are | |
| 2058 | specific to pcre_dfa_exec(): | |
| 2059 | ||
| 2060 | PCRE_ERROR_DFA_UITEM (-16) | |
| 2061 | ||
| 2062 | This return is given if pcre_dfa_exec() encounters an item in the pat- | |
| 2063 | tern that it does not support, for instance, the use of \C or a back | |
| 2064 | reference. | |
| 2065 | ||
| 2066 | PCRE_ERROR_DFA_UCOND (-17) | |
| 2067 | ||
| 2068 | This return is given if pcre_dfa_exec() encounters a condition item in | |
| 2069 | a pattern that uses a back reference for the condition. This is not | |
| 2070 | supported. | |
| 2071 | ||
| 2072 | PCRE_ERROR_DFA_UMLIMIT (-18) | |
| 2073 | ||
| 2074 | This return is given if pcre_dfa_exec() is called with an extra block | |
| 2075 | that contains a setting of the match_limit field. This is not supported | |
| 2076 | (it is meaningless). | |
| 2077 | ||
| 2078 | PCRE_ERROR_DFA_WSSIZE (-19) | |
| 2079 | ||
| 2080 | This return is given if pcre_dfa_exec() runs out of space in the | |
| 2081 | workspace vector. | |
| 2082 | ||
| 2083 | PCRE_ERROR_DFA_RECURSE (-20) | |
| 2084 | ||
| 2085 | When a recursive subpattern is processed, the matching function calls | |
| 2086 | itself recursively, using private vectors for ovector and workspace. | |
| 2087 | This error is given if the output vector is not large enough. This | |
| 2088 | should be extremely rare, as a vector of size 1000 is used. | |
| 2089 | ||
| 2090 | Last updated: 18 January 2006 | |
| 2091 | Copyright (c) 1997-2006 University of Cambridge. | |
| 2092 | ------------------------------------------------------------------------------ | |
| 2093 | ||
| MATCHING A PATTERN | ||
| The function pcre_exec() is called to match a subject string | ||
| 2094 | ||
| 2095 | PCRECALLOUT(3) PCRECALLOUT(3) | |
| 2096 | ||
| 2097 | ||
| 2098 | NAME | |
| 2099 | PCRE - Perl-compatible regular expressions | |
| 2100 | ||
| 2101 | ||
| 2102 | SunOS 5.8 Last change: 9 | PCRE CALLOUTS |
| 2103 | ||
| 2104 | int (*pcre_callout)(pcre_callout_block *); | |
| 2105 | ||
| 2106 | PCRE provides a feature called "callout", which is a means of temporar- | |
| 2107 | ily passing control to the caller of PCRE in the middle of pattern | |
| 2108 | matching. The caller of PCRE provides an external function by putting | |
| 2109 | its entry point in the global variable pcre_callout. By default, this | |
| 2110 | variable contains NULL, which disables all calling out. | |
| 2111 | ||
| 2112 | Within a regular expression, (?C) indicates the points at which the | |
| 2113 | external function is to be called. Different callout points can be | |
| 2114 | identified by putting a number less than 256 after the letter C. The | |
| 2115 | default value is zero. For example, this pattern has two callout | |
| 2116 | points: | |
| 2117 | ||
| 2118 | (?C1)eabc(?C2)def | |
| 2119 | ||
| 2120 | If the PCRE_AUTO_CALLOUT option bit is set when pcre_compile() is | |
| 2121 | called, PCRE automatically inserts callouts, all with number 255, | |
| 2122 | before each item in the pattern. For example, if PCRE_AUTO_CALLOUT is | |
| 2123 | used with the pattern | |
| 2124 | ||
| 2125 | A(\d{2}|--) | |
| 2126 | ||
| 2127 | it is processed as if it were | |
| 2128 | ||
| 2129 | (?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255) | |
| 2130 | ||
| 2131 | Notice that there is a callout before and after each parenthesis and | |
| 2132 | alternation bar. Automatic callouts can be used for tracking the | |
| 2133 | progress of pattern matching. The pcretest command has an option that | |
| 2134 | sets automatic callouts; when it is used, the output indicates how the | |
| 2135 | pattern is matched. This is useful information when you are trying to | |
| 2136 | optimize the performance of a particular pattern. | |
| 2137 | ||
| 2138 | ||
| 2139 | MISSING CALLOUTS | |
| 2140 | ||
| 2141 | You should be aware that, because of optimizations in the way PCRE | |
| 2142 | matches patterns, callouts sometimes do not happen. For example, if the | |
| 2143 | pattern is | |
| 2144 | ||
| 2145 | ab(?C4)cd | |
| 2146 | ||
| 2147 | PCRE knows that any matching string must contain the letter "d". If the | |
| 2148 | subject string is "abyz", the lack of "d" means that matching doesn't | |
| 2149 | ever start, and the callout is never reached. However, with "abyd", | |
| 2150 | though the result is still no match, the callout is obeyed. | |
| 2151 | ||
| 2152 | ||
| 2153 | THE CALLOUT INTERFACE | |
| 2154 | ||
| 2155 | During matching, when PCRE reaches a callout point, the external func- | |
| 2156 | tion defined by pcre_callout is called (if it is set). This applies to | |
| 2157 | both the pcre_exec() and the pcre_dfa_exec() matching functions. The | |
| 2158 | only argument to the callout function is a pointer to a pcre_callout | |
| 2159 | block. This structure contains the following fields: | |
| 2160 | ||
| 2161 | int version; | |
| 2162 | int callout_number; | |
| 2163 | int *offset_vector; | |
| 2164 | const char *subject; | |
| 2165 | int subject_length; | |
| 2166 | int start_match; | |
| 2167 | int current_position; | |
| 2168 | int capture_top; | |
| 2169 | int capture_last; | |
| 2170 | void *callout_data; | |
| 2171 | int pattern_position; | |
| 2172 | int next_item_length; | |
| 2173 | ||
| 2174 | The version field is an integer containing the version number of the | |
| 2175 | block format. The initial version was 0; the current version is 1. The | |
| 2176 | version number will change again in future if additional fields are | |
| 2177 | added, but the intention is never to remove any of the existing fields. | |
| 2178 | ||
| 2179 | The callout_number field contains the number of the callout, as com- | |
| 2180 | piled into the pattern (that is, the number after ?C for manual call- | |
| 2181 | outs, and 255 for automatically generated callouts). | |
| 2182 | ||
| 2183 | The offset_vector field is a pointer to the vector of offsets that was | |
| 2184 | passed by the caller to pcre_exec() or pcre_dfa_exec(). When | |
| 2185 | pcre_exec() is used, the contents can be inspected in order to extract | |
| 2186 | substrings that have been matched so far, in the same way as for | |
| 2187 | extracting substrings after a match has completed. For pcre_dfa_exec() | |
| 2188 | this field is not useful. | |
| 2189 | ||
| 2190 | The subject and subject_length fields contain copies of the values that | |
| 2191 | were passed to pcre_exec(). | |
| 2192 | ||
| 2193 | The start_match field contains the offset within the subject at which | |
| 2194 | the current match attempt started. If the pattern is not anchored, the | |
| 2195 | callout function may be called several times from the same point in the | |
| 2196 | pattern for different starting points in the subject. | |
| 2197 | ||
| 2198 | The current_position field contains the offset within the subject of | |
| 2199 | the current match pointer. | |
| 2200 | ||
| 2201 | When the pcre_exec() function is used, the capture_top field contains | |
| 2202 | one more than the number of the highest numbered captured substring so | |
| 2203 | far. If no substrings have been captured, the value of capture_top is | |
| 2204 | one. This is always the case when pcre_dfa_exec() is used, because it | |
| 2205 | does not support captured substrings. | |
| 2206 | ||
| 2207 | The capture_last field contains the number of the most recently cap- | |
| 2208 | tured substring. If no substrings have been captured, its value is -1. | |
| 2209 | This is always the case when pcre_dfa_exec() is used. | |
| 2210 | ||
| 2211 | The callout_data field contains a value that is passed to pcre_exec() | |
| 2212 | or pcre_dfa_exec() specifically so that it can be passed back in call- | |
| 2213 | outs. It is passed in the pcre_callout field of the pcre_extra data | |
| 2214 | structure. If no such data was passed, the value of callout_data in a | |
| 2215 | pcre_callout block is NULL. There is a description of the pcre_extra | |
| 2216 | structure in the pcreapi documentation. | |
| 2217 | ||
| 2218 | The pattern_position field is present from version 1 of the pcre_call- | |
| 2219 | out structure. It contains the offset to the next item to be matched in | |
| 2220 | the pattern string. | |
| 2221 | ||
| 2222 | The next_item_length field is present from version 1 of the pcre_call- | |
| 2223 | out structure. It contains the length of the next item to be matched in | |
| 2224 | the pattern string. When the callout immediately precedes an alterna- | |
| 2225 | tion bar, a closing parenthesis, or the end of the pattern, the length | |
| 2226 | is zero. When the callout precedes an opening parenthesis, the length | |
| 2227 | is that of the entire subpattern. | |
| 2228 | ||
| 2229 | The pattern_position and next_item_length fields are intended to help | |
| 2230 | in distinguishing between different automatic callouts, which all have | |
| 2231 | the same callout number. However, they are set for all callouts. | |
| 2232 | ||
| 2233 | ||
| 2234 | RETURN VALUES | |
| 2235 | ||
| 2236 | The external callout function returns an integer to PCRE. If the value | |
| 2237 | is zero, matching proceeds as normal. If the value is greater than | |
| 2238 | zero, matching fails at the current point, but the testing of other | |
| 2239 | matching possibilities goes ahead, just as if a lookahead assertion had | |
| 2240 | failed. If the value is less than zero, the match is abandoned, and | |
| 2241 | pcre_exec() (or pcre_dfa_exec()) returns the negative value. | |
| 2242 | ||
| 2243 | Negative values should normally be chosen from the set of | |
| 2244 | PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan- | |
| 2245 | dard "no match" failure. The error number PCRE_ERROR_CALLOUT is | |
| 2246 | reserved for use by callout functions; it will never be used by PCRE | |
| 2247 | itself. | |
| 2248 | ||
| 2249 | Last updated: 28 February 2005 | |
| 2250 | Copyright (c) 1997-2005 University of Cambridge. | |
| 2251 | ------------------------------------------------------------------------------ | |
| 2252 | ||
| against a pre-compiled pattern, which is passed in the code | ||
| argument. If the pattern has been studied, the result of the | ||
| study should be passed in the extra argument. Otherwise this | ||
| must be NULL. | ||
| Here is an example of a simple call to pcre_exec(): | ||
| int rc; | ||
| int ovector[30]; | ||
| rc = pcre_exec( | ||
| re, /* result of pcre_compile() */ | ||
| NULL, /* we didn't study the pattern */ | ||
| "some string", /* the subject string */ | ||
| 11, /* the length of the subject string */ | ||
| 0, /* start at offset 0 in the subject */ | ||
| 0, /* default options */ | ||
| ovector, /* vector for substring information */ | ||
| 30); /* number of elements in the vector */ | ||
| The PCRE_ANCHORED option can be passed in the options argu- | ||
| ment, whose unused bits must be zero. However, if a pattern | ||
| was compiled with PCRE_ANCHORED, or turned out to be | ||
| anchored by virtue of its contents, it cannot be made | ||
| unachored at matching time. | ||
| There are also three further options that can be set only at | ||
| matching time: | ||
| PCRE_NOTBOL | ||
| The first character of the string is not the beginning of a | ||
| line, so the circumflex metacharacter should not match | ||
| before it. Setting this without PCRE_MULTILINE (at compile | ||
| time) causes circumflex never to match. | ||
| PCRE_NOTEOL | ||
| The end of the string is not the end of a line, so the dol- | ||
| lar metacharacter should not match it nor (except in multi- | ||
| line mode) a newline immediately before it. Setting this | ||
| without PCRE_MULTILINE (at compile time) causes dollar never | ||
| to match. | ||
| PCRE_NOTEMPTY | ||
| An empty string is not considered to be a valid match if | ||
| this option is set. If there are alternatives in the pat- | ||
| tern, they are tried. If all the alternatives match the | ||
| empty string, the entire match fails. For example, if the | ||
| pattern | ||
| a?b? | ||
| is applied to a string not beginning with "a" or "b", it | ||
| matches the empty string at the start of the subject. With | ||
| PCRE_NOTEMPTY set, this match is not valid, so PCRE searches | ||
| further into the string for occurrences of "a" or "b". | ||
| Perl has no direct equivalent of PCRE_NOTEMPTY, but it does | ||
| make a special case of a pattern match of the empty string | ||
| within its split() function, and when using the /g modifier. | ||
| It is possible to emulate Perl's behaviour after matching a | ||
| null string by first trying the match again at the same | ||
| offset with PCRE_NOTEMPTY set, and then if that fails by | ||
| advancing the starting offset (see below) and trying an | ||
| ordinary match again. | ||
| The subject string is passed as a pointer in subject, a | ||
| length in length, and a starting offset in startoffset. | ||
| Unlike the pattern string, the subject may contain binary | ||
| zero characters. When the starting offset is zero, the | ||
| search for a match starts at the beginning of the subject, | ||
| and this is by far the most common case. | ||
| A non-zero starting offset is useful when searching for | ||
| another match in the same subject by calling pcre_exec() | ||
| again after a previous success. Setting startoffset differs | ||
| from just passing over a shortened string and setting | ||
| PCRE_NOTBOL in the case of a pattern that begins with any | ||
| kind of lookbehind. For example, consider the pattern | ||
| \Biss\B | ||
| which finds occurrences of "iss" in the middle of words. (\B | ||
| matches only if the current position in the subject is not a | ||
| word boundary.) When applied to the string "Mississipi" the | ||
| first call to pcre_exec() finds the first occurrence. If | ||
| pcre_exec() is called again with just the remainder of the | ||
| subject, namely "issipi", it does not match, because \B is | ||
| always false at the start of the subject, which is deemed to | ||
| be a word boundary. However, if pcre_exec() is passed the | ||
| entire string again, but with startoffset set to 4, it finds | ||
| the second occurrence of "iss" because it is able to look | ||
| behind the starting point to discover that it is preceded by | ||
| a letter. | ||
| If a non-zero starting offset is passed when the pattern is | ||
| anchored, one attempt to match at the given offset is tried. | ||
| This can only succeed if the pattern does not require the | ||
| match to be at the start of the subject. | ||
| In general, a pattern matches a certain portion of the sub- | ||
| ject, and in addition, further substrings from the subject | ||
| may be picked out by parts of the pattern. Following the | ||
| usage in Jeffrey Friedl's book, this is called "capturing" | ||
| in what follows, and the phrase "capturing subpattern" is | ||
| used for a fragment of a pattern that picks out a substring. | ||
| PCRE supports several other kinds of parenthesized subpat- | ||
| tern that do not cause substrings to be captured. | ||
| Captured substrings are returned to the caller via a vector | ||
| of integer offsets whose address is passed in ovector. The | ||
| number of elements in the vector is passed in ovecsize. The | ||
| first two-thirds of the vector is used to pass back captured | ||
| substrings, each substring using a pair of integers. The | ||
| remaining third of the vector is used as workspace by | ||
| pcre_exec() while matching capturing subpatterns, and is not | ||
| available for passing back information. The length passed in | ||
| ovecsize should always be a multiple of three. If it is not, | ||
| it is rounded down. | ||
| When a match has been successful, information about captured | ||
| substrings is returned in pairs of integers, starting at the | ||
| beginning of ovector, and continuing up to two-thirds of its | ||
| length at the most. The first element of a pair is set to | ||
| the offset of the first character in a substring, and the | ||
| second is set to the offset of the first character after the | ||
| end of a substring. The first pair, ovector[0] and ovec- | ||
| tor[1], identify the portion of the subject string matched | ||
| by the entire pattern. The next pair is used for the first | ||
| capturing subpattern, and so on. The value returned by | ||
| pcre_exec() is the number of pairs that have been set. If | ||
| there are no capturing subpatterns, the return value from a | ||
| successful match is 1, indicating that just the first pair | ||
| of offsets has been set. | ||
| Some convenience functions are provided for extracting the | ||
| captured substrings as separate strings. These are described | ||
| in the following section. | ||
| It is possible for an capturing subpattern number n+1 to | ||
| match some part of the subject when subpattern n has not | ||
| been used at all. For example, if the string "abc" is | ||
| matched against the pattern (a|(z))(bc) subpatterns 1 and 3 | ||
| are matched, but 2 is not. When this happens, both offset | ||
| values corresponding to the unused subpattern are set to -1. | ||
| If a capturing subpattern is matched repeatedly, it is the | ||
| last portion of the string that it matched that gets | ||
| returned. | ||
| If the vector is too small to hold all the captured sub- | ||
| strings, it is used as far as possible (up to two-thirds of | ||
| its length), and the function returns a value of zero. In | ||
| particular, if the substring offsets are not of interest, | ||
| pcre_exec() may be called with ovector passed as NULL and | ||
| ovecsize as zero. However, if the pattern contains back | ||
| references and the ovector isn't big enough to remember the | ||
| related substrings, PCRE has to get additional memory for | ||
| use during matching. Thus it is usually advisable to supply | ||
| an ovector. | ||
| Note that pcre_info() can be used to find out how many cap- | ||
| turing subpatterns there are in a compiled pattern. The | ||
| smallest size for ovector that will allow for n captured | ||
| substrings in addition to the offsets of the substring | ||
| matched by the whole pattern is (n+1)*3. | ||
| If pcre_exec() fails, it returns a negative number. The fol- | ||
| lowing are defined in the header file: | ||
| PCRE_ERROR_NOMATCH (-1) | ||
| The subject string did not match the pattern. | ||
| PCRE_ERROR_NULL (-2) | ||
| Either code or subject was passed as NULL, or ovector was | ||
| NULL and ovecsize was not zero. | ||
| PCRE_ERROR_BADOPTION (-3) | ||
| An unrecognized bit was set in the options argument. | ||
| PCRE_ERROR_BADMAGIC (-4) | ||
| PCRE stores a 4-byte "magic number" at the start of the com- | ||
| piled code, to catch the case when it is passed a junk | ||
| pointer. This is the error it gives when the magic number | ||
| isn't present. | ||
| PCRE_ERROR_UNKNOWN_NODE (-5) | ||
| While running the pattern match, an unknown item was encoun- | ||
| tered in the compiled pattern. This error could be caused by | ||
| a bug in PCRE or by overwriting of the compiled pattern. | ||
| PCRE_ERROR_NOMEMORY (-6) | ||
| If a pattern contains back references, but the ovector that | ||
| is passed to pcre_exec() is not big enough to remember the | ||
| referenced substrings, PCRE gets a block of memory at the | ||
| start of matching to use for this purpose. If the call via | ||
| pcre_malloc() fails, this error is given. The memory is | ||
| freed at the end of matching. | ||
| EXTRACTING CAPTURED SUBSTRINGS | ||
| Captured substrings can be accessed directly by using the | ||
| offsets returned by pcre_exec() in ovector. For convenience, | ||
| the functions pcre_copy_substring(), pcre_get_substring(), | ||
| and pcre_get_substring_list() are provided for extracting | ||
| captured substrings as new, separate, zero-terminated | ||
| strings. A substring that contains a binary zero is | ||
| correctly extracted and has a further zero added on the end, | ||
| but the result does not, of course, function as a C string. | ||
| The first three arguments are the same for all three func- | ||
| tions: subject is the subject string which has just been | ||
| successfully matched, ovector is a pointer to the vector of | ||
| integer offsets that was passed to pcre_exec(), and | ||
| stringcount is the number of substrings that were captured | ||
| by the match, including the substring that matched the | ||
| entire regular expression. This is the value returned by | ||
| pcre_exec if it is greater than zero. If pcre_exec() | ||
| returned zero, indicating that it ran out of space in ovec- | ||
| tor, the value passed as stringcount should be the size of | ||
| the vector divided by three. | ||
| The functions pcre_copy_substring() and pcre_get_substring() | ||
| extract a single substring, whose number is given as string- | ||
| number. A value of zero extracts the substring that matched | ||
| the entire pattern, while higher values extract the captured | ||
| substrings. For pcre_copy_substring(), the string is placed | ||
| in buffer, whose length is given by buffersize, while for | ||
| pcre_get_substring() a new block of memory is obtained via | ||
| pcre_malloc, and its address is returned via stringptr. The | ||
| yield of the function is the length of the string, not | ||
| including the terminating zero, or one of | ||
| PCRE_ERROR_NOMEMORY (-6) | ||
| The buffer was too small for pcre_copy_substring(), or the | ||
| attempt to get memory failed for pcre_get_substring(). | ||
| PCRE_ERROR_NOSUBSTRING (-7) | ||
| There is no substring whose number is stringnumber. | ||
| The pcre_get_substring_list() function extracts all avail- | ||
| able substrings and builds a list of pointers to them. All | ||
| this is done in a single block of memory which is obtained | ||
| via pcre_malloc. The address of the memory block is returned | ||
| via listptr, which is also the start of the list of string | ||
| pointers. The end of the list is marked by a NULL pointer. | ||
| The yield of the function is zero if all went well, or | ||
| PCRE_ERROR_NOMEMORY (-6) | ||
| if the attempt to get the memory block failed. | ||
| When any of these functions encounter a substring that is | ||
| unset, which can happen when capturing subpattern number n+1 | ||
| matches some part of the subject, but subpattern n has not | ||
| been used at all, they return an empty string. This can be | ||
| distinguished from a genuine zero-length substring by | ||
| inspecting the appropriate offset in ovector, which is nega- | ||
| tive for unset substrings. | ||
| The two convenience functions pcre_free_substring() and | ||
| pcre_free_substring_list() can be used to free the memory | ||
| returned by a previous call of pcre_get_substring() or | ||
| pcre_get_substring_list(), respectively. They do nothing | ||
| more than call the function pointed to by pcre_free, which | ||
| of course could be called directly from a C program. How- | ||
| ever, PCRE is used in some situations where it is linked via | ||
| a special interface to another programming language which | ||
| cannot use pcre_free directly; it is for these cases that | ||
| the functions are provided. | ||
| 2253 | ||
| 2254 | PCRECOMPAT(3) PCRECOMPAT(3) | |
| 2255 | ||
| 2256 | ||
| 2257 | LIMITATIONS | NAME |
| 2258 | There are some size limitations in PCRE but it is hoped that | PCRE - Perl-compatible regular expressions |
| 2259 | they will never in practice be relevant. The maximum length | |
| 2260 | of a compiled pattern is 65539 (sic) bytes. All values in | |
| 2261 | repeating quantifiers must be less than 65536. There max- | DIFFERENCES BETWEEN PCRE AND PERL |
| 2262 | imum number of capturing subpatterns is 65535. There is no | |
| 2263 | limit to the number of non-capturing subpatterns, but the | This document describes the differences in the ways that PCRE and Perl |
| 2264 | maximum depth of nesting of all kinds of parenthesized sub- | handle regular expressions. The differences described here are with |
| 2265 | pattern, including capturing subpatterns, assertions, and | respect to Perl 5.8. |
| 2266 | other types of subpattern, is 200. | |
| 2267 | 1. PCRE has only a subset of Perl's UTF-8 and Unicode support. Details | |
| 2268 | The maximum length of a subject string is the largest posi- | of what it does have are given in the section on UTF-8 support in the |
| 2269 | tive number that an integer variable can hold. However, PCRE | main pcre page. |
| 2270 | uses recursion to handle subpatterns and indefinite repeti- | |
| 2271 | tion. This means that the available stack space may limit | 2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl |
| 2272 | the size of a subject string that can be processed by cer- | permits them, but they do not mean what you might think. For example, |
| 2273 | tain patterns. | (?!a){3} does not assert that the next three characters are not "a". It |
| 2274 | just asserts that the next character is not "a" three times. | |
| 2275 | ||
| 2276 | 3. Capturing subpatterns that occur inside negative lookahead asser- | |
| 2277 | DIFFERENCES FROM PERL | tions are counted, but their entries in the offsets vector are never |
| 2278 | The differences described here are with respect to Perl | set. Perl sets its numerical variables from any such patterns that are |
| 2279 | 5.005. | matched before the assertion fails to match something (thereby succeed- |
| 2280 | ing), but only if the negative lookahead assertion contains just one | |
| 2281 | 1. By default, a whitespace character is any character that | branch. |
| 2282 | the C library function isspace() recognizes, though it is | |
| 2283 | possible to compile PCRE with alternative character type | 4. Though binary zero characters are supported in the subject string, |
| 2284 | tables. Normally isspace() matches space, formfeed, newline, | they are not allowed in a pattern string because it is passed as a nor- |
| 2285 | carriage return, horizontal tab, and vertical tab. Perl 5 no | mal C string, terminated by zero. The escape sequence \0 can be used in |
| 2286 | longer includes vertical tab in its set of whitespace char- | the pattern to represent a binary zero. |
| 2287 | acters. The \v escape that was in the Perl documentation for | |
| 2288 | a long time was never in fact recognized. However, the char- | 5. The following Perl escape sequences are not supported: \l, \u, \L, |
| 2289 | acter itself was treated as whitespace at least up to 5.002. | \U, and \N. In fact these are implemented by Perl's general string-han- |
| 2290 | In 5.004 and 5.005 it does not match \s. | dling and are not part of its pattern matching engine. If any of these |
| 2291 | are encountered by PCRE, an error is generated. | |
| 2292 | 2. PCRE does not allow repeat quantifiers on lookahead | |
| 2293 | assertions. Perl permits them, but they do not mean what you | 6. The Perl escape sequences \p, \P, and \X are supported only if PCRE |
| 2294 | might think. For example, (?!a){3} does not assert that the | is built with Unicode character property support. The properties that |
| 2295 | next three characters are not "a". It just asserts that the | can be tested with \p and \P are limited to the general category prop- |
| 2296 | next character is not "a" three times. | erties such as Lu and Nd, script names such as Greek or Han, and the |
| 2297 | derived properties Any and L&. | |
| 2298 | 3. Capturing subpatterns that occur inside negative looka- | |
| 2299 | head assertions are counted, but their entries in the | 7. PCRE does support the \Q...\E escape for quoting substrings. Charac- |
| 2300 | offsets vector are never set. Perl sets its numerical vari- | ters in between are treated as literals. This is slightly different |
| 2301 | ables from any such patterns that are matched before the | from Perl in that $ and @ are also handled as literals inside the |
| 2302 | assertion fails to match something (thereby succeeding), but | quotes. In Perl, they cause variable interpolation (but of course PCRE |
| 2303 | only if the negative lookahead assertion contains just one | does not have variables). Note the following examples: |
| 2304 | branch. | |
| 2305 | Pattern PCRE matches Perl matches | |
| 2306 | 4. Though binary zero characters are supported in the sub- | |
| 2307 | ject string, they are not allowed in a pattern string | \Qabc$xyz\E abc$xyz abc followed by the |
| 2308 | because it is passed as a normal C string, terminated by | contents of $xyz |
| 2309 | zero. The escape sequence "\0" can be used in the pattern to | \Qabc\$xyz\E abc\$xyz abc\$xyz |
| 2310 | represent a binary zero. | \Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
| 2311 | ||
| 2312 | 5. The following Perl escape sequences are not supported: | The \Q...\E sequence is recognized both inside and outside character |
| 2313 | \l, \u, \L, \U, \E, \Q. In fact these are implemented by | classes. |
| 2314 | Perl's general string-handling and are not part of its pat- | |
| 2315 | tern matching engine. | 8. Fairly obviously, PCRE does not support the (?{code}) and (?p{code}) |
| 2316 | constructions. However, there is support for recursive patterns using | |
| 2317 | 6. The Perl \G assertion is not supported as it is not | the non-Perl items (?R), (?number), and (?P>name). Also, the PCRE |
| 2318 | relevant to single pattern matches. | "callout" feature allows an external function to be called during pat- |
| 2319 | tern matching. See the pcrecallout documentation for details. | |
| 2320 | 7. Fairly obviously, PCRE does not support the (?{code}) and | |
| 2321 | (?p{code}) constructions. However, there is some experimen- | 9. There are some differences that are concerned with the settings of |
| 2322 | tal support for recursive patterns using the non-Perl item | captured strings when part of a pattern is repeated. For example, |
| 2323 | (?R). | matching "aba" against the pattern /^(a(b)?)+$/ in Perl leaves $2 |
| 2324 | unset, but in PCRE it is set to "b". | |
| 2325 | 8. There are at the time of writing some oddities in Perl | |
| 2326 | 5.005_02 concerned with the settings of captured strings | 10. PCRE provides some extensions to the Perl regular expression facil- |
| 2327 | when part of a pattern is repeated. For example, matching | ities: |
| 2328 | "aba" against the pattern /^(a(b)?)+$/ sets $2 to the value | |
| 2329 | "b", but matching "aabbaa" against /^(aa(bb)?)+$/ leaves $2 | (a) Although lookbehind assertions must match fixed length strings, |
| 2330 | unset. However, if the pattern is changed to | each alternative branch of a lookbehind assertion can match a different |
| 2331 | /^(aa(b(b))?)+$/ then $2 (and $3) are set. | length of string. Perl requires them all to have the same length. |
| 2332 | ||
| 2333 | In Perl 5.004 $2 is set in both cases, and that is also true | (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ |
| 2334 | of PCRE. If in the future Perl changes to a consistent state | meta-character matches only at the very end of the string. |
| that is different, PCRE may change to follow. | ||
| 9. Another as yet unresolved discrepancy is that in Perl | ||
| 5.005_02 the pattern /^(a)?(?(1)a|b)+$/ matches the string | ||
| "a", whereas in PCRE it does not. However, in both Perl and | ||
| PCRE /^(a)?a/ matched against "a" leaves $1 unset. | ||
| 10. PCRE provides some extensions to the Perl regular | ||
| expression facilities: | ||
| (a) Although lookbehind assertions must match fixed length | ||
| strings, each alternative branch of a lookbehind assertion | ||
| can match a different length of string. Perl 5.005 requires | ||
| them all to have the same length. | ||
| (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not | ||
| set, the $ meta- character matches only at the very end of | ||
| the string. | ||
| (c) If PCRE_EXTRA is set, a backslash followed by a letter | ||
| with no special meaning is faulted. | ||
| (d) If PCRE_UNGREEDY is set, the greediness of the repeti- | ||
| tion quantifiers is inverted, that is, by default they are | ||
| not greedy, but if followed by a question mark they are. | ||
| (e) PCRE_ANCHORED can be used to force a pattern to be tried | ||
| only at the start of the subject. | ||
| (f) The PCRE_NOTBOL, PCRE_NOTEOL, and PCRE_NOTEMPTY options | ||
| for pcre_exec() have no Perl equivalents. | ||
| (g) The (?R) construct allows for recursive pattern matching | ||
| (Perl 5.6 can do this using the (?p{code}) construct, which | ||
| PCRE cannot of course support.) | ||
| REGULAR EXPRESSION DETAILS | ||
| The syntax and semantics of the regular expressions sup- | ||
| ported by PCRE are described below. Regular expressions are | ||
| also described in the Perl documentation and in a number of | ||
| other books, some of which have copious examples. Jeffrey | ||
| Friedl's "Mastering Regular Expressions", published by | ||
| O'Reilly (ISBN 1-56592-257), covers them in great detail. | ||
| The description here is intended as reference documentation. | ||
| The basic operation of PCRE is on strings of bytes. However, | ||
| there is the beginnings of some support for UTF-8 character | ||
| strings. To use this support you must configure PCRE to | ||
| include it, and then call pcre_compile() with the PCRE_UTF8 | ||
| option. How this affects the pattern matching is described | ||
| in the final section of this document. | ||
| A regular expression is a pattern that is matched against a | ||
| subject string from left to right. Most characters stand for | ||
| themselves in a pattern, and match the corresponding charac- | ||
| ters in the subject. As a trivial example, the pattern | ||
| The quick brown fox | ||
| matches a portion of a subject string that is identical to | ||
| itself. The power of regular expressions comes from the | ||
| ability to include alternatives and repetitions in the pat- | ||
| tern. These are encoded in the pattern by the use of meta- | ||
| characters, which do not stand for themselves but instead | ||
| are interpreted in some special way. | ||
| There are two different sets of meta-characters: those that | ||
| are recognized anywhere in the pattern except within square | ||
| brackets, and those that are recognized in square brackets. | ||
| Outside square brackets, the meta-characters are as follows: | ||
| \ general escape character with several uses | ||
| ^ assert start of subject (or line, in multiline | ||
| mode) | ||
| $ assert end of subject (or line, in multiline mode) | ||
| . match any character except newline (by default) | ||
| [ start character class definition | ||
| | start of alternative branch | ||
| ( start subpattern | ||
| ) end subpattern | ||
| ? extends the meaning of ( | ||
| also 0 or 1 quantifier | ||
| also quantifier minimizer | ||
| * 0 or more quantifier | ||
| + 1 or more quantifier | ||
| { start min/max quantifier | ||
| Part of a pattern that is in square brackets is called a | ||
| "character class". In a character class the only meta- | ||
| characters are: | ||
| \ general escape character | ||
| ^ negate the class, but only if the first character | ||
| - indicates character range | ||
| ] terminates the character class | ||
| 2335 | ||
| 2336 | The following sections describe the use of each of the | (c) If PCRE_EXTRA is set, a backslash followed by a letter with no spe- |
| 2337 | meta-characters. | cial meaning is faulted. |
| 2338 | ||
| 2339 | (d) If PCRE_UNGREEDY is set, the greediness of the repetition quanti- | |
| 2340 | fiers is inverted, that is, by default they are not greedy, but if fol- | |
| 2341 | lowed by a question mark they are. | |
| 2342 | ||
| 2343 | (e) PCRE_ANCHORED can be used at matching time to force a pattern to be | |
| 2344 | tried only at the first matching position in the subject string. | |
| 2345 | ||
| 2346 | (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAP- | |
| 2347 | TURE options for pcre_exec() have no Perl equivalents. | |
| 2348 | ||
| 2349 | (g) The (?R), (?number), and (?P>name) constructs allows for recursive | |
| 2350 | pattern matching (Perl can do this using the (?p{code}) construct, | |
| 2351 | which PCRE cannot support.) | |
| 2352 | ||
| 2353 | (h) PCRE supports named capturing substrings, using the Python syntax. | |
| 2354 | ||
| 2355 | (i) PCRE supports the possessive quantifier "++" syntax, taken from | |
| 2356 | Sun's Java package. | |
| 2357 | ||
| 2358 | (j) The (R) condition, for testing recursion, is a PCRE extension. | |
| 2359 | ||
| 2360 | (k) The callout facility is PCRE-specific. | |
| 2361 | ||
| 2362 | (l) The partial matching facility is PCRE-specific. | |
| 2363 | ||
| 2364 | (m) Patterns compiled by PCRE can be saved and re-used at a later time, | |
| 2365 | even on different hosts that have the other endianness. | |
| 2366 | ||
| 2367 | (n) The alternative matching function (pcre_dfa_exec()) matches in a | |
| 2368 | different way and is not Perl-compatible. | |
| 2369 | ||
| 2370 | Last updated: 24 January 2006 | |
| 2371 | Copyright (c) 1997-2006 University of Cambridge. | |
| 2372 | ------------------------------------------------------------------------------ | |
| 2373 | ||
| 2374 | ||
| 2375 | PCREPATTERN(3) PCREPATTERN(3) | |
| 2376 | ||
| 2377 | ||
| 2378 | NAME | |
| 2379 | PCRE - Perl-compatible regular expressions | |
| 2380 | ||
| 2381 | ||
| 2382 | PCRE REGULAR EXPRESSION DETAILS | |
| 2383 | ||
| 2384 | The syntax and semantics of the regular expressions supported by PCRE | |
| 2385 | are described below. Regular expressions are also described in the Perl | |
| 2386 | documentation and in a number of books, some of which have copious | |
| 2387 | examples. Jeffrey Friedl's "Mastering Regular Expressions", published | |
| 2388 | by O'Reilly, covers regular expressions in great detail. This descrip- | |
| 2389 | tion of PCRE's regular expressions is intended as reference material. | |
| 2390 | ||
| 2391 | The original operation of PCRE was on strings of one-byte characters. | |
| 2392 | However, there is now also support for UTF-8 character strings. To use | |
| 2393 | this, you must build PCRE to include UTF-8 support, and then call | |
| 2394 | pcre_compile() with the PCRE_UTF8 option. How this affects pattern | |
| 2395 | matching is mentioned in several places below. There is also a summary | |
| 2396 | of UTF-8 features in the section on UTF-8 support in the main pcre | |
| 2397 | page. | |
| 2398 | ||
| 2399 | The remainder of this document discusses the patterns that are sup- | |
| 2400 | ported by PCRE when its main matching function, pcre_exec(), is used. | |
| 2401 | From release 6.0, PCRE offers a second matching function, | |
| 2402 | pcre_dfa_exec(), which matches using a different algorithm that is not | |
| 2403 | Perl-compatible. The advantages and disadvantages of the alternative | |
| 2404 | function, and how it differs from the normal function, are discussed in | |
| 2405 | the pcrematching page. | |
| 2406 | ||
| 2407 | A regular expression is a pattern that is matched against a subject | |
| 2408 | string from left to right. Most characters stand for themselves in a | |
| 2409 | pattern, and match the corresponding characters in the subject. As a | |
| 2410 | trivial example, the pattern | |
| 2411 | ||
| 2412 | The quick brown fox | |
| 2413 | ||
| 2414 | matches a portion of a subject string that is identical to itself. When | |
| 2415 | caseless matching is specified (the PCRE_CASELESS option), letters are | |
| 2416 | matched independently of case. In UTF-8 mode, PCRE always understands | |
| 2417 | the concept of case for characters whose values are less than 128, so | |
| 2418 | caseless matching is always possible. For characters with higher val- | |
| 2419 | ues, the concept of case is supported if PCRE is compiled with Unicode | |
| 2420 | property support, but not otherwise. If you want to use caseless | |
| 2421 | matching for characters 128 and above, you must ensure that PCRE is | |
| 2422 | compiled with Unicode property support as well as with UTF-8 support. | |
| 2423 | ||
| 2424 | The power of regular expressions comes from the ability to include | |
| 2425 | alternatives and repetitions in the pattern. These are encoded in the | |
| 2426 | pattern by the use of metacharacters, which do not stand for themselves | |
| 2427 | but instead are interpreted in some special way. | |
| 2428 | ||
| 2429 | There are two different sets of metacharacters: those that are recog- | |
| 2430 | nized anywhere in the pattern except within square brackets, and those | |
| 2431 | that are recognized in square brackets. Outside square brackets, the | |
| 2432 | metacharacters are as follows: | |
| 2433 | ||
| 2434 | \ general escape character with several uses | |
| 2435 | ^ assert start of string (or line, in multiline mode) | |
| 2436 | $ assert end of string (or line, in multiline mode) | |
| 2437 | . match any character except newline (by default) | |
| 2438 | [ start character class definition | |
| 2439 | | start of alternative branch | |
| 2440 | ( start subpattern | |
| 2441 | ) end subpattern | |
| 2442 | ? extends the meaning of ( | |
| 2443 | also 0 or 1 quantifier | |
| 2444 | also quantifier minimizer | |
| 2445 | * 0 or more quantifier | |
| 2446 | + 1 or more quantifier | |
| 2447 | also "possessive quantifier" | |
| 2448 | { start min/max quantifier | |
| 2449 | ||
| 2450 | Part of a pattern that is in square brackets is called a "character | |
| 2451 | class". In a character class the only metacharacters are: | |
| 2452 | ||
| 2453 | \ general escape character | |
| 2454 | ^ negate the class, but only if the first character | |
| 2455 | - indicates character range | |
| 2456 | [ POSIX character class (only if followed by POSIX | |
| 2457 | syntax) | |
| 2458 | ] terminates the character class | |
| 2459 | ||
| 2460 | The following sections describe the use of each of the metacharacters. | |
| 2461 | ||
| 2462 | ||
| 2463 | BACKSLASH | BACKSLASH |
| The backslash character has several uses. Firstly, if it is | ||
| followed by a non-alphameric character, it takes away any | ||
| special meaning that character may have. This use of | ||
| backslash as an escape character applies both inside and | ||
| outside character classes. | ||
| For example, if you want to match a "*" character, you write | ||
| "\*" in the pattern. This applies whether or not the follow- | ||
| ing character would otherwise be interpreted as a meta- | ||
| character, so it is always safe to precede a non-alphameric | ||
| with "\" to specify that it stands for itself. In particu- | ||
| lar, if you want to match a backslash, you write "\\". | ||
| If a pattern is compiled with the PCRE_EXTENDED option, whi- | ||
| tespace in the pattern (other than in a character class) and | ||
| characters between a "#" outside a character class and the | ||
| next newline character are ignored. An escaping backslash | ||
| can be used to include a whitespace or "#" character as part | ||
| of the pattern. | ||
| A second use of backslash provides a way of encoding non- | ||
| printing characters in patterns in a visible manner. There | ||
| is no restriction on the appearance of non-printing charac- | ||
| ters, apart from the binary zero that terminates a pattern, | ||
| but when a pattern is being prepared by text editing, it is | ||
| usually easier to use one of the following escape sequences | ||
| than the binary character it represents: | ||
| \a alarm, that is, the BEL character (hex 07) | ||
| \cx "control-x", where x is any character | ||
| \e escape (hex 1B) | ||
| \f formfeed (hex 0C) | ||
| \n newline (hex 0A) | ||
| \r carriage return (hex 0D) | ||
| \t tab (hex 09) | ||
| \xhh character with hex code hh | ||
| \ddd character with octal code ddd, or backreference | ||
| The precise effect of "\cx" is as follows: if "x" is a lower | ||
| case letter, it is converted to upper case. Then bit 6 of | ||
| the character (hex 40) is inverted. Thus "\cz" becomes hex | ||
| 1A, but "\c{" becomes hex 3B, while "\c;" becomes hex 7B. | ||
| After "\x", up to two hexadecimal digits are read (letters | ||
| can be in upper or lower case). | ||
| After "\0" up to two further octal digits are read. In both | ||
| cases, if there are fewer than two digits, just those that | ||
| are present are used. Thus the sequence "\0\x\07" specifies | ||
| two binary zeros followed by a BEL character. Make sure you | ||
| supply two digits after the initial zero if the character | ||
| that follows is itself an octal digit. | ||
| The handling of a backslash followed by a digit other than 0 | ||
| is complicated. Outside a character class, PCRE reads it | ||
| and any following digits as a decimal number. If the number | ||
| is less than 10, or if there have been at least that many | ||
| previous capturing left parentheses in the expression, the | ||
| entire sequence is taken as a back reference. A description | ||
| of how this works is given later, following the discussion | ||
| of parenthesized subpatterns. | ||
| Inside a character class, or if the decimal number is | ||
| greater than 9 and there have not been that many capturing | ||
| subpatterns, PCRE re-reads up to three octal digits follow- | ||
| ing the backslash, and generates a single byte from the | ||
| least significant 8 bits of the value. Any subsequent digits | ||
| stand for themselves. For example: | ||
| \040 is another way of writing a space | ||
| \40 is the same, provided there are fewer than 40 | ||
| previous capturing subpatterns | ||
| \7 is always a back reference | ||
| \11 might be a back reference, or another way of | ||
| writing a tab | ||
| \011 is always a tab | ||
| \0113 is a tab followed by the character "3" | ||
| \113 is the character with octal code 113 (since there | ||
| can be no more than 99 back references) | ||
| \377 is a byte consisting entirely of 1 bits | ||
| \81 is either a back reference, or a binary zero | ||
| followed by the two characters "8" and "1" | ||
| Note that octal values of 100 or greater must not be intro- | ||
| duced by a leading zero, because no more than three octal | ||
| digits are ever read. | ||
| All the sequences that define a single byte value can be | ||
| used both inside and outside character classes. In addition, | ||
| inside a character class, the sequence "\b" is interpreted | ||
| as the backspace character (hex 08). Outside a character | ||
| class it has a different meaning (see below). | ||
| The third use of backslash is for specifying generic charac- | ||
| ter types: | ||
| \d any decimal digit | ||
| \D any character that is not a decimal digit | ||
| \s any whitespace character | ||
| \S any character that is not a whitespace character | ||
| \w any "word" character | ||
| \W any "non-word" character | ||
| Each pair of escape sequences partitions the complete set of | ||
| characters into two disjoint sets. Any given character | ||
| matches one, and only one, of each pair. | ||
| A "word" character is any letter or digit or the underscore | ||
| character, that is, any character which can be part of a | ||
| Perl "word". The definition of letters and digits is con- | ||
| trolled by PCRE's character tables, and may vary if locale- | ||
| specific matching is taking place (see "Locale support" | ||
| above). For example, in the "fr" (French) locale, some char- | ||
| acter codes greater than 128 are used for accented letters, | ||
| and these are matched by \w. | ||
| These character type sequences can appear both inside and | ||
| outside character classes. They each match one character of | ||
| the appropriate type. If the current matching point is at | ||
| the end of the subject string, all of them fail, since there | ||
| is no character to match. | ||
| The fourth use of backslash is for certain simple asser- | ||
| tions. An assertion specifies a condition that has to be met | ||
| at a particular point in a match, without consuming any | ||
| characters from the subject string. The use of subpatterns | ||
| for more complicated assertions is described below. The | ||
| backslashed assertions are | ||
| \b word boundary | ||
| \B not a word boundary | ||
| \A start of subject (independent of multiline mode) | ||
| \Z end of subject or newline at end (independent of | ||
| multiline mode) | ||
| \z end of subject (independent of multiline mode) | ||
| These assertions may not appear in character classes (but | ||
| note that "\b" has a different meaning, namely the backspace | ||
| character, inside a character class). | ||
| A word boundary is a position in the subject string where | ||
| the current character and the previous character do not both | ||
| match \w or \W (i.e. one matches \w and the other matches | ||
| \W), or the start or end of the string if the first or last | ||
| character matches \w, respectively. | ||
| The \A, \Z, and \z assertions differ from the traditional | ||
| circumflex and dollar (described below) in that they only | ||
| ever match at the very start and end of the subject string, | ||
| whatever options are set. They are not affected by the | ||
| PCRE_NOTBOL or PCRE_NOTEOL options. If the startoffset argu- | ||
| ment of pcre_exec() is non-zero, \A can never match. The | ||
| difference between \Z and \z is that \Z matches before a | ||
| newline that is the last character of the string as well as | ||
| at the end of the string, whereas \z matches only at the | ||
| end. | ||
| 2464 | ||
| 2465 | The backslash character has several uses. Firstly, if it is followed by | |
| 2466 | a non-alphanumeric character, it takes away any special meaning that | |
| 2467 | character may have. This use of backslash as an escape character | |
| 2468 | applies both inside and outside character classes. | |
| 2469 | ||
| 2470 | For example, if you want to match a * character, you write \* in the | |
| 2471 | pattern. This escaping action applies whether or not the following | |
| 2472 | character would otherwise be interpreted as a metacharacter, so it is | |
| 2473 | always safe to precede a non-alphanumeric with backslash to specify | |
| 2474 | that it stands for itself. In particular, if you want to match a back- | |
| 2475 | slash, you write \\. | |
| 2476 | ||
| 2477 | If a pattern is compiled with the PCRE_EXTENDED option, whitespace in | |
| 2478 | the pattern (other than in a character class) and characters between a | |
| 2479 | # outside a character class and the next newline character are ignored. | |
| 2480 | An escaping backslash can be used to include a whitespace or # charac- | |
| 2481 | ter as part of the pattern. | |
| 2482 | ||
| 2483 | If you want to remove the special meaning from a sequence of charac- | |
| 2484 | ters, you can do so by putting them between \Q and \E. This is differ- | |
| 2485 | ent from Perl in that $ and @ are handled as literals in \Q...\E | |
| 2486 | sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- | |
| 2487 | tion. Note the following examples: | |
| 2488 | ||
| 2489 | Pattern PCRE matches Perl matches | |
| 2490 | ||
| 2491 | \Qabc$xyz\E abc$xyz abc followed by the | |
| 2492 | contents of $xyz | |
| 2493 | \Qabc\$xyz\E abc\$xyz abc\$xyz | |
| 2494 | \Qabc\E\$\Qxyz\E abc$xyz abc$xyz | |
| 2495 | ||
| 2496 | The \Q...\E sequence is recognized both inside and outside character | |
| 2497 | classes. | |
| 2498 | ||
| 2499 | Non-printing characters | |
| 2500 | ||
| 2501 | A second use of backslash provides a way of encoding non-printing char- | |
| 2502 | acters in patterns in a visible manner. There is no restriction on the | |
| 2503 | appearance of non-printing characters, apart from the binary zero that | |
| 2504 | terminates a pattern, but when a pattern is being prepared by text | |
| 2505 | editing, it is usually easier to use one of the following escape | |
| 2506 | sequences than the binary character it represents: | |
| 2507 | ||
| 2508 | \a alarm, that is, the BEL character (hex 07) | |
| 2509 | \cx "control-x", where x is any character | |
| 2510 | \e escape (hex 1B) | |
| 2511 | \f formfeed (hex 0C) | |
| 2512 | \n newline (hex 0A) | |
| 2513 | \r carriage return (hex 0D) | |
| 2514 | \t tab (hex 09) | |
| 2515 | \ddd character with octal code ddd, or backreference | |
| 2516 | \xhh character with hex code hh | |
| 2517 | \x{hhh..} character with hex code hhh.. | |
| 2518 | ||
| 2519 | The precise effect of \cx is as follows: if x is a lower case letter, | |
| 2520 | it is converted to upper case. Then bit 6 of the character (hex 40) is | |
| 2521 | inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; | |
| 2522 | becomes hex 7B. | |
| 2523 | ||
| 2524 | After \x, from zero to two hexadecimal digits are read (letters can be | |
| 2525 | in upper or lower case). Any number of hexadecimal digits may appear | |
| 2526 | between \x{ and }, but the value of the character code must be less | |
| 2527 | than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode (that is, | |
| 2528 | the maximum hexadecimal value is 7FFFFFFF). If characters other than | |
| 2529 | hexadecimal digits appear between \x{ and }, or if there is no termi- | |
| 2530 | nating }, this form of escape is not recognized. Instead, the initial | |
| 2531 | \x will be interpreted as a basic hexadecimal escape, with no following | |
| 2532 | digits, giving a character whose value is zero. | |
| 2533 | ||
| 2534 | Characters whose value is less than 256 can be defined by either of the | |
| 2535 | two syntaxes for \x. There is no difference in the way they are han- | |
| 2536 | dled. For example, \xdc is exactly the same as \x{dc}. | |
| 2537 | ||
| 2538 | After \0 up to two further octal digits are read. In both cases, if | |
| 2539 | there are fewer than two digits, just those that are present are used. | |
| 2540 | Thus the sequence \0\x\07 specifies two binary zeros followed by a BEL | |
| 2541 | character (code value 7). Make sure you supply two digits after the | |
| 2542 | initial zero if the pattern character that follows is itself an octal | |
| 2543 | digit. | |
| 2544 | ||
| 2545 | The handling of a backslash followed by a digit other than 0 is compli- | |
| 2546 | cated. Outside a character class, PCRE reads it and any following dig- | |
| 2547 | its as a decimal number. If the number is less than 10, or if there | |
| 2548 | have been at least that many previous capturing left parentheses in the | |
| 2549 | expression, the entire sequence is taken as a back reference. A | |
| 2550 | description of how this works is given later, following the discussion | |
| 2551 | of parenthesized subpatterns. | |
| 2552 | ||
| 2553 | Inside a character class, or if the decimal number is greater than 9 | |
| 2554 | and there have not been that many capturing subpatterns, PCRE re-reads | |
| 2555 | up to three octal digits following the backslash, and generates a sin- | |
| 2556 | gle byte from the least significant 8 bits of the value. Any subsequent | |
| 2557 | digits stand for themselves. For example: | |
| 2558 | ||
| 2559 | \040 is another way of writing a space | |
| 2560 | \40 is the same, provided there are fewer than 40 | |
| 2561 | previous capturing subpatterns | |
| 2562 | \7 is always a back reference | |
| 2563 | \11 might be a back reference, or another way of | |
| 2564 | writing a tab | |
| 2565 | \011 is always a tab | |
| 2566 | \0113 is a tab followed by the character "3" | |
| 2567 | \113 might be a back reference, otherwise the | |
| 2568 | character with octal code 113 | |
| 2569 | \377 might be a back reference, otherwise | |
| 2570 | the byte consisting entirely of 1 bits | |
| 2571 | \81 is either a back reference, or a binary zero | |
| 2572 | followed by the two characters "8" and "1" | |
| 2573 | ||
| 2574 | Note that octal values of 100 or greater must not be introduced by a | |
| 2575 | leading zero, because no more than three octal digits are ever read. | |
| 2576 | ||
| 2577 | All the sequences that define a single byte value or a single UTF-8 | |
| 2578 | character (in UTF-8 mode) can be used both inside and outside character | |
| 2579 | classes. In addition, inside a character class, the sequence \b is | |
| 2580 | interpreted as the backspace character (hex 08), and the sequence \X is | |
| 2581 | interpreted as the character "X". Outside a character class, these | |
| 2582 | sequences have different meanings (see below). | |
| 2583 | ||
| 2584 | Generic character types | |
| 2585 | ||
| 2586 | The third use of backslash is for specifying generic character types. | |
| 2587 | The following are always recognized: | |
| 2588 | ||
| 2589 | \d any decimal digit | |
| 2590 | \D any character that is not a decimal digit | |
| 2591 | \s any whitespace character | |
| 2592 | \S any character that is not a whitespace character | |
| 2593 | \w any "word" character | |
| 2594 | \W any "non-word" character | |
| 2595 | ||
| 2596 | Each pair of escape sequences partitions the complete set of characters | |
| 2597 | into two disjoint sets. Any given character matches one, and only one, | |
| 2598 | of each pair. | |
| 2599 | ||
| 2600 | These character type sequences can appear both inside and outside char- | |
| 2601 | acter classes. They each match one character of the appropriate type. | |
| 2602 | If the current matching point is at the end of the subject string, all | |
| 2603 | of them fail, since there is no character to match. | |
| 2604 | ||
| 2605 | For compatibility with Perl, \s does not match the VT character (code | |
| 2606 | 11). This makes it different from the the POSIX "space" class. The \s | |
| 2607 | characters are HT (9), LF (10), FF (12), CR (13), and space (32). | |
| 2608 | ||
| 2609 | A "word" character is an underscore or any character less than 256 that | |
| 2610 | is a letter or digit. The definition of letters and digits is con- | |
| 2611 | trolled by PCRE's low-valued character tables, and may vary if locale- | |
| 2612 | specific matching is taking place (see "Locale support" in the pcreapi | |
| 2613 | page). For example, in the "fr_FR" (French) locale, some character | |
| 2614 | codes greater than 128 are used for accented letters, and these are | |
| 2615 | matched by \w. | |
| 2616 | ||
| 2617 | In UTF-8 mode, characters with values greater than 128 never match \d, | |
| 2618 | \s, or \w, and always match \D, \S, and \W. This is true even when Uni- | |
| 2619 | code character property support is available. The use of locales with | |
| 2620 | Unicode is discouraged. | |
| 2621 | ||
| 2622 | Unicode character properties | |
| 2623 | ||
| 2624 | When PCRE is built with Unicode character property support, three addi- | |
| 2625 | tional escape sequences to match character properties are available | |
| 2626 | when UTF-8 mode is selected. They are: | |
| 2627 | ||
| 2628 | \p{xx} a character with the xx property | |
| 2629 | \P{xx} a character without the xx property | |
| 2630 | \X an extended Unicode sequence | |
| 2631 | ||
| 2632 | The property names represented by xx above are limited to the Unicode | |
| 2633 | script names, the general category properties, and "Any", which matches | |
| 2634 | any character (including newline). Other properties such as "InMusical- | |
| 2635 | Symbols" are not currently supported by PCRE. Note that \P{Any} does | |
| 2636 | not match any characters, so always causes a match failure. | |
| 2637 | ||
| 2638 | Sets of Unicode characters are defined as belonging to certain scripts. | |
| 2639 | A character from one of these sets can be matched using a script name. | |
| 2640 | For example: | |
| 2641 | ||
| 2642 | \p{Greek} | |
| 2643 | \P{Han} | |
| 2644 | ||
| 2645 | Those that are not part of an identified script are lumped together as | |
| 2646 | "Common". The current list of scripts is: | |
| 2647 | ||
| 2648 | Arabic, Armenian, Bengali, Bopomofo, Braille, Buginese, Buhid, Cana- | |
| 2649 | dian_Aboriginal, Cherokee, Common, Coptic, Cypriot, Cyrillic, Deseret, | |
| 2650 | Devanagari, Ethiopic, Georgian, Glagolitic, Gothic, Greek, Gujarati, | |
| 2651 | Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Inherited, Kannada, | |
| 2652 | Katakana, Kharoshthi, Khmer, Lao, Latin, Limbu, Linear_B, Malayalam, | |
| 2653 | Mongolian, Myanmar, New_Tai_Lue, Ogham, Old_Italic, Old_Persian, Oriya, | |
| 2654 | Osmanya, Runic, Shavian, Sinhala, Syloti_Nagri, Syriac, Tagalog, Tag- | |
| 2655 | banwa, Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, | |
| 2656 | Ugaritic, Yi. | |
| 2657 | ||
| 2658 | Each character has exactly one general category property, specified by | |
| 2659 | a two-letter abbreviation. For compatibility with Perl, negation can be | |
| 2660 | specified by including a circumflex between the opening brace and the | |
| 2661 | property name. For example, \p{^Lu} is the same as \P{Lu}. | |
| 2662 | ||
| 2663 | If only one letter is specified with \p or \P, it includes all the gen- | |
| 2664 | eral category properties that start with that letter. In this case, in | |
| 2665 | the absence of negation, the curly brackets in the escape sequence are | |
| 2666 | optional; these two examples have the same effect: | |
| 2667 | ||
| 2668 | \p{L} | |
| 2669 | \pL | |
| 2670 | ||
| 2671 | The following general category property codes are supported: | |
| 2672 | ||
| 2673 | C Other | |
| 2674 | Cc Control | |
| 2675 | Cf Format | |
| 2676 | Cn Unassigned | |
| 2677 | Co Private use | |
| 2678 | Cs Surrogate | |
| 2679 | ||
| 2680 | L Letter | |
| 2681 | Ll Lower case letter | |
| 2682 | Lm Modifier letter | |
| 2683 | Lo Other letter | |
| 2684 | Lt Title case letter | |
| 2685 | Lu Upper case letter | |
| 2686 | ||
| 2687 | M Mark | |
| 2688 | Mc Spacing mark | |
| 2689 | Me Enclosing mark | |
| 2690 | Mn Non-spacing mark | |
| 2691 | ||
| 2692 | N Number | |
| 2693 | Nd Decimal number | |
| 2694 | Nl Letter number | |
| 2695 | No Other number | |
| 2696 | ||
| 2697 | P Punctuation | |
| 2698 | Pc Connector punctuation | |
| 2699 | Pd Dash punctuation | |
| 2700 | Pe Close punctuation | |
| 2701 | Pf Final punctuation | |
| 2702 | Pi Initial punctuation | |
| 2703 | Po Other punctuation | |
| 2704 | Ps Open punctuation | |
| 2705 | ||
| 2706 | S Symbol | |
| 2707 | Sc Currency symbol | |
| 2708 | Sk Modifier symbol | |
| 2709 | Sm Mathematical symbol | |
| 2710 | So Other symbol | |
| 2711 | ||
| 2712 | Z Separator | |
| 2713 | Zl Line separator | |
| 2714 | Zp Paragraph separator | |
| 2715 | Zs Space separator | |
| 2716 | ||
| 2717 | The special property L& is also supported: it matches a character that | |
| 2718 | has the Lu, Ll, or Lt property, in other words, a letter that is not | |
| 2719 | classified as a modifier or "other". | |
| 2720 | ||
| 2721 | The long synonyms for these properties that Perl supports (such as | |
| 2722 | \p{Letter}) are not supported by PCRE. Nor is is permitted to prefix | |
| 2723 | any of these properties with "Is". | |
| 2724 | ||
| 2725 | No character that is in the Unicode table has the Cn (unassigned) prop- | |
| 2726 | erty. Instead, this property is assumed for any code point that is not | |
| 2727 | in the Unicode table. | |
| 2728 | ||
| 2729 | Specifying caseless matching does not affect these escape sequences. | |
| 2730 | For example, \p{Lu} always matches only upper case letters. | |
| 2731 | ||
| 2732 | The \X escape matches any number of Unicode characters that form an | |
| 2733 | extended Unicode sequence. \X is equivalent to | |
| 2734 | ||
| 2735 | (?>\PM\pM*) | |
| 2736 | ||
| 2737 | That is, it matches a character without the "mark" property, followed | |
| 2738 | by zero or more characters with the "mark" property, and treats the | |
| 2739 | sequence as an atomic group (see below). Characters with the "mark" | |
| 2740 | property are typically accents that affect the preceding character. | |
| 2741 | ||
| 2742 | Matching characters by Unicode property is not fast, because PCRE has | |
| 2743 | to search a structure that contains data for over fifteen thousand | |
| 2744 | characters. That is why the traditional escape sequences such as \d and | |
| 2745 | \w do not use Unicode properties in PCRE. | |
| 2746 | ||
| 2747 | Simple assertions | |
| 2748 | ||
| 2749 | The fourth use of backslash is for certain simple assertions. An asser- | |
| 2750 | tion specifies a condition that has to be met at a particular point in | |
| 2751 | a match, without consuming any characters from the subject string. The | |
| 2752 | use of subpatterns for more complicated assertions is described below. | |
| 2753 | The backslashed assertions are: | |
| 2754 | ||
| 2755 | \b matches at a word boundary | |
| 2756 | \B matches when not at a word boundary | |
| 2757 | \A matches at start of subject | |
| 2758 | \Z matches at end of subject or before newline at end | |
| 2759 | \z matches at end of subject | |
| 2760 | \G matches at first matching position in subject | |
| 2761 | ||
| 2762 | These assertions may not appear in character classes (but note that \b | |
| 2763 | has a different meaning, namely the backspace character, inside a char- | |
| 2764 | acter class). | |
| 2765 | ||
| 2766 | A word boundary is a position in the subject string where the current | |
| 2767 | character and the previous character do not both match \w or \W (i.e. | |
| 2768 | one matches \w and the other matches \W), or the start or end of the | |
| 2769 | string if the first or last character matches \w, respectively. | |
| 2770 | ||
| 2771 | The \A, \Z, and \z assertions differ from the traditional circumflex | |
| 2772 | and dollar (described in the next section) in that they only ever match | |
| 2773 | at the very start and end of the subject string, whatever options are | |
| 2774 | set. Thus, they are independent of multiline mode. These three asser- | |
| 2775 | tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which | |
| 2776 | affect only the behaviour of the circumflex and dollar metacharacters. | |
| 2777 | However, if the startoffset argument of pcre_exec() is non-zero, indi- | |
| 2778 | cating that matching is to start at a point other than the beginning of | |
| 2779 | the subject, \A can never match. The difference between \Z and \z is | |
| 2780 | that \Z matches before a newline that is the last character of the | |
| 2781 | string as well as at the end of the string, whereas \z matches only at | |
| 2782 | the end. | |
| 2783 | ||
| 2784 | The \G assertion is true only when the current matching position is at | |
| 2785 | the start point of the match, as specified by the startoffset argument | |
| 2786 | of pcre_exec(). It differs from \A when the value of startoffset is | |
| 2787 | non-zero. By calling pcre_exec() multiple times with appropriate argu- | |
| 2788 | ments, you can mimic Perl's /g option, and it is in this kind of imple- | |
| 2789 | mentation where \G can be useful. | |
| 2790 | ||
| 2791 | Note, however, that PCRE's interpretation of \G, as the start of the | |
| 2792 | current match, is subtly different from Perl's, which defines it as the | |
| 2793 | end of the previous match. In Perl, these can be different when the | |
| 2794 | previously matched string was empty. Because PCRE does just one match | |
| 2795 | at a time, it cannot reproduce this behaviour. | |
| 2796 | ||
| 2797 | If all the alternatives of a pattern begin with \G, the expression is | |
| 2798 | anchored to the starting match position, and the "anchored" flag is set | |
| 2799 | in the compiled regular expression. | |
| 2800 | ||
| 2801 | ||
| 2802 | CIRCUMFLEX AND DOLLAR | CIRCUMFLEX AND DOLLAR |
| Outside a character class, in the default matching mode, the | ||
| circumflex character is an assertion which is true only if | ||
| the current matching point is at the start of the subject | ||
| string. If the startoffset argument of pcre_exec() is non- | ||
| zero, circumflex can never match. Inside a character class, | ||
| circumflex has an entirely different meaning (see below). | ||
| Circumflex need not be the first character of the pattern if | ||
| a number of alternatives are involved, but it should be the | ||
| first thing in each alternative in which it appears if the | ||
| pattern is ever to match that branch. If all possible alter- | ||
| natives start with a circumflex, that is, if the pattern is | ||
| constrained to match only at the start of the subject, it is | ||
| said to be an "anchored" pattern. (There are also other con- | ||
| structs that can cause a pattern to be anchored.) | ||
| A dollar character is an assertion which is true only if the | ||
| current matching point is at the end of the subject string, | ||
| or immediately before a newline character that is the last | ||
| character in the string (by default). Dollar need not be the | ||
| last character of the pattern if a number of alternatives | ||
| are involved, but it should be the last item in any branch | ||
| in which it appears. Dollar has no special meaning in a | ||
| character class. | ||
| The meaning of dollar can be changed so that it matches only | ||
| at the very end of the string, by setting the | ||
| PCRE_DOLLAR_ENDONLY option at compile or matching time. This | ||
| does not affect the \Z assertion. | ||
| The meanings of the circumflex and dollar characters are | ||
| changed if the PCRE_MULTILINE option is set. When this is | ||
| the case, they match immediately after and immediately | ||
| before an internal "\n" character, respectively, in addition | ||
| to matching at the start and end of the subject string. For | ||
| example, the pattern /^abc$/ matches the subject string | ||
| "def\nabc" in multiline mode, but not otherwise. Conse- | ||
| quently, patterns that are anchored in single line mode | ||
| because all branches start with "^" are not anchored in mul- | ||
| tiline mode, and a match for circumflex is possible when the | ||
| startoffset argument of pcre_exec() is non-zero. The | ||
| PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is | ||
| set. | ||
| Not |