Parent Directory
|
Revision Log
|
Patch
| revision 578 by ph10, Wed Nov 17 17:55:57 2010 UTC | revision 579 by ph10, Wed Nov 24 17:39:25 2010 UTC | |
|---|---|---|
| # | Line 2 | Line 2 |
| 2 | This file contains a concatenation of the PCRE man pages, converted to plain | 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 | 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 | 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. Neither has | synopses of each function in the library have not been included. Neither has |
| 6 | the pcredemo program. There are separate text files for the pcregrep and | the pcredemo program. There are separate text files for the pcregrep and |
| 7 | pcretest commands. | pcretest commands. |
| 8 | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| # | Line 269 REVISION | Line 269 REVISION |
| 269 | Last updated: 13 November 2010 | Last updated: 13 November 2010 |
| 270 | Copyright (c) 1997-2010 University of Cambridge. | Copyright (c) 1997-2010 University of Cambridge. |
| 271 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
| 272 | ||
| 273 | ||
| 274 | PCREBUILD(3) PCREBUILD(3) | PCREBUILD(3) PCREBUILD(3) |
| 275 | ||
| 276 | ||
| # | Line 600 REVISION | Line 600 REVISION |
| 600 | Last updated: 29 September 2009 | Last updated: 29 September 2009 |
| 601 | Copyright (c) 1997-2009 University of Cambridge. | Copyright (c) 1997-2009 University of Cambridge. |
| 602 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
| 603 | ||
| 604 | ||
| 605 | PCREMATCHING(3) PCREMATCHING(3) | PCREMATCHING(3) PCREMATCHING(3) |
| 606 | ||
| 607 | ||
| # | Line 804 REVISION | Line 804 REVISION |
| 804 | Last updated: 17 November 2010 | Last updated: 17 November 2010 |
| 805 | Copyright (c) 1997-2010 University of Cambridge. | Copyright (c) 1997-2010 University of Cambridge. |
| 806 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
| 807 | ||
| 808 | ||
| 809 | PCREAPI(3) PCREAPI(3) | PCREAPI(3) PCREAPI(3) |
| 810 | ||
| 811 | ||
| # | Line 1165 COMPILING A PATTERN | Line 1165 COMPILING A PATTERN |
| 1165 | pcrepattern documentation). For those options that can be different in | pcrepattern documentation). For those options that can be different in |
| 1166 | different parts of the pattern, the contents of the options argument | different parts of the pattern, the contents of the options argument |
| 1167 | specifies their settings at the start of compilation and execution. The | specifies their settings at the start of compilation and execution. The |
| 1168 | PCRE_ANCHORED, PCRE_BSR_xxx, and PCRE_NEWLINE_xxx options can be set at | PCRE_ANCHORED, PCRE_BSR_xxx, PCRE_NEWLINE_xxx, PCRE_NO_UTF8_CHECK, and |
| 1169 | the time of matching as well as at compile time. | PCRE_NO_START_OPT options can be set at the time of matching as well as |
| 1170 | at compile time. | |
| 1171 | ||
| 1172 | If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise, | If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise, |
| 1173 | if compilation of a pattern fails, pcre_compile() returns NULL, and | if compilation of a pattern fails, pcre_compile() returns NULL, and |
| 1174 | sets the variable pointed to by errptr to point to a textual error mes- | sets the variable pointed to by errptr to point to a textual error mes- |
| 1175 | sage. This is a static string that is part of the library. You must not | sage. This is a static string that is part of the library. You must not |
| 1176 | try to free it. The offset from the start of the pattern to the byte | try to free it. The offset from the start of the pattern to the byte |
| 1177 | that was being processed when the error was discovered is placed in the | that was being processed when the error was discovered is placed in the |
| 1178 | variable pointed to by erroffset, which must not be NULL. If it is, an | variable pointed to by erroffset, which must not be NULL. If it is, an |
| 1179 | immediate error is given. Some errors are not detected until checks are | immediate error is given. Some errors are not detected until checks are |
| 1180 | carried out when the whole pattern has been scanned; in this case the | carried out when the whole pattern has been scanned; in this case the |
| 1181 | offset is set to the end of the pattern. | offset is set to the end of the pattern. |
| 1182 | ||
| 1183 | Note that the offset is in bytes, not characters, even in UTF-8 mode. | Note that the offset is in bytes, not characters, even in UTF-8 mode. |
| 1184 | It may point into the middle of a UTF-8 character (for example, when | It may point into the middle of a UTF-8 character (for example, when |
| 1185 | PCRE_ERROR_BADUTF8 is returned for an invalid UTF-8 string). | PCRE_ERROR_BADUTF8 is returned for an invalid UTF-8 string). |
| 1186 | ||
| 1187 | If pcre_compile2() is used instead of pcre_compile(), and the error- | If pcre_compile2() is used instead of pcre_compile(), and the error- |
| 1188 | codeptr argument is not NULL, a non-zero error code number is returned | codeptr argument is not NULL, a non-zero error code number is returned |
| 1189 | via this argument in the event of an error. This is in addition to the | via this argument in the event of an error. This is in addition to the |
| 1190 | textual error message. Error codes and messages are listed below. | textual error message. Error codes and messages are listed below. |
| 1191 | ||
| 1192 | If the final argument, tableptr, is NULL, PCRE uses a default set of | If the final argument, tableptr, is NULL, PCRE uses a default set of |
| 1193 | character tables that are built when PCRE is compiled, using the | character tables that are built when PCRE is compiled, using the |
| 1194 | default C locale. Otherwise, tableptr must be an address that is the | default C locale. Otherwise, tableptr must be an address that is the |
| 1195 | result of a call to pcre_maketables(). This value is stored with the | result of a call to pcre_maketables(). This value is stored with the |
| 1196 | compiled pattern, and used again by pcre_exec(), unless another table | compiled pattern, and used again by pcre_exec(), unless another table |
| 1197 | pointer is passed to it. For more discussion, see the section on locale | pointer is passed to it. For more discussion, see the section on locale |
| 1198 | support below. | support below. |
| 1199 | ||
| 1200 | This code fragment shows a typical straightforward call to pcre_com- | This code fragment shows a typical straightforward call to pcre_com- |
| 1201 | pile(): | pile(): |
| 1202 | ||
| 1203 | pcre *re; | pcre *re; |
| # | Line 1209 COMPILING A PATTERN | Line 1210 COMPILING A PATTERN |
| 1210 | &erroffset, /* for error offset */ | &erroffset, /* for error offset */ |
| 1211 | NULL); /* use default character tables */ | NULL); /* use default character tables */ |
| 1212 | ||
| 1213 | The following names for option bits are defined in the pcre.h header | The following names for option bits are defined in the pcre.h header |
| 1214 | file: | file: |
| 1215 | ||
| 1216 | PCRE_ANCHORED | PCRE_ANCHORED |
| 1217 | ||
| 1218 | If this bit is set, the pattern is forced to be "anchored", that is, it | If this bit is set, the pattern is forced to be "anchored", that is, it |
| 1219 | is constrained to match only at the first matching point in the string | is constrained to match only at the first matching point in the string |
| 1220 | that is being searched (the "subject string"). This effect can also be | that is being searched (the "subject string"). This effect can also be |
| 1221 | achieved by appropriate constructs in the pattern itself, which is the | achieved by appropriate constructs in the pattern itself, which is the |
| 1222 | only way to do it in Perl. | only way to do it in Perl. |
| 1223 | ||
| 1224 | PCRE_AUTO_CALLOUT | PCRE_AUTO_CALLOUT |
| 1225 | ||
| 1226 | If this bit is set, pcre_compile() automatically inserts callout items, | If this bit is set, pcre_compile() automatically inserts callout items, |
| 1227 | all with number 255, before each pattern item. For discussion of the | all with number 255, before each pattern item. For discussion of the |
| 1228 | callout facility, see the pcrecallout documentation. | callout facility, see the pcrecallout documentation. |
| 1229 | ||
| 1230 | PCRE_BSR_ANYCRLF | PCRE_BSR_ANYCRLF |
| 1231 | PCRE_BSR_UNICODE | PCRE_BSR_UNICODE |
| 1232 | ||
| 1233 | These options (which are mutually exclusive) control what the \R escape | These options (which are mutually exclusive) control what the \R escape |
| 1234 | sequence matches. The choice is either to match only CR, LF, or CRLF, | sequence matches. The choice is either to match only CR, LF, or CRLF, |
| 1235 | or to match any Unicode newline sequence. The default is specified when | or to match any Unicode newline sequence. The default is specified when |
| 1236 | PCRE is built. It can be overridden from within the pattern, or by set- | PCRE is built. It can be overridden from within the pattern, or by set- |
| 1237 | ting an option when a compiled pattern is matched. | ting an option when a compiled pattern is matched. |
| 1238 | ||
| 1239 | PCRE_CASELESS | PCRE_CASELESS |
| 1240 | ||
| 1241 | If this bit is set, letters in the pattern match both upper and lower | If this bit is set, letters in the pattern match both upper and lower |
| 1242 | case letters. It is equivalent to Perl's /i option, and it can be | case letters. It is equivalent to Perl's /i option, and it can be |
| 1243 | changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE | changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE |
| 1244 | always understands the concept of case for characters whose values are | always understands the concept of case for characters whose values are |
| 1245 | less than 128, so caseless matching is always possible. For characters | less than 128, so caseless matching is always possible. For characters |
| 1246 | with higher values, the concept of case is supported if PCRE is com- | with higher values, the concept of case is supported if PCRE is com- |
| 1247 | piled with Unicode property support, but not otherwise. If you want to | piled with Unicode property support, but not otherwise. If you want to |
| 1248 | use caseless matching for characters 128 and above, you must ensure | use caseless matching for characters 128 and above, you must ensure |
| 1249 | that PCRE is compiled with Unicode property support as well as with | that PCRE is compiled with Unicode property support as well as with |
| 1250 | UTF-8 support. | UTF-8 support. |
| 1251 | ||
| 1252 | PCRE_DOLLAR_ENDONLY | PCRE_DOLLAR_ENDONLY |
| 1253 | ||
| 1254 | If this bit is set, a dollar metacharacter in the pattern matches only | If this bit is set, a dollar metacharacter in the pattern matches only |
| 1255 | at the end of the subject string. Without this option, a dollar also | at the end of the subject string. Without this option, a dollar also |
| 1256 | matches immediately before a newline at the end of the string (but not | matches immediately before a newline at the end of the string (but not |
| 1257 | before any other newlines). The PCRE_DOLLAR_ENDONLY option is ignored | before any other newlines). The PCRE_DOLLAR_ENDONLY option is ignored |
| 1258 | if PCRE_MULTILINE is set. There is no equivalent to this option in | if PCRE_MULTILINE is set. There is no equivalent to this option in |
| 1259 | Perl, and no way to set it within a pattern. | Perl, and no way to set it within a pattern. |
| 1260 | ||
| 1261 | PCRE_DOTALL | PCRE_DOTALL |
| 1262 | ||
| 1263 | If this bit is set, a dot metacharacter in the pattern matches a char- | If this bit is set, a dot metacharacter in the pattern matches a char- |
| 1264 | acter of any value, including one that indicates a newline. However, it | acter of any value, including one that indicates a newline. However, it |
| 1265 | only ever matches one character, even if newlines are coded as CRLF. | only ever matches one character, even if newlines are coded as CRLF. |
| 1266 | Without this option, a dot does not match when the current position is | Without this option, a dot does not match when the current position is |
| 1267 | at a newline. This option is equivalent to Perl's /s option, and it can | at a newline. This option is equivalent to Perl's /s option, and it can |
| 1268 | be changed within a pattern by a (?s) option setting. A negative class | be changed within a pattern by a (?s) option setting. A negative class |
| 1269 | such as [^a] always matches newline characters, independent of the set- | such as [^a] always matches newline characters, independent of the set- |
| 1270 | ting of this option. | ting of this option. |
| 1271 | ||
| 1272 | PCRE_DUPNAMES | PCRE_DUPNAMES |
| 1273 | ||
| 1274 | If this bit is set, names used to identify capturing subpatterns need | If this bit is set, names used to identify capturing subpatterns need |
| 1275 | not be unique. This can be helpful for certain types of pattern when it | not be unique. This can be helpful for certain types of pattern when it |
| 1276 | is known that only one instance of the named subpattern can ever be | is known that only one instance of the named subpattern can ever be |
| 1277 | matched. There are more details of named subpatterns below; see also | matched. There are more details of named subpatterns below; see also |
| 1278 | the pcrepattern documentation. | the pcrepattern documentation. |
| 1279 | ||
| 1280 | PCRE_EXTENDED | PCRE_EXTENDED |
| 1281 | ||
| 1282 | If this bit is set, whitespace data characters in the pattern are | If this bit is set, whitespace data characters in the pattern are |
| 1283 | totally ignored except when escaped or inside a character class. White- | totally ignored except when escaped or inside a character class. White- |
| 1284 | space does not include the VT character (code 11). In addition, charac- | space does not include the VT character (code 11). In addition, charac- |
| 1285 | ters between an unescaped # outside a character class and the next new- | ters between an unescaped # outside a character class and the next new- |
| 1286 | line, inclusive, are also ignored. This is equivalent to Perl's /x | line, inclusive, are also ignored. This is equivalent to Perl's /x |
| 1287 | option, and it can be changed within a pattern by a (?x) option set- | option, and it can be changed within a pattern by a (?x) option set- |
| 1288 | ting. | ting. |
| 1289 | ||
| 1290 | Which characters are interpreted as newlines is controlled by the | Which characters are interpreted as newlines is controlled by the |
| 1291 | options passed to pcre_compile() or by a special sequence at the start | options passed to pcre_compile() or by a special sequence at the start |
| 1292 | of the pattern, as described in the section entitled "Newline conven- | of the pattern, as described in the section entitled "Newline conven- |
| 1293 | tions" in the pcrepattern documentation. Note that the end of this type | tions" in the pcrepattern documentation. Note that the end of this type |
| 1294 | of comment is a literal newline sequence in the pattern; escape | of comment is a literal newline sequence in the pattern; escape |
| 1295 | sequences that happen to represent a newline do not count. | sequences that happen to represent a newline do not count. |
| 1296 | ||
| 1297 | This option makes it possible to include comments inside complicated | This option makes it possible to include comments inside complicated |
| 1298 | patterns. Note, however, that this applies only to data characters. | patterns. Note, however, that this applies only to data characters. |
| 1299 | Whitespace characters may never appear within special character | Whitespace characters may never appear within special character |
| 1300 | sequences in a pattern, for example within the sequence (?( that intro- | sequences in a pattern, for example within the sequence (?( that intro- |
| 1301 | duces a conditional subpattern. | duces a conditional subpattern. |
| 1302 | ||
| 1303 | PCRE_EXTRA | PCRE_EXTRA |
| 1304 | ||
| 1305 | This option was invented in order to turn on additional functionality | This option was invented in order to turn on additional functionality |
| 1306 | of PCRE that is incompatible with Perl, but it is currently of very | of PCRE that is incompatible with Perl, but it is currently of very |
| 1307 | little use. When set, any backslash in a pattern that is followed by a | little use. When set, any backslash in a pattern that is followed by a |
| 1308 | letter that has no special meaning causes an error, thus reserving | letter that has no special meaning causes an error, thus reserving |
| 1309 | these combinations for future expansion. By default, as in Perl, a | these combinations for future expansion. By default, as in Perl, a |
| 1310 | backslash followed by a letter with no special meaning is treated as a | backslash followed by a letter with no special meaning is treated as a |
| 1311 | literal. (Perl can, however, be persuaded to give an error for this, by | literal. (Perl can, however, be persuaded to give an error for this, by |
| 1312 | running it with the -w option.) There are at present no other features | running it with the -w option.) There are at present no other features |
| 1313 | controlled by this option. It can also be set by a (?X) option setting | controlled by this option. It can also be set by a (?X) option setting |
| 1314 | within a pattern. | within a pattern. |
| 1315 | ||
| 1316 | PCRE_FIRSTLINE | PCRE_FIRSTLINE |
| 1317 | ||
| 1318 | If this option is set, an unanchored pattern is required to match | If this option is set, an unanchored pattern is required to match |
| 1319 | before or at the first newline in the subject string, though the | before or at the first newline in the subject string, though the |
| 1320 | matched text may continue over the newline. | matched text may continue over the newline. |
| 1321 | ||
| 1322 | PCRE_JAVASCRIPT_COMPAT | PCRE_JAVASCRIPT_COMPAT |
| 1323 | ||
| 1324 | If this option is set, PCRE's behaviour is changed in some ways so that | If this option is set, PCRE's behaviour is changed in some ways so that |
| 1325 | it is compatible with JavaScript rather than Perl. The changes are as | it is compatible with JavaScript rather than Perl. The changes are as |
| 1326 | follows: | follows: |
| 1327 | ||
| 1328 | (1) A lone closing square bracket in a pattern causes a compile-time | (1) A lone closing square bracket in a pattern causes a compile-time |
| 1329 | error, because this is illegal in JavaScript (by default it is treated | error, because this is illegal in JavaScript (by default it is treated |
| 1330 | as a data character). Thus, the pattern AB]CD becomes illegal when this | as a data character). Thus, the pattern AB]CD becomes illegal when this |
| 1331 | option is set. | option is set. |
| 1332 | ||
| 1333 | (2) At run time, a back reference to an unset subpattern group matches | (2) At run time, a back reference to an unset subpattern group matches |
| 1334 | an empty string (by default this causes the current matching alterna- | an empty string (by default this causes the current matching alterna- |
| 1335 | tive to fail). A pattern such as (\1)(a) succeeds when this option is | tive to fail). A pattern such as (\1)(a) succeeds when this option is |
| 1336 | set (assuming it can find an "a" in the subject), whereas it fails by | set (assuming it can find an "a" in the subject), whereas it fails by |
| 1337 | default, for Perl compatibility. | default, for Perl compatibility. |
| 1338 | ||
| 1339 | PCRE_MULTILINE | PCRE_MULTILINE |
| 1340 | ||
| 1341 | By default, PCRE treats the subject string as consisting of a single | By default, PCRE treats the subject string as consisting of a single |
| 1342 | line of characters (even if it actually contains newlines). The "start | line of characters (even if it actually contains newlines). The "start |
| 1343 | of line" metacharacter (^) matches only at the start of the string, | of line" metacharacter (^) matches only at the start of the string, |
| 1344 | while the "end of line" metacharacter ($) matches only at the end of | while the "end of line" metacharacter ($) matches only at the end of |
| 1345 | the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY | the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY |
| 1346 | is set). This is the same as Perl. | is set). This is the same as Perl. |
| 1347 | ||
| 1348 | When PCRE_MULTILINE it is set, the "start of line" and "end of line" | When PCRE_MULTILINE it is set, the "start of line" and "end of line" |
| 1349 | constructs match immediately following or immediately before internal | constructs match immediately following or immediately before internal |
| 1350 | newlines in the subject string, respectively, as well as at the very | newlines in the subject string, respectively, as well as at the very |
| 1351 | start and end. This is equivalent to Perl's /m option, and it can be | start and end. This is equivalent to Perl's /m option, and it can be |
| 1352 | changed within a pattern by a (?m) option setting. If there are no new- | changed within a pattern by a (?m) option setting. If there are no new- |
| 1353 | lines in a subject string, or no occurrences of ^ or $ in a pattern, | lines in a subject string, or no occurrences of ^ or $ in a pattern, |
| 1354 | setting PCRE_MULTILINE has no effect. | setting PCRE_MULTILINE has no effect. |
| 1355 | ||
| 1356 | PCRE_NEWLINE_CR | PCRE_NEWLINE_CR |
| # | Line 1358 COMPILING A PATTERN | Line 1359 COMPILING A PATTERN |
| 1359 | PCRE_NEWLINE_ANYCRLF | PCRE_NEWLINE_ANYCRLF |
| 1360 | PCRE_NEWLINE_ANY | PCRE_NEWLINE_ANY |
| 1361 | ||
| 1362 | These options override the default newline definition that was chosen | These options override the default newline definition that was chosen |
| 1363 | when PCRE was built. Setting the first or the second specifies that a | when PCRE was built. Setting the first or the second specifies that a |
| 1364 | newline is indicated by a single character (CR or LF, respectively). | newline is indicated by a single character (CR or LF, respectively). |
| 1365 | Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by the | Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by the |
| 1366 | two-character CRLF sequence. Setting PCRE_NEWLINE_ANYCRLF specifies | two-character CRLF sequence. Setting PCRE_NEWLINE_ANYCRLF specifies |
| 1367 | that any of the three preceding sequences should be recognized. Setting | that any of the three preceding sequences should be recognized. Setting |
| 1368 | PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should be | PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should be |
| 1369 | recognized. The Unicode newline sequences are the three just mentioned, | recognized. The Unicode newline sequences are the three just mentioned, |
| 1370 | plus the single characters VT (vertical tab, U+000B), FF (formfeed, | plus the single characters VT (vertical tab, U+000B), FF (formfeed, |
| 1371 | U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and PS | U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and PS |
| 1372 | (paragraph separator, U+2029). The last two are recognized only in | (paragraph separator, U+2029). The last two are recognized only in |
| 1373 | UTF-8 mode. | UTF-8 mode. |
| 1374 | ||
| 1375 | The newline setting in the options word uses three bits that are | The newline setting in the options word uses three bits that are |
| 1376 | treated as a number, giving eight possibilities. Currently only six are | treated as a number, giving eight possibilities. Currently only six are |
| 1377 | used (default plus the five values above). This means that if you set | used (default plus the five values above). This means that if you set |
| 1378 | more than one newline option, the combination may or may not be sensi- | more than one newline option, the combination may or may not be sensi- |
| 1379 | ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to | ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to |
| 1380 | PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers and | PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers and |
| 1381 | cause an error. | cause an error. |
| 1382 | ||
| 1383 | The only time that a line break in a pattern is specially recognized | The only time that a line break in a pattern is specially recognized |
| 1384 | when compiling is when PCRE_EXTENDED is set. CR and LF are whitespace | when compiling is when PCRE_EXTENDED is set. CR and LF are whitespace |
| 1385 | characters, and so are ignored in this mode. Also, an unescaped # out- | characters, and so are ignored in this mode. Also, an unescaped # out- |
| 1386 | side a character class indicates a comment that lasts until after the | side a character class indicates a comment that lasts until after the |
| 1387 | next line break sequence. In other circumstances, line break sequences | next line break sequence. In other circumstances, line break sequences |
| 1388 | in patterns are treated as literal data. | in patterns are treated as literal data. |
| 1389 | ||
| 1390 | The newline option that is set at compile time becomes the default that | The newline option that is set at compile time becomes the default that |
| # | Line 1392 COMPILING A PATTERN | Line 1393 COMPILING A PATTERN |
| 1393 | PCRE_NO_AUTO_CAPTURE | PCRE_NO_AUTO_CAPTURE |
| 1394 | ||
| 1395 | If this option is set, it disables the use of numbered capturing paren- | If this option is set, it disables the use of numbered capturing paren- |
| 1396 | theses in the pattern. Any opening parenthesis that is not followed by | theses in the pattern. Any opening parenthesis that is not followed by |
| 1397 | ? behaves as if it were followed by ?: but named parentheses can still | ? behaves as if it were followed by ?: but named parentheses can still |
| 1398 | be used for capturing (and they acquire numbers in the usual way). | be used for capturing (and they acquire numbers in the usual way). |
| 1399 | There is no equivalent of this option in Perl. | There is no equivalent of this option in Perl. |
| 1400 | ||
| 1401 | NO_START_OPTIMIZE | |
| 1402 | ||
| 1403 | This is an option that acts at matching time; that is, it is really an | |
| 1404 | option for pcre_exec() or pcre_dfa_exec(). If it is set at compile | |
| 1405 | time, it is remembered with the compiled pattern and assumed at match- | |
| 1406 | ing time. For details see the discussion of PCRE_NO_START_OPTIMIZE | |
| 1407 | below. | |
| 1408 | ||
| 1409 | PCRE_UCP | PCRE_UCP |
| 1410 | ||
| 1411 | This option changes the way PCRE processes \B, \b, \D, \d, \S, \s, \W, | This option changes the way PCRE processes \B, \b, \D, \d, \S, \s, \W, |
| 1412 | \w, and some of the POSIX character classes. By default, only ASCII | \w, and some of the POSIX character classes. By default, only ASCII |
| 1413 | characters are recognized, but if PCRE_UCP is set, Unicode properties | characters are recognized, but if PCRE_UCP is set, Unicode properties |
| 1414 | are used instead to classify characters. More details are given in the | are used instead to classify characters. More details are given in the |
| 1415 | section on generic character types in the pcrepattern page. If you set | section on generic character types in the pcrepattern page. If you set |
| 1416 | PCRE_UCP, matching one of the items it affects takes much longer. The | PCRE_UCP, matching one of the items it affects takes much longer. The |
| 1417 | option is available only if PCRE has been compiled with Unicode prop- | option is available only if PCRE has been compiled with Unicode prop- |
| 1418 | erty support. | erty support. |
| 1419 | ||
| 1420 | PCRE_UNGREEDY | PCRE_UNGREEDY |
| 1421 | ||
| 1422 | This option inverts the "greediness" of the quantifiers so that they | This option inverts the "greediness" of the quantifiers so that they |
| 1423 | are not greedy by default, but become greedy if followed by "?". It is | are not greedy by default, but become greedy if followed by "?". It is |
| 1424 | not compatible with Perl. It can also be set by a (?U) option setting | not compatible with Perl. It can also be set by a (?U) option setting |
| 1425 | within the pattern. | within the pattern. |
| 1426 | ||
| 1427 | PCRE_UTF8 | PCRE_UTF8 |
| 1428 | ||
| 1429 | This option causes PCRE to regard both the pattern and the subject as | This option causes PCRE to regard both the pattern and the subject as |
| 1430 | strings of UTF-8 characters instead of single-byte character strings. | strings of UTF-8 characters instead of single-byte character strings. |
| 1431 | However, it is available only when PCRE is built to include UTF-8 sup- | However, it is available only when PCRE is built to include UTF-8 sup- |
| 1432 | port. If not, the use of this option provokes an error. Details of how | port. If not, the use of this option provokes an error. Details of how |
| 1433 | this option changes the behaviour of PCRE are given in the section on | this option changes the behaviour of PCRE are given in the section on |
| 1434 | UTF-8 support in the main pcre page. | UTF-8 support in the main pcre page. |
| 1435 | ||
| 1436 | PCRE_NO_UTF8_CHECK | PCRE_NO_UTF8_CHECK |
| 1437 | ||
| 1438 | When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is | When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is |
| 1439 | automatically checked. There is a discussion about the validity of | automatically checked. There is a discussion about the validity of |
| 1440 | UTF-8 strings in the main pcre page. If an invalid UTF-8 sequence of | UTF-8 strings in the main pcre page. If an invalid UTF-8 sequence of |
| 1441 | bytes is found, pcre_compile() returns an error. If you already know | bytes is found, pcre_compile() returns an error. If you already know |
| 1442 | that your pattern is valid, and you want to skip this check for perfor- | that your pattern is valid, and you want to skip this check for perfor- |
| 1443 | mance reasons, you can set the PCRE_NO_UTF8_CHECK option. When it is | mance reasons, you can set the PCRE_NO_UTF8_CHECK option. When it is |
| 1444 | set, the effect of passing an invalid UTF-8 string as a pattern is | set, the effect of passing an invalid UTF-8 string as a pattern is |
| 1445 | undefined. It may cause your program to crash. Note that this option | undefined. It may cause your program to crash. Note that this option |
| 1446 | can also be passed to pcre_exec() and pcre_dfa_exec(), to suppress the | can also be passed to pcre_exec() and pcre_dfa_exec(), to suppress the |
| 1447 | UTF-8 validity checking of subject strings. | UTF-8 validity checking of subject strings. |
| 1448 | ||
| 1449 | ||
| 1450 | COMPILATION ERROR CODES | COMPILATION ERROR CODES |
| 1451 | ||
| 1452 | The following table lists the error codes than may be returned by | The following table lists the error codes than may be returned by |
| 1453 | pcre_compile2(), along with the error messages that may be returned by | pcre_compile2(), along with the error messages that may be returned by |
| 1454 | both compiling functions. As PCRE has developed, some error codes have | both compiling functions. As PCRE has developed, some error codes have |
| 1455 | fallen out of use. To avoid confusion, they have not been re-used. | fallen out of use. To avoid confusion, they have not been re-used. |
| 1456 | ||
| 1457 | 0 no error | 0 no error |
| # | Line 1517 COMPILATION ERROR CODES | Line 1526 COMPILATION ERROR CODES |
| 1526 | 66 (*MARK) must have an argument | 66 (*MARK) must have an argument |
| 1527 | 67 this version of PCRE is not compiled with PCRE_UCP support | 67 this version of PCRE is not compiled with PCRE_UCP support |
| 1528 | ||
| 1529 | The numbers 32 and 10000 in errors 48 and 49 are defaults; different | The numbers 32 and 10000 in errors 48 and 49 are defaults; different |
| 1530 | values may be used if the limits were changed when PCRE was built. | values may be used if the limits were changed when PCRE was built. |
| 1531 | ||
| 1532 | ||
| # | Line 1526 STUDYING A PATTERN | Line 1535 STUDYING A PATTERN |
| 1535 | pcre_extra *pcre_study(const pcre *code, int options | pcre_extra *pcre_study(const pcre *code, int options |
| 1536 | const char **errptr); | const char **errptr); |
| 1537 | ||
| 1538 | If a compiled pattern is going to be used several times, it is worth | If a compiled pattern is going to be used several times, it is worth |
| 1539 | spending more time analyzing it in order to speed up the time taken for | spending more time analyzing it in order to speed up the time taken for |
| 1540 | matching. The function pcre_study() takes a pointer to a compiled pat- | matching. The function pcre_study() takes a pointer to a compiled pat- |
| 1541 | tern as its first argument. If studying the pattern produces additional | tern as its first argument. If studying the pattern produces additional |
| 1542 | information that will help speed up matching, pcre_study() returns a | information that will help speed up matching, pcre_study() returns a |
| 1543 | pointer to a pcre_extra block, in which the study_data field points to | pointer to a pcre_extra block, in which the study_data field points to |
| 1544 | the results of the study. | the results of the study. |
| 1545 | ||
| 1546 | The returned value from pcre_study() can be passed directly to | The returned value from pcre_study() can be passed directly to |
| 1547 | pcre_exec() or pcre_dfa_exec(). However, a pcre_extra block also con- | pcre_exec() or pcre_dfa_exec(). However, a pcre_extra block also con- |
| 1548 | tains other fields that can be set by the caller before the block is | tains other fields that can be set by the caller before the block is |
| 1549 | passed; these are described below in the section on matching a pattern. | passed; these are described below in the section on matching a pattern. |
| 1550 | ||
| 1551 | If studying the pattern does not produce any useful information, | If studying the pattern does not produce any useful information, |
| 1552 | pcre_study() returns NULL. In that circumstance, if the calling program | pcre_study() returns NULL. In that circumstance, if the calling program |
| 1553 | wants to pass any of the other fields to pcre_exec() or | wants to pass any of the other fields to pcre_exec() or |
| 1554 | pcre_dfa_exec(), it must set up its own pcre_extra block. | pcre_dfa_exec(), it must set up its own pcre_extra block. |
| 1555 | ||
| 1556 | The second argument of pcre_study() contains option bits. At present, | The second argument of pcre_study() contains option bits. At present, |
| 1557 | no options are defined, and this argument should always be zero. | no options are defined, and this argument should always be zero. |
| 1558 | ||
| 1559 | The third argument for pcre_study() is a pointer for an error message. | The third argument for pcre_study() is a pointer for an error message. |
| 1560 | If studying succeeds (even if no data is returned), the variable it | If studying succeeds (even if no data is returned), the variable it |
| 1561 | points to is set to NULL. Otherwise it is set to point to a textual | points to is set to NULL. Otherwise it is set to point to a textual |
| 1562 | error message. This is a static string that is part of the library. You | error message. This is a static string that is part of the library. You |
| 1563 | must not try to free it. You should test the error pointer for NULL | must not try to free it. You should test the error pointer for NULL |
| 1564 | after calling pcre_study(), to be sure that it has run successfully. | after calling pcre_study(), to be sure that it has run successfully. |
| 1565 | ||
| 1566 | This is a typical call to pcre_study(): | This is a typical call to pcre_study(): |
| # | Line 1565 STUDYING A PATTERN | Line 1574 STUDYING A PATTERN |
| 1574 | Studying a pattern does two things: first, a lower bound for the length | Studying a pattern does two things: first, a lower bound for the length |
| 1575 | of subject string that is needed to match the pattern is computed. This | of subject string that is needed to match the pattern is computed. This |
| 1576 | does not mean that there are any strings of that length that match, but | does not mean that there are any strings of that length that match, but |
| 1577 | it does guarantee that no shorter strings match. The value is used by | it does guarantee that no shorter strings match. The value is used by |
| 1578 | pcre_exec() and pcre_dfa_exec() to avoid wasting time by trying to | pcre_exec() and pcre_dfa_exec() to avoid wasting time by trying to |
| 1579 | match strings that are shorter than the lower bound. You can find out | match strings that are shorter than the lower bound. You can find out |
| 1580 | the value in a calling program via the pcre_fullinfo() function. | the value in a calling program via the pcre_fullinfo() function. |
| 1581 | ||
| 1582 | Studying a pattern is also useful for non-anchored patterns that do not | Studying a pattern is also useful for non-anchored patterns that do not |
| 1583 | have a single fixed starting character. A bitmap of possible starting | have a single fixed starting character. A bitmap of possible starting |
| 1584 | bytes is created. This speeds up finding a position in the subject at | bytes is created. This speeds up finding a position in the subject at |
| 1585 | which to start matching. | which to start matching. |
| 1586 | ||
| 1587 | The two optimizations just described can be disabled by setting the | The two optimizations just described can be disabled by setting the |
| 1588 | PCRE_NO_START_OPTIMIZE option when calling pcre_exec() or | PCRE_NO_START_OPTIMIZE option when calling pcre_exec() or |
| 1589 | pcre_dfa_exec(). You might want to do this if your pattern contains | pcre_dfa_exec(). You might want to do this if your pattern contains |
| 1590 | callouts or (*MARK), and you want to make use of these facilities in | callouts or (*MARK), and you want to make use of these facilities in |
| 1591 | cases where matching fails. See the discussion of PCRE_NO_START_OPTI- | cases where matching fails. See the discussion of PCRE_NO_START_OPTI- |
| 1592 | MIZE below. | MIZE below. |
| 1593 | ||
| 1594 | ||
| 1595 | LOCALE SUPPORT | LOCALE SUPPORT |
| 1596 | ||
| 1597 | PCRE handles caseless matching, and determines whether characters are | PCRE handles caseless matching, and determines whether characters are |
| 1598 | letters, digits, or whatever, by reference to a set of tables, indexed | letters, digits, or whatever, by reference to a set of tables, indexed |
| 1599 | by character value. When running in UTF-8 mode, this applies only to | by character value. When running in UTF-8 mode, this applies only to |
| 1600 | characters with codes less than 128. By default, higher-valued codes | characters with codes less than 128. By default, higher-valued codes |
| 1601 | never match escapes such as \w or \d, but they can be tested with \p if | never match escapes such as \w or \d, but they can be tested with \p if |
| 1602 | PCRE is built with Unicode character property support. Alternatively, | PCRE is built with Unicode character property support. Alternatively, |
| 1603 | the PCRE_UCP option can be set at compile time; this causes \w and | the PCRE_UCP option can be set at compile time; this causes \w and |
| 1604 | friends to use Unicode property support instead of built-in tables. The | friends to use Unicode property support instead of built-in tables. The |
| 1605 | use of locales with Unicode is discouraged. If you are handling charac- | use of locales with Unicode is discouraged. If you are handling charac- |
| 1606 | ters with codes greater than 128, you should either use UTF-8 and Uni- | ters with codes greater than 128, you should either use UTF-8 and Uni- |
| 1607 | code, or use locales, but not try to mix the two. | code, or use locales, but not try to mix the two. |
| 1608 | ||
| 1609 | PCRE contains an internal set of tables that are used when the final | PCRE contains an internal set of tables that are used when the final |
| 1610 | argument of pcre_compile() is NULL. These are sufficient for many | argument of pcre_compile() is NULL. These are sufficient for many |
| 1611 | applications. Normally, the internal tables recognize only ASCII char- | applications. Normally, the internal tables recognize only ASCII char- |
| 1612 | acters. However, when PCRE is built, it is possible to cause the inter- | acters. However, when PCRE is built, it is possible to cause the inter- |
| 1613 | nal tables to be rebuilt in the default "C" locale of the local system, | nal tables to be rebuilt in the default "C" locale of the local system, |
| 1614 | which may cause them to be different. | which may cause them to be different. |
| 1615 | ||
| 1616 | The internal tables can always be overridden by tables supplied by the | The internal tables can always be overridden by tables supplied by the |
| 1617 | application that calls PCRE. These may be created in a different locale | application that calls PCRE. These may be created in a different locale |
| 1618 | from the default. As more and more applications change to using Uni- | from the default. As more and more applications change to using Uni- |
| 1619 | code, the need for this locale support is expected to die away. | code, the need for this locale support is expected to die away. |
| 1620 | ||
| 1621 | External tables are built by calling the pcre_maketables() function, | External tables are built by calling the pcre_maketables() function, |
| 1622 | which has no arguments, in the relevant locale. The result can then be | which has no arguments, in the relevant locale. The result can then be |
| 1623 | passed to pcre_compile() or pcre_exec() as often as necessary. For | passed to pcre_compile() or pcre_exec() as often as necessary. For |
| 1624 | example, to build and use tables that are appropriate for the French | example, to build and use tables that are appropriate for the French |
| 1625 | locale (where accented characters with values greater than 128 are | locale (where accented characters with values greater than 128 are |
| 1626 | treated as letters), the following code could be used: | treated as letters), the following code could be used: |
| 1627 | ||
| 1628 | setlocale(LC_CTYPE, "fr_FR"); | setlocale(LC_CTYPE, "fr_FR"); |
| 1629 | tables = pcre_maketables(); | tables = pcre_maketables(); |
| 1630 | re = pcre_compile(..., tables); | re = pcre_compile(..., tables); |
| 1631 | ||
| 1632 | The locale name "fr_FR" is used on Linux and other Unix-like systems; | The locale name "fr_FR" is used on Linux and other Unix-like systems; |
| 1633 | if you are using Windows, the name for the French locale is "french". | if you are using Windows, the name for the French locale is "french". |
| 1634 | ||
| 1635 | When pcre_maketables() runs, the tables are built in memory that is | When pcre_maketables() runs, the tables are built in memory that is |
| 1636 | obtained via pcre_malloc. It is the caller's responsibility to ensure | obtained via pcre_malloc. It is the caller's responsibility to ensure |
| 1637 | that the memory containing the tables remains available for as long as | that the memory containing the tables remains available for as long as |
| 1638 | it is needed. | it is needed. |
| 1639 | ||
| 1640 | The pointer that is passed to pcre_compile() is saved with the compiled | The pointer that is passed to pcre_compile() is saved with the compiled |
| 1641 | pattern, and the same tables are used via this pointer by pcre_study() | pattern, and the same tables are used via this pointer by pcre_study() |
| 1642 | and normally also by pcre_exec(). Thus, by default, for any single pat- | and normally also by pcre_exec(). Thus, by default, for any single pat- |
| 1643 | tern, compilation, studying and matching all happen in the same locale, | tern, compilation, studying and matching all happen in the same locale, |
| 1644 | but different patterns can be compiled in different locales. | but different patterns can be compiled in different locales. |
| 1645 | ||
| 1646 | It is possible to pass a table pointer or NULL (indicating the use of | It is possible to pass a table pointer or NULL (indicating the use of |
| 1647 | the internal tables) to pcre_exec(). Although not intended for this | the internal tables) to pcre_exec(). Although not intended for this |
| 1648 | purpose, this facility could be used to match a pattern in a different | purpose, this facility could be used to match a pattern in a different |
| 1649 | locale from the one in which it was compiled. Passing table pointers at | locale from the one in which it was compiled. Passing table pointers at |
| 1650 | run time is discussed below in the section on matching a pattern. | run time is discussed below in the section on matching a pattern. |
| 1651 | ||
| # | Line 1646 INFORMATION ABOUT A PATTERN | Line 1655 INFORMATION ABOUT A PATTERN |
| 1655 | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, |
| 1656 | int what, void *where); | int what, void *where); |
| 1657 | ||
| 1658 | The pcre_fullinfo() function returns information about a compiled pat- | The pcre_fullinfo() function returns information about a compiled pat- |
| 1659 | tern. It replaces the obsolete pcre_info() function, which is neverthe- | tern. It replaces the obsolete pcre_info() function, which is neverthe- |
| 1660 | less retained for backwards compability (and is documented below). | less retained for backwards compability (and is documented below). |
| 1661 | ||
| 1662 | The first argument for pcre_fullinfo() is a pointer to the compiled | The first argument for pcre_fullinfo() is a pointer to the compiled |
| 1663 | pattern. The second argument is the result of pcre_study(), or NULL if | pattern. The second argument is the result of pcre_study(), or NULL if |
| 1664 | the pattern was not studied. The third argument specifies which piece | the pattern was not studied. The third argument specifies which piece |
| 1665 | of information is required, and the fourth argument is a pointer to a | of information is required, and the fourth argument is a pointer to a |
| 1666 | variable to receive the data. The yield of the function is zero for | variable to receive the data. The yield of the function is zero for |
| 1667 | success, or one of the following negative numbers: | success, or one of the following negative numbers: |
| 1668 | ||
| 1669 | PCRE_ERROR_NULL the argument code was NULL | PCRE_ERROR_NULL the argument code was NULL |
| # | Line 1662 INFORMATION ABOUT A PATTERN | Line 1671 INFORMATION ABOUT A PATTERN |
| 1671 | PCRE_ERROR_BADMAGIC the "magic number" was not found | PCRE_ERROR_BADMAGIC the "magic number" was not found |
| 1672 | PCRE_ERROR_BADOPTION the value of what was invalid | PCRE_ERROR_BADOPTION the value of what was invalid |
| 1673 | ||
| 1674 | The "magic number" is placed at the start of each compiled pattern as | The "magic number" is placed at the start of each compiled pattern as |
| 1675 | an simple check against passing an arbitrary memory pointer. Here is a | an simple check against passing an arbitrary memory pointer. Here is a |
| 1676 | typical call of pcre_fullinfo(), to obtain the length of the compiled | typical call of pcre_fullinfo(), to obtain the length of the compiled |
| 1677 | pattern: | pattern: |
| 1678 | ||
| 1679 | int rc; | int rc; |
| # | Line 1675 INFORMATION ABOUT A PATTERN | Line 1684 INFORMATION ABOUT A PATTERN |
| 1684 | PCRE_INFO_SIZE, /* what is required */ | PCRE_INFO_SIZE, /* what is required */ |
| 1685 | &length); /* where to put the data */ | &length); /* where to put the data */ |
| 1686 | ||
| 1687 | The possible values for the third argument are defined in pcre.h, and | The possible values for the third argument are defined in pcre.h, and |
| 1688 | are as follows: | are as follows: |
| 1689 | ||
| 1690 | PCRE_INFO_BACKREFMAX | PCRE_INFO_BACKREFMAX |
| 1691 | ||
| 1692 | Return the number of the highest back reference in the pattern. The | Return the number of the highest back reference in the pattern. The |
| 1693 | fourth argument should point to an int variable. Zero is returned if | fourth argument should point to an int variable. Zero is returned if |
| 1694 | there are no back references. | there are no back references. |
| 1695 | ||
| 1696 | PCRE_INFO_CAPTURECOUNT | PCRE_INFO_CAPTURECOUNT |
| 1697 | ||
| 1698 | Return the number of capturing subpatterns in the pattern. The fourth | Return the number of capturing subpatterns in the pattern. The fourth |
| 1699 | argument should point to an int variable. | argument should point to an int variable. |
| 1700 | ||
| 1701 | PCRE_INFO_DEFAULT_TABLES | PCRE_INFO_DEFAULT_TABLES |
| 1702 | ||
| 1703 | Return a pointer to the internal default character tables within PCRE. | Return a pointer to the internal default character tables within PCRE. |
| 1704 | The fourth argument should point to an unsigned char * variable. This | The fourth argument should point to an unsigned char * variable. This |
| 1705 | information call is provided for internal use by the pcre_study() func- | information call is provided for internal use by the pcre_study() func- |
| 1706 | tion. External callers can cause PCRE to use its internal tables by | tion. External callers can cause PCRE to use its internal tables by |
| 1707 | passing a NULL table pointer. | passing a NULL table pointer. |
| 1708 | ||
| 1709 | PCRE_INFO_FIRSTBYTE | PCRE_INFO_FIRSTBYTE |
| 1710 | ||
| 1711 | Return information about the first byte of any matched string, for a | Return information about the first byte of any matched string, for a |
| 1712 | non-anchored pattern. The fourth argument should point to an int vari- | non-anchored pattern. The fourth argument should point to an int vari- |
| 1713 | able. (This option used to be called PCRE_INFO_FIRSTCHAR; the old name | able. (This option used to be called PCRE_INFO_FIRSTCHAR; the old name |
| 1714 | is still recognized for backwards compatibility.) | is still recognized for backwards compatibility.) |
| 1715 | ||
| 1716 | If there is a fixed first byte, for example, from a pattern such as | If there is a fixed first byte, for example, from a pattern such as |
| 1717 | (cat|cow|coyote), its value is returned. Otherwise, if either | (cat|cow|coyote), its value is returned. Otherwise, if either |
| 1718 | ||
| 1719 | (a) the pattern was compiled with the PCRE_MULTILINE option, and every | (a) the pattern was compiled with the PCRE_MULTILINE option, and every |
| 1720 | branch starts with "^", or | branch starts with "^", or |
| 1721 | ||
| 1722 | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not |
| 1723 | set (if it were set, the pattern would be anchored), | set (if it were set, the pattern would be anchored), |
| 1724 | ||
| 1725 | -1 is returned, indicating that the pattern matches only at the start | -1 is returned, indicating that the pattern matches only at the start |
| 1726 | of a subject string or after any newline within the string. Otherwise | of a subject string or after any newline within the string. Otherwise |
| 1727 | -2 is returned. For anchored patterns, -2 is returned. | -2 is returned. For anchored patterns, -2 is returned. |
| 1728 | ||
| 1729 | PCRE_INFO_FIRSTTABLE | PCRE_INFO_FIRSTTABLE |
| 1730 | ||
| 1731 | If the pattern was studied, and this resulted in the construction of a | If the pattern was studied, and this resulted in the construction of a |
| 1732 | 256-bit table indicating a fixed set of bytes for the first byte in any | 256-bit table indicating a fixed set of bytes for the first byte in any |
| 1733 | matching string, a pointer to the table is returned. Otherwise NULL is | matching string, a pointer to the table is returned. Otherwise NULL is |
| 1734 | returned. The fourth argument should point to an unsigned char * vari- | returned. The fourth argument should point to an unsigned char * vari- |
| 1735 | able. | able. |
| 1736 | ||
| 1737 | PCRE_INFO_HASCRORLF | PCRE_INFO_HASCRORLF |
| 1738 | ||
| 1739 | Return 1 if the pattern contains any explicit matches for CR or LF | Return 1 if the pattern contains any explicit matches for CR or LF |
| 1740 | characters, otherwise 0. The fourth argument should point to an int | characters, otherwise 0. The fourth argument should point to an int |
| 1741 | variable. An explicit match is either a literal CR or LF character, or | variable. An explicit match is either a literal CR or LF character, or |
| 1742 | \r or \n. | \r or \n. |
| 1743 | ||
| 1744 | PCRE_INFO_JCHANGED | PCRE_INFO_JCHANGED |
| 1745 | ||
| 1746 | Return 1 if the (?J) or (?-J) option setting is used in the pattern, | Return 1 if the (?J) or (?-J) option setting is used in the pattern, |
| 1747 | otherwise 0. The fourth argument should point to an int variable. (?J) | otherwise 0. The fourth argument should point to an int variable. (?J) |
| 1748 | and (?-J) set and unset the local PCRE_DUPNAMES option, respectively. | and (?-J) set and unset the local PCRE_DUPNAMES option, respectively. |
| 1749 | ||
| 1750 | PCRE_INFO_LASTLITERAL | PCRE_INFO_LASTLITERAL |
| 1751 | ||
| 1752 | Return the value of the rightmost literal byte that must exist in any | Return the value of the rightmost literal byte that must exist in any |
| 1753 | matched string, other than at its start, if such a byte has been | matched string, other than at its start, if such a byte has been |
| 1754 | recorded. The fourth argument should point to an int variable. If there | recorded. The fourth argument should point to an int variable. If there |
| 1755 | is no such byte, -1 is returned. For anchored patterns, a last literal | is no such byte, -1 is returned. For anchored patterns, a last literal |
| 1756 | byte is recorded only if it follows something of variable length. For | byte is recorded only if it follows something of variable length. For |
| 1757 | example, for the pattern /^a\d+z\d+/ the returned value is "z", but for | example, for the pattern /^a\d+z\d+/ the returned value is "z", but for |
| 1758 | /^a\dz\d/ the returned value is -1. | /^a\dz\d/ the returned value is -1. |
| 1759 | ||
| 1760 | PCRE_INFO_MINLENGTH | PCRE_INFO_MINLENGTH |
| 1761 | ||
| 1762 | If the pattern was studied and a minimum length for matching subject | If the pattern was studied and a minimum length for matching subject |
| 1763 | strings was computed, its value is returned. Otherwise the returned | strings was computed, its value is returned. Otherwise the returned |
| 1764 | value is -1. The value is a number of characters, not bytes (this may | value is -1. The value is a number of characters, not bytes (this may |
| 1765 | be relevant in UTF-8 mode). The fourth argument should point to an int | be relevant in UTF-8 mode). The fourth argument should point to an int |
| 1766 | variable. A non-negative value is a lower bound to the length of any | variable. A non-negative value is a lower bound to the length of any |
| 1767 | matching string. There may not be any strings of that length that do | matching string. There may not be any strings of that length that do |
| 1768 | actually match, but every string that does match is at least that long. | actually match, but every string that does match is at least that long. |
| 1769 | ||
| 1770 | PCRE_INFO_NAMECOUNT | PCRE_INFO_NAMECOUNT |
| 1771 | PCRE_INFO_NAMEENTRYSIZE | PCRE_INFO_NAMEENTRYSIZE |
| 1772 | PCRE_INFO_NAMETABLE | PCRE_INFO_NAMETABLE |
| 1773 | ||
| 1774 | PCRE supports the use of named as well as numbered capturing parenthe- | PCRE supports the use of named as well as numbered capturing parenthe- |
| 1775 | ses. The names are just an additional way of identifying the parenthe- | ses. The names are just an additional way of identifying the parenthe- |
| 1776 | ses, which still acquire numbers. Several convenience functions such as | ses, which still acquire numbers. Several convenience functions such as |
| 1777 | pcre_get_named_substring() are provided for extracting captured sub- | pcre_get_named_substring() are provided for extracting captured sub- |
| 1778 | strings by name. It is also possible to extract the data directly, by | strings by name. It is also possible to extract the data directly, by |
| 1779 | first converting the name to a number in order to access the correct | first converting the name to a number in order to access the correct |
| 1780 | pointers in the output vector (described with pcre_exec() below). To do | pointers in the output vector (described with pcre_exec() below). To do |
| 1781 | the conversion, you need to use the name-to-number map, which is | the conversion, you need to use the name-to-number map, which is |
| 1782 | described by these three values. | described by these three values. |
| 1783 | ||
| 1784 | The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT | The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT |
| 1785 | gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size | gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size |
| 1786 | of each entry; both of these return an int value. The entry size | of each entry; both of these return an int value. The entry size |
| 1787 | depends on the length of the longest name. PCRE_INFO_NAMETABLE returns | depends on the length of the longest name. PCRE_INFO_NAMETABLE returns |
| 1788 | a pointer to the first entry of the table (a pointer to char). The | a pointer to the first entry of the table (a pointer to char). The |
| 1789 | first two bytes of each entry are the number of the capturing parenthe- | first two bytes of each entry are the number of the capturing parenthe- |
| 1790 | sis, most significant byte first. The rest of the entry is the corre- | sis, most significant byte first. The rest of the entry is the corre- |
| 1791 | sponding name, zero terminated. | sponding name, zero terminated. |
| 1792 | ||
| 1793 | The names are in alphabetical order. Duplicate names may appear if (?| | The names are in alphabetical order. Duplicate names may appear if (?| |
| 1794 | is used to create multiple groups with the same number, as described in | is used to create multiple groups with the same number, as described in |
| 1795 | the section on duplicate subpattern numbers in the pcrepattern page. | the section on duplicate subpattern numbers in the pcrepattern page. |
| 1796 | Duplicate names for subpatterns with different numbers are permitted | Duplicate names for subpatterns with different numbers are permitted |
| 1797 | only if PCRE_DUPNAMES is set. In all cases of duplicate names, they | only if PCRE_DUPNAMES is set. In all cases of duplicate names, they |
| 1798 | appear in the table in the order in which they were found in the pat- | appear in the table in the order in which they were found in the pat- |
| 1799 | tern. In the absence of (?| this is the order of increasing number; | tern. In the absence of (?| this is the order of increasing number; |
| 1800 | when (?| is used this is not necessarily the case because later subpat- | when (?| is used this is not necessarily the case because later subpat- |
| 1801 | terns may have lower numbers. | terns may have lower numbers. |
| 1802 | ||
| 1803 | As a simple example of the name/number table, consider the following | As a simple example of the name/number table, consider the following |
| 1804 | pattern (assume PCRE_EXTENDED is set, so white space - including new- | pattern (assume PCRE_EXTENDED is set, so white space - including new- |
| 1805 | lines - is ignored): | lines - is ignored): |
| 1806 | ||
| 1807 | (?<date> (?<year>(\d\d)?\d\d) - | (?<date> (?<year>(\d\d)?\d\d) - |
| 1808 | (?<month>\d\d) - (?<day>\d\d) ) | (?<month>\d\d) - (?<day>\d\d) ) |
| 1809 | ||
| 1810 | There are four named subpatterns, so the table has four entries, and | There are four named subpatterns, so the table has four entries, and |
| 1811 | each entry in the table is eight bytes long. The table is as follows, | each entry in the table is eight bytes long. The table is as follows, |
| 1812 | with non-printing bytes shows in hexadecimal, and undefined bytes shown | with non-printing bytes shows in hexadecimal, and undefined bytes shown |
| 1813 | as ??: | as ??: |
| 1814 | ||
| # | Line 1808 INFORMATION ABOUT A PATTERN | Line 1817 INFORMATION ABOUT A PATTERN |
| 1817 | 00 04 m o n t h 00 | 00 04 m o n t h 00 |
| 1818 | 00 02 y e a r 00 ?? | 00 02 y e a r 00 ?? |
| 1819 | ||
| 1820 | When writing code to extract data from named subpatterns using the | When writing code to extract data from named subpatterns using the |
| 1821 | name-to-number map, remember that the length of the entries is likely | name-to-number map, remember that the length of the entries is likely |
| 1822 | to be different for each compiled pattern. | to be different for each compiled pattern. |
| 1823 | ||
| 1824 | PCRE_INFO_OKPARTIAL | PCRE_INFO_OKPARTIAL |
| 1825 | ||
| 1826 | Return 1 if the pattern can be used for partial matching with | Return 1 if the pattern can be used for partial matching with |
| 1827 | pcre_exec(), otherwise 0. The fourth argument should point to an int | pcre_exec(), otherwise 0. The fourth argument should point to an int |
| 1828 | variable. From release 8.00, this always returns 1, because the | variable. From release 8.00, this always returns 1, because the |
| 1829 | restrictions that previously applied to partial matching have been | restrictions that previously applied to partial matching have been |
| 1830 | lifted. The pcrepartial documentation gives details of partial match- | lifted. The pcrepartial documentation gives details of partial match- |
| 1831 | ing. | ing. |
| 1832 | ||
| 1833 | PCRE_INFO_OPTIONS | PCRE_INFO_OPTIONS |
| 1834 | ||
| 1835 | Return a copy of the options with which the pattern was compiled. The | Return a copy of the options with which the pattern was compiled. The |
| 1836 | fourth argument should point to an unsigned long int variable. These | fourth argument should point to an unsigned long int variable. These |
| 1837 | option bits are those specified in the call to pcre_compile(), modified | option bits are those specified in the call to pcre_compile(), modified |
| 1838 | by any top-level option settings at the start of the pattern itself. In | by any top-level option settings at the start of the pattern itself. In |
| 1839 | other words, they are the options that will be in force when matching | other words, they are the options that will be in force when matching |
| 1840 | starts. For example, if the pattern /(?im)abc(?-i)d/ is compiled with | starts. For example, if the pattern /(?im)abc(?-i)d/ is compiled with |
| 1841 | the PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE, | the PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE, |
| 1842 | and PCRE_EXTENDED. | and PCRE_EXTENDED. |
| 1843 | ||
| 1844 | A pattern is automatically anchored by PCRE if all of its top-level | A pattern is automatically anchored by PCRE if all of its top-level |
| 1845 | alternatives begin with one of the following: | alternatives begin with one of the following: |
| 1846 | ||
| 1847 | ^ unless PCRE_MULTILINE is set | ^ unless PCRE_MULTILINE is set |
| # | Line 1846 INFORMATION ABOUT A PATTERN | Line 1855 INFORMATION ABOUT A PATTERN |
| 1855 | ||
| 1856 | PCRE_INFO_SIZE | PCRE_INFO_SIZE |
| 1857 | ||
| 1858 | Return the size of the compiled pattern, that is, the value that was | Return the size of the compiled pattern, that is, the value that was |
| 1859 | passed as the argument to pcre_malloc() when PCRE was getting memory in | passed as the argument to pcre_malloc() when PCRE was getting memory in |
| 1860 | which to place the compiled data. The fourth argument should point to a | which to place the compiled data. The fourth argument should point to a |
| 1861 | size_t variable. | size_t variable. |
| # | Line 1854 INFORMATION ABOUT A PATTERN | Line 1863 INFORMATION ABOUT A PATTERN |
| 1863 | PCRE_INFO_STUDYSIZE | PCRE_INFO_STUDYSIZE |
| 1864 | ||
| 1865 | Return the size of the data block pointed to by the study_data field in | Return the size of the data block pointed to by the study_data field in |
| 1866 | a pcre_extra block. That is, it is the value that was passed to | a pcre_extra block. That is, it is the value that was passed to |
| 1867 | pcre_malloc() when PCRE was getting memory into which to place the data | pcre_malloc() when PCRE was getting memory into which to place the data |
| 1868 | created by pcre_study(). If pcre_extra is NULL, or there is no study | created by pcre_study(). If pcre_extra is NULL, or there is no study |
| 1869 | data, zero is returned. The fourth argument should point to a size_t | data, zero is returned. The fourth argument should point to a size_t |
| 1870 | variable. | variable. |
| 1871 | ||
| 1872 | ||
| # | Line 1865 OBSOLETE INFO FUNCTION | Line 1874 OBSOLETE INFO FUNCTION |
| 1874 | ||
| 1875 | int pcre_info(const pcre *code, int *optptr, int *firstcharptr); | int pcre_info(const pcre *code, int *optptr, int *firstcharptr); |
| 1876 | ||
| 1877 | The pcre_info() function is now obsolete because its interface is too | The pcre_info() function is now obsolete because its interface is too |
| 1878 | restrictive to return all the available data about a compiled pattern. | restrictive to return all the available data about a compiled pattern. |
| 1879 | New programs should use pcre_fullinfo() instead. The yield of | New programs should use pcre_fullinfo() instead. The yield of |
| 1880 | pcre_info() is the number of capturing subpatterns, or one of the fol- | pcre_info() is the number of capturing subpatterns, or one of the fol- |
| 1881 | lowing negative numbers: | lowing negative numbers: |
| 1882 | ||
| 1883 | PCRE_ERROR_NULL the argument code was NULL | PCRE_ERROR_NULL the argument code was NULL |
| 1884 | PCRE_ERROR_BADMAGIC the "magic number" was not found | PCRE_ERROR_BADMAGIC the "magic number" was not found |
| 1885 | ||
| 1886 | If the optptr argument is not NULL, a copy of the options with which | If the optptr argument is not NULL, a copy of the options with which |
| 1887 | the pattern was compiled is placed in the integer it points to (see | the pattern was compiled is placed in the integer it points to (see |
| 1888 | PCRE_INFO_OPTIONS above). | PCRE_INFO_OPTIONS above). |
| 1889 | ||
| 1890 | If the pattern is not anchored and the firstcharptr argument is not | If the pattern is not anchored and the firstcharptr argument is not |
| 1891 | NULL, it is used to pass back information about the first character of | NULL, it is used to pass back information about the first character of |
| 1892 | any matched string (see PCRE_INFO_FIRSTBYTE above). | any matched string (see PCRE_INFO_FIRSTBYTE above). |
| 1893 | ||
| 1894 | ||
| # | Line 1887 REFERENCE COUNTS | Line 1896 REFERENCE COUNTS |
| 1896 | ||
| 1897 | int pcre_refcount(pcre *code, int adjust); | int pcre_refcount(pcre *code, int adjust); |
| 1898 | ||
| 1899 | The pcre_refcount() function is used to maintain a reference count in | The pcre_refcount() function is used to maintain a reference count in |
| 1900 | the data block that contains a compiled pattern. It is provided for the | the data block that contains a compiled pattern. It is provided for the |
| 1901 | benefit of applications that operate in an object-oriented manner, | benefit of applications that operate in an object-oriented manner, |
| 1902 | where different parts of the application may be using the same compiled | where different parts of the application may be using the same compiled |
| 1903 | pattern, but you want to free the block when they are all done. | pattern, but you want to free the block when they are all done. |
| 1904 | ||
| 1905 | When a pattern is compiled, the reference count field is initialized to | When a pattern is compiled, the reference count field is initialized to |
| 1906 | zero. It is changed only by calling this function, whose action is to | zero. It is changed only by calling this function, whose action is to |
| 1907 | add the adjust value (which may be positive or negative) to it. The | add the adjust value (which may be positive or negative) to it. The |
| 1908 | yield of the function is the new value. However, the value of the count | yield of the function is the new value. However, the value of the count |
| 1909 | is constrained to lie between 0 and 65535, inclusive. If the new value | is constrained to lie between 0 and 65535, inclusive. If the new value |
| 1910 | is outside these limits, it is forced to the appropriate limit value. | is outside these limits, it is forced to the appropriate limit value. |
| 1911 | ||
| 1912 | Except when it is zero, the reference count is not correctly preserved | Except when it is zero, the reference count is not correctly preserved |
| 1913 | if a pattern is compiled on one host and then transferred to a host | if a pattern is compiled on one host and then transferred to a host |
| 1914 | whose byte-order is different. (This seems a highly unlikely scenario.) | whose byte-order is different. (This seems a highly unlikely scenario.) |
| 1915 | ||
| 1916 | ||
| # | Line 1911 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 1920 MATCHING A PATTERN: THE TRADITIONAL FUNC |
| 1920 | const char *subject, int length, int startoffset, | const char *subject, int length, int startoffset, |
| 1921 | int options, int *ovector, int ovecsize); | int options, int *ovector, int ovecsize); |
| 1922 | ||
| 1923 | The function pcre_exec() is called to match a subject string against a | The function pcre_exec() is called to match a subject string against a |
| 1924 | compiled pattern, which is passed in the code argument. If the pattern | compiled pattern, which is passed in the code argument. If the pattern |
| 1925 | was studied, the result of the study should be passed in the extra | was studied, the result of the study should be passed in the extra |
| 1926 | argument. This function is the main matching facility of the library, | argument. This function is the main matching facility of the library, |
| 1927 | and it operates in a Perl-like manner. For specialist use there is also | and it operates in a Perl-like manner. For specialist use there is also |
| 1928 | an alternative matching function, which is described below in the sec- | an alternative matching function, which is described below in the sec- |
| 1929 | tion about the pcre_dfa_exec() function. | tion about the pcre_dfa_exec() function. |
| 1930 | ||
| 1931 | In most applications, the pattern will have been compiled (and option- | In most applications, the pattern will have been compiled (and option- |
| 1932 | ally studied) in the same process that calls pcre_exec(). However, it | ally studied) in the same process that calls pcre_exec(). However, it |
| 1933 | is possible to save compiled patterns and study data, and then use them | is possible to save compiled patterns and study data, and then use them |
| 1934 | later in different processes, possibly even on different hosts. For a | later in different processes, possibly even on different hosts. For a |
| 1935 | discussion about this, see the pcreprecompile documentation. | discussion about this, see the pcreprecompile documentation. |
| 1936 | ||
| 1937 | Here is an example of a simple call to pcre_exec(): | Here is an example of a simple call to pcre_exec(): |
| # | Line 1941 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 1950 MATCHING A PATTERN: THE TRADITIONAL FUNC |
| 1950 | ||
| 1951 | Extra data for pcre_exec() | Extra data for pcre_exec() |
| 1952 | ||
| 1953 | If the extra argument is not NULL, it must point to a pcre_extra data | If the extra argument is not NULL, it must point to a pcre_extra data |
| 1954 | block. The pcre_study() function returns such a block (when it doesn't | block. The pcre_study() function returns such a block (when it doesn't |
| 1955 | return NULL), but you can also create one for yourself, and pass addi- | return NULL), but you can also create one for yourself, and pass addi- |
| 1956 | tional information in it. The pcre_extra block contains the following | tional information in it. The pcre_extra block contains the following |
| 1957 | fields (not necessarily in this order): | fields (not necessarily in this order): |
| 1958 | ||
| 1959 | unsigned long int flags; | unsigned long int flags; |
| # | Line 1955 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 1964 MATCHING A PATTERN: THE TRADITIONAL FUNC |
| 1964 | const unsigned char *tables; | const unsigned char *tables; |
| 1965 | unsigned char **mark; | unsigned char **mark; |
| 1966 | ||
| 1967 | The flags field is a bitmap that specifies which of the other fields | The flags field is a bitmap that specifies which of the other fields |
| 1968 | are set. The flag bits are: | are set. The flag bits are: |
| 1969 | ||
| 1970 | PCRE_EXTRA_STUDY_DATA | PCRE_EXTRA_STUDY_DATA |
| # | Line 1965 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 1974 MATCHING A PATTERN: THE TRADITIONAL FUNC |
| 1974 | PCRE_EXTRA_TABLES | PCRE_EXTRA_TABLES |
| 1975 | PCRE_EXTRA_MARK | PCRE_EXTRA_MARK |
| 1976 | ||
| 1977 | Other flag bits should be set to zero. The study_data field is set in | Other flag bits should be set to zero. The study_data field is set in |
| 1978 | the pcre_extra block that is returned by pcre_study(), together with | the pcre_extra block that is returned by pcre_study(), together with |
| 1979 | the appropriate flag bit. You should not set this yourself, but you may | the appropriate flag bit. You should not set this yourself, but you may |
| 1980 | add to the block by setting the other fields and their corresponding | add to the block by setting the other fields and their corresponding |
| 1981 | flag bits. | flag bits. |
| 1982 | ||
| 1983 | The match_limit field provides a means of preventing PCRE from using up | The match_limit field provides a means of preventing PCRE from using up |
| 1984 | a vast amount of resources when running patterns that are not going to | a vast amount of resources when running patterns that are not going to |
| 1985 | match, but which have a very large number of possibilities in their | match, but which have a very large number of possibilities in their |
| 1986 | search trees. The classic example is a pattern that uses nested unlim- | search trees. The classic example is a pattern that uses nested unlim- |
| 1987 | ited repeats. | ited repeats. |
| 1988 | ||
| 1989 | Internally, PCRE uses a function called match() which it calls repeat- | Internally, PCRE uses a function called match() which it calls repeat- |
| 1990 | edly (sometimes recursively). The limit set by match_limit is imposed | edly (sometimes recursively). The limit set by match_limit is imposed |
| 1991 | on the number of times this function is called during a match, which | on the number of times this function is called during a match, which |
| 1992 | has the effect of limiting the amount of backtracking that can take | has the effect of limiting the amount of backtracking that can take |
| 1993 | place. For patterns that are not anchored, the count restarts from zero | place. For patterns that are not anchored, the count restarts from zero |
| 1994 | for each position in the subject string. | for each position in the subject string. |
| 1995 | ||
| 1996 | The default value for the limit can be set when PCRE is built; the | The default value for the limit can be set when PCRE is built; the |
| 1997 | default default is 10 million, which handles all but the most extreme | default default is 10 million, which handles all but the most extreme |
| 1998 | cases. You can override the default by suppling pcre_exec() with a | cases. You can override the default by suppling pcre_exec() with a |
| 1999 | pcre_extra block in which match_limit is set, and | pcre_extra block in which match_limit is set, and |
| 2000 | PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is | PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is |
| 2001 | exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT. | exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT. |
| 2002 | ||
| 2003 | The match_limit_recursion field is similar to match_limit, but instead | The match_limit_recursion field is similar to match_limit, but instead |
| 2004 | of limiting the total number of times that match() is called, it limits | of limiting the total number of times that match() is called, it limits |
| 2005 | the depth of recursion. The recursion depth is a smaller number than | the depth of recursion. The recursion depth is a smaller number than |
| 2006 | the total number of calls, because not all calls to match() are recur- | the total number of calls, because not all calls to match() are recur- |
| 2007 | sive. This limit is of use only if it is set smaller than match_limit. | sive. This limit is of use only if it is set smaller than match_limit. |
| 2008 | ||
| 2009 | Limiting the recursion depth limits the amount of stack that can be | Limiting the recursion depth limits the amount of stack that can be |
| 2010 | used, or, when PCRE has been compiled to use memory on the heap instead | used, or, when PCRE has been compiled to use memory on the heap instead |
| 2011 | of the stack, the amount of heap memory that can be used. | of the stack, the amount of heap memory that can be used. |
| 2012 | ||
| 2013 | The default value for match_limit_recursion can be set when PCRE is | The default value for match_limit_recursion can be set when PCRE is |
| 2014 | built; the default default is the same value as the default for | built; the default default is the same value as the default for |
| 2015 | match_limit. You can override the default by suppling pcre_exec() with | match_limit. You can override the default by suppling pcre_exec() with |
| 2016 | a pcre_extra block in which match_limit_recursion is set, and | a pcre_extra block in which match_limit_recursion is set, and |
| 2017 | PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the | PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the |
| 2018 | limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT. | limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT. |
| 2019 | ||
| 2020 | The callout_data field is used in conjunction with the "callout" fea- | The callout_data field is used in conjunction with the "callout" fea- |
| 2021 | ture, and is described in the pcrecallout documentation. | ture, and is described in the pcrecallout documentation. |
| 2022 | ||
| 2023 | The tables field is used to pass a character tables pointer to | The tables field is used to pass a character tables pointer to |
| 2024 | pcre_exec(); this overrides the value that is stored with the compiled | pcre_exec(); this overrides the value that is stored with the compiled |
| 2025 | pattern. A non-NULL value is stored with the compiled pattern only if | pattern. A non-NULL value is stored with the compiled pattern only if |
| 2026 | custom tables were supplied to pcre_compile() via its tableptr argu- | custom tables were supplied to pcre_compile() via its tableptr argu- |
| 2027 | ment. If NULL is passed to pcre_exec() using this mechanism, it forces | ment. If NULL is passed to pcre_exec() using this mechanism, it forces |
| 2028 | PCRE's internal tables to be used. This facility is helpful when re- | PCRE's internal tables to be used. This facility is helpful when re- |
| 2029 | using patterns that have been saved after compiling with an external | using patterns that have been saved after compiling with an external |
| 2030 | set of tables, because the external tables might be at a different | set of tables, because the external tables might be at a different |
| 2031 | address when pcre_exec() is called. See the pcreprecompile documenta- | address when pcre_exec() is called. See the pcreprecompile documenta- |
| 2032 | tion for a discussion of saving compiled patterns for later use. | tion for a discussion of saving compiled patterns for later use. |
| 2033 | ||
| 2034 | If PCRE_EXTRA_MARK is set in the flags field, the mark field must be | If PCRE_EXTRA_MARK is set in the flags field, the mark field must be |
| 2035 | set to point to a char * variable. If the pattern contains any back- | set to point to a char * variable. If the pattern contains any back- |
| 2036 | tracking control verbs such as (*MARK:NAME), and the execution ends up | tracking control verbs such as (*MARK:NAME), and the execution ends up |
| 2037 | with a name to pass back, a pointer to the name string (zero termi- | with a name to pass back, a pointer to the name string (zero termi- |
| 2038 | nated) is placed in the variable pointed to by the mark field. The | nated) is placed in the variable pointed to by the mark field. The |
| 2039 | names are within the compiled pattern; if you wish to retain such a | names are within the compiled pattern; if you wish to retain such a |
| 2040 | name you must copy it before freeing the memory of a compiled pattern. | name you must copy it before freeing the memory of a compiled pattern. |
| 2041 | If there is no name to pass back, the variable pointed to by the mark | If there is no name to pass back, the variable pointed to by the mark |
| 2042 | field set to NULL. For details of the backtracking control verbs, see | field set to NULL. For details of the backtracking control verbs, see |
| 2043 | the section entitled "Backtracking control" in the pcrepattern documen- | the section entitled "Backtracking control" in the pcrepattern documen- |
| 2044 | tation. | tation. |
| 2045 | ||
| 2046 | Option bits for pcre_exec() | Option bits for pcre_exec() |
| 2047 | ||
| 2048 | The unused bits of the options argument for pcre_exec() must be zero. | The unused bits of the options argument for pcre_exec() must be zero. |
| 2049 | The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx, | The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx, |
| 2050 | PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, | PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, |
| 2051 | PCRE_NO_START_OPTIMIZE, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_SOFT, and | PCRE_NO_START_OPTIMIZE, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_SOFT, and |
| 2052 | PCRE_PARTIAL_HARD. | PCRE_PARTIAL_HARD. |
| 2053 | ||
| 2054 | PCRE_ANCHORED | PCRE_ANCHORED |
| 2055 | ||
| 2056 | The PCRE_ANCHORED option limits pcre_exec() to matching at the first | The PCRE_ANCHORED option limits pcre_exec() to matching at the first |
| 2057 | matching position. If a pattern was compiled with PCRE_ANCHORED, or | matching position. If a pattern was compiled with PCRE_ANCHORED, or |
| 2058 | turned out to be anchored by virtue of its contents, it cannot be made | turned out to be anchored by virtue of its contents, it cannot be made |
| 2059 | unachored at matching time. | unachored at matching time. |
| 2060 | ||
| 2061 | PCRE_BSR_ANYCRLF | PCRE_BSR_ANYCRLF |
| 2062 | PCRE_BSR_UNICODE | PCRE_BSR_UNICODE |
| 2063 | ||
| 2064 | These options (which are mutually exclusive) control what the \R escape | These options (which are mutually exclusive) control what the \R escape |
| 2065 | sequence matches. The choice is either to match only CR, LF, or CRLF, | sequence matches. The choice is either to match only CR, LF, or CRLF, |
| 2066 | or to match any Unicode newline sequence. These options override the | or to match any Unicode newline sequence. These options override the |
| 2067 | choice that was made or defaulted when the pattern was compiled. | choice that was made or defaulted when the pattern was compiled. |
| 2068 | ||
| 2069 | PCRE_NEWLINE_CR | PCRE_NEWLINE_CR |
| # | Line 2063 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 2072 MATCHING A PATTERN: THE TRADITIONAL FUNC |
| 2072 | PCRE_NEWLINE_ANYCRLF | PCRE_NEWLINE_ANYCRLF |
| 2073 | PCRE_NEWLINE_ANY | PCRE_NEWLINE_ANY |
| 2074 | ||
| 2075 | These options override the newline definition that was chosen or | These options override the newline definition that was chosen or |
| 2076 | defaulted when the pattern was compiled. For details, see the descrip- | defaulted when the pattern was compiled. For details, see the descrip- |
| 2077 | tion of pcre_compile() above. During matching, the newline choice | tion of pcre_compile() above. During matching, the newline choice |
| 2078 | affects the behaviour of the dot, circumflex, and dollar metacharac- | affects the behaviour of the dot, circumflex, and dollar metacharac- |
| 2079 | ters. It may also alter the way the match position is advanced after a | ters. It may also alter the way the match position is advanced after a |
| 2080 | match failure for an unanchored pattern. | match failure for an unanchored pattern. |
| 2081 | ||
| 2082 | When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is | When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is |
| 2083 | set, and a match attempt for an unanchored pattern fails when the cur- | set, and a match attempt for an unanchored pattern fails when the cur- |
| 2084 | rent position is at a CRLF sequence, and the pattern contains no | rent position is at a CRLF sequence, and the pattern contains no |
| 2085 | explicit matches for CR or LF characters, the match position is | explicit matches for CR or LF characters, the match position is |
| 2086 | advanced by two characters instead of one, in other words, to after the | advanced by two characters instead of one, in other words, to after the |
| 2087 | CRLF. | CRLF. |
| 2088 | ||
| 2089 | The above rule is a compromise that makes the most common cases work as | The above rule is a compromise that makes the most common cases work as |
| 2090 | expected. For example, if the pattern is .+A (and the PCRE_DOTALL | expected. For example, if the pattern is .+A (and the PCRE_DOTALL |
| 2091 | option is not set), it does not match the string "\r\nA" because, after | option is not set), it does not match the string "\r\nA" because, after |
| 2092 | failing at the start, it skips both the CR and the LF before retrying. | failing at the start, it skips both the CR and the LF before retrying. |
| 2093 | However, the pattern [\r\n]A does match that string, because it con- | However, the pattern [\r\n]A does match that string, because it con- |
| 2094 | tains an explicit CR or LF reference, and so advances only by one char- | tains an explicit CR or LF reference, and so advances only by one char- |
| 2095 | acter after the first failure. | acter after the first failure. |
| 2096 | ||
| 2097 | An explicit match for CR of LF is either a literal appearance of one of | An explicit match for CR of LF is either a literal appearance of one of |
| 2098 | those characters, or one of the \r or \n escape sequences. Implicit | those characters, or one of the \r or \n escape sequences. Implicit |
| 2099 | matches such as [^X] do not count, nor does \s (which includes CR and | matches such as [^X] do not count, nor does \s (which includes CR and |
| 2100 | LF in the characters that it matches). | LF in the characters that it matches). |
| 2101 | ||
| 2102 | Notwithstanding the above, anomalous effects may still occur when CRLF | Notwithstanding the above, anomalous effects may still occur when CRLF |
| 2103 | is a valid newline sequence and explicit \r or \n escapes appear in the | is a valid newline sequence and explicit \r or \n escapes appear in the |
| 2104 | pattern. | pattern. |
| 2105 | ||
| 2106 | PCRE_NOTBOL | PCRE_NOTBOL |
| 2107 | ||
| 2108 | This option specifies that first character of the subject string is not | This option specifies that first character of the subject string is not |
| 2109 | the beginning of a line, so the circumflex metacharacter should not | the beginning of a line, so the circumflex metacharacter should not |
| 2110 | match before it. Setting this without PCRE_MULTILINE (at compile time) | match before it. Setting this without PCRE_MULTILINE (at compile time) |
| 2111 | causes circumflex never to match. This option affects only the behav- | causes circumflex never to match. This option affects only the behav- |
| 2112 | iour of the circumflex metacharacter. It does not affect \A. | iour of the circumflex metacharacter. It does not affect \A. |
| 2113 | ||
| 2114 | PCRE_NOTEOL | PCRE_NOTEOL |
| 2115 | ||
| 2116 | This option specifies that the end of the subject string is not the end | This option specifies that the end of the subject string is not the end |
| 2117 | of a line, so the dollar metacharacter should not match it nor (except | of a line, so the dollar metacharacter should not match it nor (except |
| 2118 | in multiline mode) a newline immediately before it. Setting this with- | in multiline mode) a newline immediately before it. Setting this with- |
| 2119 | out PCRE_MULTILINE (at compile time) causes dollar never to match. This | out PCRE_MULTILINE (at compile time) causes dollar never to match. This |
| 2120 | option affects only the behaviour of the dollar metacharacter. It does | option affects only the behaviour of the dollar metacharacter. It does |
| 2121 | not affect \Z or \z. | not affect \Z or \z. |
| 2122 | ||
| 2123 | PCRE_NOTEMPTY | PCRE_NOTEMPTY |
| 2124 | ||
| 2125 | An empty string is not considered to be a valid match if this option is | An empty string is not considered to be a valid match if this option is |
| 2126 | set. If there are alternatives in the pattern, they are tried. If all | set. If there are alternatives in the pattern, they are tried. If all |
| 2127 | the alternatives match the empty string, the entire match fails. For | the alternatives match the empty string, the entire match fails. For |
| 2128 | example, if the pattern | example, if the pattern |
| 2129 | ||
| 2130 | a?b? | a?b? |
| 2131 | ||
| 2132 | is applied to a string not beginning with "a" or "b", it matches an | is applied to a string not beginning with "a" or "b", it matches an |
| 2133 | empty string at the start of the subject. With PCRE_NOTEMPTY set, this | empty string at the start of the subject. With PCRE_NOTEMPTY set, this |
| 2134 | match is not valid, so PCRE searches further into the string for occur- | match is not valid, so PCRE searches further into the string for occur- |
| 2135 | rences of "a" or "b". | rences of "a" or "b". |
| 2136 | ||
| 2137 | PCRE_NOTEMPTY_ATSTART | PCRE_NOTEMPTY_ATSTART |
| 2138 | ||
| 2139 | This is like PCRE_NOTEMPTY, except that an empty string match that is | This is like PCRE_NOTEMPTY, except that an empty string match that is |
| 2140 | not at the start of the subject is permitted. If the pattern is | not at the start of the subject is permitted. If the pattern is |
| 2141 | anchored, such a match can occur only if the pattern contains \K. | anchored, such a match can occur only if the pattern contains \K. |
| 2142 | ||
| 2143 | Perl has no direct equivalent of PCRE_NOTEMPTY or | Perl has no direct equivalent of PCRE_NOTEMPTY or |
| 2144 | PCRE_NOTEMPTY_ATSTART, but it does make a special case of a pattern | PCRE_NOTEMPTY_ATSTART, but it does make a special case of a pattern |
| 2145 | match of the empty string within its split() function, and when using | match of the empty string within its split() function, and when using |
| 2146 | the /g modifier. It is possible to emulate Perl's behaviour after | the /g modifier. It is possible to emulate Perl's behaviour after |
| 2147 | matching a null string by first trying the match again at the same off- | matching a null string by first trying the match again at the same off- |
| 2148 | set with PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED, and then if that | set with PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED, and then if that |
| 2149 | fails, by advancing the starting offset (see below) and trying an ordi- | fails, by advancing the starting offset (see below) and trying an ordi- |
| 2150 | nary match again. There is some code that demonstrates how to do this | nary match again. There is some code that demonstrates how to do this |
| 2151 | in the pcredemo sample program. In the most general case, you have to | in the pcredemo sample program. In the most general case, you have to |
| 2152 | check to see if the newline convention recognizes CRLF as a newline, | check to see if the newline convention recognizes CRLF as a newline, |
| 2153 | and if so, and the current character is CR followed by LF, advance the | and if so, and the current character is CR followed by LF, advance the |
| 2154 | starting offset by two characters instead of one. | starting offset by two characters instead of one. |
| 2155 | ||
| 2156 | PCRE_NO_START_OPTIMIZE | PCRE_NO_START_OPTIMIZE |
| 2157 | ||
| 2158 | There are a number of optimizations that pcre_exec() uses at the start | There are a number of optimizations that pcre_exec() uses at the start |
| 2159 | of a match, in order to speed up the process. For example, if it is | of a match, in order to speed up the process. For example, if it is |
| 2160 | known that an unanchored match must start with a specific character, it | known that an unanchored match must start with a specific character, it |
| 2161 | searches the subject for that character, and fails immediately if it | searches the subject for that character, and fails immediately if it |
| 2162 | cannot find it, without actually running the main matching function. | cannot find it, without actually running the main matching function. |
| 2163 | This means that a special item such as (*COMMIT) at the start of a pat- | This means that a special item such as (*COMMIT) at the start of a pat- |
| 2164 | tern is not considered until after a suitable starting point for the | tern is not considered until after a suitable starting point for the |
| 2165 | match has been found. When callouts or (*MARK) items are in use, these | match has been found. When callouts or (*MARK) items are in use, these |
| 2166 | "start-up" optimizations can cause them to be skipped if the pattern is | "start-up" optimizations can cause them to be skipped if the pattern is |
| 2167 | never actually used. The start-up optimizations are in effect a pre- | never actually used. The start-up optimizations are in effect a pre- |
| 2168 | scan of the subject that takes place before the pattern is run. | scan of the subject that takes place before the pattern is run. |
| 2169 | ||
| 2170 | The PCRE_NO_START_OPTIMIZE option disables the start-up optimizations, | The PCRE_NO_START_OPTIMIZE option disables the start-up optimizations, |
| 2171 | possibly causing performance to suffer, but ensuring that in cases | possibly causing performance to suffer, but ensuring that in cases |
| 2172 | where the result is "no match", the callouts do occur, and that items | where the result is "no match", the callouts do occur, and that items |
| 2173 | such as (*COMMIT) and (*MARK) are considered at every possible starting | such as (*COMMIT) and (*MARK) are considered at every possible starting |
| 2174 | position in the subject string. Setting PCRE_NO_START_OPTIMIZE can | position in the subject string. If PCRE_NO_START_OPTIMIZE is set at |
| 2175 | change the outcome of a matching operation. Consider the pattern | compile time, it cannot be unset at matching time. |
| 2176 | ||
| 2177 | Setting PCRE_NO_START_OPTIMIZE can change the outcome of a matching | |
| 2178 | operation. Consider the pattern | |
| 2179 | ||
| 2180 | (*COMMIT)ABC | (*COMMIT)ABC |
| 2181 | ||
| # | Line 2862 AUTHOR | Line 2874 AUTHOR |
| 2874 | ||
| 2875 | REVISION | REVISION |
| 2876 | ||
| 2877 | Last updated: 13 November 2010 | Last updated: 21 November 2010 |
| 2878 | Copyright (c) 1997-2010 University of Cambridge. | Copyright (c) 1997-2010 University of Cambridge. |
| 2879 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
| 2880 | ||
| 2881 | ||
| 2882 | PCRECALLOUT(3) PCRECALLOUT(3) | PCRECALLOUT(3) PCRECALLOUT(3) |
| 2883 | ||
| 2884 | ||
| # | Line 2930 MISSING CALLOUTS | Line 2942 MISSING CALLOUTS |
| 2942 | patterns, if it has been scanned far enough. | patterns, if it has been scanned far enough. |
| 2943 | ||
| 2944 | You can disable these optimizations by passing the PCRE_NO_START_OPTI- | You can disable these optimizations by passing the PCRE_NO_START_OPTI- |
| 2945 | MIZE option to pcre_exec() or pcre_dfa_exec(). This slows down the | MIZE option to pcre_compile(), pcre_exec(), or pcre_dfa_exec(), or by |
| 2946 | matching process, but does ensure that callouts such as the example | starting the pattern with (*NO_START_OPT). This slows down the matching |
| 2947 | above are obeyed. | process, but does ensure that callouts such as the example above are |
| 2948 | obeyed. | |
| 2949 | ||
| 2950 | ||
| 2951 | THE CALLOUT INTERFACE | THE CALLOUT INTERFACE |
| 2952 | ||
| 2953 | During matching, when PCRE reaches a callout point, the external func- | During matching, when PCRE reaches a callout point, the external func- |
| 2954 | tion defined by pcre_callout is called (if it is set). This applies to | tion defined by pcre_callout is called (if it is set). This applies to |
| 2955 | both the pcre_exec() and the pcre_dfa_exec() matching functions. The | both the pcre_exec() and the pcre_dfa_exec() matching functions. The |
| 2956 | only argument to the callout function is a pointer to a pcre_callout | only argument to the callout function is a pointer to a pcre_callout |
| 2957 | block. This structure contains the following fields: | block. This structure contains the following fields: |
| 2958 | ||
| 2959 | int version; | int version; |
| # | Line 2956 THE CALLOUT INTERFACE | Line 2969 THE CALLOUT INTERFACE |
| 2969 | int pattern_position; | int pattern_position; |
| 2970 | int next_item_length; | int next_item_length; |
| 2971 | ||
| 2972 | The version field is an integer containing the version number of the | The version field is an integer containing the version number of the |
| 2973 | block format. The initial version was 0; the current version is 1. The | block format. The initial version was 0; the current version is 1. The |
| 2974 | version number will change again in future if additional fields are | version number will change again in future if additional fields are |
| 2975 | added, but the intention is never to remove any of the existing fields. | added, but the intention is never to remove any of the existing fields. |
| 2976 | ||
| 2977 | The callout_number field contains the number of the callout, as com- | The callout_number field contains the number of the callout, as com- |
| 2978 | piled into the pattern (that is, the number after ?C for manual call- | piled into the pattern (that is, the number after ?C for manual call- |
| 2979 | outs, and 255 for automatically generated callouts). | outs, and 255 for automatically generated callouts). |
| 2980 | ||
| 2981 | The offset_vector field is a pointer to the vector of offsets that was | The offset_vector field is a pointer to the vector of offsets that was |
| 2982 | passed by the caller to pcre_exec() or pcre_dfa_exec(). When | passed by the caller to pcre_exec() or pcre_dfa_exec(). When |
| 2983 | pcre_exec() is used, the contents can be inspected in order to extract | pcre_exec() is used, the contents can be inspected in order to extract |
| 2984 | substrings that have been matched so far, in the same way as for | substrings that have been matched so far, in the same way as for |
| 2985 | extracting substrings after a match has completed. For pcre_dfa_exec() | extracting substrings after a match has completed. For pcre_dfa_exec() |
| 2986 | this field is not useful. | this field is not useful. |
| 2987 | ||
| 2988 | The subject and subject_length fields contain copies of the values that | The subject and subject_length fields contain copies of the values that |
| 2989 | were passed to pcre_exec(). | were passed to pcre_exec(). |
| 2990 | ||
| 2991 | The start_match field normally contains the offset within the subject | The start_match field normally contains the offset within the subject |
| 2992 | at which the current match attempt started. However, if the escape | at which the current match attempt started. However, if the escape |
| 2993 | sequence \K has been encountered, this value is changed to reflect the | sequence \K has been encountered, this value is changed to reflect the |
| 2994 | modified starting point. If the pattern is not anchored, the callout | modified starting point. If the pattern is not anchored, the callout |
| 2995 | function may be called several times from the same point in the pattern | function may be called several times from the same point in the pattern |
| 2996 | for different starting points in the subject. | for different starting points in the subject. |
| 2997 | ||
| 2998 | The current_position field contains the offset within the subject of | The current_position field contains the offset within the subject of |
| 2999 | the current match pointer. | the current match pointer. |
| 3000 | ||
| 3001 | When the pcre_exec() function is used, the capture_top field contains | When the pcre_exec() function is used, the capture_top field contains |
| 3002 | one more than the number of the highest numbered captured substring so | one more than the number of the highest numbered captured substring so |
| 3003 | far. If no substrings have been captured, the value of capture_top is | far. If no substrings have been captured, the value of capture_top is |
| 3004 | one. This is always the case when pcre_dfa_exec() is used, because it | one. This is always the case when pcre_dfa_exec() is used, because it |
| 3005 | does not support captured substrings. | does not support captured substrings. |
| 3006 | ||
| 3007 | The capture_last field contains the number of the most recently cap- | The capture_last field contains the number of the most recently cap- |
| 3008 | tured substring. If no substrings have been captured, its value is -1. | tured substring. If no substrings have been captured, its value is -1. |
| 3009 | This is always the case when pcre_dfa_exec() is used. | This is always the case when pcre_dfa_exec() is used. |
| 3010 | ||
| 3011 | The callout_data field contains a value that is passed to pcre_exec() | The callout_data field contains a value that is passed to pcre_exec() |
| 3012 | or pcre_dfa_exec() specifically so that it can be passed back in call- | or pcre_dfa_exec() specifically so that it can be passed back in call- |
| 3013 | outs. It is passed in the pcre_callout field of the pcre_extra data | outs. It is passed in the pcre_callout field of the pcre_extra data |
| 3014 | structure. If no such data was passed, the value of callout_data in a | structure. If no such data was passed, the value of callout_data in a |
| 3015 | pcre_callout block is NULL. There is a description of the pcre_extra | pcre_callout block is NULL. There is a description of the pcre_extra |
| 3016 | structure in the pcreapi documentation. | structure in the pcreapi documentation. |
| 3017 | ||
| 3018 | The pattern_position field is present from version 1 of the pcre_call- | The pattern_position field is present from version 1 of the pcre_call- |
| 3019 | out structure. It contains the offset to the next item to be matched in | out structure. It contains the offset to the next item to be matched in |
| 3020 | the pattern string. | the pattern string. |
| 3021 | ||
| 3022 | The next_item_length field is present from version 1 of the pcre_call- | The next_item_length field is present from version 1 of the pcre_call- |
| 3023 | out structure. It contains the length of the next item to be matched in | out structure. It contains the length of the next item to be matched in |
| 3024 | the pattern string. When the callout immediately precedes an alterna- | the pattern string. When the callout immediately precedes an alterna- |
| 3025 | tion bar, a closing parenthesis, or the end of the pattern, the length | tion bar, a closing parenthesis, or the end of the pattern, the length |
| 3026 | is zero. When the callout precedes an opening parenthesis, the length | is zero. When the callout precedes an opening parenthesis, the length |
| 3027 | is that of the entire subpattern. | is that of the entire subpattern. |
| 3028 | ||
| 3029 | The pattern_position and next_item_length fields are intended to help | The pattern_position and next_item_length fields are intended to help |
| 3030 | in distinguishing between different automatic callouts, which all have | in distinguishing between different automatic callouts, which all have |
| 3031 | the same callout number. However, they are set for all callouts. | the same callout number. However, they are set for all callouts. |
| 3032 | ||
| 3033 | ||
| 3034 | RETURN VALUES | RETURN VALUES |
| 3035 | ||
| 3036 | The external callout function returns an integer to PCRE. If the value | The external callout function returns an integer to PCRE. If the value |
| 3037 | is zero, matching proceeds as normal. If the value is greater than | is zero, matching proceeds as normal. If the value is greater than |
| 3038 | zero, matching fails at the current point, but the testing of other | zero, matching fails at the current point, but the testing of other |
| 3039 | matching possibilities goes ahead, just as if a lookahead assertion had | matching possibilities goes ahead, just as if a lookahead assertion had |
| 3040 | failed. If the value is less than zero, the match is abandoned, and | failed. If the value is less than zero, the match is abandoned, and |
| 3041 | pcre_exec() or pcre_dfa_exec() returns the negative value. | pcre_exec() or pcre_dfa_exec() returns the negative value. |
| 3042 | ||
| 3043 | Negative values should normally be chosen from the set of | Negative values should normally be chosen from the set of |
| 3044 | PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan- | PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan- |
| 3045 | dard "no match" failure. The error number PCRE_ERROR_CALLOUT is | dard "no match" failure. The error number PCRE_ERROR_CALLOUT is |
| 3046 | reserved for use by callout functions; it will never be used by PCRE | reserved for use by callout functions; it will never be used by PCRE |
| 3047 | itself. | itself. |
| 3048 | ||
| 3049 | ||
| # | Line 3043 AUTHOR | Line 3056 AUTHOR |
| 3056 | ||
| 3057 | REVISION | REVISION |
| 3058 | ||
| 3059 | Last updated: 29 September 2009 | Last updated: 21 November 2010 |
| 3060 | Copyright (c) 1997-2009 University of Cambridge. | Copyright (c) 1997-2010 University of Cambridge. |
| 3061 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
| 3062 | ||
| 3063 | ||
| 3064 | PCRECOMPAT(3) PCRECOMPAT(3) | PCRECOMPAT(3) PCRECOMPAT(3) |
| 3065 | ||
| 3066 | ||
| # | Line 3202 REVISION | Line 3215 REVISION |
| 3215 | Last updated: 31 October 2010 | Last updated: 31 October 2010 |
| 3216 | Copyright (c) 1997-2010 University of Cambridge. | Copyright (c) 1997-2010 University of Cambridge. |
| 3217 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
| 3218 | ||
| 3219 | ||
| 3220 | PCREPATTERN(3) PCREPATTERN(3) | PCREPATTERN(3) PCREPATTERN(3) |
| 3221 | ||
| 3222 | ||
| # | Line 3252 PCRE REGULAR EXPRESSION DETAILS | Line 3265 PCRE REGULAR EXPRESSION DETAILS |
| 3265 | character types, instead of recognizing only characters with codes less | character types, instead of recognizing only characters with codes less |
| 3266 | than 128 via a lookup table. | than 128 via a lookup table. |
| 3267 | ||
| 3268 | The remainder of this document discusses the patterns that are sup- | If a pattern starts with (*NO_START_OPT), it has the same effect as |
| 3269 | ported by PCRE when its main matching function, pcre_exec(), is used. | setting the PCRE_NO_START_OPTIMIZE option either at compile or matching |
| 3270 | From release 6.0, PCRE offers a second matching function, | time. There are also some more of these special sequences that are con- |
| 3271 | pcre_dfa_exec(), which matches using a different algorithm that is not | cerned with the handling of newlines; they are described below. |
| 3272 | ||
| 3273 | The remainder of this document discusses the patterns that are sup- | |
| 3274 | ported by PCRE when its main matching function, pcre_exec(), is used. | |
| 3275 | From release 6.0, PCRE offers a second matching function, | |
| 3276 | pcre_dfa_exec(), which matches using a different algorithm that is not | |
| 3277 | Perl-compatible. Some of the features discussed below are not available | Perl-compatible. Some of the features discussed below are not available |
| 3278 | when pcre_dfa_exec() is used. The advantages and disadvantages of the | when pcre_dfa_exec() is used. The advantages and disadvantages of the |
| 3279 | alternative function, and how it differs from the normal function, are | alternative function, and how it differs from the normal function, are |
| 3280 | discussed in the pcrematching page. | discussed in the pcrematching page. |
| 3281 | ||
| 3282 | ||
| 3283 | NEWLINE CONVENTIONS | NEWLINE CONVENTIONS |
| 3284 | ||
| 3285 | PCRE supports five different conventions for indicating line breaks in | PCRE supports five different conventions for indicating line breaks in |
| 3286 | strings: a single CR (carriage return) character, a single LF (line- | strings: a single CR (carriage return) character, a single LF (line- |
| 3287 | feed) character, the two-character sequence CRLF, any of the three pre- | feed) character, the two-character sequence CRLF, any of the three pre- |
| 3288 | ceding, or any Unicode newline sequence. The pcreapi page has further | ceding, or any Unicode newline sequence. The pcreapi page has further |
| 3289 | discussion about newlines, and shows how to set the newline convention | discussion about newlines, and shows how to set the newline convention |
| 3290 | in the options arguments for the compiling and matching functions. | in the options arguments for the compiling and matching functions. |
| 3291 | ||
| 3292 | It is also possible to specify a newline convention by starting a pat- | It is also possible to specify a newline convention by starting a pat- |
| 3293 | tern string with one of the following five sequences: | tern string with one of the following five sequences: |
| 3294 | ||
| 3295 | (*CR) carriage return | (*CR) carriage return |
| # | Line 3280 NEWLINE CONVENTIONS | Line 3298 NEWLINE CONVENTIONS |
| 3298 | (*ANYCRLF) any of the three above | (*ANYCRLF) any of the three above |
| 3299 | (*ANY) all Unicode newline sequences | (*ANY) all Unicode newline sequences |
| 3300 | ||
| 3301 | These override the default and the options given to pcre_compile() or | These override the default and the options given to pcre_compile() or |
| 3302 | pcre_compile2(). For example, on a Unix system where LF is the default | pcre_compile2(). For example, on a Unix system where LF is the default |
| 3303 | newline sequence, the pattern | newline sequence, the pattern |
| 3304 | ||
| 3305 | (*CR)a.b | (*CR)a.b |
| 3306 | ||
| 3307 | changes the convention to CR. That pattern matches "a\nb" because LF is | changes the convention to CR. That pattern matches "a\nb" because LF is |
| 3308 | no longer a newline. Note that these special settings, which are not | no longer a newline. Note that these special settings, which are not |
| 3309 | Perl-compatible, are recognized only at the very start of a pattern, | Perl-compatible, are recognized only at the very start of a pattern, |
| 3310 | and that they must be in upper case. If more than one of them is | and that they must be in upper case. If more than one of them is |
| 3311 | present, the last one is used. | present, the last one is used. |
| 3312 | ||
| 3313 | The newline convention affects the interpretation of the dot metachar- | The newline convention affects the interpretation of the dot metachar- |
| 3314 | acter when PCRE_DOTALL is not set, and also the behaviour of \N. How- | acter when PCRE_DOTALL is not set, and also the behaviour of \N. How- |
| 3315 | ever, it does not affect what the \R escape sequence matches. By | ever, it does not affect what the \R escape sequence matches. By |
| 3316 | default, this is any Unicode newline sequence, for Perl compatibility. | default, this is any Unicode newline sequence, for Perl compatibility. |
| 3317 | However, this can be changed; see the description of \R in the section | However, this can be changed; see the description of \R in the section |
| 3318 | entitled "Newline sequences" below. A change of \R setting can be com- | entitled "Newline sequences" below. A change of \R setting can be com- |
| 3319 | bined with a change of newline convention. | bined with a change of newline convention. |
| 3320 | ||
| 3321 | ||
| 3322 | CHARACTERS AND METACHARACTERS | CHARACTERS AND METACHARACTERS |
| 3323 | ||
| 3324 | A regular expression is a pattern that is matched against a subject | A regular expression is a pattern that is matched against a subject |
| 3325 | string from left to right. Most characters stand for themselves in a | string from left to right. Most characters stand for themselves in a |
| 3326 | pattern, and match the corresponding characters in the subject. As a | pattern, and match the corresponding characters in the subject. As a |
| 3327 | trivial example, the pattern | trivial example, the pattern |
| 3328 | ||
| 3329 | The quick brown fox | The quick brown fox |
| 3330 | ||
| 3331 | matches a portion of a subject string that is identical to itself. When | matches a portion of a subject string that is identical to itself. When |
| 3332 | caseless matching is specified (the PCRE_CASELESS option), letters are | caseless matching is specified (the PCRE_CASELESS option), letters are |
| 3333 | matched independently of case. In UTF-8 mode, PCRE always understands | matched independently of case. In UTF-8 mode, PCRE always understands |
| 3334 | the concept of case for characters whose values are less than 128, so | the concept of case for characters whose values are less than 128, so |
| 3335 | caseless matching is always possible. For characters with higher val- | caseless matching is always possible. For characters with higher val- |
| 3336 | ues, the concept of case is supported if PCRE is compiled with Unicode | ues, the concept of case is supported if PCRE is compiled with Unicode |
| 3337 | property support, but not otherwise. If you want to use caseless | property support, but not otherwise. If you want to use caseless |
| 3338 | matching for characters 128 and above, you must ensure that PCRE is | matching for characters 128 and above, you must ensure that PCRE is |
| 3339 | compiled with Unicode property support as well as with UTF-8 support. | compiled with Unicode property support as well as with UTF-8 support. |
| 3340 | ||
| 3341 | The power of regular expressions comes from the ability to include | The power of regular expressions comes from the ability to include |
| 3342 | alternatives and repetitions in the pattern. These are encoded in the | alternatives and repetitions in the pattern. These are encoded in the |
| 3343 | pattern by the use of metacharacters, which do not stand for themselves | pattern by the use of metacharacters, which do not stand for themselves |
| 3344 | but instead are interpreted in some special way. | but instead are interpreted in some special way. |
| 3345 | ||
| 3346 | There are two different sets of metacharacters: those that are recog- | There are two different sets of metacharacters: those that are recog- |
| 3347 | nized anywhere in the pattern except within square brackets, and those | nized anywhere in the pattern except within square brackets, and those |
| 3348 | that are recognized within square brackets. Outside square brackets, | that are recognized within square brackets. Outside square brackets, |
| 3349 | the metacharacters are as follows: | the metacharacters are as follows: |
| 3350 | ||
| 3351 | \ general escape character with several uses | \ general escape character with several uses |
| # | Line 3346 CHARACTERS AND METACHARACTERS | Line 3364 CHARACTERS AND METACHARACTERS |
| 3364 | also "possessive quantifier" | also "possessive quantifier" |
| 3365 | { start min/max quantifier | { start min/max quantifier |
| 3366 | ||
| 3367 | Part of a pattern that is in square brackets is called a "character | Part of a pattern that is in square brackets is called a "character |
| 3368 | class". In a character class the only metacharacters are: | class". In a character class the only metacharacters are: |
| 3369 | ||
| 3370 | \ general escape character | \ general escape character |
| # | Line 3362 CHARACTERS AND METACHARACTERS | Line 3380 CHARACTERS AND METACHARACTERS |
| 3380 | BACKSLASH | BACKSLASH |
| 3381 | ||
| 3382 | The backslash character has several uses. Firstly, if it is followed by | The backslash character has several uses. Firstly, if it is followed by |
| 3383 | a non-alphanumeric character, it takes away any special meaning that | a character that is not a number or a letter, it takes away any special |
| 3384 | character may have. This use of backslash as an escape character | meaning that character may have. This use of backslash as an escape |
| 3385 | applies both inside and outside character classes. | character applies both inside and outside character classes. |
| 3386 | ||
| 3387 | For example, if you want to match a * character, you write \* in the | For example, if you want to match a * character, you write \* in the |
| 3388 | pattern. This escaping action applies whether or not the following | pattern. This escaping action applies whether or not the following |
| 3389 | character would otherwise be interpreted as a metacharacter, so it is | character would otherwise be interpreted as a metacharacter, so it is |
| 3390 | always safe to precede a non-alphanumeric with backslash to specify | always safe to precede a non-alphanumeric with backslash to specify |
| 3391 | that it stands for itself. In particular, if you want to match a back- | that it stands for itself. In particular, if you want to match a back- |
| 3392 | slash, you write \\. | slash, you write \\. |
| 3393 | ||
| 3394 | If a pattern is compiled with the PCRE_EXTENDED option, whitespace in | In UTF-8 mode, only ASCII numbers and letters have any special meaning |
| 3395 | the pattern (other than in a character class) and characters between a | after a backslash. All other characters (in particular, those whose |
| 3396 | codepoints are greater than 127) are treated as literals. | |
| 3397 | ||
| 3398 | If a pattern is compiled with the PCRE_EXTENDED option, whitespace in | |
| 3399 | the pattern (other than in a character class) and characters between a | |
| 3400 | # outside a character class and the next newline are ignored. An escap- | # outside a character class and the next newline are ignored. An escap- |
| 3401 | ing backslash can be used to include a whitespace or # character as | ing backslash can be used to include a whitespace or # character as |
| 3402 | part of the pattern. | part of the pattern. |
| 3403 | ||
| 3404 | If you want to remove the special meaning from a sequence of charac- | If you want to remove the special meaning from a sequence of charac- |
| 3405 | ters, you can do so by putting them between \Q and \E. This is differ- | ters, you can do so by putting them between \Q and \E. This is differ- |
| 3406 | ent from Perl in that $ and @ are handled as literals in \Q...\E | ent from Perl in that $ and @ are handled as literals in \Q...\E |
| 3407 | sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- | sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- |
| 3408 | tion. Note the following examples: | tion. Note the following examples: |
| 3409 | ||
| 3410 | Pattern PCRE matches Perl matches | Pattern PCRE matches Perl matches |
| # | Line 3392 BACKSLASH | Line 3414 BACKSLASH |
| 3414 | \Qabc\$xyz\E abc\$xyz abc\$xyz | \Qabc\$xyz\E abc\$xyz abc\$xyz |
| 3415 | \Qabc\E\$\Qxyz\E abc$xyz abc$xyz | \Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
| 3416 | ||
| 3417 | The \Q...\E sequence is recognized both inside and outside character | The \Q...\E sequence is recognized both inside and outside character |
| 3418 | classes. An isolated \E that is not preceded by \Q is ignored. | classes. An isolated \E that is not preceded by \Q is ignored. |
| 3419 | ||
| 3420 | Non-printing characters | Non-printing characters |
| 3421 | ||
| 3422 | A second use of backslash provides a way of encoding non-printing char- | A second use of backslash provides a way of encoding non-printing char- |
| 3423 | acters in patterns in a visible manner. There is no restriction on the | acters in patterns in a visible manner. There is no restriction on the |
| 3424 | appearance of non-printing characters, apart from the binary zero that | appearance of non-printing characters, apart from the binary zero that |
| 3425 | terminates a pattern, but when a pattern is being prepared by text | terminates a pattern, but when a pattern is being prepared by text |
| 3426 | editing, it is often easier to use one of the following escape | editing, it is often easier to use one of the following escape |
| 3427 | sequences than the binary character it represents: | sequences than the binary character it represents: |
| 3428 | ||
| 3429 | \a alarm, that is, the BEL character (hex 07) | \a alarm, that is, the BEL character (hex 07) |
| 3430 | \cx "control-x", where x is any character | \cx "control-x", where x is any ASCII character |
| 3431 | \e escape (hex 1B) | \e escape (hex 1B) |
| 3432 | \f formfeed (hex 0C) | \f formfeed (hex 0C) |
| 3433 | \n linefeed (hex 0A) | \n linefeed (hex 0A) |
| # | Line 3415 BACKSLASH | Line 3437 BACKSLASH |
| 3437 | \xhh character with hex code hh | \xhh character with hex code hh |
| 3438 | \x{hhh..} character with hex code hhh.. | \x{hhh..} character with hex code hhh.. |
| 3439 | ||
| 3440 | The precise effect of \cx is as follows: if x is a lower case letter, | The precise effect of \cx is as follows: if x is a lower case letter, |
| 3441 | it is converted to upper case. Then bit 6 of the character (hex 40) is | it is converted to upper case. Then bit 6 of the character (hex 40) is |
| 3442 | inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; | inverted. Thus \cz becomes hex 1A (z is 7A), but \c{ becomes hex 3B ({ |
| 3443 | becomes hex 7B. | is 7B), while \c; becomes hex 7B (; is 3B). If the byte following \c |
| 3444 | has a value greater than 127, a compile-time error occurs. This locks | |
| 3445 | After \x, from zero to two hexadecimal digits are read (letters can be | out non-ASCII characters in both byte mode and UTF-8 mode. (When PCRE |
| 3446 | in upper or lower case). Any number of hexadecimal digits may appear | is compiled in EBCDIC mode, all byte values are valid. A lower case |
| 3447 | between \x{ and }, but the value of the character code must be less | letter is converted to upper case, and then the 0xc0 bits are flipped.) |
| 3448 | ||
| 3449 | After \x, from zero to two hexadecimal digits are read (letters can be | |
| 3450 | in upper or lower case). Any number of hexadecimal digits may appear | |
| 3451 | between \x{ and }, but the value of the character code must be less | |
| 3452 | than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode. That is, | than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode. That is, |
| 3453 | the maximum value in hexadecimal is 7FFFFFFF. Note that this is bigger | the maximum value in hexadecimal is 7FFFFFFF. Note that this is bigger |
| 3454 | than the largest Unicode code point, which is 10FFFF. | than the largest Unicode code point, which is 10FFFF. |
| 3455 | ||
| 3456 | If characters other than hexadecimal digits appear between \x{ and }, | If characters other than hexadecimal digits appear between \x{ and }, |
| 3457 | or if there is no terminating }, this form of escape is not recognized. | or if there is no terminating }, this form of escape is not recognized. |
| 3458 | Instead, the initial \x will be interpreted as a basic hexadecimal | Instead, the initial \x will be interpreted as a basic hexadecimal |
| 3459 | escape, with no following digits, giving a character whose value is | escape, with no following digits, giving a character whose value is |
| 3460 | zero. | zero. |
| 3461 | ||
| 3462 | Characters whose value is less than 256 can be defined by either of the | Characters whose value is less than 256 can be defined by either of the |
| 3463 | two syntaxes for \x. There is no difference in the way they are han- | two syntaxes for \x. There is no difference in the way they are han- |
| 3464 | dled. For example, \xdc is exactly the same as \x{dc}. | dled. For example, \xdc is exactly the same as \x{dc}. |
| 3465 | ||
| 3466 | After \0 up to two further octal digits are read. If there are fewer | After \0 up to two further octal digits are read. If there are fewer |
| 3467 | than two digits, just those that are present are used. Thus the | than two digits, just those that are present are used. Thus the |
| 3468 | sequence \0\x\07 specifies two binary zeros followed by a BEL character | sequence \0\x\07 specifies two binary zeros followed by a BEL character |
| 3469 | (code value 7). Make sure you supply two digits after the initial zero | (code value 7). Make sure you supply two digits after the initial zero |
| 3470 | if the pattern character that follows is itself an octal digit. | if the pattern character that follows is itself an octal digit. |
| 3471 | ||
| 3472 | The handling of a backslash followed by a digit other than 0 is compli- | The handling of a backslash followed by a digit other than 0 is compli- |
| 3473 | cated. Outside a character class, PCRE reads it and any following dig- | cated. Outside a character class, PCRE reads it and any following dig- |
| 3474 | its as a decimal number. If the number is less than 10, or if there | its as a decimal number. If the number is less than 10, or if there |
| 3475 | have been at least that many previous capturing left parentheses in the | have been at least that many previous capturing left parentheses in the |
| 3476 | expression, the entire sequence is taken as a back reference. A | expression, the entire sequence is taken as a back reference. A |
| 3477 | description of how this works is given later, following the discussion | description of how this works is given later, following the discussion |
| 3478 | of parenthesized subpatterns. | of parenthesized subpatterns. |
| 3479 | ||
| 3480 | Inside a character class, or if the decimal number is greater than 9 | Inside a character class, or if the decimal number is greater than 9 |
| 3481 | and there have not been that many capturing subpatterns, PCRE re-reads | and there have not been that many capturing subpatterns, PCRE re-reads |
| 3482 | up to three octal digits following the backslash, and uses them to gen- | up to three octal digits following the backslash, and uses them to gen- |
| 3483 | erate a data character. Any subsequent digits stand for themselves. In | erate a data character. Any subsequent digits stand for themselves. In |
| 3484 | non-UTF-8 mode, the value of a character specified in octal must be | non-UTF-8 mode, the value of a character specified in octal must be |
| 3485 | less than \400. In UTF-8 mode, values up to \777 are permitted. For | less than \400. In UTF-8 mode, values up to \777 are permitted. For |
| 3486 | example: | example: |
| 3487 | ||
| 3488 | \040 is another way of writing a space | \040 is another way of writing a space |
| # | Line 3474 BACKSLASH | Line 3500 BACKSLASH |
| 3500 | \81 is either a back reference, or a binary zero | \81 is either a back reference, or a binary zero |
| 3501 | followed by the two characters "8" and "1" | followed by the two characters "8" and "1" |
| 3502 | ||
| 3503 | Note that octal values of 100 or greater must not be introduced by a | Note that octal values of 100 or greater must not be introduced by a |
| 3504 | leading zero, because no more than three octal digits are ever read. | leading zero, because no more than three octal digits are ever read. |
| 3505 | ||
| 3506 | All the sequences that define a single character value can be used both | All the sequences that define a single character value can be used both |
| 3507 | inside and outside character classes. In addition, inside a character | inside and outside character classes. In addition, inside a character |
| 3508 | class, the sequence \b is interpreted as the backspace character (hex | class, the sequence \b is interpreted as the backspace character (hex |
| 3509 | 08). The sequences \B, \N, \R, and \X are not special inside a charac- | 08). The sequences \B, \N, \R, and \X are not special inside a charac- |
| 3510 | ter class. Like any other unrecognized escape sequences, they are | ter class. Like any other unrecognized escape sequences, they are |
| 3511 | treated as the literal characters "B", "N", "R", and "X" by default, | treated as the literal characters "B", "N", "R", and "X" by default, |
| 3512 | but cause an error if the PCRE_EXTRA option is set. Outside a character | but cause an error if the PCRE_EXTRA option is set. Outside a character |
| 3513 | class, these sequences have different meanings. | class, these sequences have different meanings. |
| 3514 | ||
| 3515 | Absolute and relative back references | Absolute and relative back references |
| 3516 | ||
| 3517 | The sequence \g followed by an unsigned or a negative number, option- | The sequence \g followed by an unsigned or a negative number, option- |
| 3518 | ally enclosed in braces, is an absolute or relative back reference. A | ally enclosed in braces, is an absolute or relative back reference. A |
| 3519 | named back reference can be coded as \g{name}. Back references are dis- | named back reference can be coded as \g{name}. Back references are dis- |
| 3520 | cussed later, following the discussion of parenthesized subpatterns. | cussed later, following the discussion of parenthesized subpatterns. |
| 3521 | ||
| 3522 | Absolute and relative subroutine calls | Absolute and relative subroutine calls |
| 3523 | ||
| 3524 | For compatibility with Oniguruma, the non-Perl syntax \g followed by a | For compatibility with Oniguruma, the non-Perl syntax \g followed by a |
| 3525 | name or a number enclosed either in angle brackets or single quotes, is | name or a number enclosed either in angle brackets or single quotes, is |
| 3526 | an alternative syntax for referencing a subpattern as a "subroutine". | an alternative syntax for referencing a subpattern as a "subroutine". |
| 3527 | Details are discussed later. Note that \g{...} (Perl syntax) and | Details are discussed later. Note that \g{...} (Perl syntax) and |
| 3528 | \g<...> (Oniguruma syntax) are not synonymous. The former is a back | \g<...> (Oniguruma syntax) are not synonymous. The former is a back |
| 3529 | reference; the latter is a subroutine call. | reference; the latter is a subroutine call. |
| 3530 | ||
| 3531 | Generic character types | Generic character types |
| # | Line 3518 BACKSLASH | Line 3544 BACKSLASH |
| 3544 | \W any "non-word" character | \W any "non-word" character |
| 3545 | ||
| 3546 | There is also the single sequence \N, which matches a non-newline char- | There is also the single sequence \N, which matches a non-newline char- |
| 3547 | acter. This is the same as the "." metacharacter when PCRE_DOTALL is | acter. This is the same as the "." metacharacter when PCRE_DOTALL is |
| 3548 | not set. | not set. |
| 3549 | ||
| 3550 | Each pair of lower and upper case escape sequences partitions the com- | Each pair of lower and upper case escape sequences partitions the com- |
| 3551 | plete set of characters into two disjoint sets. Any given character | plete set of characters into two disjoint sets. Any given character |
| 3552 | matches one, and only one, of each pair. The sequences can appear both | matches one, and only one, of each pair. The sequences can appear both |
| 3553 | inside and outside character classes. They each match one character of | inside and outside character classes. They each match one character of |
| 3554 | the appropriate type. If the current matching point is at the end of | the appropriate type. If the current matching point is at the end of |
| 3555 | the subject string, all of them fail, because there is no character to | the subject string, all of them fail, because there is no character to |
| 3556 | match. | match. |
| 3557 | ||
| 3558 | For compatibility with Perl, \s does not match the VT character (code | For compatibility with Perl, \s does not match the VT character (code |
| 3559 | 11). This makes it different from the the POSIX "space" class. The \s | 11). This makes it different from the the POSIX "space" class. The \s |
| 3560 | characters are HT (9), LF (10), FF (12), CR (13), and space (32). If | characters are HT (9), LF (10), FF (12), CR (13), and space (32). If |
| 3561 | "use locale;" is included in a Perl script, \s may match the VT charac- | "use locale;" is included in a Perl script, \s may match the VT charac- |
| 3562 | ter. In PCRE, it never does. | ter. In PCRE, it never does. |
| 3563 | ||
| 3564 | A "word" character is an underscore or any character that is a letter | A "word" character is an underscore or any character that is a letter |
| 3565 | or digit. By default, the definition of letters and digits is con- | or digit. By default, the definition of letters and digits is con- |
| 3566 | trolled by PCRE's low-valued character tables, and may vary if locale- | trolled by PCRE's low-valued character tables, and may vary if locale- |
| 3567 | specific matching is taking place (see "Locale support" in the pcreapi | specific matching is taking place (see "Locale support" in the pcreapi |
| 3568 | page). For example, in a French locale such as "fr_FR" in Unix-like | page). For example, in a French locale such as "fr_FR" in Unix-like |
| 3569 | systems, or "french" in Windows, some character codes greater than 128 | systems, or "french" in Windows, some character codes greater than 128 |
| 3570 | are used for accented letters, and these are then matched by \w. The | are used for accented letters, and these are then matched by \w. The |
| 3571 | use of locales with Unicode is discouraged. | use of locales with Unicode is discouraged. |
| 3572 | ||
| 3573 | By default, in UTF-8 mode, characters with values greater than 128 | By default, in UTF-8 mode, characters with values greater than 128 |
| 3574 | never match \d, \s, or \w, and always match \D, \S, and \W. These | never match \d, \s, or \w, and always match \D, \S, and \W. These |
| 3575 | sequences retain their original meanings from before UTF-8 support was | sequences retain their original meanings from before UTF-8 support was |
| 3576 | available, mainly for efficiency reasons. However, if PCRE is compiled | available, mainly for efficiency reasons. However, if PCRE is compiled |
| 3577 | with Unicode property support, and the PCRE_UCP option is set, the be- | with Unicode property support, and the PCRE_UCP option is set, the be- |
| 3578 | haviour is changed so that Unicode properties are used to determine | haviour is changed so that Unicode properties are used to determine |
| 3579 | character types, as follows: | character types, as follows: |
| 3580 | ||
| 3581 | \d any character that \p{Nd} matches (decimal digit) | \d any character that \p{Nd} matches (decimal digit) |
| 3582 | \s any character that \p{Z} matches, plus HT, LF, FF, CR | \s any character that \p{Z} matches, plus HT, LF, FF, CR |
| 3583 | \w any character that \p{L} or \p{N} matches, plus underscore | \w any character that \p{L} or \p{N} matches, plus underscore |
| 3584 | ||
| 3585 | The upper case escapes match the inverse sets of characters. Note that | The upper case escapes match the inverse sets of characters. Note that |
| 3586 | \d matches only decimal digits, whereas \w matches any Unicode digit, | \d matches only decimal digits, whereas \w matches any Unicode digit, |
| 3587 | as well as any Unicode letter, and underscore. Note also that PCRE_UCP | as well as any Unicode letter, and underscore. Note also that PCRE_UCP |
| 3588 | affects \b, and \B because they are defined in terms of \w and \W. | affects \b, and \B because they are defined in terms of \w and \W. |
| 3589 | Matching these sequences is noticeably slower when PCRE_UCP is set. | Matching these sequences is noticeably slower when PCRE_UCP is set. |
| 3590 | ||
| 3591 | The sequences \h, \H, \v, and \V are features that were added to Perl | The sequences \h, \H, \v, and \V are features that were added to Perl |
| 3592 | at release 5.10. In contrast to the other sequences, which match only | at release 5.10. In contrast to the other sequences, which match only |
| 3593 | ASCII characters by default, these always match certain high-valued | ASCII characters by default, these always match certain high-valued |
| 3594 | codepoints in UTF-8 mode, whether or not PCRE_UCP is set. The horizon- | codepoints in UTF-8 mode, whether or not PCRE_UCP is set. The horizon- |
| 3595 | tal space characters are: | tal space characters are: |
| 3596 | ||
| 3597 | U+0009 Horizontal tab | U+0009 Horizontal tab |
| # | Line 3600 BACKSLASH | Line 3626 BACKSLASH |
| 3626 | ||
| 3627 | Newline sequences | Newline sequences |
| 3628 | ||
| 3629 | Outside a character class, by default, the escape sequence \R matches | Outside a character class, by default, the escape sequence \R matches |
| 3630 | any Unicode newline sequence. In non-UTF-8 mode \R is equivalent to the | any Unicode newline sequence. In non-UTF-8 mode \R is equivalent to the |
| 3631 | following: | following: |
| 3632 | ||
| 3633 | (?>\r\n|\n|\x0b|\f|\r|\x85) | (?>\r\n|\n|\x0b|\f|\r|\x85) |
| 3634 | ||
| 3635 | This is an example of an "atomic group", details of which are given | This is an example of an "atomic group", details of which are given |
| 3636 | below. This particular group matches either the two-character sequence | below. This particular group matches either the two-character sequence |
| 3637 | CR followed by LF, or one of the single characters LF (linefeed, | CR followed by LF, or one of the single characters LF (linefeed, |
| 3638 | U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage | U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage |
| 3639 | return, U+000D), or NEL (next line, U+0085). The two-character sequence | return, U+000D), or NEL (next line, U+0085). The two-character sequence |
| 3640 | is treated as a single unit that cannot be split. | is treated as a single unit that cannot be split. |
| 3641 | ||
| 3642 | In UTF-8 mode, two additional characters whose codepoints are greater | In UTF-8 mode, two additional characters whose codepoints are greater |
| 3643 | than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa- | than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa- |
| 3644 | rator, U+2029). Unicode character property support is not needed for | rator, U+2029). Unicode character property support is not needed for |
| 3645 | these characters to be recognized. | these characters to be recognized. |
| 3646 | ||
| 3647 | It is possible to restrict \R to match only CR, LF, or CRLF (instead of | It is possible to restrict \R to match only CR, LF, or CRLF (instead of |
| 3648 | the complete set of Unicode line endings) by setting the option | the complete set of Unicode line endings) by setting the option |
| 3649 | PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched. | PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched. |
| 3650 | (BSR is an abbrevation for "backslash R".) This can be made the default | (BSR is an abbrevation for "backslash R".) This can be made the default |
| 3651 | when PCRE is built; if this is the case, the other behaviour can be | when PCRE is built; if this is the case, the other behaviour can be |
| 3652 | requested via the PCRE_BSR_UNICODE option. It is also possible to | requested via the PCRE_BSR_UNICODE option. It is also possible to |
| 3653 | specify these settings by starting a pattern string with one of the | specify these settings by starting a pattern string with one of the |
| 3654 | following sequences: | following sequences: |
| 3655 | ||
| 3656 | (*BSR_ANYCRLF) CR, LF, or CRLF only | (*BSR_ANYCRLF) CR, LF, or CRLF only |
| 3657 | (*BSR_UNICODE) any Unicode newline sequence | (*BSR_UNICODE) any Unicode newline sequence |
| 3658 | ||
| 3659 | These override the default and the options given to pcre_compile() or | These override the default and the options given to pcre_compile() or |
| 3660 | pcre_compile2(), but they can be overridden by options given to | pcre_compile2(), but they can be overridden by options given to |
| 3661 | pcre_exec() or pcre_dfa_exec(). Note that these special settings, which | pcre_exec() or pcre_dfa_exec(). Note that these special settings, which |
| 3662 | are not Perl-compatible, are recognized only at the very start of a | are not Perl-compatible, are recognized only at the very start of a |
| 3663 | pattern, and that they must be in upper case. If more than one of them | pattern, and that they must be in upper case. If more than one of them |
| 3664 | is present, the last one is used. They can be combined with a change of | is present, the last one is used. They can be combined with a change of |
| 3665 | newline convention; for example, a pattern can start with: | newline convention; for example, a pattern can start with: |
| 3666 | ||
| 3667 | (*ANY)(*BSR_ANYCRLF) | (*ANY)(*BSR_ANYCRLF) |
| 3668 | ||
| 3669 | They can also be combined with the (*UTF8) or (*UCP) special sequences. | They can also be combined with the (*UTF8) or (*UCP) special sequences. |
| 3670 | Inside a character class, \R is treated as an unrecognized escape | Inside a character class, \R is treated as an unrecognized escape |
| 3671 | sequence, and so matches the letter "R" by default, but causes an error | sequence, and so matches the letter "R" by default, but causes an error |
| 3672 | if PCRE_EXTRA is set. | if PCRE_EXTRA is set. |
| 3673 | ||
| 3674 | Unicode character properties | Unicode character properties |
| 3675 | ||
| 3676 | When PCRE is built with Unicode character property support, three addi- | When PCRE is built with Unicode character property support, three addi- |
| 3677 | tional escape sequences that match characters with specific properties | tional escape sequences that match characters with specific properties |
| 3678 | are available. When not in UTF-8 mode, these sequences are of course | are available. When not in UTF-8 mode, these sequences are of course |
| 3679 | limited to testing characters whose codepoints are less than 256, but | limited to testing characters whose codepoints are less than 256, but |
| 3680 | they do work in this mode. The extra escape sequences are: | they do work in this mode. The extra escape sequences are: |
| 3681 | ||
| 3682 | \p{xx} a character with the xx property | \p{xx} a character with the xx property |
| 3683 | \P{xx} a character without the xx property | \P{xx} a character without the xx property |
| 3684 | \X an extended Unicode sequence | \X an extended Unicode sequence |
| 3685 | ||
| 3686 | The property names represented by xx above are limited to the Unicode | The property names represented by xx above are limited to the Unicode |
| 3687 | script names, the general category properties, "Any", which matches any | script names, the general category properties, "Any", which matches any |
| 3688 | character (including newline), and some special PCRE properties | character (including newline), and some special PCRE properties |
| 3689 | (described in the next section). Other Perl properties such as "InMu- | (described in the next section). Other Perl properties such as "InMu- |
| 3690 | sicalSymbols" are not currently supported by PCRE. Note that \P{Any} | sicalSymbols" are not currently supported by PCRE. Note that \P{Any} |
| 3691 | does not match any characters, so always causes a match failure. | does not match any characters, so always causes a match failure. |
| 3692 | ||
| 3693 | Sets of Unicode characters are defined as belonging to certain scripts. | Sets of Unicode characters are defined as belonging to certain scripts. |
| 3694 | A character from one of these sets can be matched using a script name. | A character from one of these sets can be matched using a script name. |
| 3695 | For example: | For example: |
| 3696 | ||
| 3697 | \p{Greek} | \p{Greek} |
| 3698 | \P{Han} | \P{Han} |
| 3699 | ||
| 3700 | Those that are not part of an identified script are lumped together as | Those that are not part of an identified script are lumped together as |
| 3701 | "Common". The current list of scripts is: | "Common". The current list of scripts is: |
| 3702 | ||
| 3703 | Arabic, Armenian, Avestan, Balinese, Bamum, Bengali, Bopomofo, Braille, | Arabic, Armenian, Avestan, Balinese, Bamum, Bengali, Bopomofo, Braille, |
| 3704 | Buginese, Buhid, Canadian_Aboriginal, Carian, Cham, Cherokee, Common, | Buginese, Buhid, Canadian_Aboriginal, Carian, Cham, Cherokee, Common, |
| 3705 | Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, Devanagari, Egyp- | Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, Devanagari, Egyp- |
| 3706 | tian_Hieroglyphs, Ethiopic, Georgian, Glagolitic, Gothic, Greek, | tian_Hieroglyphs, Ethiopic, Georgian, Glagolitic, Gothic, Greek, |
| 3707 | Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Impe- | Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Impe- |
| 3708 | rial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian, | rial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian, |
| 3709 | Javanese, Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khmer, Lao, | Javanese, Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khmer, Lao, |
| 3710 | Latin, Lepcha, Limbu, Linear_B, Lisu, Lycian, Lydian, Malayalam, | Latin, Lepcha, Limbu, Linear_B, Lisu, Lycian, Lydian, Malayalam, |
| 3711 | Meetei_Mayek, Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic, | Meetei_Mayek, Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic, |
| 3712 | Old_Persian, Old_South_Arabian, Old_Turkic, Ol_Chiki, Oriya, Osmanya, | Old_Persian, Old_South_Arabian, Old_Turkic, Ol_Chiki, Oriya, Osmanya, |
| 3713 | Phags_Pa, Phoenician, Rejang, Runic, Samaritan, Saurashtra, Shavian, | Phags_Pa, Phoenician, Rejang, Runic, Samaritan, Saurashtra, Shavian, |
| 3714 | Sinhala, Sundanese, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le, | Sinhala, Sundanese, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le, |
| 3715 | Tai_Tham, Tai_Viet, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, | Tai_Tham, Tai_Viet, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, |
| 3716 | Ugaritic, Vai, Yi. | Ugaritic, Vai, Yi. |
| 3717 | ||
| 3718 | Each character has exactly one Unicode general category property, spec- | Each character has exactly one Unicode general category property, spec- |
| 3719 | ified by a two-letter abbreviation. For compatibility with Perl, nega- | ified by a two-letter abbreviation. For compatibility with Perl, nega- |
| 3720 | tion can be specified by including a circumflex between the opening | tion can be specified by including a circumflex between the opening |
| 3721 | brace and the property name. For example, \p{^Lu} is the same as | brace and the property name. For example, \p{^Lu} is the same as |
| 3722 | \P{Lu}. | \P{Lu}. |
| 3723 | ||
| 3724 | If only one letter is specified with \p or \P, it includes all the gen- | If only one letter is specified with \p or \P, it includes all the gen- |
| 3725 | eral category properties that start with that letter. In this case, in | eral category properties that start with that letter. In this case, in |
| 3726 | the absence of negation, the curly brackets in the escape sequence are | the absence of negation, the curly brackets in the escape sequence are |
| 3727 | optional; these two examples have the same effect: | optional; these two examples have the same effect: |
| 3728 | ||
| 3729 | \p{L} | \p{L} |
| # | Line 3749 BACKSLASH | Line 3775 BACKSLASH |
| 3775 | Zp Paragraph separator | Zp Paragraph separator |
| 3776 | Zs Space separator | Zs Space separator |
| 3777 | ||
| 3778 | The special property L& is also supported: it matches a character that | The special property L& is also supported: it matches a character that |
| 3779 | has the Lu, Ll, or Lt property, in other words, a letter that is not | has the Lu, Ll, or Lt property, in other words, a letter that is not |
| 3780 | classified as a modifier or "other". | classified as a modifier or "other". |
| 3781 | ||
| 3782 | The Cs (Surrogate) property applies only to characters in the range | The Cs (Surrogate) property applies only to characters in the range |
| 3783 | U+D800 to U+DFFF. Such characters are not valid in UTF-8 strings (see | U+D800 to U+DFFF. Such characters are not valid in UTF-8 strings (see |
| 3784 | RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check- | RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check- |
| 3785 | ing has been turned off (see the discussion of PCRE_NO_UTF8_CHECK in | ing has been turned off (see the discussion of PCRE_NO_UTF8_CHECK in |
| 3786 | the pcreapi page). Perl does not support the Cs property. | the pcreapi page). Perl does not support the Cs property. |
| 3787 | ||
| 3788 | The long synonyms for property names that Perl supports (such as | The long synonyms for property names that Perl supports (such as |
| 3789 | \p{Letter}) are not supported by PCRE, nor is it permitted to prefix | \p{Letter}) are not supported by PCRE, nor is it permitted to prefix |
| 3790 | any of these properties with "Is". | any of these properties with "Is". |
| 3791 | ||
| 3792 | No character that is in the Unicode table has the Cn (unassigned) prop- | No character that is in the Unicode table has the Cn (unassigned) prop- |
| 3793 | erty. Instead, this property is assumed for any code point that is not | erty. Instead, this property is assumed for any code point that is not |
| 3794 | in the Unicode table. | in the Unicode table. |
| 3795 | ||
| 3796 | Specifying caseless matching does not affect these escape sequences. | Specifying caseless matching does not affect these escape sequences. |
| 3797 | For example, \p{Lu} always matches only upper case letters. | For example, \p{Lu} always matches only upper case letters. |
| 3798 | ||
| 3799 | The \X escape matches any number of Unicode characters that form an | The \X escape matches any number of Unicode characters that form an |
| 3800 | extended Unicode sequence. \X is equivalent to | extended Unicode sequence. \X is equivalent to |
| 3801 | ||
| 3802 | (?>\PM\pM*) | (?>\PM\pM*) |
| 3803 | ||
| 3804 | That is, it matches a character without the "mark" property, followed | That is, it matches a character without the "mark" property, followed |
| 3805 | by zero or more characters with the "mark" property, and treats the | by zero or more characters with the "mark" property, and treats the |
| 3806 | sequence as an atomic group (see below). Characters with the "mark" | sequence as an atomic group (see below). Characters with the "mark" |
| 3807 | property are typically accents that affect the preceding character. | property are typically accents that affect the preceding character. |
| 3808 | None of them have codepoints less than 256, so in non-UTF-8 mode \X | None of them have codepoints less than 256, so in non-UTF-8 mode \X |
| 3809 | matches any one character. | matches any one character. |
| 3810 | ||
| 3811 | Matching characters by Unicode property is not fast, because PCRE has | Matching characters by Unicode property is not fast, because PCRE has |
| 3812 | to search a structure that contains data for over fifteen thousand | to search a structure that contains data for over fifteen thousand |
| 3813 | characters. That is why the traditional escape sequences such as \d and | characters. That is why the traditional escape sequences such as \d and |
| 3814 | \w do not use Unicode properties in PCRE by default, though you can | \w do not use Unicode properties in PCRE by default, though you can |
| 3815 | make them do so by setting the PCRE_UCP option for pcre_compile() or by | make them do so by setting the PCRE_UCP option for pcre_compile() or by |
| 3816 | starting the pattern with (*UCP). | starting the pattern with (*UCP). |
| 3817 | ||
| 3818 | PCRE's additional properties | PCRE's additional properties |
| 3819 | ||
| 3820 | As well as the standard Unicode properties described in the previous | As well as the standard Unicode properties described in the previous |
| 3821 | section, PCRE supports four more that make it possible to convert tra- | section, PCRE supports four more that make it possible to convert tra- |
| 3822 | ditional escape sequences such as \w and \s and POSIX character classes | ditional escape sequences such as \w and \s and POSIX character classes |
| 3823 | to use Unicode properties. PCRE uses these non-standard, non-Perl prop- | to use Unicode properties. PCRE uses these non-standard, non-Perl prop- |
| 3824 | erties internally when PCRE_UCP is set. They are: | erties internally when PCRE_UCP is set. They are: |
| # | Line 3802 BACKSLASH | Line 3828 BACKSLASH |
| 3828 | Xsp Any Perl space character | Xsp Any Perl space character |
| 3829 | Xwd Any Perl "word" character | Xwd Any Perl "word" character |
| 3830 | ||
| 3831 | Xan matches characters that have either the L (letter) or the N (num- | Xan matches characters that have either the L (letter) or the N (num- |
| 3832 | ber) property. Xps matches the characters tab, linefeed, vertical tab, | ber) property. Xps matches the characters tab, linefeed, vertical tab, |
| 3833 | formfeed, or carriage return, and any other character that has the Z | formfeed, or carriage return, and any other character that has the Z |
| 3834 | (separator) property. Xsp is the same as Xps, except that vertical tab | (separator) property. Xsp is the same as Xps, except that vertical tab |
| 3835 | is excluded. Xwd matches the same characters as Xan, plus underscore. | is excluded. Xwd matches the same characters as Xan, plus underscore. |
| 3836 | ||
| 3837 | Resetting the match start | Resetting the match start |
| 3838 | ||
| 3839 | The escape sequence \K causes any previously matched characters not to | The escape sequence \K causes any previously matched characters not to |
| 3840 | be included in the final matched sequence. For example, the pattern: | be included in the final matched sequence. For example, the pattern: |
| 3841 | ||
| 3842 | foo\Kbar | foo\Kbar |
| 3843 | ||
| 3844 | matches "foobar", but reports that it has matched "bar". This feature | matches "foobar", but reports that it has matched "bar". This feature |
| 3845 | is similar to a lookbehind assertion (described below). However, in | is similar to a lookbehind assertion (described below). However, in |
| 3846 | this case, the part of the subject before the real match does not have | this case, the part of the subject before the real match does not have |
| 3847 | to be of fixed length, as lookbehind assertions do. The use of \K does | to be of fixed length, as lookbehind assertions do. The use of \K does |
| 3848 | not interfere with the setting of captured substrings. For example, | not interfere with the setting of captured substrings. For example, |
| 3849 | when the pattern | when the pattern |
| 3850 | ||
| 3851 | (foo)\Kbar | (foo)\Kbar |
| 3852 | ||
| 3853 | matches "foobar", the first substring is still set to "foo". | matches "foobar", the first substring is still set to "foo". |
| 3854 | ||
| 3855 | Perl documents that the use of \K within assertions is "not well | Perl documents that the use of \K within assertions is "not well |
| 3856 | defined". In PCRE, \K is acted upon when it occurs inside positive | defined". In PCRE, \K is acted upon when it occurs inside positive |
| 3857 | assertions, but is ignored in negative assertions. | assertions, but is ignored in negative assertions. |
| 3858 | ||
| 3859 | Simple assertions | Simple assertions |
| 3860 | ||
| 3861 | The final use of backslash is for certain simple assertions. An asser- | The final use of backslash is for certain simple assertions. An asser- |
| 3862 | tion specifies a condition that has to be met at a particular point in | tion specifies a condition that has to be met at a particular point in |
| 3863 | a match, without consuming any characters from the subject string. The | a match, without consuming any characters from the subject string. The |
| 3864 | use of subpatterns for more complicated assertions is described below. | use of subpatterns for more complicated assertions is described below. |
| 3865 | The backslashed assertions are: | The backslashed assertions are: |
| 3866 | ||
| 3867 | \b matches at a word boundary | \b matches at a word boundary |
| # | Line 3846 BACKSLASH | Line 3872 BACKSLASH |
| 3872 | \z matches only at the end of the subject | \z matches only at the end of the subject |
| 3873 | \G matches at the first matching position in the subject | \G matches at the first matching position in the subject |
| 3874 | ||
| 3875 | Inside a character class, \b has a different meaning; it matches the | Inside a character class, \b has a different meaning; it matches the |
| 3876 | backspace character. If any other of these assertions appears in a | backspace character. If any other of these assertions appears in a |
| 3877 | character class, by default it matches the corresponding literal char- | character class, by default it matches the corresponding literal char- |
| 3878 | acter (for example, \B matches the letter B). However, if the | acter (for example, \B matches the letter B). However, if the |
| 3879 | PCRE_EXTRA option is set, an "invalid escape sequence" error is gener- | PCRE_EXTRA option is set, an "invalid escape sequence" error is gener- |
| 3880 | ated instead. | ated instead. |
| 3881 | ||
| 3882 | A word boundary is a position in the subject string where the current | A word boundary is a position in the subject string where the current |
| 3883 | character and the previous character do not both match \w or \W (i.e. | character and the previous character do not both match \w or \W (i.e. |
| 3884 | one matches \w and the other matches \W), or the start or end of the | one matches \w and the other matches \W), or the start or end of the |
| 3885 | string if the first or last character matches \w, respectively. In | string if the first or last character matches \w, respectively. In |
| 3886 | UTF-8 mode, the meanings of \w and \W can be changed by setting the | UTF-8 mode, the meanings of \w and \W can be changed by setting the |
| 3887 | PCRE_UCP option. When this is done, it also affects \b and \B. Neither | PCRE_UCP option. When this is done, it also affects \b and \B. Neither |
| 3888 | PCRE nor Perl has a separate "start of word" or "end of word" metase- | PCRE nor Perl has a separate "start of word" or "end of word" metase- |
| 3889 | quence. However, whatever follows \b normally determines which it is. | quence. However, whatever follows \b normally determines which it is. |
| 3890 | For example, the fragment \ba matches "a" at the start of a word. | For example, the fragment \ba matches "a" at the start of a word. |
| 3891 | ||
| 3892 | The \A, \Z, and \z assertions differ from the traditional circumflex | The \A, \Z, and \z assertions differ from the traditional circumflex |
| 3893 | and dollar (described in the next section) in that they only ever match | and dollar (described in the next section) in that they only ever match |
| 3894 | at the very start and end of the subject string, whatever options are | at the very start and end of the subject string, whatever options are |
| 3895 | set. Thus, they are independent of multiline mode. These three asser- | set. Thus, they are independent of multiline mode. These three asser- |
| 3896 | tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which | tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which |
| 3897 | affect only the behaviour of the circumflex and dollar metacharacters. | affect only the behaviour of the circumflex and dollar metacharacters. |
| 3898 | However, if the startoffset argument of pcre_exec() is non-zero, indi- | However, if the startoffset argument of pcre_exec() is non-zero, indi- |
| 3899 | cating that matching is to start at a point other than the beginning of | cating that matching is to start at a point other than the beginning of |
| 3900 | the subject, \A can never match. The difference between \Z and \z is | the subject, \A can never match. The difference between \Z and \z is |
| 3901 | that \Z matches before a newline at the end of the string as well as at | that \Z matches before a newline at the end of the string as well as at |
| 3902 | the very end, whereas \z matches only at the end. | the very end, whereas \z matches only at the end. |
| 3903 | ||
| 3904 | The \G assertion is true only when the current matching position is at | The \G assertion is true only when the current matching position is at |
| 3905 | the start point of the match, as specified by the startoffset argument | the start point of the match, as specified by the startoffset argument |
| 3906 | of pcre_exec(). It differs from \A when the value of startoffset is | of pcre_exec(). It differs from \A when the value of startoffset is |
| 3907 | non-zero. By calling pcre_exec() multiple times with appropriate argu- | non-zero. By calling pcre_exec() multiple times with appropriate argu- |
| 3908 | ments, you can mimic Perl's /g option, and it is in this kind of imple- | ments, you can mimic Perl's /g option, and it is in this kind of imple- |
| 3909 | mentation where \G can be useful. | mentation where \G can be useful. |
| 3910 | ||
| 3911 | Note, however, that PCRE's interpretation of \G, as the start of the | Note, however, that PCRE's interpretation of \G, as the start of the |
| 3912 | current match, is subtly different from Perl's, which defines it as the | current match, is subtly different from Perl's, which defines it as the |
| 3913 | end of the previous match. In Perl, these can be different when the | end of the previous match. In Perl, these can be different when the |
| 3914 | previously matched string was empty. Because PCRE does just one match | previously matched string was empty. Because PCRE does just one match |
| 3915 | at a time, it cannot reproduce this behaviour. | at a time, it cannot reproduce this behaviour. |
| 3916 | ||
| 3917 | If all the alternatives of a pattern begin with \G, the expression is | If all the alternatives of a pattern begin with \G, the expression is |
| 3918 | anchored to the starting match position, and the "anchored" flag is set | anchored to the starting match position, and the "anchored" flag is set |
| 3919 | in the compiled regular expression. | in the compiled regular expression. |
| 3920 | ||
| # | Line 3896 BACKSLASH | Line 3922 BACKSLASH |
| 3922 | CIRCUMFLEX AND DOLLAR | CIRCUMFLEX AND DOLLAR |
| 3923 | ||
| 3924 | Outside a character class, in the default matching mode, the circumflex | Outside a character class, in the default matching mode, the circumflex |
| 3925 | character is an assertion that is true only if the current matching | character is an assertion that is true only if the current matching |
| 3926 | point is at the start of the subject string. If the startoffset argu- | point is at the start of the subject string. If the startoffset argu- |
| 3927 | ment of pcre_exec() is non-zero, circumflex can never match if the | ment of pcre_exec() is non-zero, circumflex can never match if the |
| 3928 | PCRE_MULTILINE option is unset. Inside a character class, circumflex | PCRE_MULTILINE option is unset. Inside a character class, circumflex |
| 3929 | has an entirely different meaning (see below). | has an entirely different meaning (see below). |
| 3930 | ||
| 3931 | Circumflex need not be the first character of the pattern if a number | Circumflex need not be the first character of the pattern if a number |
| 3932 | of alternatives are involved, but it should be the first thing in each | of alternatives are involved, but it should be the first thing in each |
| 3933 | alternative in which it appears if the pattern is ever to match that | alternative in which it appears if the pattern is ever to match that |
| 3934 | branch. If all possible alternatives start with a circumflex, that is, | branch. If all possible alternatives start with a circumflex, that is, |
| 3935 | if the pattern is constrained to match only at the start of the sub- | if the pattern is constrained to match only at the start of the sub- |
| 3936 | ject, it is said to be an "anchored" pattern. (There are also other | ject, it is said to be an "anchored" pattern. (There are also other |
| 3937 | constructs that can cause a pattern to be anchored.) | constructs that can cause a pattern to be anchored.) |
| 3938 | ||
| 3939 | A dollar character is an assertion that is true only if the current | A dollar character is an assertion that is true only if the current |
| 3940 | matching point is at the end of the subject string, or immediately | matching point is at the end of the subject string, or immediately |
| 3941 | before a newline at the end of the string (by default). Dollar need not | before a newline at the end of the string (by default). Dollar need not |
| 3942 | be the last character of the pattern if a number of alternatives are | be the last character of the pattern if a number of alternatives are |
| 3943 | involved, but it should be the last item in any branch in which it | involved, but it should be the last item in any branch in which it |
| 3944 | appears. Dollar has no special meaning in a character class. | appears. Dollar has no special meaning in a character class. |
| 3945 | ||
| 3946 | The meaning of dollar can be changed so that it matches only at the | The meaning of dollar can be changed so that it matches only at the |
| 3947 | very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at | very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at |
| 3948 | compile time. This does not affect the \Z assertion. | compile time. This does not affect the \Z assertion. |
| 3949 | ||
| 3950 | The meanings of the circumflex and dollar characters are changed if the | The meanings of the circumflex and dollar characters are changed if the |
| 3951 | PCRE_MULTILINE option is set. When this is the case, a circumflex | PCRE_MULTILINE option is set. When this is the case, a circumflex |
| 3952 | matches immediately after internal newlines as well as at the start of | matches immediately after internal newlines as well as at the start of |
| 3953 | the subject string. It does not match after a newline that ends the | the subject string. It does not match after a newline that ends the |
| 3954 | string. A dollar matches before any newlines in the string, as well as | string. A dollar matches before any newlines in the string, as well as |
| 3955 | at the very end, when PCRE_MULTILINE is set. When newline is specified | at the very end, when PCRE_MULTILINE is set. When newline is specified |
| 3956 | as the two-character sequence CRLF, isolated CR and LF characters do | as the two-character sequence CRLF, isolated CR and LF characters do |
| 3957 | not indicate newlines. | not indicate newlines. |
| 3958 | ||
| 3959 | For example, the pattern /^abc$/ matches the subject string "def\nabc" | For example, the pattern /^abc$/ matches the subject string "def\nabc" |
| 3960 | (where \n represents a newline) in multiline mode, but not otherwise. | (where \n represents a newline) in multiline mode, but not otherwise. |
| 3961 | Consequently, patterns that are anchored in single line mode because | Consequently, patterns that are anchored in single line mode because |
| 3962 | all branches start with ^ are not anchored in multiline mode, and a | all branches start with ^ are not anchored in multiline mode, and a |
| 3963 | match for circumflex is possible when the startoffset argument of | match for circumflex is possible when the startoffset argument of |
| 3964 | pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if | pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if |
| 3965 | PCRE_MULTILINE is set. | PCRE_MULTILINE is set. |
| 3966 | ||
| 3967 | Note that the sequences \A, \Z, and \z can be used to match the start | Note that the sequences \A, \Z, and \z can be used to match the start |
| 3968 | and end of the subject in both modes, and if all branches of a pattern | and end of the subject in both modes, and if all branches of a pattern |
| 3969 | start with \A it is always anchored, whether or not PCRE_MULTILINE is | start with \A it is always anchored, whether or not PCRE_MULTILINE is |
| 3970 | set. | set. |
| 3971 | ||
| 3972 | ||
| 3973 | FULL STOP (PERIOD, DOT) AND \N | FULL STOP (PERIOD, DOT) AND \N |
| 3974 | ||
| 3975 | Outside a character class, a dot in the pattern matches any one charac- | Outside a character class, a dot in the pattern matches any one charac- |
| 3976 | ter in the subject string except (by default) a character that signi- | ter in the subject string except (by default) a character that signi- |
| 3977 | fies the end of a line. In UTF-8 mode, the matched character may be | fies the end of a line. In UTF-8 mode, the matched character may be |
| 3978 | more than one byte long. | more than one byte long. |
| 3979 | ||
| 3980 | When a line ending is defined as a single character, dot never matches | When a line ending is defined as a single character, dot never matches |
| 3981 | that character; when the two-character sequence CRLF is used, dot does | that character; when the two-character sequence CRLF is used, dot does |
| 3982 | not match CR if it is immediately followed by LF, but otherwise it | not match CR if it is immediately followed by LF, but otherwise it |
| 3983 | matches all characters (including isolated CRs and LFs). When any Uni- | matches all characters (including isolated CRs and LFs). When any Uni- |
| 3984 | code line endings are being recognized, dot does not match CR or LF or | code line endings are being recognized, dot does not match CR or LF or |
| 3985 | any of the other line ending characters. | any of the other line ending characters. |
| 3986 | ||
| 3987 | The behaviour of dot with regard to newlines can be changed. If the | The behaviour of dot with regard to newlines can be changed. If the |
| 3988 | PCRE_DOTALL option is set, a dot matches any one character, without | PCRE_DOTALL option is set, a dot matches any one character, without |
| 3989 | exception. If the two-character sequence CRLF is present in the subject | exception. If the two-character sequence CRLF is present in the subject |
| 3990 | string, it takes two dots to match it. | string, it takes two dots to match it. |
| 3991 | ||
| 3992 | The handling of dot is entirely independent of the handling of circum- | The handling of dot is entirely independent of the handling of circum- |
| 3993 | flex and dollar, the only relationship being that they both involve | flex and dollar, the only relationship being that they both involve |
| 3994 | newlines. Dot has no special meaning in a character class. | newlines. Dot has no special meaning in a character class. |
| 3995 | ||
| 3996 | The escape sequence \N behaves like a dot, except that it is not | The escape sequence \N behaves like a dot, except that it is not |
| 3997 | affected by the PCRE_DOTALL option. In other words, it matches any | affected by the PCRE_DOTALL option. In other words, it matches any |
| 3998 | character except one that signifies the end of a line. | character except one that signifies the end of a line. |
| 3999 | ||
| 4000 | ||
| 4001 | MATCHING A SINGLE BYTE | MATCHING A SINGLE BYTE |
| 4002 | ||
| 4003 | Outside a character class, the escape sequence \C matches any one byte, | Outside a character class, the escape sequence \C matches any one byte, |
| 4004 | both in and out of UTF-8 mode. Unlike a dot, it always matches any | both in and out of UTF-8 mode. Unlike a dot, it always matches any |
| 4005 | line-ending characters. The feature is provided in Perl in order to | line-ending characters. The feature is provided in Perl in order to |
| 4006 | match individual bytes in UTF-8 mode. Because it breaks up UTF-8 char- | match individual bytes in UTF-8 mode. Because it breaks up UTF-8 char- |
| 4007 | acters into individual bytes, the rest of the string may start with a | acters into individual bytes, the rest of the string may start with a |
| 4008 | malformed UTF-8 character. For this reason, the \C escape sequence is | malformed UTF-8 character. For this reason, the \C escape sequence is |
| 4009 | best avoided. | best avoided. |
| 4010 | ||
| 4011 | PCRE does not allow \C to appear in lookbehind assertions (described | PCRE does not allow \C to appear in lookbehind assertions (described |
| 4012 | below), because in UTF-8 mode this would make it impossible to calcu- | below), because in UTF-8 mode this would make it impossible to calcu- |
| 4013 | late the length of the lookbehind. | late the length of the lookbehind. |
| 4014 | ||
| 4015 | ||
| # | Line 3993 SQUARE BRACKETS AND CHARACTER CLASSES | Line 4019 SQUARE BRACKETS AND CHARACTER CLASSES |
| 4019 | closing square bracket. A closing square bracket on its own is not spe- | closing square bracket. A closing square bracket on its own is not spe- |
| 4020 | cial by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set, | cial by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set, |
| 4021 | a lone closing square bracket causes a compile-time error. If a closing | a lone closing square bracket causes a compile-time error. If a closing |
| 4022 | square bracket is required as a member of the class, it should be the | square bracket is required as a member of the class, it should be the |
| 4023 | first data character in the class (after an initial circumflex, if | first data character in the class (after an initial circumflex, if |
| 4024 | present) or escaped with a backslash. | present) or escaped with a backslash. |
| 4025 | ||
| 4026 | A character class matches a single character in the subject. In UTF-8 | A character class matches a single character in the subject. In UTF-8 |
| 4027 | mode, the character may be more than one byte long. A matched character | mode, the character may be more than one byte long. A matched character |
| 4028 | must be in the set of characters defined by the class, unless the first | must be in the set of characters defined by the class, unless the first |
| 4029 | character in the class definition is a circumflex, in which case the | character in the class definition is a circumflex, in which case the |
| 4030 | subject character must not be in the set defined by the class. If a | subject character must not be in the set defined by the class. If a |
| 4031 | circumflex is actually required as a member of the class, ensure it is | circumflex is actually required as a member of the class, ensure it is |
| 4032 | not the first character, or escape it with a backslash. | not the first character, or escape it with a backslash. |
| 4033 | ||
| 4034 | For example, the character class [aeiou] matches any lower case vowel, | For example, the character class [aeiou] matches any lower case vowel, |
| 4035 | while [^aeiou] matches any character that is not a lower case vowel. | while [^aeiou] matches any character that is not a lower case vowel. |
| 4036 | Note that a circumflex is just a convenient notation for specifying the | Note that a circumflex is just a convenient notation for specifying the |
| 4037 | characters that are in the class by enumerating those that are not. A | characters that are in the class by enumerating those that are not. A |
| 4038 | class that starts with a circumflex is not an assertion; it still con- | class that starts with a circumflex is not an assertion; it still con- |
| 4039 | sumes a character from the subject string, and therefore it fails if | sumes a character from the subject string, and therefore it fails if |
| 4040 | the current pointer is at the end of the string. | the current pointer is at the end of the string. |
| 4041 | ||
| 4042 | In UTF-8 mode, characters with values greater than 255 can be included | In UTF-8 mode, characters with values greater than 255 can be included |
| 4043 | in a class as a literal string of bytes, or by using the \x{ escaping | in a class as a literal string of bytes, or by using the \x{ escaping |
| 4044 | mechanism. | mechanism. |
| 4045 | ||
| 4046 | When caseless matching is set, any letters in a class represent both | When caseless matching is set, any letters in a class represent both |
| 4047 | their upper case and lower case versions, so for example, a caseless | their upper case and lower case versions, so for example, a caseless |
| 4048 | [aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not | [aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not |
| 4049 | match "A", whereas a caseful version would. In UTF-8 mode, PCRE always | match "A", whereas a caseful version would. In UTF-8 mode, PCRE always |
| 4050 | understands the concept of case for characters whose values are less | understands the concept of case for characters whose values are less |
| 4051 | than 128, so caseless matching is always possible. For characters with | than 128, so caseless matching is always possible. For characters with |
| 4052 | higher values, the concept of case is supported if PCRE is compiled | higher values, the concept of case is supported if PCRE is compiled |
| 4053 | with Unicode property support, but not otherwise. If you want to use | with Unicode property support, but not otherwise. If you want to use |
| 4054 | caseless matching in UTF8-mode for characters 128 and above, you must | caseless matching in UTF8-mode for characters 128 and above, you must |
| 4055 | ensure that PCRE is compiled with Unicode property support as well as | ensure that PCRE is compiled with Unicode property support as well as |
| 4056 | with UTF-8 support. | with UTF-8 support. |
| 4057 | ||
| 4058 | Characters that might indicate line breaks are never treated in any | Characters that might indicate line breaks are never treated in any |
| 4059 | special way when matching character classes, whatever line-ending | special way when matching character classes, whatever line-ending |
| 4060 | sequence is in use, and whatever setting of the PCRE_DOTALL and | sequence is in use, and whatever setting of the PCRE_DOTALL and |
| 4061 | PCRE_MULTILINE options is used. A class such as [^a] always matches one | PCRE_MULTILINE options is used. A class such as [^a] always matches one |
| 4062 | of these characters. | of these characters. |
| 4063 | ||
| 4064 | The minus (hyphen) character can be used to specify a range of charac- | The minus (hyphen) character can be used to specify a range of charac- |
| 4065 | ters in a character class. For example, [d-m] matches any letter | ters in a character class. For example, [d-m] matches any letter |
| 4066 | between d and m, inclusive. If a minus character is required in a | between d and m, inclusive. If a minus character is required in a |
| 4067 | class, it must be escaped with a backslash or appear in a position | class, it must be escaped with a backslash or appear in a position |
| 4068 | where it cannot be interpreted as indicating a range, typically as the | where it cannot be interpreted as indicating a range, typically as the |
| 4069 | first or last character in the class. | first or last character in the class. |
| 4070 | ||
| 4071 | It is not possible to have the literal character "]" as the end charac- | It is not possible to have the literal character "]" as the end charac- |
| 4072 | ter of a range. A pattern such as [W-]46] is interpreted as a class of | ter of a range. A pattern such as [W-]46] is interpreted as a class of |
| 4073 | two characters ("W" and "-") followed by a literal string "46]", so it | two characters ("W" and "-") followed by a literal string "46]", so it |
| 4074 | would match "W46]" or "-46]". However, if the "]" is escaped with a | would match "W46]" or "-46]". However, if the "]" is escaped with a |
| 4075 | backslash it is interpreted as the end of range, so [W-\]46] is inter- | backslash it is interpreted as the end of range, so [W-\]46] is inter- |
| 4076 | preted as a class containing a range followed by two other characters. | preted as a class containing a range followed by two other characters. |
| 4077 | The octal or hexadecimal representation of "]" can also be used to end | The octal or hexadecimal representation of "]" can also be used to end |
| 4078 | a range. | a range. |
| 4079 | ||
| 4080 | Ranges operate in the collating sequence of character values. They can | Ranges operate in the collating sequence of character values. They can |
| 4081 | also be used for characters specified numerically, for example | also be used for characters specified numerically, for example |
| 4082 | [\000-\037]. In UTF-8 mode, ranges can include characters whose values | [\000-\037]. In UTF-8 mode, ranges can include characters whose values |
| 4083 | are greater than 255, for example [\x{100}-\x{2ff}]. | are greater than 255, for example [\x{100}-\x{2ff}]. |
| 4084 | ||
| 4085 | If a range that includes letters is used when caseless matching is set, | If a range that includes letters is used when caseless matching is set, |
| 4086 | it matches the letters in either case. For example, [W-c] is equivalent | it matches the letters in either case. For example, [W-c] is equivalent |
| 4087 | to [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if | to [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if |
| 4088 | character tables for a French locale are in use, [\xc8-\xcb] matches | character tables for a French locale are in use, [\xc8-\xcb] matches |
| 4089 | accented E characters in both cases. In UTF-8 mode, PCRE supports the | accented E characters in both cases. In UTF-8 mode, PCRE supports the |
| 4090 | concept of case for characters with values greater than 128 only when | concept of case for characters with values greater than 128 only when |
| 4091 | it is compiled with Unicode property support. | it is compiled with Unicode property support. |
| 4092 | ||
| 4093 | The character types \d, \D, \h, \H, \p, \P, \s, \S, \v, \V, \w, and \W | The character escape sequences \d, \D, \h, \H, \p, \P, \s, \S, \v, \V, |
| 4094 | may also appear in a character class, and add the characters that they | \w, and \W may appear in a character class, and add the characters that |
| 4095 | match to the class. For example, [\dABCDEF] matches any hexadecimal | they match to the class. For example, [\dABCDEF] matches any hexadeci- |
| 4096 | digit. A circumflex can conveniently be used with the upper case char- | mal digit. In UTF-8 mode, the PCRE_UCP option affects the meanings of |
| 4097 | acter types to specify a more restricted set of characters than the | \d, \s, \w and their upper case partners, just as it does when they |
| 4098 | matching lower case type. For example, the class [^\W_] matches any | appear outside a character class, as described in the section entitled |
| 4099 | letter or digit, but not underscore. | "Generic character types" above. The escape sequence \b has a different |
| 4100 | meaning inside a character class; it matches the backspace character. | |
| 4101 | The only metacharacters that are recognized in character classes are | The sequences \B, \N, \R, and \X are not special inside a character |
| 4102 | backslash, hyphen (only where it can be interpreted as specifying a | class. Like any other unrecognized escape sequences, they are treated |
| 4103 | range), circumflex (only at the start), opening square bracket (only | as the literal characters "B", "N", "R", and "X" by default, but cause |
| 4104 | when it can be interpreted as introducing a POSIX class name - see the | an error if the PCRE_EXTRA option is set. |
| 4105 | next section), and the terminating closing square bracket. However, | |
| 4106 | A circumflex can conveniently be used with the upper case character | |
| 4107 | types to specify a more restricted set of characters than the matching | |
| 4108 | lower case type. For example, the class [^\W_] matches any letter or | |
| 4109 | digit, but not underscore, whereas [\w] includes underscore. A positive | |
| 4110 | character class should be read as "something OR something OR ..." and a | |
| 4111 | negative class as "NOT something AND NOT something AND NOT ...". | |
| 4112 | ||
| 4113 | The only metacharacters that are recognized in character classes are | |
| 4114 | backslash, hyphen (only where it can be interpreted as specifying a | |
| 4115 | range), circumflex (only at the start), opening square bracket (only | |
| 4116 | when it can be interpreted as introducing a POSIX class name - see the | |
| 4117 | next section), and the terminating closing square bracket. However, | |
| 4118 | escaping other non-alphanumeric characters does no harm. | escaping other non-alphanumeric characters does no harm. |
| 4119 | ||
| 4120 | ||
| 4121 | POSIX CHARACTER CLASSES | POSIX CHARACTER CLASSES |
| 4122 | ||
| 4123 | Perl supports the POSIX notation for character classes. This uses names | Perl supports the POSIX notation for character classes. This uses names |
| 4124 | enclosed by [: and :] within the enclosing square brackets. PCRE also | enclosed by [: and :] within the enclosing square brackets. PCRE also |
| 4125 | supports this notation. For example, | supports this notation. For example, |
| 4126 | ||
| 4127 | [01[:alpha:]%] | [01[:alpha:]%] |
| # | Line 4106 POSIX CHARACTER CLASSES | Line 4144 POSIX CHARACTER CLASSES |
| 4144 | word "word" characters (same as \w) | word "word" characters (same as \w) |
| 4145 | xdigit hexadecimal digits | xdigit hexadecimal digits |
| 4146 | ||
| 4147 | The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), | The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), |
| 4148 | and space (32). Notice that this list includes the VT character (code | and space (32). Notice that this list includes the VT character (code |
| 4149 | 11). This makes "space" different to \s, which does not include VT (for | 11). This makes "space" different to \s, which does not include VT (for |
| 4150 | Perl compatibility). | Perl compatibility). |
| 4151 | ||
| 4152 | The name "word" is a Perl extension, and "blank" is a GNU extension | The name "word" is a Perl extension, and "blank" is a GNU extension |
| 4153 | from Perl 5.8. Another Perl extension is negation, which is indicated | from Perl 5.8. Another Perl extension is negation, which is indicated |
| 4154 | by a ^ character after the colon. For example, | by a ^ character after the colon. For example, |
| 4155 | ||
| 4156 | [12[:^digit:]] | [12[:^digit:]] |
| 4157 | ||
| 4158 | matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the | matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the |
| 4159 | POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but | POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but |
| 4160 | these are not supported, and an error is given if they are encountered. | these are not supported, and an error is given if they are encountered. |
| 4161 | ||
| 4162 | By default, in UTF-8 mode, characters with values greater than 128 do | By default, in UTF-8 mode, characters with values greater than 128 do |
| 4163 | not match any of the POSIX character classes. However, if the PCRE_UCP | not match any of the POSIX character classes. However, if the PCRE_UCP |
| 4164 | option is passed to pcre_compile(), some of the classes are changed so | option is passed to pcre_compile(), some of the classes are changed so |
| 4165 | that Unicode character properties are used. This is achieved by replac- | that Unicode character properties are used. This is achieved by replac- |
| 4166 | ing the POSIX classes by other sequences, as follows: | ing the POSIX classes by other sequences, as follows: |
| 4167 | ||
| # | Line 4136 POSIX CHARACTER CLASSES | Line 4174 POSIX CHARACTER CLASSES |
| 4174 | [:upper:] becomes \p{Lu} | [:upper:] becomes \p{Lu} |
| 4175 | [:word:] becomes \p{Xwd} | [:word:] becomes \p{Xwd} |
| 4176 | ||
| 4177 | Negated versions, such as [:^alpha:] use \P instead of \p. The other | Negated versions, such as [:^alpha:] use \P instead of \p. The other |
| 4178 | POSIX classes are unchanged, and match only characters with code points | POSIX classes are unchanged, and match only characters with code points |
| 4179 | less than 128. | less than 128. |
| 4180 | ||
| 4181 | ||
| 4182 | VERTICAL BAR | VERTICAL BAR |
| 4183 | ||
| 4184 | Vertical bar characters are used to separate alternative patterns. For | Vertical bar characters are used to separate alternative patterns. For |
| 4185 | example, the pattern | example, the pattern |
| 4186 | ||
| 4187 | gilbert|sullivan | gilbert|sullivan |
| 4188 | ||
| 4189 | matches either "gilbert" or "sullivan". Any number of alternatives may | matches either "gilbert" or "sullivan". Any number of alternatives may |
| 4190 | appear, and an empty alternative is permitted (matching the empty | appear, and an empty alternative is permitted (matching the empty |
| 4191 | string). The matching process tries each alternative in turn, from left | string). The matching process tries each alternative in turn, from left |
| 4192 | to right, and the first one that succeeds is used. If the alternatives | to right, and the first one that succeeds is used. If the alternatives |
| 4193 | are within a subpattern (defined below), "succeeds" means matching the | are within a subpattern (defined below), "succeeds" means matching the |
| 4194 | rest of the main pattern as well as the alternative in the subpattern. | rest of the main pattern as well as the alternative in the subpattern. |
| 4195 | ||
| 4196 | ||
| 4197 | INTERNAL OPTION SETTING | INTERNAL OPTION SETTING |
| 4198 | ||
| 4199 | The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and | The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
| 4200 | PCRE_EXTENDED options (which are Perl-compatible) can be changed from | PCRE_EXTENDED options (which are Perl-compatible) can be changed from |
| 4201 | within the pattern by a sequence of Perl option letters enclosed | within the pattern by a sequence of Perl option letters enclosed |
| 4202 | between "(?" and ")". The option letters are | between "(?" and ")". The option letters are |
| 4203 | ||
| 4204 | i for PCRE_CASELESS | i for PCRE_CASELESS |
| # | Line 4170 INTERNAL OPTION SETTING | Line 4208 INTERNAL OPTION SETTING |
| 4208 | ||
| 4209 | For example, (?im) sets caseless, multiline matching. It is also possi- | For example, (?im) sets caseless, multiline matching. It is also possi- |
| 4210 | ble to unset these options by preceding the letter with a hyphen, and a | ble to unset these options by preceding the letter with a hyphen, and a |
| 4211 | combined setting and unsetting such as (?im-sx), which sets PCRE_CASE- | combined setting and unsetting such as (?im-sx), which sets PCRE_CASE- |
| 4212 | LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, | LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, |
| 4213 | is also permitted. If a letter appears both before and after the | is also permitted. If a letter appears both before and after the |
| 4214 | hyphen, the option is unset. | hyphen, the option is unset. |
| 4215 | ||
| 4216 | The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA | The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA |
| 4217 | can be changed in the same way as the Perl-compatible options by using | can be changed in the same way as the Perl-compatible options by using |
| 4218 | the characters J, U and X respectively. | the characters J, U and X respectively. |
| 4219 | ||
| 4220 | When one of these option changes occurs at top level (that is, not | When one of these option changes occurs at top level (that is, not |
| 4221 | inside subpattern parentheses), the change applies to the remainder of | inside subpattern parentheses), the change applies to the remainder of |
| 4222 | the pattern that follows. If the change is placed right at the start of | the pattern that follows. If the change is placed right at the start of |
| 4223 | a pattern, PCRE extracts it into the global options (and it will there- | a pattern, PCRE extracts it into the global options (and it will there- |
| 4224 | fore show up in data extracted by the pcre_fullinfo() function). | fore show up in data extracted by the pcre_fullinfo() function). |
| 4225 | ||
| 4226 | An option change within a subpattern (see below for a description of | An option change within a subpattern (see below for a description of |
| 4227 | subpatterns) affects only that part of the subpattern that follows it, | subpatterns) affects only that part of the subpattern that follows it, |
| 4228 | so | so |
| 4229 | ||
| 4230 | (a(?i)b)c | (a(?i)b)c |
| 4231 | ||
| 4232 | matches abc and aBc and no other strings (assuming PCRE_CASELESS is not | matches abc and aBc and no other strings (assuming PCRE_CASELESS is not |
| 4233 | used). By this means, options can be made to have different settings | used). By this means, options can be made to have different settings |
| 4234 | in different parts of the pattern. Any changes made in one alternative | in different parts of the pattern. Any changes made in one alternative |
| 4235 | do carry on into subsequent branches within the same subpattern. For | do carry on into subsequent branches within the same subpattern. For |
| 4236 | example, | example, |
| 4237 | ||
| 4238 | (a(?i)b|c) | (a(?i)b|c) |
| 4239 | ||
| 4240 | matches "ab", "aB", "c", and "C", even though when matching "C" the | matches "ab", "aB", "c", and "C", even though when matching "C" the |
| 4241 | first branch is abandoned before the option setting. This is because | first branch is abandoned before the option setting. This is because |
| 4242 | the effects of option settings happen at compile time. There would be | the effects of option settings happen at compile time. There would be |
| 4243 | some very weird behaviour otherwise. | some very weird behaviour otherwise. |
| 4244 | ||
| 4245 | Note: There are other PCRE-specific options that can be set by the | Note: There are other PCRE-specific options that can be set by the |
| 4246 | application when the compile or match functions are called. In some | application when the compile or match functions are called. In some |
| 4247 | cases the pattern can contain special leading sequences such as (*CRLF) | cases the pattern can contain special leading sequences such as (*CRLF) |
| 4248 | to override what the application has set or what has been defaulted. | to override what the application has set or what has been defaulted. |
| 4249 | Details are given in the section entitled "Newline sequences" above. | Details are given in the section entitled "Newline sequences" above. |
| 4250 | There are also the (*UTF8) and (*UCP) leading sequences that can be | There are also the (*UTF8) and (*UCP) leading sequences that can be |
| 4251 | used to set UTF-8 and Unicode property modes; they are equivalent to | used to set UTF-8 and Unicode property modes; they are equivalent to |
| 4252 | setting the PCRE_UTF8 and the PCRE_UCP options, respectively. | setting the PCRE_UTF8 and the PCRE_UCP options, respectively. |
| 4253 | ||
| 4254 | ||
| # | Line 4223 SUBPATTERNS | Line 4261 SUBPATTERNS |
| 4261 | ||
| 4262 | cat(aract|erpillar|) | cat(aract|erpillar|) |
| 4263 | ||
| 4264 | matches "cataract", "caterpillar", or "cat". Without the parentheses, | matches "cataract", "caterpillar", or "cat". Without the parentheses, |
| 4265 | it would match "cataract", "erpillar" or an empty string. | it would match "cataract", "erpillar" or an empty string. |
| 4266 | ||
| 4267 | 2. It sets up the subpattern as a capturing subpattern. This means | 2. It sets up the subpattern as a capturing subpattern. This means |
| 4268 | that, when the whole pattern matches, that portion of the subject | that, when the whole pattern matches, that portion of the subject |
| 4269 | string that matched the subpattern is passed back to the caller via the | string that matched the subpattern is passed back to the caller via the |
| 4270 | ovector argument of pcre_exec(). Opening parentheses are counted from | ovector argument of pcre_exec(). Opening parentheses are counted from |
| 4271 | left to right (starting from 1) to obtain numbers for the capturing | left to right (starting from 1) to obtain numbers for the capturing |
| 4272 | subpatterns. For example, if the string "the red king" is matched | subpatterns. For example, if the string "the red king" is matched |
| 4273 | against the pattern | against the pattern |
| 4274 | ||
| 4275 | the ((red|white) (king|queen)) | the ((red|white) (king|queen)) |
| # | Line 4239 SUBPATTERNS | Line 4277 SUBPATTERNS |
| 4277 | the captured substrings are "red king", "red", and "king", and are num- | the captured substrings are "red king", "red", and "king", and are num- |
| 4278 | bered 1, 2, and 3, respectively. | bered 1, 2, and 3, respectively. |
| 4279 | ||
| 4280 | The fact that plain parentheses fulfil two functions is not always | The fact that plain parentheses fulfil two functions is not always |
| 4281 | helpful. There are often times when a grouping subpattern is required | helpful. There are often times when a grouping subpattern is required |
| 4282 | without a capturing requirement. If an opening parenthesis is followed | without a capturing requirement. If an opening parenthesis is followed |
| 4283 | by a question mark and a colon, the subpattern does not do any captur- | by a question mark and a colon, the subpattern does not do any captur- |
| 4284 | ing, and is not counted when computing the number of any subsequent | ing, and is not counted when computing the number of any subsequent |
| 4285 | capturing subpatterns. For example, if the string "the white queen" is | capturing subpatterns. For example, if the string "the white queen" is |
| 4286 | matched against the pattern | matched against the pattern |
| 4287 | ||
| 4288 | the ((?:red|white) (king|queen)) | the ((?:red|white) (king|queen)) |
| # | Line 4252 SUBPATTERNS | Line 4290 SUBPATTERNS |
| 4290 | the captured substrings are "white queen" and "queen", and are numbered | the captured substrings are "white queen" and "queen", and are numbered |
| 4291 | 1 and 2. The maximum number of capturing subpatterns is 65535. | 1 and 2. The maximum number of capturing subpatterns is 65535. |
| 4292 | ||
| 4293 | As a convenient shorthand, if any option settings are required at the | As a convenient shorthand, if any option settings are required at the |
| 4294 | start of a non-capturing subpattern, the option letters may appear | start of a non-capturing subpattern, the option letters may appear |
| 4295 | between the "?" and the ":". Thus the two patterns | between the "?" and the ":". Thus the two patterns |
| 4296 | ||
| 4297 | (?i:saturday|sunday) | (?i:saturday|sunday) |
| 4298 | (?:(?i)saturday|sunday) | (?:(?i)saturday|sunday) |
| 4299 | ||
| 4300 | match exactly the same set of strings. Because alternative branches are | match exactly the same set of strings. Because alternative branches are |
| 4301 | tried from left to right, and options are not reset until the end of | tried from left to right, and options are not reset until the end of |
| 4302 | the subpattern is reached, an option setting in one branch does affect | the subpattern is reached, an option setting in one branch does affect |
| 4303 | subsequent branches, so the above patterns match "SUNDAY" as well as | subsequent branches, so the above patterns match "SUNDAY" as well as |
| 4304 | "Saturday". | "Saturday". |
| 4305 | ||
| 4306 | ||
| 4307 | DUPLICATE SUBPATTERN NUMBERS | DUPLICATE SUBPATTERN NUMBERS |
| 4308 | ||
| 4309 | Perl 5.10 introduced a feature whereby each alternative in a subpattern | Perl 5.10 introduced a feature whereby each alternative in a subpattern |
| 4310 | uses the same numbers for its capturing parentheses. Such a subpattern | uses the same numbers for its capturing parentheses. Such a subpattern |
| 4311 | starts with (?| and is itself a non-capturing subpattern. For example, | starts with (?| and is itself a non-capturing subpattern. For example, |
| 4312 | consider this pattern: | consider this pattern: |
| 4313 | ||
| 4314 | (?|(Sat)ur|(Sun))day | (?|(Sat)ur|(Sun))day |
| 4315 | ||
| 4316 | Because the two alternatives are inside a (?| group, both sets of cap- | Because the two alternatives are inside a (?| group, both sets of cap- |
| 4317 | turing parentheses are numbered one. Thus, when the pattern matches, | turing parentheses are numbered one. Thus, when the pattern matches, |
| 4318 | you can look at captured substring number one, whichever alternative | you can look at captured substring number one, whichever alternative |
| 4319 | matched. This construct is useful when you want to capture part, but | matched. This construct is useful when you want to capture part, but |
| 4320 | not all, of one of a number of alternatives. Inside a (?| group, paren- | not all, of one of a number of alternatives. Inside a (?| group, paren- |
| 4321 | theses are numbered as usual, but the number is reset at the start of | theses are numbered as usual, but the number is reset at the start of |
| 4322 | each branch. The numbers of any capturing parentheses that follow the | each branch. The numbers of any capturing parentheses that follow the |
| 4323 | subpattern start after the highest number used in any branch. The fol- | subpattern start after the highest number used in any branch. The fol- |
| 4324 | lowing example is taken from the Perl documentation. The numbers under- | lowing example is taken from the Perl documentation. The numbers under- |
| 4325 | neath show in which buffer the captured content will be stored. | neath show in which buffer the captured content will be stored. |
| 4326 | ||
| # | Line 4290 DUPLICATE SUBPATTERN NUMBERS | Line 4328 DUPLICATE SUBPATTERN NUMBERS |
| 4328 | / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x | / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
| 4329 | # 1 2 2 3 2 3 4 | # 1 2 2 3 2 3 4 |
| 4330 | ||
| 4331 | A back reference to a numbered subpattern uses the most recent value | A back reference to a numbered subpattern uses the most recent value |
| 4332 | that is set for that number by any subpattern. The following pattern | that is set for that number by any subpattern. The following pattern |
| 4333 | matches "abcabc" or "defdef": | matches "abcabc" or "defdef": |
| 4334 | ||
| 4335 | /(?|(abc)|(def))\1/ | /(?|(abc)|(def))\1/ |
| 4336 | ||
| 4337 | In contrast, a recursive or "subroutine" call to a numbered subpattern | In contrast, a recursive or "subroutine" call to a numbered subpattern |
| 4338 | always refers to the first one in the pattern with the given number. | always refers to the first one in the pattern with the given number. |
| 4339 | The following pattern matches "abcabc" or "defabc": | The following pattern matches "abcabc" or "defabc": |
| 4340 | ||
| 4341 | /(?|(abc)|(def))(?1)/ | /(?|(abc)|(def))(?1)/ |
| 4342 | ||
| 4343 | If a condition test for a subpattern's having matched refers to a non- | If a condition test for a subpattern's having matched refers to a non- |
| 4344 | unique number, the test is true if any of the subpatterns of that num- | unique number, the test is true if any of the subpatterns of that num- |
| 4345 | ber have matched. | ber have matched. |
| 4346 | ||
| 4347 | An alternative approach to using this "branch reset" feature is to use | An alternative approach to using this "branch reset" feature is to use |
| 4348 | duplicate named subpatterns, as described in the next section. | duplicate named subpatterns, as described in the next section. |
| 4349 | ||
| 4350 | ||
| 4351 | NAMED SUBPATTERNS | NAMED SUBPATTERNS |
| 4352 | ||
| 4353 | Identifying capturing parentheses by number is simple, but it can be | Identifying capturing parentheses by number is simple, but it can be |
| 4354 | very hard to keep track of the numbers in complicated regular expres- | very hard to keep track of the numbers in complicated regular expres- |
| 4355 | sions. Furthermore, if an expression is modified, the numbers may | sions. Furthermore, if an expression is modified, the numbers may |
| 4356 | change. To help with this difficulty, PCRE supports the naming of sub- | change. To help with this difficulty, PCRE supports the naming of sub- |
| 4357 | patterns. This feature was not added to Perl until release 5.10. Python | patterns. This feature was not added to Perl until release 5.10. Python |
| 4358 | had the feature earlier, and PCRE introduced it at release 4.0, using | had the feature earlier, and PCRE introduced it at release 4.0, using |
| 4359 | the Python syntax. PCRE now supports both the Perl and the Python syn- | the Python syntax. PCRE now supports both the Perl and the Python syn- |
| 4360 | tax. Perl allows identically numbered subpatterns to have different | tax. Perl allows identically numbered subpatterns to have different |
| 4361 | names, but PCRE does not. | names, but PCRE does not. |
| 4362 | ||
| 4363 | In PCRE, a subpattern can be named in one of three ways: (?<name>...) | In PCRE, a subpattern can be named in one of three ways: (?<name>...) |
| 4364 | or (?'name'...) as in Perl, or (?P<name>...) as in Python. References | or (?'name'...) as in Perl, or (?P<name>...) as in Python. References |
| 4365 | to capturing parentheses from other parts of the pattern, such as back | to capturing parentheses from other parts of the pattern, such as back |
| 4366 | references, recursion, and conditions, can be made by name as well as | references, recursion, and conditions, can be made by name as well as |
| 4367 | by number. | by number. |
| 4368 | ||
| 4369 | Names consist of up to 32 alphanumeric characters and underscores. | Names consist of up to 32 alphanumeric characters and underscores. |
| 4370 | Named capturing parentheses are still allocated numbers as well as | Named capturing parentheses are still allocated numbers as well as |
| 4371 | names, exactly as if the names were not present. The PCRE API provides | names, exactly as if the names were not present. The PCRE API provides |
| 4372 | function calls for extracting the name-to-number translation table from | function calls for extracting the name-to-number translation table from |
| 4373 | a compiled pattern. There is also a convenience function for extracting | a compiled pattern. There is also a convenience function for extracting |
| 4374 | a captured substring by name. | a captured substring by name. |
| 4375 | ||
| 4376 | By default, a name must be unique within a pattern, but it is possible | By default, a name must be unique within a pattern, but it is possible |
| 4377 | to relax this constraint by setting the PCRE_DUPNAMES option at compile | to relax this constraint by setting the PCRE_DUPNAMES option at compile |
| 4378 | time. (Duplicate names are also always permitted for subpatterns with | time. (Duplicate names are also always permitted for subpatterns with |
| 4379 | the same number, set up as described in the previous section.) Dupli- | the same number, set up as described in the previous section.) Dupli- |
| 4380 | cate names can be useful for patterns where only one instance of the | cate names can be useful for patterns where only one instance of the |
| 4381 | named parentheses can match. Suppose you want to match the name of a | named parentheses can match. Suppose you want to match the name of a |
| 4382 | weekday, either as a 3-letter abbreviation or as the full name, and in | weekday, either as a 3-letter abbreviation or as the full name, and in |
| 4383 | both cases you want to extract the abbreviation. This pattern (ignoring | both cases you want to extract the abbreviation. This pattern (ignoring |
| 4384 | the line breaks) does the job: | the line breaks) does the job: |
| 4385 | ||
| # | Line 4351 NAMED SUBPATTERNS | Line 4389 NAMED SUBPATTERNS |
| 4389 | (?<DN>Thu)(?:rsday)?| | (?<DN>Thu)(?:rsday)?| |
| 4390 | (?<DN>Sat)(?:urday)? | (?<DN>Sat)(?:urday)? |
| 4391 | ||
| 4392 | There are five capturing substrings, but only one is ever set after a | There are five capturing substrings, but only one is ever set after a |
| 4393 | match. (An alternative way of solving this problem is to use a "branch | match. (An alternative way of solving this problem is to use a "branch |
| 4394 | reset" subpattern, as described in the previous section.) | reset" subpattern, as described in the previous section.) |
| 4395 | ||
| 4396 | The convenience function for extracting the data by name returns the | The convenience function for extracting the data by name returns the |
| 4397 | substring for the first (and in this example, the only) subpattern of | substring for the first (and in this example, the only) subpattern of |
| 4398 | that name that matched. This saves searching to find which numbered | that name that matched. This saves searching to find which numbered |
| 4399 | subpattern it was. | subpattern it was. |
| 4400 | ||
| 4401 | If you make a back reference to a non-unique named subpattern from | If you make a back reference to a non-unique named subpattern from |
| 4402 | elsewhere in the pattern, the one that corresponds to the first occur- | elsewhere in the pattern, the one that corresponds to the first occur- |
| 4403 | rence of the name is used. In the absence of duplicate numbers (see the | rence of the name is used. In the absence of duplicate numbers (see the |
| 4404 | previous section) this is the one with the lowest number. If you use a | previous section) this is the one with the lowest number. If you use a |
| 4405 | named reference in a condition test (see the section about conditions | named reference in a condition test (see the section about conditions |
| 4406 | below), either to check whether a subpattern has matched, or to check | below), either to check whether a subpattern has matched, or to check |
| 4407 | for recursion, all subpatterns with the same name are tested. If the | for recursion, all subpatterns with the same name are tested. If the |
| 4408 | condition is true for any one of them, the overall condition is true. | condition is true for any one of them, the overall condition is true. |
| 4409 | This is the same behaviour as testing by number. For further details of | This is the same behaviour as testing by number. For further details of |
| 4410 | the interfaces for handling named subpatterns, see the pcreapi documen- | the interfaces for handling named subpatterns, see the pcreapi documen- |
| 4411 | tation. | tation. |
| 4412 | ||
| 4413 | Warning: You cannot use different names to distinguish between two sub- | Warning: You cannot use different names to distinguish between two sub- |
| 4414 | patterns with the same number because PCRE uses only the numbers when | patterns with the same number because PCRE uses only the numbers when |
| 4415 | matching. For this reason, an error is given at compile time if differ- | matching. For this reason, an error is given at compile time if differ- |
| 4416 | ent names are given to subpatterns with the same number. However, you | ent names are given to subpatterns with the same number. However, you |
| 4417 | can give the same name to subpatterns with the same number, even when | can give the same name to subpatterns with the same number, even when |
| 4418 | PCRE_DUPNAMES is not set. | PCRE_DUPNAMES is not set. |
| 4419 | ||
| 4420 | ||
| 4421 | REPETITION | REPETITION |
| 4422 | ||
| 4423 | Repetition is specified by quantifiers, which can follow any of the | Repetition is specified by quantifiers, which can follow any of the |
| 4424 | following items: | following items: |
| 4425 | ||
| 4426 | a literal data character | a literal data character |
| # | Line 4396 REPETITION | Line 4434 REPETITION |
| 4434 | a parenthesized subpattern (unless it is an assertion) | a parenthesized subpattern (unless it is an assertion) |
| 4435 | a recursive or "subroutine" call to a subpattern | a recursive or "subroutine" call to a subpattern |
| 4436 | ||
| 4437 | The general repetition quantifier specifies a minimum and maximum num- | The general repetition quantifier specifies a minimum and maximum num- |
| 4438 | ber of permitted matches, by giving the two numbers in curly brackets | ber of permitted matches, by giving the two numbers in curly brackets |
| 4439 | (braces), separated by a comma. The numbers must be less than 65536, | (braces), separated by a comma. The numbers must be less than 65536, |
| 4440 | and the first must be less than or equal to the second. For example: | and the first must be less than or equal to the second. For example: |
| 4441 | ||
| 4442 | z{2,4} | z{2,4} |
| 4443 | ||
| 4444 | matches "zz", "zzz", or "zzzz". A closing brace on its own is not a | matches "zz", "zzz", or "zzzz". A closing brace on its own is not a |
| 4445 | special character. If the second number is omitted, but the comma is | special character. If the second number is omitted, but the comma is |
| 4446 | present, there is no upper limit; if the second number and the comma | present, there is no upper limit; if the second number and the comma |
| 4447 | are both omitted, the quantifier specifies an exact number of required | are both omitted, the quantifier specifies an exact number of required |
| 4448 | matches. Thus | matches. Thus |
| 4449 | ||
| 4450 | [aeiou]{3,} | [aeiou]{3,} |
| # | Line 4415 REPETITION | Line 4453 REPETITION |
| 4453 | ||
| 4454 | \d{8} | \d{8} |
| 4455 | ||
| 4456 | matches exactly 8 digits. An opening curly bracket that appears in a | matches exactly 8 digits. An opening curly bracket that appears in a |
| 4457 | position where a quantifier is not allowed, or one that does not match | position where a quantifier is not allowed, or one that does not match |
| 4458 | the syntax of a quantifier, is taken as a literal character. For exam- | the syntax of a quantifier, is taken as a literal character. For exam- |
| 4459 | ple, {,6} is not a quantifier, but a literal string of four characters. | ple, {,6} is not a quantifier, but a literal string of four characters. |
| 4460 | ||
| 4461 | In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to | In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to |
| 4462 | individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char- | individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char- |
| 4463 | acters, each of which is represented by a two-byte sequence. Similarly, | acters, each of which is represented by a two-byte sequence. Similarly, |
| 4464 | when Unicode property support is available, \X{3} matches three Unicode | when Unicode property support is available, \X{3} matches three Unicode |
| 4465 | extended sequences, each of which may be several bytes long (and they | extended sequences, each of which may be several bytes long (and they |
| 4466 | may be of different lengths). | may be of different lengths). |
| 4467 | ||
| 4468 | The quantifier {0} is permitted, causing the expression to behave as if | The quantifier {0} is permitted, causing the expression to behave as if |
| 4469 | the previous item and the quantifier were not present. This may be use- | the previous item and the quantifier were not present. This may be use- |
| 4470 | ful for subpatterns that are referenced as subroutines from elsewhere | ful for subpatterns that are referenced as subroutines from elsewhere |
| 4471 | in the pattern (but see also the section entitled "Defining subpatterns | in the pattern (but see also the section entitled "Defining subpatterns |
| 4472 | for use by reference only" below). Items other than subpatterns that | for use by reference only" below). Items other than subpatterns that |
| 4473 | have a {0} quantifier are omitted from the compiled pattern. | have a {0} quantifier are omitted from the compiled pattern. |
| 4474 | ||
| 4475 | For convenience, the three most common quantifiers have single-charac- | For convenience, the three most common quantifiers have single-charac- |
| 4476 | ter abbreviations: | ter abbreviations: |
| 4477 | ||
| 4478 | * is equivalent to {0,} | * is equivalent to {0,} |
| 4479 | + is equivalent to {1,} | + is equivalent to {1,} |
| 4480 | ? is equivalent to {0,1} | ? is equivalent to {0,1} |
| 4481 | ||
| 4482 | It is possible to construct infinite loops by following a subpattern | It is possible to construct infinite loops by following a subpattern |
| 4483 | that can match no characters with a quantifier that has no upper limit, | that can match no characters with a quantifier that has no upper limit, |
| 4484 | for example: | for example: |
| 4485 | ||
| 4486 | (a?)* | (a?)* |
| 4487 | ||
| 4488 | Earlier versions of Perl and PCRE used to give an error at compile time | Earlier versions of Perl and PCRE used to give an error at compile time |
| 4489 | for such patterns. However, because there are cases where this can be | for such patterns. However, because there are cases where this can be |
| 4490 | useful, such patterns are now accepted, but if any repetition of the | useful, such patterns are now accepted, but if any repetition of the |
| 4491 | subpattern does in fact match no characters, the loop is forcibly bro- | subpattern does in fact match no characters, the loop is forcibly bro- |
| 4492 | ken. | ken. |
| 4493 | ||
| 4494 | By default, the quantifiers are "greedy", that is, they match as much | By default, the quantifiers are "greedy", that is, they match as much |
| 4495 | as possible (up to the maximum number of permitted times), without | as possible (up to the maximum number of permitted times), without |
| 4496 | causing the rest of the pattern to fail. The classic example of where | causing the rest of the pattern to fail. The classic example of where |
| 4497 | this gives problems is in trying to match comments in C programs. These | this gives problems is in trying to match comments in C programs. These |
| 4498 | appear between /* and */ and within the comment, individual * and / | appear between /* and */ and within the comment, individual * and / |
| 4499 | characters may appear. An attempt to match C comments by applying the | characters may appear. An attempt to match C comments by applying the |
| 4500 | pattern | pattern |
| 4501 | ||
| 4502 | /\*.*\*/ | /\*.*\*/ |
| # | Line 4467 REPETITION | Line 4505 REPETITION |
| 4505 | ||
| 4506 | /* first comment */ not comment /* second comment */ | /* first comment */ not comment /* second comment */ |
| 4507 | ||
| 4508 | fails, because it matches the entire string owing to the greediness of | fails, because it matches the entire string owing to the greediness of |
| 4509 | the .* item. | the .* item. |
| 4510 | ||
| 4511 | However, if a quantifier is followed by a question mark, it ceases to | However, if a quantifier is followed by a question mark, it ceases to |
| 4512 | be greedy, and instead matches the minimum number of times possible, so | be greedy, and instead matches the minimum number of times possible, so |
| 4513 | the pattern | the pattern |
| 4514 | ||
| 4515 | /\*.*?\*/ | /\*.*?\*/ |
| 4516 | ||
| 4517 | does the right thing with the C comments. The meaning of the various | does the right thing with the C comments. The meaning of the various |
| 4518 | quantifiers is not otherwise changed, just the preferred number of | quantifiers is not otherwise changed, just the preferred number of |
| 4519 | matches. Do not confuse this use of question mark with its use as a | matches. Do not confuse this use of question mark with its use as a |
| 4520 | quantifier in its own right. Because it has two uses, it can sometimes | quantifier in its own right. Because it has two uses, it can sometimes |
| 4521 | appear doubled, as in | appear doubled, as in |
| 4522 | ||
| 4523 | \d??\d | \d??\d |
| # | Line 4487 REPETITION | Line 4525 REPETITION |
| 4525 | which matches one digit by preference, but can match two if that is the | which matches one digit by preference, but can match two if that is the |
| 4526 | only way the rest of the pattern matches. | only way the rest of the pattern matches. |
| 4527 | ||
| 4528 | If the PCRE_UNGREEDY option is set (an option that is not available in | If the PCRE_UNGREEDY option is set (an option that is not available in |
| 4529 | Perl), the quantifiers are not greedy by default, but individual ones | Perl), the quantifiers are not greedy by default, but individual ones |
| 4530 | can be made greedy by following them with a question mark. In other | can be made greedy by following them with a question mark. In other |
| 4531 | words, it inverts the default behaviour. | words, it inverts the default behaviour. |
| 4532 | ||
| 4533 | When a parenthesized subpattern is quantified with a minimum repeat | When a parenthesized subpattern is quantified with a minimum repeat |
| 4534 | count that is greater than 1 or with a limited maximum, more memory is | count that is greater than 1 or with a limited maximum, more memory is |
| 4535 | required for the compiled pattern, in proportion to the size of the | required for the compiled pattern, in proportion to the size of the |
| 4536 | minimum or maximum. | minimum or maximum. |
| 4537 | ||
| 4538 | If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv- | If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv- |
| 4539 | alent to Perl's /s) is set, thus allowing the dot to match newlines, | alent to Perl's /s) is set, thus allowing the dot to match newlines, |
| 4540 | the pattern is implicitly anchored, because whatever follows will be | the pattern is implicitly anchored, because whatever follows will be |
| 4541 | tried against every character position in the subject string, so there | tried against every character position in the subject string, so there |
| 4542 | is no point in retrying the overall match at any position after the | is no point in retrying the overall match at any position after the |
| 4543 | first. PCRE normally treats such a pattern as though it were preceded | first. PCRE normally treats such a pattern as though it were preceded |
| 4544 | by \A. | by \A. |
| 4545 | ||
| 4546 | In cases where it is known that the subject string contains no new- | In cases where it is known that the subject string contains no new- |
| 4547 | lines, it is worth setting PCRE_DOTALL in order to obtain this opti- | lines, it is worth setting PCRE_DOTALL in order to obtain this opti- |
| 4548 | mization, or alternatively using ^ to indicate anchoring explicitly. | mization, or alternatively using ^ to indicate anchoring explicitly. |
| 4549 | ||
| 4550 | However, there is one situation where the optimization cannot be used. | However, there is one situation where the optimization cannot be used. |
| 4551 | When .* is inside capturing parentheses that are the subject of a back | When .* is inside capturing parentheses that are the subject of a back |
| 4552 | reference elsewhere in the pattern, a match at the start may fail where | reference elsewhere in the pattern, a match at the start may fail where |
| 4553 | a later one succeeds. Consider, for example: | a later one succeeds. Consider, for example: |
| 4554 | ||
| 4555 | (.*)abc\1 | (.*)abc\1 |
| 4556 | ||
| 4557 | If the subject is "xyz123abc123" the match point is the fourth charac- | If the subject is "xyz123abc123" the match point is the fourth charac- |
| 4558 | ter. For this reason, such a pattern is not implicitly anchored. | ter. For this reason, such a pattern is not implicitly anchored. |
| 4559 | ||
| 4560 | When a capturing subpattern is repeated, the value captured is the sub- | When a capturing subpattern is repeated, the value captured is the sub- |
| # | Line 4525 REPETITION | Line 4563 REPETITION |
| 4563 | (tweedle[dume]{3}\s*)+ | (tweedle[dume]{3}\s*)+ |
| 4564 | ||
| 4565 | has matched "tweedledum tweedledee" the value of the captured substring | has matched "tweedledum tweedledee" the value of the captured substring |
| 4566 | is "tweedledee". However, if there are nested capturing subpatterns, | is "tweedledee". However, if there are nested capturing subpatterns, |
| 4567 | the corresponding captured values may have been set in previous itera- | the corresponding captured values may have been set in previous itera- |
| 4568 | tions. For example, after | tions. For example, after |
| 4569 | ||
| 4570 | /(a|(b))+/ | /(a|(b))+/ |
| # | Line 4536 REPETITION | Line 4574 REPETITION |
| 4574 | ||
| 4575 | ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS | ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS |
| 4576 | ||
| 4577 | With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") | With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
| 4578 | repetition, failure of what follows normally causes the repeated item | repetition, failure of what follows normally causes the repeated item |
| 4579 | to be re-evaluated to see if a different number of repeats allows the | to be re-evaluated to see if a different number of repeats allows the |
| 4580 | rest of the pattern to match. Sometimes it is useful to prevent this, | rest of the pattern to match. Sometimes it is useful to prevent this, |
| 4581 | either to change the nature of the match, or to cause it fail earlier | either to change the nature of the match, or to cause it fail earlier |
| 4582 | than it otherwise might, when the author of the pattern knows there is | than it otherwise might, when the author of the pattern knows there is |
| 4583 | no point in carrying on. | no point in carrying on. |
| 4584 | ||
| 4585 | Consider, for example, the pattern \d+foo when applied to the subject | Consider, for example, the pattern \d+foo when applied to the subject |
| 4586 | line | line |
| 4587 | ||
| 4588 | 123456bar | 123456bar |
| 4589 | ||
| 4590 | After matching all 6 digits and then failing to match "foo", the normal | After matching all 6 digits and then failing to match "foo", the normal |
| 4591 | action of the matcher is to try again with only 5 digits matching the | action of the matcher is to try again with only 5 digits matching the |
| 4592 | \d+ item, and then with 4, and so on, before ultimately failing. | \d+ item, and then with 4, and so on, before ultimately failing. |
| 4593 | "Atomic grouping" (a term taken from Jeffrey Friedl's book) provides | "Atomic grouping" (a term taken from Jeffrey Friedl's book) provides |
| 4594 | the means for specifying that once a subpattern has matched, it is not | the means for specifying that once a subpattern has matched, it is not |
| 4595 | to be re-evaluated in this way. | to be re-evaluated in this way. |
| 4596 | ||
| 4597 | If we use atomic grouping for the previous example, the matcher gives | If we use atomic grouping for the previous example, the matcher gives |
| 4598 | up immediately on failing to match "foo" the first time. The notation | up immediately on failing to match "foo" the first time. The notation |
| 4599 | is a kind of special parenthesis, starting with (?> as in this example: | is a kind of special parenthesis, starting with (?> as in this example: |
| 4600 | ||
| 4601 | (?>\d+)foo | (?>\d+)foo |
| 4602 | ||
| 4603 | This kind of parenthesis "locks up" the part of the pattern it con- | This kind of parenthesis "locks up" the part of the pattern it con- |
| 4604 | tains once it has matched, and a failure further into the pattern is | tains once it has matched, and a failure further into the pattern is |
| 4605 | prevented from backtracking into it. Backtracking past it to previous | prevented from backtracking into it. Backtracking past it to previous |
| 4606 | items, however, works as normal. | items, however, works as normal. |
| 4607 | ||
| 4608 | An alternative description is that a subpattern of this type matches | An alternative description is that a subpattern of this type matches |
| 4609 | the string of characters that an identical standalone pattern would | the string of characters that an identical standalone pattern would |
| 4610 | match, if anchored at the current point in the subject string. | match, if anchored at the current point in the subject string. |
| 4611 | ||
| 4612 | Atomic grouping subpatterns are not capturing subpatterns. Simple cases | Atomic grouping subpatterns are not capturing subpatterns. Simple cases |
| 4613 | such as the above example can be thought of as a maximizing repeat that | such as the above example can be thought of as a maximizing repeat that |
| 4614 | must swallow everything it can. So, while both \d+ and \d+? are pre- | must swallow everything it can. So, while both \d+ and \d+? are pre- |
| 4615 | pared to adjust the number of digits they match in order to make the | pared to adjust the number of digits they match in order to make the |
| 4616 | rest of the pattern match, (?>\d+) can only match an entire sequence of | rest of the pattern match, (?>\d+) can only match an entire sequence of |
| 4617 | digits. | digits. |
| 4618 | ||
| 4619 | Atomic groups in general can of course contain arbitrarily complicated | Atomic groups in general can of course contain arbitrarily complicated |
| 4620 | subpatterns, and can be nested. However, when the subpattern for an | subpatterns, and can be nested. However, when the subpattern for an |
| 4621 | atomic group is just a single repeated item, as in the example above, a | atomic group is just a single repeated item, as in the example above, a |
| 4622 | simpler notation, called a "possessive quantifier" can be used. This | simpler notation, called a "possessive quantifier" can be used. This |
| 4623 | consists of an additional + character following a quantifier. Using | consists of an additional + character following a quantifier. Using |
| 4624 | this notation, the previous example can be rewritten as | this notation, the previous example can be rewritten as |
| 4625 | ||
| 4626 | \d++foo | \d++foo |
| # | Line 4592 ATOMIC GROUPING AND POSSESSIVE QUANTIFIE | Line 4630 ATOMIC GROUPING AND POSSESSIVE QUANTIFIE |
| 4630 | ||
| 4631 | (abc|xyz){2,3}+ | (abc|xyz){2,3}+ |
| 4632 | ||
| 4633 | Possessive quantifiers are always greedy; the setting of the | Possessive quantifiers are always greedy; the setting of the |
| 4634 | PCRE_UNGREEDY option is ignored. They are a convenient notation for the | PCRE_UNGREEDY option is ignored. They are a convenient notation for the |
| 4635 | simpler forms of atomic group. However, there is no difference in the | simpler forms of atomic group. However, there is no difference in the |
| 4636 | meaning of a possessive quantifier and the equivalent atomic group, | meaning of a possessive quantifier and the equivalent atomic group, |
| 4637 | though there may be a performance difference; possessive quantifiers | though there may be a performance difference; possessive quantifiers |
| 4638 | should be slightly faster. | should be slightly faster. |
| 4639 | ||
| 4640 | The possessive quantifier syntax is an extension to the Perl 5.8 syn- | The possessive quantifier syntax is an extension to the Perl 5.8 syn- |
| 4641 | tax. Jeffrey Friedl originated the idea (and the name) in the first | tax. Jeffrey Friedl originated the idea (and the name) in the first |
| 4642 | edition of his book. Mike McCloskey liked it, so implemented it when he | edition of his book. Mike McCloskey liked it, so implemented it when he |
| 4643 | built Sun's Java package, and PCRE copied it from there. It ultimately | built Sun's Java package, and PCRE copied it from there. It ultimately |
| 4644 | found its way into Perl at release 5.10. | found its way into Perl at release 5.10. |
| 4645 | ||
| 4646 | PCRE has an optimization that automatically "possessifies" certain sim- | PCRE has an optimization that automatically "possessifies" certain sim- |
| 4647 | ple pattern constructs. For example, the sequence A+B is treated as | ple pattern constructs. For example, the sequence A+B is treated as |
| 4648 | A++B because there is no point in backtracking into a sequence of A's | A++B because there is no point in backtracking into a sequence of A's |
| 4649 | when B must follow. | when B must follow. |
| 4650 | ||
| 4651 | When a pattern contains an unlimited repeat inside a subpattern that | When a pattern contains an unlimited repeat inside a subpattern that |
| 4652 | can itself be repeated an unlimited number of times, the use of an | can itself be repeated an unlimited number of times, the use of an |
| 4653 | atomic group is the only way to avoid some failing matches taking a | atomic group is the only way to avoid some failing matches taking a |
| 4654 | very long time indeed. The pattern | very long time indeed. The pattern |
| 4655 | ||
| 4656 | (\D+|<\d+>)*[!?] | (\D+|<\d+>)*[!?] |
| 4657 | ||
| 4658 | matches an unlimited number of substrings that either consist of non- | matches an unlimited number of substrings that either consist of non- |
| 4659 | digits, or digits enclosed in <>, followed by either ! or ?. When it | digits, or digits enclosed in <>, followed by either ! or ?. When it |
| 4660 | matches, it runs quickly. However, if it is applied to | matches, it runs quickly. However, if it is applied to |
| 4661 | ||
| 4662 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 4663 | ||
| 4664 | it takes a long time before reporting failure. This is because the | it takes a long time before reporting failure. This is because the |
| 4665 | string can be divided between the internal \D+ repeat and the external | string can be divided between the internal \D+ repeat and the external |
| 4666 | * repeat in a large number of ways, and all have to be tried. (The | * repeat in a large number of ways, and all have to be tried. (The |
| 4667 | example uses [!?] rather than a single character at the end, because | example uses [!?] rather than a single character at the end, because |
| 4668 | both PCRE and Perl have an optimization that allows for fast failure | both PCRE and Perl have an optimization that allows for fast failure |
| 4669 | when a single character is used. They remember the last single charac- | when a single character is used. They remember the last single charac- |
| 4670 | ter that is required for a match, and fail early if it is not present | ter that is required for a match, and fail early if it is not present |
| 4671 | in the string.) If the pattern is changed so that it uses an atomic | in the string.) If the pattern is changed so that it uses an atomic |
| 4672 | group, like this: | group, like this: |
| 4673 | ||
| 4674 | ((?>\D+)|<\d+>)*[!?] | ((?>\D+)|<\d+>)*[!?] |
| # | Line 4642 BACK REFERENCES | Line 4680 BACK REFERENCES |
| 4680 | ||
| 4681 | Outside a character class, a backslash followed by a digit greater than | Outside a character class, a backslash followed by a digit greater than |
| 4682 | 0 (and possibly further digits) is a back reference to a capturing sub- | 0 (and possibly further digits) is a back reference to a capturing sub- |
| 4683 | pattern earlier (that is, to its left) in the pattern, provided there | pattern earlier (that is, to its left) in the pattern, provided there |
| 4684 | have been that many previous capturing left parentheses. | have been that many previous capturing left parentheses. |
| 4685 | ||
| 4686 | However, if the decimal number following the backslash is less than 10, | However, if the decimal number following the backslash is less than 10, |
| 4687 | it is always taken as a back reference, and causes an error only if | it is always taken as a back reference, and causes an error only if |
| 4688 | there are not that many capturing left parentheses in the entire pat- | there are not that many capturing left parentheses in the entire pat- |
| 4689 | tern. In other words, the parentheses that are referenced need not be | tern. In other words, the parentheses that are referenced need not be |
| 4690 | to the left of the reference for numbers less than 10. A "forward back | to the left of the reference for numbers less than 10. A "forward back |
| 4691 | reference" of this type can make sense when a repetition is involved | reference" of this type can make sense when a repetition is involved |
| 4692 | and the subpattern to the right has participated in an earlier itera- | and the subpattern to the right has participated in an earlier itera- |
| 4693 | tion. | tion. |
| 4694 | ||
| 4695 | It is not possible to have a numerical "forward back reference" to a | It is not possible to have a numerical "forward back reference" to a |
| 4696 | subpattern whose number is 10 or more using this syntax because a | subpattern whose number is 10 or more using this syntax because a |
| 4697 | sequence such as \50 is interpreted as a character defined in octal. | sequence such as \50 is interpreted as a character defined in octal. |
| 4698 | See the subsection entitled "Non-printing characters" above for further | See the subsection entitled "Non-printing characters" above for further |
| 4699 | details of the handling of digits following a backslash. There is no | details of the handling of digits following a backslash. There is no |
| 4700 | such problem when named parentheses are used. A back reference to any | such problem when named parentheses are used. A back reference to any |
| 4701 | subpattern is possible using named parentheses (see below). | subpattern is possible using named parentheses (see below). |
| 4702 | ||
| 4703 | Another way of avoiding the ambiguity inherent in the use of digits | Another way of avoiding the ambiguity inherent in the use of digits |
| 4704 | following a backslash is to use the \g escape sequence. This escape | following a backslash is to use the \g escape sequence. This escape |
| 4705 | must be followed by an unsigned number or a negative number, optionally | must be followed by an unsigned number or a negative number, optionally |
| 4706 | enclosed in braces. These examples are all identical: | enclosed in braces. These examples are all identical: |
| 4707 | ||
| # | Line 4671 BACK REFERENCES | Line 4709 BACK REFERENCES |
| 4709 | (ring), \g1 | (ring), \g1 |
| 4710 | (ring), \g{1} | (ring), \g{1} |
| 4711 | ||
| 4712 | An unsigned number specifies an absolute reference without the ambigu- | An unsigned number specifies an absolute reference without the ambigu- |
| 4713 | ity that is present in the older syntax. It is also useful when literal | ity that is present in the older syntax. It is also useful when literal |
| 4714 | digits follow the reference. A negative number is a relative reference. | digits follow the reference. A negative number is a relative reference. |
| 4715 | Consider this example: | Consider this example: |
| # | Line 4679 BACK REFERENCES | Line 4717 BACK REFERENCES |
| 4717 | (abc(def)ghi)\g{-1} | (abc(def)ghi)\g{-1} |
| 4718 | ||
| 4719 | The sequence \g{-1} is a reference to the most recently started captur- | The sequence \g{-1} is a reference to the most recently started captur- |
| 4720 | ing subpattern before \g, that is, is it equivalent to \2. Similarly, | ing subpattern before \g, that is, is it equivalent to \2 in this exam- |
| 4721 | \g{-2} would be equivalent to \1. The use of relative references can be | ple. Similarly, \g{-2} would be equivalent to \1. The use of relative |
| 4722 | helpful in long patterns, and also in patterns that are created by | references can be helpful in long patterns, and also in patterns that |
| 4723 | joining together fragments that contain references within themselves. | are created by joining together fragments that contain references |
| 4724 | within themselves. | |
| 4725 | ||
| 4726 | A back reference matches whatever actually matched the capturing sub- | A back reference matches whatever actually matched the capturing sub- |
| 4727 | pattern in the current subject string, rather than anything matching | pattern in the current subject string, rather than anything matching |
| # | Line 4802 ASSERTIONS | Line 4841 ASSERTIONS |
| 4841 | most convenient way to do it is with (?!) because an empty string | most convenient way to do it is with (?!) because an empty string |
| 4842 | always matches, so an assertion that requires there not to be an empty | always matches, so an assertion that requires there not to be an empty |
| 4843 | string must always fail. The backtracking control verb (*FAIL) or (*F) | string must always fail. The backtracking control verb (*FAIL) or (*F) |
| 4844 | is essentially a synonym for (?!). | is a synonym for (?!). |
| 4845 | ||
| 4846 | Lookbehind assertions | Lookbehind assertions |
| 4847 | ||
| # | Line 4945 CONDITIONAL SUBPATTERNS | Line 4984 CONDITIONAL SUBPATTERNS |
| 4984 | the condition is true if a capturing subpattern of that number has pre- | the condition is true if a capturing subpattern of that number has pre- |
| 4985 | viously matched. If there is more than one capturing subpattern with | viously matched. If there is more than one capturing subpattern with |
| 4986 | the same number (see the earlier section about duplicate subpattern | the same number (see the earlier section about duplicate subpattern |
| 4987 | numbers), the condition is true if any of them have been set. An alter- | numbers), the condition is true if any of them have matched. An alter- |
| 4988 | native notation is to precede the digits with a plus or minus sign. In | native notation is to precede the digits with a plus or minus sign. In |
| 4989 | this case, the subpattern number is relative rather than absolute. The | this case, the subpattern number is relative rather than absolute. The |
| 4990 | most recently opened parentheses can be referenced by (?(-1), the next | most recently opened parentheses can be referenced by (?(-1), the next |
| 4991 | most recent by (?(-2), and so on. In looping constructs it can also | most recent by (?(-2), and so on. Inside loops it can also make sense |
| 4992 | make sense to refer to subsequent groups with constructs such as | to refer to subsequent groups. The next parentheses to be opened can be |
| 4993 | (?(+2). | referenced as (?(+1), and so on. (The value zero in any of these forms |
| 4994 | is not used; it provokes a compile-time error.) | |
| 4995 | ||
| 4996 | Consider the following pattern, which contains non-significant white | Consider the following pattern, which contains non-significant white |
| 4997 | space to make it more readable (assume the PCRE_EXTENDED option) and to | space to make it more readable (assume the PCRE_EXTENDED option) and to |
| 4998 | divide it into three parts for ease of discussion: | divide it into three parts for ease of discussion: |
| 4999 | ||
| 5000 | ( \( )? [^()]+ (?(1) \) ) | ( \( )? [^()]+ (?(1) \) ) |
| 5001 | ||
| 5002 | The first part matches an optional opening parenthesis, and if that | The first part matches an optional opening parenthesis, and if that |
| 5003 | character is present, sets it as the first captured substring. The sec- | character is present, sets it as the first captured substring. The sec- |
| 5004 | ond part matches one or more characters that are not parentheses. The | ond part matches one or more characters that are not parentheses. The |
| 5005 | third part is a conditional subpattern that tests whether the first set | third part is a conditional subpattern that tests whether or not the |
| 5006 | of parentheses matched or not. If they did, that is, if subject started | first set of parentheses matched. If they did, that is, if subject |
| 5007 | with an opening parenthesis, the condition is true, and so the yes-pat- | started with an opening parenthesis, the condition is true, and so the |
| 5008 | tern is executed and a closing parenthesis is required. Otherwise, | yes-pattern is executed and a closing parenthesis is required. Other- |
| 5009 | since no-pattern is not present, the subpattern matches nothing. In | wise, since no-pattern is not present, the subpattern matches nothing. |
| 5010 | other words, this pattern matches a sequence of non-parentheses, | In other words, this pattern matches a sequence of non-parentheses, |
| 5011 | optionally enclosed in parentheses. | optionally enclosed in parentheses. |
| 5012 | ||
| 5013 | If you were embedding this pattern in a larger one, you could use a | If you were embedding this pattern in a larger one, you could use a |
| 5014 | relative reference: | relative reference: |
| 5015 | ||
| 5016 | ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... | ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... |
| 5017 | ||
| 5018 | This makes the fragment independent of the parentheses in the larger | This makes the fragment independent of the parentheses in the larger |
| 5019 | pattern. | pattern. |
| 5020 | ||
| 5021 | Checking for a used subpattern by name | Checking for a used subpattern by name |
| 5022 | ||
| 5023 | Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a | Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a |
| 5024 | used subpattern by name. For compatibility with earlier versions of | used subpattern by name. For compatibility with earlier versions of |
| 5025 | PCRE, which had this facility before Perl, the syntax (?(name)...) is | PCRE, which had this facility before Perl, the syntax (?(name)...) is |
| 5026 | also recognized. However, there is a possible ambiguity with this syn- | also recognized. However, there is a possible ambiguity with this syn- |
| 5027 | tax, because subpattern names may consist entirely of digits. PCRE | tax, because subpattern names may consist entirely of digits. PCRE |
| 5028 | looks first for a named subpattern; if it cannot find one and the name | looks first for a named subpattern; if it cannot find one and the name |
| 5029 | consists entirely of digits, PCRE looks for a subpattern of that num- | consists entirely of digits, PCRE looks for a subpattern of that num- |
| 5030 | ber, which must be greater than zero. Using subpattern names that con- | ber, which must be greater than zero. Using subpattern names that con- |
| 5031 | sist entirely of digits is not recommended. | sist entirely of digits is not recommended. |
| 5032 | ||
| 5033 | Rewriting the above example to use a named subpattern gives this: | Rewriting the above example to use a named subpattern gives this: |
| 5034 | ||
| 5035 | (?<OPEN> \( )? [^()]+ (?(<OPEN>) \) ) | (?<OPEN> \( )? [^()]+ (?(<OPEN>) \) ) |
| 5036 | ||
| 5037 | If the name used in a condition of this kind is a duplicate, the test | If the name used in a condition of this kind is a duplicate, the test |
| 5038 | is applied to all subpatterns of the same name, and is true if any one | is applied to all subpatterns of the same name, and is true if any one |
| 5039 | of them has matched. | of them has matched. |
| 5040 | ||
| 5041 | Checking for pattern recursion | Checking for pattern recursion |
| 5042 | ||
| 5043 | If the condition is the string (R), and there is no subpattern with the | If the condition is the string (R), and there is no subpattern with the |
| 5044 | name R, the condition is true if a recursive call to the whole pattern | name R, the condition is true if a recursive call to the whole pattern |
| 5045 | or any subpattern has been made. If digits or a name preceded by amper- | or any subpattern has been made. If digits or a name preceded by amper- |
| 5046 | sand follow the letter R, for example: | sand follow the letter R, for example: |
| 5047 | ||
| # | Line 5009 CONDITIONAL SUBPATTERNS | Line 5049 CONDITIONAL SUBPATTERNS |
| 5049 | ||
| 5050 | the condition is true if the most recent recursion is into a subpattern | the condition is true if the most recent recursion is into a subpattern |
| 5051 | whose number or name is given. This condition does not check the entire | whose number or name is given. This condition does not check the entire |
| 5052 | recursion stack. If the name used in a condition of this kind is a | recursion stack. If the name used in a condition of this kind is a |
| 5053 | duplicate, the test is applied to all subpatterns of the same name, and | duplicate, the test is applied to all subpatterns of the same name, and |
| 5054 | is true if any one of them is the most recent recursion. | is true if any one of them is the most recent recursion. |
| 5055 | ||
| 5056 | At "top level", all these recursion test conditions are false. The | At "top level", all these recursion test conditions are false. The |
| 5057 | syntax for recursive patterns is described below. | syntax for recursive patterns is described below. |
| 5058 | ||
| 5059 | Defining subpatterns for use by reference only | Defining subpatterns for use by reference only |
| 5060 | ||
| 5061 | If the condition is the string (DEFINE), and there is no subpattern | If the condition is the string (DEFINE), and there is no subpattern |
| 5062 | with the name DEFINE, the condition is always false. In this case, | with the name DEFINE, the condition is always false. In this case, |
| 5063 | there may be only one alternative in the subpattern. It is always | there may be only one alternative in the subpattern. It is always |
| 5064 | skipped if control reaches this point in the pattern; the idea of | skipped if control reaches this point in the pattern; the idea of |
| 5065 | DEFINE is that it can be used to define "subroutines" that can be ref- | DEFINE is that it can be used to define "subroutines" that can be ref- |
| 5066 | erenced from elsewhere. (The use of "subroutines" is described below.) | erenced from elsewhere. (The use of "subroutines" is described below.) |
| 5067 | For example, a pattern to match an IPv4 address could be written like | For example, a pattern to match an IPv4 address such as |
| 5068 | this (ignore whitespace and line breaks): | "192.168.23.245" could be written like this (ignore whitespace and line |
| 5069 | breaks): | |
| 5070 | ||
| 5071 | (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) ) | (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) ) |
| 5072 | \b (?&byte) (\.(?&byte)){3} \b | \b (?&byte) (\.(?&byte)){3} \b |
| # | Line 5242 RECURSIVE PATTERNS | Line 5283 RECURSIVE PATTERNS |
| 5283 | remaining alternative is at a deeper recursion level, which PCRE cannot | remaining alternative is at a deeper recursion level, which PCRE cannot |
| 5284 | use. | use. |
| 5285 | ||
| 5286 | To change the pattern so that matches all palindromic strings, not just | To change the pattern so that it matches all palindromic strings, not |
| 5287 | those with an odd number of characters, it is tempting to change the | just those with an odd number of characters, it is tempting to change |
| 5288 | pattern to this: | the pattern to this: |
| 5289 | ||
| 5290 | ^((.)(?1)\2|.?)$ | ^((.)(?1)\2|.?)$ |
| 5291 | ||
| # | Line 5411 BACKTRACKING CONTROL | Line 5452 BACKTRACKING CONTROL |
| 5452 | character must be present. When one of these optimizations suppresses | character must be present. When one of these optimizations suppresses |
| 5453 | the running of a match, any included backtracking verbs will not, of | the running of a match, any included backtracking verbs will not, of |
| 5454 | course, be processed. You can suppress the start-of-match optimizations | course, be processed. You can suppress the start-of-match optimizations |
| 5455 | by setting the PCRE_NO_START_OPTIMIZE option when calling pcre_exec(). | by setting the PCRE_NO_START_OPTIMIZE option when calling pcre_com- |
| 5456 | pile() or pcre_exec(), or by starting the pattern with (*NO_START_OPT). | |
| 5457 | ||
| 5458 | Verbs that act immediately | Verbs that act immediately |
| 5459 | ||
| 5460 | The following verbs act as soon as they are encountered. They may not | The following verbs act as soon as they are encountered. They may not |
| 5461 | be followed by a name. | be followed by a name. |
| 5462 | ||
| 5463 | (*ACCEPT) | (*ACCEPT) |
| 5464 | ||
| 5465 | This verb causes the match to end successfully, skipping the remainder | This verb causes the match to end successfully, skipping the remainder |
| 5466 | of the pattern. When inside a recursion, only the innermost pattern is | of the pattern. When inside a recursion, only the innermost pattern is |
| 5467 | ended immediately. If (*ACCEPT) is inside capturing parentheses, the | ended immediately. If (*ACCEPT) is inside capturing parentheses, the |
| 5468 | data so far is captured. (This feature was added to PCRE at release | data so far is captured. (This feature was added to PCRE at release |
| 5469 | 8.00.) For example: | 8.00.) For example: |
| 5470 | ||
| 5471 | A((?:A|B(*ACCEPT)|C)D) | A((?:A|B(*ACCEPT)|C)D) |
| 5472 | ||
| 5473 | This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is cap- | This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is cap- |
| 5474 | tured by the outer parentheses. | tured by the outer parentheses. |
| 5475 | ||
| 5476 | (*FAIL) or (*F) | (*FAIL) or (*F) |
| 5477 | ||
| 5478 | This verb causes the match to fail, forcing backtracking to occur. It | This verb causes the match to fail, forcing backtracking to occur. It |
| 5479 | is equivalent to (?!) but easier to read. The Perl documentation notes | is equivalent to (?!) but easier to read. The Perl documentation notes |
| 5480 | that it is probably useful only when combined with (?{}) or (??{}). | that it is probably useful only when combined with (?{}) or (??{}). |
| 5481 | Those are, of course, Perl features that are not present in PCRE. The | Those are, of course, Perl features that are not present in PCRE. The |
| 5482 | nearest equivalent is the callout feature, as for example in this pat- | nearest equivalent is the callout feature, as for example in this pat- |
| 5483 | tern: | tern: |
| 5484 | ||
| 5485 | a+(?C)(*FAIL) | a+(?C)(*FAIL) |
| 5486 | ||
| 5487 | A match with the string "aaaa" always fails, but the callout is taken | A match with the string "aaaa" always fails, but the callout is taken |
| 5488 | before each backtrack happens (in this example, 10 times). | before each backtrack happens (in this example, 10 times). |
| 5489 | ||
| 5490 | Recording which path was taken | Recording which path was taken |
| 5491 | ||
| 5492 | There is one verb whose main purpose is to track how a match was | There is one verb whose main purpose is to track how a match was |
| 5493 | arrived at, though it also has a secondary use in conjunction with | arrived at, though it also has a secondary use in conjunction with |
| 5494 | advancing the match starting point (see (*SKIP) below). | advancing the match starting point (see (*SKIP) below). |
| 5495 | ||
| 5496 | (*MARK:NAME) or (*:NAME) | (*MARK:NAME) or (*:NAME) |
| 5497 | ||
| 5498 | A name is always required with this verb. There may be as many | A name is always required with this verb. There may be as many |
| 5499 | instances of (*MARK) as you like in a pattern, and their names do not | instances of (*MARK) as you like in a pattern, and their names do not |
| 5500 | have to be unique. | have to be unique. |
| 5501 | ||
| 5502 | When a match succeeds, the name of the last-encountered (*MARK) is | When a match succeeds, the name of the last-encountered (*MARK) is |
| 5503 | passed back to the caller via the pcre_extra data structure, as | passed back to the caller via the pcre_extra data structure, as |
| 5504 | described in the section on pcre_extra in the pcreapi documentation. No | described in the section on pcre_extra in the pcreapi documentation. No |
| 5505 | data is returned for a partial match. Here is an example of pcretest | data is returned for a partial match. Here is an example of pcretest |
| 5506 | output, where the /K modifier requests the retrieval and outputting of | output, where the /K modifier requests the retrieval and outputting of |
| 5507 | (*MARK) data: | (*MARK) data: |
| 5508 | ||
| 5509 | /X(*MARK:A)Y|X(*MARK:B)Z/K | /X(*MARK:A)Y|X(*MARK:B)Z/K |
| # | Line 5473 BACKTRACKING CONTROL | Line 5515 BACKTRACKING CONTROL |
| 5515 | MK: B | MK: B |
| 5516 | ||
| 5517 | The (*MARK) name is tagged with "MK:" in this output, and in this exam- | The (*MARK) name is tagged with "MK:" in this output, and in this exam- |
| 5518 | ple it indicates which of the two alternatives matched. This is a more | ple it indicates which of the two alternatives matched. This is a more |
| 5519 | efficient way of obtaining this information than putting each alterna- | efficient way of obtaining this information than putting each alterna- |
| 5520 | tive in its own capturing parentheses. | tive in its own capturing parentheses. |
| 5521 | ||
| 5522 | A name may also be returned after a failed match if the final path | A name may also be returned after a failed match if the final path |
| 5523 | through the pattern involves (*MARK). However, unless (*MARK) used in | through the pattern involves (*MARK). However, unless (*MARK) used in |
| 5524 | conjunction with (*COMMIT), this is unlikely to happen for an unan- | conjunction with (*COMMIT), this is unlikely to happen for an unan- |
| 5525 | chored pattern because, as the starting point for matching is advanced, | chored pattern because, as the starting point for matching is advanced, |
| 5526 | the final check is often with an empty string, causing a failure before | the final check is often with an empty string, causing a failure before |
| 5527 | (*MARK) is reached. For example: | (*MARK) is reached. For example: |
| # | Line 5489 BACKTRACKING CONTROL | Line 5531 BACKTRACKING CONTROL |
| 5531 | No match | No match |
| 5532 | ||
| 5533 | There are three potential starting points for this match (starting with | There are three potential starting points for this match (starting with |
| 5534 | X, starting with P, and with an empty string). If the pattern is | X, starting with P, and with an empty string). If the pattern is |
| 5535 | anchored, the result is different: | anchored, the result is different: |
| 5536 | ||
| 5537 | /^X(*MARK:A)Y|^X(*MARK:B)Z/K | /^X(*MARK:A)Y|^X(*MARK:B)Z/K |
| 5538 | XP | XP |
| 5539 | No match, mark = B | No match, mark = B |
| 5540 | ||
| 5541 | PCRE's start-of-match optimizations can also interfere with this. For | PCRE's start-of-match optimizations can also interfere with this. For |
| 5542 | example, if, as a result of a call to pcre_study(), it knows the mini- | example, if, as a result of a call to pcre_study(), it knows the mini- |
| 5543 | mum subject length for a match, a shorter subject will not be scanned | mum subject length for a match, a shorter subject will not be scanned |
| 5544 | at all. | at all. |
| 5545 | ||
| 5546 | Note that similar anomalies (though different in detail) exist in Perl, | Note that similar anomalies (though different in detail) exist in Perl, |
| 5547 | no doubt for the same reasons. The use of (*MARK) data after a failed | no doubt for the same reasons. The use of (*MARK) data after a failed |
| 5548 | match of an unanchored pattern is not recommended, unless (*COMMIT) is | match of an unanchored pattern is not recommended, unless (*COMMIT) is |
| 5549 | involved. | involved. |
| 5550 | ||
| 5551 | Verbs that act after backtracking | Verbs that act after backtracking |
| 5552 | ||
| 5553 | The following verbs do nothing when they are encountered. Matching con- | The following verbs do nothing when they are encountered. Matching con- |
| 5554 | tinues with what follows, but if there is no subsequent match, causing | tinues with what follows, but if there is no subsequent match, causing |
| 5555 | a backtrack to the verb, a failure is forced. That is, backtracking | a backtrack to the verb, a failure is forced. That is, backtracking |
| 5556 | cannot pass to the left of the verb. However, when one of these verbs | cannot pass to the left of the verb. However, when one of these verbs |
| 5557 | appears inside an atomic group, its effect is confined to that group, | appears inside an atomic group, its effect is confined to that group, |
| 5558 | because once the group has been matched, there is never any backtrack- | because once the group has been matched, there is never any backtrack- |
| 5559 | ing into it. In this situation, backtracking can "jump back" to the | ing into it. In this situation, backtracking can "jump back" to the |
| 5560 | left of the entire atomic group. (Remember also, as stated above, that | left of the entire atomic group. (Remember also, as stated above, that |
| 5561 | this localization also applies in subroutine calls and assertions.) | this localization also applies in subroutine calls and assertions.) |
| 5562 | ||
| 5563 | These verbs differ in exactly what kind of failure occurs when back- | These verbs differ in exactly what kind of failure occurs when back- |
| 5564 | tracking reaches them. | tracking reaches them. |
| 5565 | ||
| 5566 | (*COMMIT) | (*COMMIT) |
| 5567 | ||
| 5568 | This verb, which may not be followed by a name, causes the whole match | This verb, which may not be followed by a name, causes the whole match |
| 5569 | to fail outright if the rest of the pattern does not match. Even if the | to fail outright if the rest of the pattern does not match. Even if the |
| 5570 | pattern is unanchored, no further attempts to find a match by advancing | pattern is unanchored, no further attempts to find a match by advancing |
| 5571 | the starting point take place. Once (*COMMIT) has been passed, | the starting point take place. Once (*COMMIT) has been passed, |
| 5572 | pcre_exec() is committed to finding a match at the current starting | pcre_exec() is committed to finding a match at the current starting |
| 5573 | point, or not at all. For example: | point, or not at all. For example: |
| 5574 | ||
| 5575 | a+(*COMMIT)b | a+(*COMMIT)b |
| 5576 | ||
| 5577 | This matches "xxaab" but not "aacaab". It can be thought of as a kind | This matches "xxaab" but not "aacaab". It can be thought of as a kind |
| 5578 | of dynamic anchor, or "I've started, so I must finish." The name of the | of dynamic anchor, or "I've started, so I must finish." The name of the |
| 5579 | most recently passed (*MARK) in the path is passed back when (*COMMIT) | most recently passed (*MARK) in the path is passed back when (*COMMIT) |
| 5580 | forces a match failure. | forces a match failure. |
| 5581 |