/[pcre]/code/trunk/doc/pcre.txt
ViewVC logotype

Diff of /code/trunk/doc/pcre.txt

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 461 by ph10, Mon Oct 5 10:59:35 2009 UTC revision 548 by ph10, Fri Jun 25 14:42:00 2010 UTC
# Line 26  INTRODUCTION Line 26  INTRODUCTION
26         give better JavaScript compatibility.         give better JavaScript compatibility.
27    
28         The  current implementation of PCRE corresponds approximately with Perl         The  current implementation of PCRE corresponds approximately with Perl
29         5.10, including support for UTF-8 encoded strings and  Unicode  general         5.10/5.11, including support for UTF-8 encoded strings and Unicode gen-
30         category  properties.  However,  UTF-8  and  Unicode  support has to be         eral  category properties. However, UTF-8 and Unicode support has to be
31         explicitly enabled; it is not the default. The  Unicode  tables  corre-         explicitly enabled; it is not the default. The  Unicode  tables  corre-
32         spond to Unicode release 5.1.         spond to Unicode release 5.2.0.
33    
34         In  addition to the Perl-compatible matching function, PCRE contains an         In  addition to the Perl-compatible matching function, PCRE contains an
35         alternative function that matches the same compiled patterns in a  dif-         alternative function that matches the same compiled patterns in a  dif-
# Line 222  UTF-8 AND UNICODE PROPERTY SUPPORT Line 222  UTF-8 AND UNICODE PROPERTY SUPPORT
222         not available in the alternative matching function, pcre_dfa_exec().         not available in the alternative matching function, pcre_dfa_exec().
223    
224         6.  The  character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly         6.  The  character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly
225         test characters of any code value, but the characters that PCRE  recog-         test characters of any code value, but, by default, the characters that
226         nizes  as  digits,  spaces,  or  word characters remain the same set as         PCRE  recognizes  as digits, spaces, or word characters remain the same
227         before, all with values less than 256. This remains true even when PCRE         set as before, all with values less than 256. This  remains  true  even
228         includes  Unicode  property support, because to do otherwise would slow         when  PCRE  is built to include Unicode property support, because to do
229         down PCRE in many common cases. If you really want to test for a  wider         otherwise would slow down PCRE in many common  cases.  Note  that  this
230         sense  of,  say,  "digit",  you must use Unicode property tests such as         also applies to \b, because it is defined in terms of \w and \W. If you
231         \p{Nd}. Note that this also applies to \b, because  it  is  defined  in         really want to test for a wider sense of, say,  "digit",  you  can  use
232         terms of \w and \W.         explicit  Unicode property tests such as \p{Nd}.  Alternatively, if you
233           set the PCRE_UCP option, the way that the  character  escapes  work  is
234           changed  so that Unicode properties are used to determine which charac-
235           ters match. There are more details in the section on generic  character
236           types in the pcrepattern documentation.
237    
238         7.  Similarly,  characters that match the POSIX named character classes         7.  Similarly,  characters that match the POSIX named character classes
239         are all low-valued characters.         are all low-valued characters, unless the PCRE_UCP option is set.
240    
241         8. However, the Perl 5.10 horizontal and vertical  whitespace  matching         8. However, the Perl 5.10 horizontal and vertical  whitespace  matching
242         escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char-         escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char-
243         acters.         acters, whether or not PCRE_UCP is set.
244    
245         9. Case-insensitive matching applies only to  characters  whose  values         9. Case-insensitive matching applies only to  characters  whose  values
246         are  less than 128, unless PCRE is built with Unicode property support.         are  less than 128, unless PCRE is built with Unicode property support.
# Line 263  AUTHOR Line 267  AUTHOR
267    
268  REVISION  REVISION
269    
270         Last updated: 28 September 2009         Last updated: 12 May 2010
271         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
272  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
273    
274    
# Line 902  PCRE API OVERVIEW Line 906  PCRE API OVERVIEW
906         bers for the library.  Applications can use these  to  include  support         bers for the library.  Applications can use these  to  include  support
907         for different releases of PCRE.         for different releases of PCRE.
908    
909           In a Windows environment, if you want to statically link an application
910           program against a non-dll pcre.a  file,  you  must  define  PCRE_STATIC
911           before  including  pcre.h or pcrecpp.h, because otherwise the pcre_mal-
912           loc()   and   pcre_free()   exported   functions   will   be   declared
913           __declspec(dllimport), with unwanted results.
914    
915         The   functions   pcre_compile(),  pcre_compile2(),  pcre_study(),  and         The   functions   pcre_compile(),  pcre_compile2(),  pcre_study(),  and
916         pcre_exec() are used for compiling and matching regular expressions  in         pcre_exec() are used for compiling and matching regular expressions  in
917         a  Perl-compatible  manner. A sample program that demonstrates the sim-         a  Perl-compatible  manner. A sample program that demonstrates the sim-
# Line 1281  COMPILING A PATTERN Line 1291  COMPILING A PATTERN
1291         letter that has no special meaning  causes  an  error,  thus  reserving         letter that has no special meaning  causes  an  error,  thus  reserving
1292         these  combinations  for  future  expansion.  By default, as in Perl, a         these  combinations  for  future  expansion.  By default, as in Perl, a
1293         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
1294         literal.  (Perl can, however, be persuaded to give a warning for this.)         literal. (Perl can, however, be persuaded to give an error for this, by
1295         There are at present no other features controlled by  this  option.  It         running it with the -w option.) There are at present no other  features
1296         can also be set by a (?X) option setting within a pattern.         controlled  by this option. It can also be set by a (?X) option setting
1297           within a pattern.
1298    
1299           PCRE_FIRSTLINE           PCRE_FIRSTLINE
1300    
1301         If  this  option  is  set,  an  unanchored pattern is required to match         If this option is set, an  unanchored  pattern  is  required  to  match
1302         before or at the first  newline  in  the  subject  string,  though  the         before  or  at  the  first  newline  in  the subject string, though the
1303         matched text may continue over the newline.         matched text may continue over the newline.
1304    
1305           PCRE_JAVASCRIPT_COMPAT           PCRE_JAVASCRIPT_COMPAT
1306    
1307         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
1308         it is compatible with JavaScript rather than Perl. The changes  are  as         it  is  compatible with JavaScript rather than Perl. The changes are as
1309         follows:         follows:
1310    
1311         (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
1312         error, because this is illegal in JavaScript (by default it is  treated         error,  because this is illegal in JavaScript (by default it is treated
1313         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
1314         option is set.         option is set.
1315    
1316         (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
1317         an  empty  string (by default this causes the current matching alterna-         an empty string (by default this causes the current  matching  alterna-
1318         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
1319         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
1320         default, for Perl compatibility.         default, for Perl compatibility.
1321    
1322           PCRE_MULTILINE           PCRE_MULTILINE
1323    
1324         By default, PCRE treats the subject string as consisting  of  a  single         By  default,  PCRE  treats the subject string as consisting of a single
1325         line  of characters (even if it actually contains newlines). The "start         line of characters (even if it actually contains newlines). The  "start
1326         of line" metacharacter (^) matches only at the  start  of  the  string,         of  line"  metacharacter  (^)  matches only at the start of the string,
1327         while  the  "end  of line" metacharacter ($) matches only at the end of         while the "end of line" metacharacter ($) matches only at  the  end  of
1328         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY
1329         is set). This is the same as Perl.         is set). This is the same as Perl.
1330    
1331         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"
1332         constructs match immediately following or immediately  before  internal         constructs  match  immediately following or immediately before internal
1333         newlines  in  the  subject string, respectively, as well as at the very         newlines in the subject string, respectively, as well as  at  the  very
1334         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
1335         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-
1336         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,
1337         setting PCRE_MULTILINE has no effect.         setting PCRE_MULTILINE has no effect.
1338    
1339           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
# Line 1331  COMPILING A PATTERN Line 1342  COMPILING A PATTERN
1342           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1343           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1344    
1345         These  options  override the default newline definition that was chosen         These options override the default newline definition that  was  chosen
1346         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
1347         newline  is  indicated  by a single character (CR or LF, respectively).         newline is indicated by a single character (CR  or  LF,  respectively).
1348         Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by  the         Setting  PCRE_NEWLINE_CRLF specifies that a newline is indicated by the
1349         two-character  CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF specifies         two-character CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF  specifies
1350         that any of the three preceding sequences should be recognized. Setting         that any of the three preceding sequences should be recognized. Setting
1351         PCRE_NEWLINE_ANY  specifies that any Unicode newline sequence should be         PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should  be
1352         recognized. The Unicode newline sequences are the three just mentioned,         recognized. The Unicode newline sequences are the three just mentioned,
1353         plus  the  single  characters  VT (vertical tab, U+000B), FF (formfeed,         plus the single characters VT (vertical  tab,  U+000B),  FF  (formfeed,
1354         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
1355         (paragraph  separator,  U+2029).  The  last  two are recognized only in         (paragraph separator, U+2029). The last  two  are  recognized  only  in
1356         UTF-8 mode.         UTF-8 mode.
1357    
1358         The newline setting in the  options  word  uses  three  bits  that  are         The  newline  setting  in  the  options  word  uses three bits that are
1359         treated as a number, giving eight possibilities. Currently only six are         treated as a number, giving eight possibilities. Currently only six are
1360         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
1361         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-
1362         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
1363         PCRE_NEWLINE_CRLF,  but other combinations may yield unused numbers and         PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers  and
1364         cause an error.         cause an error.
1365    
1366         The only time that a line break is specially recognized when  compiling         The  only time that a line break is specially recognized when compiling
1367         a  pattern  is  if  PCRE_EXTENDED  is set, and an unescaped # outside a         a pattern is if PCRE_EXTENDED is set, and  an  unescaped  #  outside  a
1368         character class is encountered. This indicates  a  comment  that  lasts         character  class  is  encountered.  This indicates a comment that lasts
1369         until  after the next line break sequence. In other circumstances, line         until after the next line break sequence. In other circumstances,  line
1370         break  sequences  are  treated  as  literal  data,   except   that   in         break   sequences   are   treated  as  literal  data,  except  that  in
1371         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters
1372         and are therefore ignored.         and are therefore ignored.
1373    
# Line 1366  COMPILING A PATTERN Line 1377  COMPILING A PATTERN
1377           PCRE_NO_AUTO_CAPTURE           PCRE_NO_AUTO_CAPTURE
1378    
1379         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-
1380         theses in the pattern. Any opening parenthesis that is not followed  by         theses  in the pattern. Any opening parenthesis that is not followed by
1381         ?  behaves as if it were followed by ?: but named parentheses can still         ? behaves as if it were followed by ?: but named parentheses can  still
1382         be used for capturing (and they acquire  numbers  in  the  usual  way).         be  used  for  capturing  (and  they acquire numbers in the usual way).
1383         There is no equivalent of this option in Perl.         There is no equivalent of this option in Perl.
1384    
1385             PCRE_UCP
1386    
1387           This option changes the way PCRE processes \b, \d, \s, \w, and some  of
1388           the POSIX character classes. By default, only ASCII characters are rec-
1389           ognized, but if PCRE_UCP is set, Unicode properties are used instead to
1390           classify  characters.  More details are given in the section on generic
1391           character types in the pcrepattern page. If you set PCRE_UCP,  matching
1392           one  of the items it affects takes much longer. The option is available
1393           only if PCRE has been compiled with Unicode property support.
1394    
1395           PCRE_UNGREEDY           PCRE_UNGREEDY
1396    
1397         This  option  inverts  the "greediness" of the quantifiers so that they         This option inverts the "greediness" of the quantifiers  so  that  they
1398         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
1399         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
1400         within the pattern.         within the pattern.
1401    
1402           PCRE_UTF8           PCRE_UTF8
1403    
1404         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
1405         strings  of  UTF-8 characters instead of single-byte character strings.         strings of UTF-8 characters instead of single-byte  character  strings.
1406         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-
1407         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
1408         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
1409         UTF-8 support in the main pcre page.         UTF-8 support in the main pcre page.
1410    
1411           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
1412    
1413         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
1414         automatically checked. There is a  discussion  about  the  validity  of         automatically  checked.  There  is  a  discussion about the validity of
1415         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
1416         bytes is found, pcre_compile() returns an error. If  you  already  know         bytes  is  found,  pcre_compile() returns an error. If you already know
1417         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-
1418         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
1419         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
1420         undefined. It may cause your program to crash. Note  that  this  option         undefined.  It  may  cause your program to crash. Note that this option
1421         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
1422         UTF-8 validity checking of subject strings.         UTF-8 validity checking of subject strings.
1423    
1424    
1425  COMPILATION ERROR CODES  COMPILATION ERROR CODES
1426    
1427         The following table lists the error  codes  than  may  be  returned  by         The  following  table  lists  the  error  codes than may be returned by
1428         pcre_compile2(),  along with the error messages that may be returned by         pcre_compile2(), along with the error messages that may be returned  by
1429         both compiling functions. As PCRE has developed, some error codes  have         both  compiling functions. As PCRE has developed, some error codes have
1430         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.
1431    
1432            0  no error            0  no error
# Line 1461  COMPILATION ERROR CODES Line 1482  COMPILATION ERROR CODES
1482           50  [this code is not in use]           50  [this code is not in use]
1483           51  octal value is greater than \377 (not in UTF-8 mode)           51  octal value is greater than \377 (not in UTF-8 mode)
1484           52  internal error: overran compiling workspace           52  internal error: overran compiling workspace
1485           53   internal  error:  previously-checked  referenced  subpattern not           53  internal error: previously-checked referenced subpattern
1486         found                 not found
1487           54  DEFINE group contains more than one branch           54  DEFINE group contains more than one branch
1488           55  repeating a DEFINE group is not allowed           55  repeating a DEFINE group is not allowed
1489           56  inconsistent NEWLINE options           56  inconsistent NEWLINE options
1490           57  \g is not followed by a braced, angle-bracketed, or quoted           57  \g is not followed by a braced, angle-bracketed, or quoted
1491                 name/number or by a plain number                 name/number or by a plain number
1492           58  a numbered reference must not be zero           58  a numbered reference must not be zero
1493           59  (*VERB) with an argument is not supported           59  an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)
1494           60  (*VERB) not recognized           60  (*VERB) not recognized
1495           61  number is too big           61  number is too big
1496           62  subpattern name expected           62  subpattern name expected
1497           63  digit expected after (?+           63  digit expected after (?+
1498           64  ] is an invalid data character in JavaScript compatibility mode           64  ] is an invalid data character in JavaScript compatibility mode
1499             65  different names for subpatterns of the same number are
1500                   not allowed
1501             66  (*MARK) must have an argument
1502             67  this version of PCRE is not compiled with PCRE_UCP support
1503    
1504         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
1505         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.
# Line 1534  STUDYING A PATTERN Line 1559  STUDYING A PATTERN
1559         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
1560         which to start matching.         which to start matching.
1561    
1562           The two optimizations just described can be  disabled  by  setting  the
1563           PCRE_NO_START_OPTIMIZE    option    when    calling    pcre_exec()   or
1564           pcre_dfa_exec(). You might want to do this  if  your  pattern  contains
1565           callouts,  or  make  use of (*MARK), and you make use of these in cases
1566           where matching fails.  See  the  discussion  of  PCRE_NO_START_OPTIMIZE
1567           below.
1568    
1569    
1570  LOCALE SUPPORT  LOCALE SUPPORT
1571    
1572         PCRE handles caseless matching, and determines whether  characters  are         PCRE  handles  caseless matching, and determines whether characters are
1573         letters,  digits, or whatever, by reference to a set of tables, indexed         letters, digits, or whatever, by reference to a set of tables,  indexed
1574         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
1575         characters  with  codes  less than 128. Higher-valued codes never match         characters with codes less than 128. By  default,  higher-valued  codes
1576         escapes such as \w or \d, but can be tested with \p if  PCRE  is  built         never match escapes such as \w or \d, but they can be tested with \p if
1577         with  Unicode  character property support. The use of locales with Uni-         PCRE is built with Unicode character property  support.  Alternatively,
1578         code is discouraged. If you are handling characters with codes  greater         the  PCRE_UCP  option  can  be  set at compile time; this causes \w and
1579         than  128, you should either use UTF-8 and Unicode, or use locales, but         friends to use Unicode property support instead of built-in tables. The
1580         not try to mix the two.         use of locales with Unicode is discouraged. If you are handling charac-
1581           ters with codes greater than 128, you should either use UTF-8 and  Uni-
1582           code, or use locales, but not try to mix the two.
1583    
1584         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
1585         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many         argument of pcre_compile() is  NULL.  These  are  sufficient  for  many
1586         applications.  Normally, the internal tables recognize only ASCII char-         applications.  Normally, the internal tables recognize only ASCII char-
1587         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-
1588         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,
1589         which may cause them to be different.         which may cause them to be different.
1590    
1591         The  internal tables can always be overridden by tables supplied by the         The internal tables can always be overridden by tables supplied by  the
1592         application that calls PCRE. These may be created in a different locale         application that calls PCRE. These may be created in a different locale
1593         from  the  default.  As more and more applications change to using Uni-         from the default. As more and more applications change  to  using  Uni-
1594         code, the need for this locale support is expected to die away.         code, the need for this locale support is expected to die away.
1595    
1596         External tables are built by calling  the  pcre_maketables()  function,         External  tables  are  built by calling the pcre_maketables() function,
1597         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
1598         passed to pcre_compile() or pcre_exec()  as  often  as  necessary.  For         passed  to  pcre_compile()  or  pcre_exec()  as often as necessary. For
1599         example,  to  build  and use tables that are appropriate for the French         example, to build and use tables that are appropriate  for  the  French
1600         locale (where accented characters with  values  greater  than  128  are         locale  (where  accented  characters  with  values greater than 128 are
1601         treated as letters), the following code could be used:         treated as letters), the following code could be used:
1602    
1603           setlocale(LC_CTYPE, "fr_FR");           setlocale(LC_CTYPE, "fr_FR");
1604           tables = pcre_maketables();           tables = pcre_maketables();
1605           re = pcre_compile(..., tables);           re = pcre_compile(..., tables);
1606    
1607         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;
1608         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".
1609    
1610         When pcre_maketables() runs, the tables are built  in  memory  that  is         When  pcre_maketables()  runs,  the  tables are built in memory that is
1611         obtained  via  pcre_malloc. It is the caller's responsibility to ensure         obtained via pcre_malloc. It is the caller's responsibility  to  ensure
1612         that the memory containing the tables remains available for as long  as         that  the memory containing the tables remains available for as long as
1613         it is needed.         it is needed.
1614    
1615         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
1616         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()
1617         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-
1618         tern, compilation, studying and matching all happen in the same locale,         tern, compilation, studying and matching all happen in the same locale,
1619         but different patterns can be compiled in different locales.         but different patterns can be compiled in different locales.
1620    
1621         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
1622         the internal tables) to pcre_exec(). Although  not  intended  for  this         the  internal  tables)  to  pcre_exec(). Although not intended for this
1623         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
1624         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
1625         run time is discussed below in the section on matching a pattern.         run time is discussed below in the section on matching a pattern.
1626    
# Line 1596  INFORMATION ABOUT A PATTERN Line 1630  INFORMATION ABOUT A PATTERN
1630         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
1631              int what, void *where);              int what, void *where);
1632    
1633         The  pcre_fullinfo() function returns information about a compiled pat-         The pcre_fullinfo() function returns information about a compiled  pat-
1634         tern. It replaces the obsolete pcre_info() function, which is neverthe-         tern. It replaces the obsolete pcre_info() function, which is neverthe-
1635         less retained for backwards compability (and is documented below).         less retained for backwards compability (and is documented below).
1636    
1637         The  first  argument  for  pcre_fullinfo() is a pointer to the compiled         The first argument for pcre_fullinfo() is a  pointer  to  the  compiled
1638         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
1639         the  pattern  was not studied. The third argument specifies which piece         the pattern was not studied. The third argument specifies  which  piece
1640         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
1641         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
1642         success, or one of the following negative numbers:         success, or one of the following negative numbers:
1643    
1644           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
# Line 1612  INFORMATION ABOUT A PATTERN Line 1646  INFORMATION ABOUT A PATTERN
1646           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1647           PCRE_ERROR_BADOPTION  the value of what was invalid           PCRE_ERROR_BADOPTION  the value of what was invalid
1648    
1649         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
1650         an  simple check against passing an arbitrary memory pointer. Here is a         an simple check against passing an arbitrary memory pointer. Here is  a
1651         typical call of pcre_fullinfo(), to obtain the length of  the  compiled         typical  call  of pcre_fullinfo(), to obtain the length of the compiled
1652         pattern:         pattern:
1653    
1654           int rc;           int rc;
# Line 1625  INFORMATION ABOUT A PATTERN Line 1659  INFORMATION ABOUT A PATTERN
1659             PCRE_INFO_SIZE,   /* what is required */             PCRE_INFO_SIZE,   /* what is required */
1660             &length);         /* where to put the data */             &length);         /* where to put the data */
1661    
1662         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
1663         are as follows:         are as follows:
1664    
1665           PCRE_INFO_BACKREFMAX           PCRE_INFO_BACKREFMAX
1666    
1667         Return the number of the highest back reference  in  the  pattern.  The         Return  the  number  of  the highest back reference in the pattern. The
1668         fourth  argument  should  point to an int variable. Zero is returned if         fourth argument should point to an int variable. Zero  is  returned  if
1669         there are no back references.         there are no back references.
1670    
1671           PCRE_INFO_CAPTURECOUNT           PCRE_INFO_CAPTURECOUNT
1672    
1673         Return the number of capturing subpatterns in the pattern.  The  fourth         Return  the  number of capturing subpatterns in the pattern. The fourth
1674         argument should point to an int variable.         argument should point to an int variable.
1675    
1676           PCRE_INFO_DEFAULT_TABLES           PCRE_INFO_DEFAULT_TABLES
1677    
1678         Return  a pointer to the internal default character tables within PCRE.         Return a pointer to the internal default character tables within  PCRE.
1679         The fourth argument should point to an unsigned char *  variable.  This         The  fourth  argument should point to an unsigned char * variable. This
1680         information call is provided for internal use by the pcre_study() func-         information call is provided for internal use by the pcre_study() func-
1681         tion. External callers can cause PCRE to use  its  internal  tables  by         tion.  External  callers  can  cause PCRE to use its internal tables by
1682         passing a NULL table pointer.         passing a NULL table pointer.
1683    
1684           PCRE_INFO_FIRSTBYTE           PCRE_INFO_FIRSTBYTE
1685    
1686         Return  information  about  the first byte of any matched string, for a         Return information about the first byte of any matched  string,  for  a
1687         non-anchored pattern. The fourth argument should point to an int  vari-         non-anchored  pattern. The fourth argument should point to an int vari-
1688         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
1689         is still recognized for backwards compatibility.)         is still recognized for backwards compatibility.)
1690    
1691         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
1692         (cat|cow|coyote), its value is returned. Otherwise, if either         (cat|cow|coyote), its value is returned. Otherwise, if either
1693    
1694         (a)  the pattern was compiled with the PCRE_MULTILINE option, and every         (a) the pattern was compiled with the PCRE_MULTILINE option, and  every
1695         branch starts with "^", or         branch starts with "^", or
1696    
1697         (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
1698         set (if it were set, the pattern would be anchored),         set (if it were set, the pattern would be anchored),
1699    
1700         -1  is  returned, indicating that the pattern matches only at the start         -1 is returned, indicating that the pattern matches only at  the  start
1701         of a subject string or after any newline within the  string.  Otherwise         of  a  subject string or after any newline within the string. Otherwise
1702         -2 is returned. For anchored patterns, -2 is returned.         -2 is returned. For anchored patterns, -2 is returned.
1703    
1704           PCRE_INFO_FIRSTTABLE           PCRE_INFO_FIRSTTABLE
1705    
1706         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
1707         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
1708         matching  string, a pointer to the table is returned. Otherwise NULL is         matching string, a pointer to the table is returned. Otherwise NULL  is
1709         returned. The fourth argument should point to an unsigned char *  vari-         returned.  The fourth argument should point to an unsigned char * vari-
1710         able.         able.
1711    
1712           PCRE_INFO_HASCRORLF           PCRE_INFO_HASCRORLF
1713    
1714         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
1715         characters, otherwise 0. The fourth argument should  point  to  an  int         characters,  otherwise  0.  The  fourth argument should point to an int
1716         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
1717         \r or \n.         \r or \n.
1718    
1719           PCRE_INFO_JCHANGED           PCRE_INFO_JCHANGED
1720    
1721         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,
1722         otherwise  0. The fourth argument should point to an int variable. (?J)         otherwise 0. The fourth argument should point to an int variable.  (?J)
1723         and (?-J) set and unset the local PCRE_DUPNAMES option, respectively.         and (?-J) set and unset the local PCRE_DUPNAMES option, respectively.
1724    
1725           PCRE_INFO_LASTLITERAL           PCRE_INFO_LASTLITERAL
1726    
1727         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
1728         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
1729         recorded. The fourth argument should point to an int variable. If there         recorded. The fourth argument should point to an int variable. If there
1730         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
1731         byte is recorded only if it follows something of variable  length.  For         byte  is  recorded only if it follows something of variable length. For
1732         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
1733         /^a\dz\d/ the returned value is -1.         /^a\dz\d/ the returned value is -1.
1734    
1735           PCRE_INFO_MINLENGTH           PCRE_INFO_MINLENGTH
1736    
1737         If the pattern was studied and a minimum length  for  matching  subject         If  the  pattern  was studied and a minimum length for matching subject
1738         strings  was  computed,  its  value is returned. Otherwise the returned         strings was computed, its value is  returned.  Otherwise  the  returned
1739         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
1740         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
1741         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
1742         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
1743         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.
1744    
1745           PCRE_INFO_NAMECOUNT           PCRE_INFO_NAMECOUNT
1746           PCRE_INFO_NAMEENTRYSIZE           PCRE_INFO_NAMEENTRYSIZE
1747           PCRE_INFO_NAMETABLE           PCRE_INFO_NAMETABLE
1748    
1749         PCRE supports the use of named as well as numbered capturing  parenthe-         PCRE  supports the use of named as well as numbered capturing parenthe-
1750         ses.  The names are just an additional way of identifying the parenthe-         ses. The names are just an additional way of identifying the  parenthe-
1751         ses, which still acquire numbers. Several convenience functions such as         ses, which still acquire numbers. Several convenience functions such as
1752         pcre_get_named_substring()  are  provided  for extracting captured sub-         pcre_get_named_substring() are provided for  extracting  captured  sub-
1753         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
1754         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
1755         pointers in the output vector (described with pcre_exec() below). To do         pointers in the output vector (described with pcre_exec() below). To do
1756         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
1757         described by these three values.         described by these three values.
1758    
1759         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
1760         gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size         gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size
1761         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
1762         depends  on the length of the longest name. PCRE_INFO_NAMETABLE returns         depends on the length of the longest name. PCRE_INFO_NAMETABLE  returns
1763         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
1764         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-
1765         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-
1766         sponding name, zero terminated.         sponding name, zero terminated.
1767    
1768         The  names are in alphabetical order. Duplicate names may appear if (?|         The names are in alphabetical order. Duplicate names may appear if  (?|
1769         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
1770         the  section  on  duplicate subpattern numbers in the pcrepattern page.         the section on duplicate subpattern numbers in  the  pcrepattern  page.
1771         Duplicate names for subpatterns with different  numbers  are  permitted         Duplicate  names  for  subpatterns with different numbers are permitted
1772         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
1773         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-
1774         tern.  In  the  absence  of (?| this is the order of increasing number;         tern. In the absence of (?| this is the  order  of  increasing  number;
1775         when (?| is used this is not necessarily the case because later subpat-         when (?| is used this is not necessarily the case because later subpat-
1776         terns may have lower numbers.         terns may have lower numbers.
1777    
1778         As  a  simple  example of the name/number table, consider the following         As a simple example of the name/number table,  consider  the  following
1779         pattern (assume PCRE_EXTENDED is set, so white space -  including  new-         pattern  (assume  PCRE_EXTENDED is set, so white space - including new-
1780         lines - is ignored):         lines - is ignored):
1781    
1782           (?<date> (?<year>(\d\d)?\d\d) -           (?<date> (?<year>(\d\d)?\d\d) -
1783           (?<month>\d\d) - (?<day>\d\d) )           (?<month>\d\d) - (?<day>\d\d) )
1784    
1785         There  are  four  named subpatterns, so the table has four entries, and         There are four named subpatterns, so the table has  four  entries,  and
1786         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,
1787         with non-printing bytes shows in hexadecimal, and undefined bytes shown         with non-printing bytes shows in hexadecimal, and undefined bytes shown
1788         as ??:         as ??:
1789    
# Line 1758  INFORMATION ABOUT A PATTERN Line 1792  INFORMATION ABOUT A PATTERN
1792           00 04 m  o  n  t  h  00           00 04 m  o  n  t  h  00
1793           00 02 y  e  a  r  00 ??           00 02 y  e  a  r  00 ??
1794    
1795         When writing code to extract data  from  named  subpatterns  using  the         When  writing  code  to  extract  data from named subpatterns using the
1796         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
1797         to be different for each compiled pattern.         to be different for each compiled pattern.
1798    
1799           PCRE_INFO_OKPARTIAL           PCRE_INFO_OKPARTIAL
1800    
1801         Return 1  if  the  pattern  can  be  used  for  partial  matching  with         Return  1  if  the  pattern  can  be  used  for  partial  matching with
1802         pcre_exec(),  otherwise  0.  The fourth argument should point to an int         pcre_exec(), otherwise 0. The fourth argument should point  to  an  int
1803         variable. From  release  8.00,  this  always  returns  1,  because  the         variable.  From  release  8.00,  this  always  returns  1,  because the
1804         restrictions  that  previously  applied  to  partial matching have been         restrictions that previously applied  to  partial  matching  have  been
1805         lifted. The pcrepartial documentation gives details of  partial  match-         lifted.  The  pcrepartial documentation gives details of partial match-
1806         ing.         ing.
1807    
1808           PCRE_INFO_OPTIONS           PCRE_INFO_OPTIONS
1809    
1810         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
1811         fourth argument should point to an unsigned long  int  variable.  These         fourth  argument  should  point to an unsigned long int variable. These
1812         option bits are those specified in the call to pcre_compile(), modified         option bits are those specified in the call to pcre_compile(), modified
1813         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
1814         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
1815         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
1816         the  PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE,         the PCRE_EXTENDED option, the result is PCRE_CASELESS,  PCRE_MULTILINE,
1817         and PCRE_EXTENDED.         and PCRE_EXTENDED.
1818    
1819         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
1820         alternatives begin with one of the following:         alternatives begin with one of the following:
1821    
1822           ^     unless PCRE_MULTILINE is set           ^     unless PCRE_MULTILINE is set
# Line 1796  INFORMATION ABOUT A PATTERN Line 1830  INFORMATION ABOUT A PATTERN
1830    
1831           PCRE_INFO_SIZE           PCRE_INFO_SIZE
1832    
1833         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
1834         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
1835         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
1836         size_t variable.         size_t variable.
# Line 1804  INFORMATION ABOUT A PATTERN Line 1838  INFORMATION ABOUT A PATTERN
1838           PCRE_INFO_STUDYSIZE           PCRE_INFO_STUDYSIZE
1839    
1840         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
1841         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
1842         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
1843         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
1844         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
1845         variable.         variable.
1846    
1847    
# Line 1815  OBSOLETE INFO FUNCTION Line 1849  OBSOLETE INFO FUNCTION
1849    
1850         int pcre_info(const pcre *code, int *optptr, int *firstcharptr);         int pcre_info(const pcre *code, int *optptr, int *firstcharptr);
1851    
1852         The pcre_info() function is now obsolete because its interface  is  too         The  pcre_info()  function is now obsolete because its interface is too
1853         restrictive  to return all the available data about a compiled pattern.         restrictive to return all the available data about a compiled  pattern.
1854         New  programs  should  use  pcre_fullinfo()  instead.  The   yield   of         New   programs   should  use  pcre_fullinfo()  instead.  The  yield  of
1855         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-
1856         lowing negative numbers:         lowing negative numbers:
1857    
1858           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
1859           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1860    
1861         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
1862         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
1863         PCRE_INFO_OPTIONS above).         PCRE_INFO_OPTIONS above).
1864    
1865         If the pattern is not anchored and the  firstcharptr  argument  is  not         If  the  pattern  is  not anchored and the firstcharptr argument is not
1866         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
1867         any matched string (see PCRE_INFO_FIRSTBYTE above).         any matched string (see PCRE_INFO_FIRSTBYTE above).
1868    
1869    
# Line 1837  REFERENCE COUNTS Line 1871  REFERENCE COUNTS
1871    
1872         int pcre_refcount(pcre *code, int adjust);         int pcre_refcount(pcre *code, int adjust);
1873    
1874         The pcre_refcount() function is used to maintain a reference  count  in         The  pcre_refcount()  function is used to maintain a reference count in
1875         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
1876         benefit of applications that  operate  in  an  object-oriented  manner,         benefit  of  applications  that  operate  in an object-oriented manner,
1877         where different parts of the application may be using the same compiled         where different parts of the application may be using the same compiled
1878         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.
1879    
1880         When a pattern is compiled, the reference count field is initialized to         When a pattern is compiled, the reference count field is initialized to
1881         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
1882         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
1883         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
1884         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
1885         is outside these limits, it is forced to the appropriate limit value.         is outside these limits, it is forced to the appropriate limit value.
1886    
1887         Except  when it is zero, the reference count is not correctly preserved         Except when it is zero, the reference count is not correctly  preserved
1888         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
1889         whose byte-order is different. (This seems a highly unlikely scenario.)         whose byte-order is different. (This seems a highly unlikely scenario.)
1890    
1891    
# Line 1861  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1895  MATCHING A PATTERN: THE TRADITIONAL FUNC
1895              const char *subject, int length, int startoffset,              const char *subject, int length, int startoffset,
1896              int options, int *ovector, int ovecsize);              int options, int *ovector, int ovecsize);
1897    
1898         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
1899         compiled pattern, which is passed in the code argument. If the  pattern         compiled  pattern, which is passed in the code argument. If the pattern
1900         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
1901         argument. This function is the main matching facility of  the  library,         argument.  This  function is the main matching facility of the library,
1902         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
1903         an alternative matching function, which is described below in the  sec-         an  alternative matching function, which is described below in the sec-
1904         tion about the pcre_dfa_exec() function.         tion about the pcre_dfa_exec() function.
1905    
1906         In  most applications, the pattern will have been compiled (and option-         In most applications, the pattern will have been compiled (and  option-
1907         ally studied) in the same process that calls pcre_exec().  However,  it         ally  studied)  in the same process that calls pcre_exec(). However, it
1908         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
1909         later in different processes, possibly even on different hosts.  For  a         later  in  different processes, possibly even on different hosts. For a
1910         discussion about this, see the pcreprecompile documentation.         discussion about this, see the pcreprecompile documentation.
1911    
1912         Here is an example of a simple call to pcre_exec():         Here is an example of a simple call to pcre_exec():
# Line 1891  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1925  MATCHING A PATTERN: THE TRADITIONAL FUNC
1925    
1926     Extra data for pcre_exec()     Extra data for pcre_exec()
1927    
1928         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
1929         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
1930         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-
1931         tional information in it. The pcre_extra block contains  the  following         tional  information  in it. The pcre_extra block contains the following
1932         fields (not necessarily in this order):         fields (not necessarily in this order):
1933    
1934           unsigned long int flags;           unsigned long int flags;
# Line 1903  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1937  MATCHING A PATTERN: THE TRADITIONAL FUNC
1937           unsigned long int match_limit_recursion;           unsigned long int match_limit_recursion;
1938           void *callout_data;           void *callout_data;
1939           const unsigned char *tables;           const unsigned char *tables;
1940             unsigned char **mark;
1941    
1942         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
1943         are set. The flag bits are:         are set. The flag bits are:
1944    
1945           PCRE_EXTRA_STUDY_DATA           PCRE_EXTRA_STUDY_DATA
# Line 1912  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1947  MATCHING A PATTERN: THE TRADITIONAL FUNC
1947           PCRE_EXTRA_MATCH_LIMIT_RECURSION           PCRE_EXTRA_MATCH_LIMIT_RECURSION
1948           PCRE_EXTRA_CALLOUT_DATA           PCRE_EXTRA_CALLOUT_DATA
1949           PCRE_EXTRA_TABLES           PCRE_EXTRA_TABLES
1950             PCRE_EXTRA_MARK
1951    
1952         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
1953         the  pcre_extra  block  that is returned by pcre_study(), together with         the pcre_extra block that is returned by  pcre_study(),  together  with
1954         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
1955         add  to  the  block by setting the other fields and their corresponding         add to the block by setting the other fields  and  their  corresponding
1956         flag bits.         flag bits.
1957    
1958         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
1959         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
1960         match, but which have a very large number  of  possibilities  in  their         match,  but  which  have  a very large number of possibilities in their
1961         search  trees. The classic example is a pattern that uses nested unlim-         search trees. The classic example is a pattern that uses nested  unlim-
1962         ited repeats.         ited repeats.
1963    
1964         Internally, PCRE uses a function called match() which it calls  repeat-         Internally,  PCRE uses a function called match() which it calls repeat-
1965         edly  (sometimes  recursively). The limit set by match_limit is imposed         edly (sometimes recursively). The limit set by match_limit  is  imposed
1966         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
1967         has  the  effect  of  limiting the amount of backtracking that can take         has the effect of limiting the amount of  backtracking  that  can  take
1968         place. For patterns that are not anchored, the count restarts from zero         place. For patterns that are not anchored, the count restarts from zero
1969         for each position in the subject string.         for each position in the subject string.
1970    
1971         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
1972         default default is 10 million, which handles all but the  most  extreme         default  default  is 10 million, which handles all but the most extreme
1973         cases.  You  can  override  the  default by suppling pcre_exec() with a         cases. You can override the default  by  suppling  pcre_exec()  with  a
1974         pcre_extra    block    in    which    match_limit    is    set,     and         pcre_extra     block    in    which    match_limit    is    set,    and
1975         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
1976         exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.         exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.
1977    
1978         The match_limit_recursion field is similar to match_limit, but  instead         The  match_limit_recursion field is similar to match_limit, but instead
1979         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
1980         the depth of recursion. The recursion depth is a  smaller  number  than         the  depth  of  recursion. The recursion depth is a smaller number than
1981         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-
1982         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.
1983    
1984         Limiting the recursion depth limits the amount of  stack  that  can  be         Limiting  the  recursion  depth  limits the amount of stack that can be
1985         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
1986         of the stack, the amount of heap memory that can be used.         of the stack, the amount of heap memory that can be used.
1987    
1988         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
1989         built;  the  default  default  is  the  same  value  as the default for         built; the default default  is  the  same  value  as  the  default  for
1990         match_limit. You can override the default by suppling pcre_exec()  with         match_limit.  You can override the default by suppling pcre_exec() with
1991         a   pcre_extra   block  in  which  match_limit_recursion  is  set,  and         a  pcre_extra  block  in  which  match_limit_recursion  is   set,   and
1992         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
1993         limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.         limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.
1994    
1995         The  callout_data  field is used in conjunction with the "callout" fea-         The callout_data field is used in conjunction with the  "callout"  fea-
1996         ture, and is described in the pcrecallout documentation.         ture, and is described in the pcrecallout documentation.
1997    
1998         The tables field  is  used  to  pass  a  character  tables  pointer  to         The  tables  field  is  used  to  pass  a  character  tables pointer to
1999         pcre_exec();  this overrides the value that is stored with the compiled         pcre_exec(); this overrides the value that is stored with the  compiled
2000         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
2001         custom  tables  were  supplied to pcre_compile() via its tableptr argu-         custom tables were supplied to pcre_compile() via  its  tableptr  argu-
2002         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
2003         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-
2004         using patterns that have been saved after compiling  with  an  external         using  patterns  that  have been saved after compiling with an external
2005         set  of  tables,  because  the  external tables might be at a different         set of tables, because the external tables  might  be  at  a  different
2006         address when pcre_exec() is called. See the  pcreprecompile  documenta-         address  when  pcre_exec() is called. See the pcreprecompile documenta-
2007         tion for a discussion of saving compiled patterns for later use.         tion for a discussion of saving compiled patterns for later use.
2008    
2009           If PCRE_EXTRA_MARK is set in the flags field, the mark  field  must  be
2010           set  to  point  to a char * variable. If the pattern contains any back-
2011           tracking control verbs such as (*MARK:NAME), and the execution ends  up
2012           with  a  name  to  pass back, a pointer to the name string (zero termi-
2013           nated) is placed in the variable pointed to  by  the  mark  field.  The
2014           names  are  within  the  compiled pattern; if you wish to retain such a
2015           name you must copy it before freeing the memory of a compiled  pattern.
2016           If  there  is no name to pass back, the variable pointed to by the mark
2017           field set to NULL. For details of the backtracking control  verbs,  see
2018           the section entitled "Backtracking control" in the pcrepattern documen-
2019           tation.
2020    
2021     Option bits for pcre_exec()     Option bits for pcre_exec()
2022    
2023         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.
2024         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,
2025         PCRE_NOTBOL,    PCRE_NOTEOL,    PCRE_NOTEMPTY,   PCRE_NOTEMPTY_ATSTART,         PCRE_NOTBOL,   PCRE_NOTEOL,    PCRE_NOTEMPTY,    PCRE_NOTEMPTY_ATSTART,
2026         PCRE_NO_START_OPTIMIZE,  PCRE_NO_UTF8_CHECK,   PCRE_PARTIAL_SOFT,   and         PCRE_NO_START_OPTIMIZE,   PCRE_NO_UTF8_CHECK,   PCRE_PARTIAL_SOFT,  and
2027         PCRE_PARTIAL_HARD.         PCRE_PARTIAL_HARD.
2028    
2029           PCRE_ANCHORED           PCRE_ANCHORED
2030    
2031         The  PCRE_ANCHORED  option  limits pcre_exec() to matching at the first         The PCRE_ANCHORED option limits pcre_exec() to matching  at  the  first
2032         matching position. If a pattern was  compiled  with  PCRE_ANCHORED,  or         matching  position.  If  a  pattern was compiled with PCRE_ANCHORED, or
2033         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
2034         unachored at matching time.         unachored at matching time.
2035    
2036           PCRE_BSR_ANYCRLF           PCRE_BSR_ANYCRLF
2037           PCRE_BSR_UNICODE           PCRE_BSR_UNICODE
2038    
2039         These options (which are mutually exclusive) control what the \R escape         These options (which are mutually exclusive) control what the \R escape
2040         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,
2041         or to match any Unicode newline sequence. These  options  override  the         or  to  match  any Unicode newline sequence. These options override the
2042         choice that was made or defaulted when the pattern was compiled.         choice that was made or defaulted when the pattern was compiled.
2043    
2044           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
# Line 1999  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2047  MATCHING A PATTERN: THE TRADITIONAL FUNC
2047           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
2048           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
2049    
2050         These  options  override  the  newline  definition  that  was chosen or         These options override  the  newline  definition  that  was  chosen  or
2051         defaulted when the pattern was compiled. For details, see the  descrip-         defaulted  when the pattern was compiled. For details, see the descrip-
2052         tion  of  pcre_compile()  above.  During  matching,  the newline choice         tion of pcre_compile()  above.  During  matching,  the  newline  choice
2053         affects the behaviour of the dot, circumflex,  and  dollar  metacharac-         affects  the  behaviour  of the dot, circumflex, and dollar metacharac-
2054         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
2055         match failure for an unanchored pattern.         match failure for an unanchored pattern.
2056    
2057         When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF,  or  PCRE_NEWLINE_ANY  is         When  PCRE_NEWLINE_CRLF,  PCRE_NEWLINE_ANYCRLF,  or PCRE_NEWLINE_ANY is
2058         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-
2059         rent position is at a  CRLF  sequence,  and  the  pattern  contains  no         rent  position  is  at  a  CRLF  sequence,  and the pattern contains no
2060         explicit  matches  for  CR  or  LF  characters,  the  match position is         explicit matches for  CR  or  LF  characters,  the  match  position  is
2061         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
2062         CRLF.         CRLF.
2063    
2064         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
2065         expected. For example, if the  pattern  is  .+A  (and  the  PCRE_DOTALL         expected.  For  example,  if  the  pattern  is .+A (and the PCRE_DOTALL
2066         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
2067         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.
2068         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-
2069         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-
2070         acter after the first failure.         acter after the first failure.
2071    
2072         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
2073         those characters, or one of the \r or  \n  escape  sequences.  Implicit         those  characters,  or  one  of the \r or \n escape sequences. Implicit
2074         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
2075         LF in the characters that it matches).         LF in the characters that it matches).
2076    
2077         Notwithstanding the above, anomalous effects may still occur when  CRLF         Notwithstanding  the above, anomalous effects may still occur when CRLF
2078         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
2079         pattern.         pattern.
2080    
2081           PCRE_NOTBOL           PCRE_NOTBOL
2082    
2083         This option specifies that first character of the subject string is not         This option specifies that first character of the subject string is not
2084         the  beginning  of  a  line, so the circumflex metacharacter should not         the beginning of a line, so the  circumflex  metacharacter  should  not
2085         match before it. Setting this without PCRE_MULTILINE (at compile  time)         match  before it. Setting this without PCRE_MULTILINE (at compile time)
2086         causes  circumflex  never to match. This option affects only the behav-         causes circumflex never to match. This option affects only  the  behav-
2087         iour of the circumflex metacharacter. It does not affect \A.         iour of the circumflex metacharacter. It does not affect \A.
2088    
2089           PCRE_NOTEOL           PCRE_NOTEOL
2090    
2091         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
2092         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
2093         in multiline mode) a newline immediately before it. Setting this  with-         in  multiline mode) a newline immediately before it. Setting this with-
2094         out PCRE_MULTILINE (at compile time) causes dollar never to match. This         out PCRE_MULTILINE (at compile time) causes dollar never to match. This
2095         option affects only the behaviour of the dollar metacharacter. It  does         option  affects only the behaviour of the dollar metacharacter. It does
2096         not affect \Z or \z.         not affect \Z or \z.
2097    
2098           PCRE_NOTEMPTY           PCRE_NOTEMPTY
2099    
2100         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
2101         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
2102         the  alternatives  match  the empty string, the entire match fails. For         the alternatives match the empty string, the entire  match  fails.  For
2103         example, if the pattern         example, if the pattern
2104    
2105           a?b?           a?b?
2106    
2107         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
2108         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
2109         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-
2110         rences of "a" or "b".         rences of "a" or "b".
2111    
2112           PCRE_NOTEMPTY_ATSTART           PCRE_NOTEMPTY_ATSTART
2113    
2114         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
2115         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
2116         anchored, such a match can occur only if the pattern contains \K.         anchored, such a match can occur only if the pattern contains \K.
2117    
2118         Perl     has    no    direct    equivalent    of    PCRE_NOTEMPTY    or         Perl    has    no    direct    equivalent    of    PCRE_NOTEMPTY     or
2119         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
2120         match  of  the empty string within its split() function, and when using         match of the empty string within its split() function, and  when  using
2121         the /g modifier. It is  possible  to  emulate  Perl's  behaviour  after         the  /g  modifier.  It  is  possible  to emulate Perl's behaviour after
2122         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-
2123         set with PCRE_NOTEMPTY_ATSTART and  PCRE_ANCHORED,  and  then  if  that         set  with  PCRE_NOTEMPTY_ATSTART  and  PCRE_ANCHORED,  and then if that
2124         fails, by advancing the starting offset (see below) and trying an ordi-         fails, by advancing the starting offset (see below) and trying an ordi-
2125         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
2126         in the pcredemo sample program.         in the pcredemo sample program.
2127    
2128           PCRE_NO_START_OPTIMIZE           PCRE_NO_START_OPTIMIZE
2129    
2130         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
2131         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
2132         known  that  a  match must start with a specific character, it searches         known that an unanchored match must start with a specific character, it
2133         the subject for that character, and fails immediately if it cannot find         searches  the  subject  for that character, and fails immediately if it
2134         it,  without actually running the main matching function. When callouts         cannot find it, without actually running the  main  matching  function.
2135         are in use, these optimizations can cause  them  to  be  skipped.  This         This means that a special item such as (*COMMIT) at the start of a pat-
2136         option  disables  the  "start-up" optimizations, causing performance to         tern is not considered until after a suitable starting  point  for  the
2137         suffer, but ensuring that the callouts do occur.         match  has been found. When callouts or (*MARK) items are in use, these
2138           "start-up" optimizations can cause them to be skipped if the pattern is
2139           never  actually  used.  The start-up optimizations are in effect a pre-
2140           scan of the subject that takes place before the pattern is run.
2141    
2142           The PCRE_NO_START_OPTIMIZE option disables the start-up  optimizations,
2143           possibly  causing  performance  to  suffer,  but ensuring that in cases
2144           where the result is "no match", the callouts do occur, and  that  items
2145           such as (*COMMIT) and (*MARK) are considered at every possible starting
2146           position in the subject  string.   Setting  PCRE_NO_START_OPTIMIZE  can
2147           change the outcome of a matching operation.  Consider the pattern
2148    
2149             (*COMMIT)ABC
2150    
2151           When  this  is  compiled, PCRE records the fact that a match must start
2152           with the character "A". Suppose the subject  string  is  "DEFABC".  The
2153           start-up  optimization  scans along the subject, finds "A" and runs the
2154           first match attempt from there. The (*COMMIT) item means that the  pat-
2155           tern  must  match the current starting position, which in this case, it
2156           does. However, if the same match  is  run  with  PCRE_NO_START_OPTIMIZE
2157           set,  the  initial  scan  along the subject string does not happen. The
2158           first match attempt is run starting  from  "D"  and  when  this  fails,
2159           (*COMMIT)  prevents  any  further  matches  being tried, so the overall
2160           result is "no match". If the pattern is studied,  more  start-up  opti-
2161           mizations  may  be  used. For example, a minimum length for the subject
2162           may be recorded. Consider the pattern
2163    
2164             (*MARK:A)(X|Y)
2165    
2166           The minimum length for a match is one  character.  If  the  subject  is
2167           "ABC",  there  will  be  attempts  to  match "ABC", "BC", "C", and then
2168           finally an empty string.  If the pattern is studied, the final  attempt
2169           does  not take place, because PCRE knows that the subject is too short,
2170           and so the (*MARK) is never encountered.  In this  case,  studying  the
2171           pattern  does  not  affect the overall match result, which is still "no
2172           match", but it does affect the auxiliary information that is returned.
2173    
2174           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
2175    
# Line 2273  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2356  MATCHING A PATTERN: THE TRADITIONAL FUNC
2356         purpose.  If the call via pcre_malloc() fails, this error is given. The         purpose.  If the call via pcre_malloc() fails, this error is given. The
2357         memory is automatically freed at the end of matching.         memory is automatically freed at the end of matching.
2358    
2359           This error is also given if pcre_stack_malloc() fails  in  pcre_exec().
2360           This  can happen only when PCRE has been compiled with --disable-stack-
2361           for-recursion.
2362    
2363           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2364    
2365         This error is used by the pcre_copy_substring(),  pcre_get_substring(),         This error is used by the pcre_copy_substring(),  pcre_get_substring(),
# Line 2584  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2671  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2671         The unused bits of the options argument  for  pcre_dfa_exec()  must  be         The unused bits of the options argument  for  pcre_dfa_exec()  must  be
2672         zero.  The  only  bits  that  may  be  set are PCRE_ANCHORED, PCRE_NEW-         zero.  The  only  bits  that  may  be  set are PCRE_ANCHORED, PCRE_NEW-
2673         LINE_xxx,        PCRE_NOTBOL,        PCRE_NOTEOL,        PCRE_NOTEMPTY,         LINE_xxx,        PCRE_NOTBOL,        PCRE_NOTEOL,        PCRE_NOTEMPTY,
2674         PCRE_NOTEMPTY_ATSTART, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_HARD, PCRE_PAR-         PCRE_NOTEMPTY_ATSTART,       PCRE_NO_UTF8_CHECK,      PCRE_BSR_ANYCRLF,
2675         TIAL_SOFT, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All  but  the  last         PCRE_BSR_UNICODE, PCRE_NO_START_OPTIMIZE, PCRE_PARTIAL_HARD,  PCRE_PAR-
2676         four  of  these  are  exactly  the  same  as  for pcre_exec(), so their         TIAL_SOFT,  PCRE_DFA_SHORTEST,  and PCRE_DFA_RESTART.  All but the last
2677           four of these are  exactly  the  same  as  for  pcre_exec(),  so  their
2678         description is not repeated here.         description is not repeated here.
2679    
2680           PCRE_PARTIAL_HARD           PCRE_PARTIAL_HARD
2681           PCRE_PARTIAL_SOFT           PCRE_PARTIAL_SOFT
2682    
2683         These have the same general effect as they do for pcre_exec(), but  the         These  have the same general effect as they do for pcre_exec(), but the
2684         details  are  slightly  different.  When  PCRE_PARTIAL_HARD  is set for         details are slightly  different.  When  PCRE_PARTIAL_HARD  is  set  for
2685         pcre_dfa_exec(), it returns PCRE_ERROR_PARTIAL if the end of  the  sub-         pcre_dfa_exec(),  it  returns PCRE_ERROR_PARTIAL if the end of the sub-
2686         ject  is  reached  and there is still at least one matching possibility         ject is reached and there is still at least  one  matching  possibility
2687         that requires additional characters. This happens even if some complete         that requires additional characters. This happens even if some complete
2688         matches have also been found. When PCRE_PARTIAL_SOFT is set, the return         matches have also been found. When PCRE_PARTIAL_SOFT is set, the return
2689         code PCRE_ERROR_NOMATCH is converted into PCRE_ERROR_PARTIAL if the end         code PCRE_ERROR_NOMATCH is converted into PCRE_ERROR_PARTIAL if the end
2690         of  the  subject  is  reached, there have been no complete matches, but         of the subject is reached, there have been  no  complete  matches,  but
2691         there is still at least one matching possibility. The  portion  of  the         there  is  still  at least one matching possibility. The portion of the
2692         string  that  was inspected when the longest partial match was found is         string that was inspected when the longest partial match was  found  is
2693         set as the first matching string in both cases.         set as the first matching string in both cases.
2694    
2695           PCRE_DFA_SHORTEST           PCRE_DFA_SHORTEST
2696    
2697         Setting the PCRE_DFA_SHORTEST option causes the matching  algorithm  to         Setting  the  PCRE_DFA_SHORTEST option causes the matching algorithm to
2698         stop as soon as it has found one match. Because of the way the alterna-         stop as soon as it has found one match. Because of the way the alterna-
2699         tive algorithm works, this is necessarily the shortest  possible  match         tive  algorithm  works, this is necessarily the shortest possible match
2700         at the first possible matching point in the subject string.         at the first possible matching point in the subject string.
2701    
2702           PCRE_DFA_RESTART           PCRE_DFA_RESTART
2703    
2704         When pcre_dfa_exec() returns a partial match, it is possible to call it         When pcre_dfa_exec() returns a partial match, it is possible to call it
2705         again, with additional subject characters, and have  it  continue  with         again,  with  additional  subject characters, and have it continue with
2706         the  same match. The PCRE_DFA_RESTART option requests this action; when         the same match. The PCRE_DFA_RESTART option requests this action;  when
2707         it is set, the workspace and wscount options must  reference  the  same         it  is  set,  the workspace and wscount options must reference the same
2708         vector  as  before  because data about the match so far is left in them         vector as before because data about the match so far is  left  in  them
2709         after a partial match. There is more discussion of this facility in the         after a partial match. There is more discussion of this facility in the
2710         pcrepartial documentation.         pcrepartial documentation.
2711    
2712     Successful returns from pcre_dfa_exec()     Successful returns from pcre_dfa_exec()
2713    
2714         When  pcre_dfa_exec()  succeeds, it may have matched more than one sub-         When pcre_dfa_exec() succeeds, it may have matched more than  one  sub-
2715         string in the subject. Note, however, that all the matches from one run         string in the subject. Note, however, that all the matches from one run
2716         of  the  function  start  at the same point in the subject. The shorter         of the function start at the same point in  the  subject.  The  shorter
2717         matches are all initial substrings of the longer matches. For  example,         matches  are all initial substrings of the longer matches. For example,
2718         if the pattern         if the pattern
2719    
2720           <.*>           <.*>
# Line 2641  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2729  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2729           <something> <something else>           <something> <something else>
2730           <something> <something else> <something further>           <something> <something else> <something further>
2731    
2732         On  success,  the  yield of the function is a number greater than zero,         On success, the yield of the function is a number  greater  than  zero,
2733         which is the number of matched substrings.  The  substrings  themselves         which  is  the  number of matched substrings. The substrings themselves
2734         are  returned  in  ovector. Each string uses two elements; the first is         are returned in ovector. Each string uses two elements;  the  first  is
2735         the offset to the start, and the second is the offset to  the  end.  In         the  offset  to  the start, and the second is the offset to the end. In
2736         fact,  all  the  strings  have the same start offset. (Space could have         fact, all the strings have the same start  offset.  (Space  could  have
2737         been saved by giving this only once, but it was decided to retain  some         been  saved by giving this only once, but it was decided to retain some
2738         compatibility  with  the  way pcre_exec() returns data, even though the         compatibility with the way pcre_exec() returns data,  even  though  the
2739         meaning of the strings is different.)         meaning of the strings is different.)
2740    
2741         The strings are returned in reverse order of length; that is, the long-         The strings are returned in reverse order of length; that is, the long-
2742         est  matching  string is given first. If there were too many matches to         est matching string is given first. If there were too many  matches  to
2743         fit into ovector, the yield of the function is zero, and the vector  is         fit  into ovector, the yield of the function is zero, and the vector is
2744         filled with the longest matches.         filled with the longest matches.
2745    
2746     Error returns from pcre_dfa_exec()     Error returns from pcre_dfa_exec()
2747    
2748         The  pcre_dfa_exec()  function returns a negative number when it fails.         The pcre_dfa_exec() function returns a negative number when  it  fails.
2749         Many of the errors are the same  as  for  pcre_exec(),  and  these  are         Many  of  the  errors  are  the  same as for pcre_exec(), and these are
2750         described  above.   There are in addition the following errors that are         described above.  There are in addition the following errors  that  are
2751         specific to pcre_dfa_exec():         specific to pcre_dfa_exec():
2752    
2753           PCRE_ERROR_DFA_UITEM      (-16)           PCRE_ERROR_DFA_UITEM      (-16)
2754    
2755         This return is given if pcre_dfa_exec() encounters an item in the  pat-         This  return is given if pcre_dfa_exec() encounters an item in the pat-
2756         tern  that  it  does not support, for instance, the use of \C or a back         tern that it does not support, for instance, the use of \C  or  a  back
2757         reference.         reference.
2758    
2759           PCRE_ERROR_DFA_UCOND      (-17)           PCRE_ERROR_DFA_UCOND      (-17)
2760    
2761         This return is given if pcre_dfa_exec()  encounters  a  condition  item         This  return  is  given  if pcre_dfa_exec() encounters a condition item
2762         that  uses  a back reference for the condition, or a test for recursion         that uses a back reference for the condition, or a test  for  recursion
2763         in a specific group. These are not supported.         in a specific group. These are not supported.
2764    
2765           PCRE_ERROR_DFA_UMLIMIT    (-18)           PCRE_ERROR_DFA_UMLIMIT    (-18)
2766    
2767         This return is given if pcre_dfa_exec() is called with an  extra  block         This  return  is given if pcre_dfa_exec() is called with an extra block
2768         that contains a setting of the match_limit field. This is not supported         that contains a setting of the match_limit field. This is not supported
2769         (it is meaningless).         (it is meaningless).
2770    
2771           PCRE_ERROR_DFA_WSSIZE     (-19)           PCRE_ERROR_DFA_WSSIZE     (-19)
2772    
2773         This return is given if  pcre_dfa_exec()  runs  out  of  space  in  the         This  return  is  given  if  pcre_dfa_exec()  runs  out of space in the
2774         workspace vector.         workspace vector.
2775    
2776           PCRE_ERROR_DFA_RECURSE    (-20)           PCRE_ERROR_DFA_RECURSE    (-20)
2777    
2778         When  a  recursive subpattern is processed, the matching function calls         When a recursive subpattern is processed, the matching  function  calls
2779         itself recursively, using private vectors for  ovector  and  workspace.         itself  recursively,  using  private vectors for ovector and workspace.
2780         This  error  is  given  if  the output vector is not large enough. This         This error is given if the output vector  is  not  large  enough.  This
2781         should be extremely rare, as a vector of size 1000 is used.         should be extremely rare, as a vector of size 1000 is used.
2782    
2783    
2784  SEE ALSO  SEE ALSO
2785    
2786         pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3),  pcrepar-         pcrebuild(3),  pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-
2787         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
2788    
2789    
# Line 2708  AUTHOR Line 2796  AUTHOR
2796    
2797  REVISION  REVISION
2798    
2799         Last updated: 03 October 2009         Last updated: 21 June 2010
2800         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
2801  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2802    
2803    
# Line 2905  DIFFERENCES BETWEEN PCRE AND PERL Line 2993  DIFFERENCES BETWEEN PCRE AND PERL
2993    
2994         This  document describes the differences in the ways that PCRE and Perl         This  document describes the differences in the ways that PCRE and Perl
2995         handle regular expressions. The differences  described  here  are  with         handle regular expressions. The differences  described  here  are  with
2996         respect to Perl 5.10.         respect to Perl 5.10/5.11.
2997    
2998         1.  PCRE has only a subset of Perl's UTF-8 and Unicode support. Details         1.  PCRE has only a subset of Perl's UTF-8 and Unicode support. Details
2999         of what it does have are given in the section on UTF-8 support  in  the         of what it does have are given in the section on UTF-8 support  in  the
# Line 2976  DIFFERENCES BETWEEN PCRE AND PERL Line 3064  DIFFERENCES BETWEEN PCRE AND PERL
3064         matching "aba" against the  pattern  /^(a(b)?)+$/  in  Perl  leaves  $2         matching "aba" against the  pattern  /^(a(b)?)+$/  in  Perl  leaves  $2
3065         unset, but in PCRE it is set to "b".         unset, but in PCRE it is set to "b".
3066    
3067         11.  PCRE  does  support  Perl  5.10's  backtracking  verbs  (*ACCEPT),         11.  PCRE's handling of duplicate subpattern numbers and duplicate sub-
        (*FAIL), (*F), (*COMMIT), (*PRUNE), (*SKIP), and (*THEN), but  only  in  
        the forms without an argument. PCRE does not support (*MARK).  
   
        12.  PCRE's handling of duplicate subpattern numbers and duplicate sub-  
3068         pattern names is not as general as Perl's. This is a consequence of the         pattern names is not as general as Perl's. This is a consequence of the
3069         fact the PCRE works internally just with numbers, using an external ta-         fact the PCRE works internally just with numbers, using an external ta-
3070         ble to translate between numbers and names. In  particular,  a  pattern         ble to translate between numbers and names. In  particular,  a  pattern
# Line 2991  DIFFERENCES BETWEEN PCRE AND PERL Line 3075  DIFFERENCES BETWEEN PCRE AND PERL
3075         turing subpattern number 1. To avoid this confusing situation, an error         turing subpattern number 1. To avoid this confusing situation, an error
3076         is given at compile time.         is given at compile time.
3077    
3078         13. PCRE provides some extensions to the Perl regular expression facil-         12. PCRE provides some extensions to the Perl regular expression facil-
3079         ities.   Perl  5.10  includes new features that are not in earlier ver-         ities.   Perl  5.10  includes new features that are not in earlier ver-
3080         sions of Perl, some of which (such as named parentheses) have  been  in         sions of Perl, some of which (such as named parentheses) have  been  in
3081         PCRE for some time. This list is with respect to Perl 5.10:         PCRE for some time. This list is with respect to Perl 5.10:
# Line 3046  AUTHOR Line 3130  AUTHOR
3130    
3131  REVISION  REVISION
3132    
3133         Last updated: 04 October 2009         Last updated: 12 May 2010
3134         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
3135  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
3136    
3137    
# Line 3089  PCRE REGULAR EXPRESSION DETAILS Line 3173  PCRE REGULAR EXPRESSION DETAILS
3173         below.  There  is  also  a  summary of UTF-8 features in the section on         below.  There  is  also  a  summary of UTF-8 features in the section on
3174         UTF-8 support in the main pcre page.         UTF-8 support in the main pcre page.
3175    
3176           Another special sequence that may appear at the start of a  pattern  or
3177           in combination with (*UTF8) is:
3178    
3179             (*UCP)
3180    
3181           This  has  the  same  effect  as setting the PCRE_UCP option: it causes
3182           sequences such as \d and \w to  use  Unicode  properties  to  determine
3183           character types, instead of recognizing only characters with codes less
3184           than 128 via a lookup table.
3185    
3186         The remainder of this document discusses the  patterns  that  are  sup-         The remainder of this document discusses the  patterns  that  are  sup-
3187         ported  by  PCRE when its main matching function, pcre_exec(), is used.         ported  by  PCRE when its main matching function, pcre_exec(), is used.
3188         From  release  6.0,   PCRE   offers   a   second   matching   function,         From  release  6.0,   PCRE   offers   a   second   matching   function,
# Line 3129  NEWLINE CONVENTIONS Line 3223  NEWLINE CONVENTIONS
3223         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
3224         present, the last one is used.         present, the last one is used.
3225    
3226         The  newline  convention  does  not  affect what the \R escape sequence         The  newline convention affects the interpretation of the dot metachar-
3227         matches. By default, this is any Unicode  newline  sequence,  for  Perl         acter when PCRE_DOTALL is not set, and also the behaviour of  \N.  How-
3228         compatibility.  However, this can be changed; see the description of \R         ever,  it  does  not  affect  what  the  \R escape sequence matches. By
3229         in the section entitled "Newline sequences" below. A change of \R  set-         default, this is any Unicode newline sequence, for Perl  compatibility.
3230         ting can be combined with a change of newline convention.         However,  this can be changed; see the description of \R in the section
3231           entitled "Newline sequences" below. A change of \R setting can be  com-
3232           bined with a change of newline convention.
3233    
3234    
3235  CHARACTERS AND METACHARACTERS  CHARACTERS AND METACHARACTERS
# Line 3246  BACKSLASH Line 3342  BACKSLASH
3342           \n        linefeed (hex 0A)           \n        linefeed (hex 0A)
3343           \r        carriage return (hex 0D)           \r        carriage return (hex 0D)
3344           \t        tab (hex 09)           \t        tab (hex 09)
3345           \ddd      character with octal code ddd, or backreference           \ddd      character with octal code ddd, or back reference
3346           \xhh      character with hex code hh           \xhh      character with hex code hh
3347           \x{hhh..} character with hex code hhh..           \x{hhh..} character with hex code hhh..
3348    
# Line 3315  BACKSLASH Line 3411  BACKSLASH
3411         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
3412         inside  and  outside character classes. In addition, inside a character         inside  and  outside character classes. In addition, inside a character
3413         class, the sequence \b is interpreted as the backspace  character  (hex         class, the sequence \b is interpreted as the backspace  character  (hex
3414         08),  and the sequences \R and \X are interpreted as the characters "R"         08).  The sequences \B, \N, \R, and \X are not special inside a charac-
3415         and "X", respectively. Outside a character class, these sequences  have         ter class. Like any  other  unrecognized  escape  sequences,  they  are
3416         different meanings (see below).         treated  as  the  literal characters "B", "N", "R", and "X" by default,
3417           but cause an error if the PCRE_EXTRA option is set. Outside a character
3418           class, these sequences have different meanings.
3419    
3420     Absolute and relative back references     Absolute and relative back references
3421    
# Line 3337  BACKSLASH Line 3435  BACKSLASH
3435    
3436     Generic character types     Generic character types
3437    
3438         Another use of backslash is for specifying generic character types. The         Another use of backslash is for specifying generic character types:
        following are always recognized:  
3439    
3440           \d     any decimal digit           \d     any decimal digit
3441           \D     any character that is not a decimal digit           \D     any character that is not a decimal digit
# Line 3351  BACKSLASH Line 3448  BACKSLASH
3448           \w     any "word" character           \w     any "word" character
3449           \W     any "non-word" character           \W     any "non-word" character
3450    
3451         Each pair of escape sequences partitions the complete set of characters         There is also the single sequence \N, which matches a non-newline char-
3452         into  two disjoint sets. Any given character matches one, and only one,         acter.  This is the same as the "." metacharacter when  PCRE_DOTALL  is
3453         of each pair.         not set.
3454    
3455         These character type sequences can appear both inside and outside char-         Each  pair of lower and upper case escape sequences partitions the com-
3456         acter  classes.  They each match one character of the appropriate type.         plete set of characters into two disjoint  sets.  Any  given  character
3457         If the current matching point is at the end of the subject string,  all         matches  one, and only one, of each pair. The sequences can appear both
3458         of them fail, since there is no character to match.         inside and outside character classes. They each match one character  of
3459           the  appropriate  type.  If the current matching point is at the end of
3460           the subject string, all of them fail, because there is no character  to
3461           match.
3462    
3463         For  compatibility  with Perl, \s does not match the VT character (code         For  compatibility  with Perl, \s does not match the VT character (code
3464         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
# Line 3366  BACKSLASH Line 3466  BACKSLASH
3466         "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-
3467         ter. In PCRE, it never does.         ter. In PCRE, it never does.
3468    
3469         In  UTF-8 mode, characters with values greater than 128 never match \d,         A  "word"  character is an underscore or any character that is a letter
3470         \s, or \w, and always match \D, \S, and \W. This is true even when Uni-         or digit.  By default, the definition of letters  and  digits  is  con-
3471         code  character  property  support is available. These sequences retain         trolled  by PCRE's low-valued character tables, and may vary if locale-
3472         their original meanings from before UTF-8 support was available, mainly         specific matching is taking place (see "Locale support" in the  pcreapi
3473         for  efficiency  reasons. Note that this also affects \b, because it is         page).  For  example,  in  a French locale such as "fr_FR" in Unix-like
3474         defined in terms of \w and \W.         systems, or "french" in Windows, some character codes greater than  128
3475           are  used  for  accented letters, and these are then matched by \w. The
3476           use of locales with Unicode is discouraged.
3477    
3478           By default, in UTF-8 mode, characters  with  values  greater  than  128
3479           never  match  \d,  \s,  or  \w,  and always match \D, \S, and \W. These
3480           sequences retain their original meanings from before UTF-8 support  was
3481           available,  mainly for efficiency reasons. However, if PCRE is compiled
3482           with Unicode property support, and the PCRE_UCP option is set, the  be-
3483           haviour  is  changed  so  that Unicode properties are used to determine
3484           character types, as follows:
3485    
3486             \d  any character that \p{Nd} matches (decimal digit)
3487             \s  any character that \p{Z} matches, plus HT, LF, FF, CR
3488             \w  any character that \p{L} or \p{N} matches, plus underscore
3489    
3490           The upper case escapes match the inverse sets of characters. Note  that
3491           \d  matches  only decimal digits, whereas \w matches any Unicode digit,
3492           as well as any Unicode letter, and underscore. Note also that  PCRE_UCP
3493           affects  \b,  and  \B  because  they are defined in terms of \w and \W.
3494           Matching these sequences is noticeably slower when PCRE_UCP is set.
3495    
3496         The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to         The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to
3497         the  other  sequences, these do match certain high-valued codepoints in         the  other  sequences,  which  match  only ASCII characters by default,
3498         UTF-8 mode.  The horizontal space characters are:         these always  match  certain  high-valued  codepoints  in  UTF-8  mode,
3499           whether or not PCRE_UCP is set. The horizontal space characters are:
3500    
3501           U+0009     Horizontal tab           U+0009     Horizontal tab
3502           U+0020     Space           U+0020     Space
# Line 3407  BACKSLASH Line 3528  BACKSLASH
3528           U+2028     Line separator           U+2028     Line separator
3529           U+2029     Paragraph separator           U+2029     Paragraph separator
3530    
        A "word" character is an underscore or any character less than 256 that  
        is  a  letter  or  digit.  The definition of letters and digits is con-  
        trolled by PCRE's low-valued character tables, and may vary if  locale-  
        specific  matching is taking place (see "Locale support" in the pcreapi  
        page). For example, in a French locale such  as  "fr_FR"  in  Unix-like  
        systems,  or "french" in Windows, some character codes greater than 128  
        are used for accented letters, and these are matched by \w. The use  of  
        locales with Unicode is discouraged.  
   
3531     Newline sequences     Newline sequences
3532    
3533         Outside  a  character class, by default, the escape sequence \R matches         Outside  a  character class, by default, the escape sequence \R matches
# Line 3454  BACKSLASH Line 3566  BACKSLASH
3566         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
3567         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
3568         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
3569         newline convention, for example, a pattern can start with:         newline convention; for example, a pattern can start with:
3570    
3571           (*ANY)(*BSR_ANYCRLF)           (*ANY)(*BSR_ANYCRLF)
3572    
3573         Inside a character class, \R matches the letter "R".         They can also be combined with the (*UTF8) or (*UCP) special sequences.
3574           Inside  a  character  class,  \R  is  treated as an unrecognized escape
3575           sequence, and so matches the letter "R" by default, but causes an error
3576           if PCRE_EXTRA is set.
3577    
3578     Unicode character properties     Unicode character properties
3579    
3580         When PCRE is built with Unicode character property support, three addi-         When PCRE is built with Unicode character property support, three addi-
3581         tional  escape sequences that match characters with specific properties         tional escape sequences that match characters with specific  properties
3582         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
3583         limited  to  testing characters whose codepoints are less than 256, but         limited to testing characters whose codepoints are less than  256,  but
3584         they do work in this mode.  The extra escape sequences are:         they do work in this mode.  The extra escape sequences are:
3585    
3586           \p{xx}   a character with the xx property           \p{xx}   a character with the xx property
3587           \P{xx}   a character without the xx property           \P{xx}   a character without the xx property
3588           \X       an extended Unicode sequence           \X       an extended Unicode sequence
3589    
3590         The property names represented by xx above are limited to  the  Unicode         The  property  names represented by xx above are limited to the Unicode
3591         script names, the general category properties, and "Any", which matches         script names, the general category properties, "Any", which matches any
3592         any character (including newline). Other properties such as "InMusical-         character   (including  newline),  and  some  special  PCRE  properties
3593         Symbols"  are  not  currently supported by PCRE. Note that \P{Any} does         (described in the next section).  Other Perl properties such as  "InMu-
3594         not match any characters, so always causes a match failure.         sicalSymbols"  are  not  currently supported by PCRE. Note that \P{Any}
3595           does not match any characters, so always causes a match failure.
3596    
3597         Sets of Unicode characters are defined as belonging to certain scripts.         Sets of Unicode characters are defined as belonging to certain scripts.
3598         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.
# Line 3488  BACKSLASH Line 3604  BACKSLASH
3604         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
3605         "Common". The current list of scripts is:         "Common". The current list of scripts is:
3606    
3607         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,         Arabic, Armenian, Avestan, Balinese, Bamum, Bengali, Bopomofo, Braille,
3608         Buhid,  Canadian_Aboriginal,  Cherokee,  Common,   Coptic,   Cuneiform,         Buginese, Buhid, Canadian_Aboriginal, Carian, Cham,  Cherokee,  Common,
3609         Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,         Coptic,   Cuneiform,  Cypriot,  Cyrillic,  Deseret,  Devanagari,  Egyp-
3610         Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew,  Hira-         tian_Hieroglyphs,  Ethiopic,  Georgian,  Glagolitic,   Gothic,   Greek,
3611         gana,  Inherited,  Kannada,  Katakana,  Kharoshthi,  Khmer, Lao, Latin,         Gujarati,  Gurmukhi,  Han,  Hangul,  Hanunoo,  Hebrew,  Hiragana, Impe-
3612         Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,         rial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian,
3613         Ogham,  Old_Italic,  Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,         Javanese,  Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khmer, Lao,
3614         Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,         Latin,  Lepcha,  Limbu,  Linear_B,  Lisu,  Lycian,  Lydian,  Malayalam,
3615         Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.         Meetei_Mayek,  Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic,
3616           Old_Persian, Old_South_Arabian, Old_Turkic, Ol_Chiki,  Oriya,  Osmanya,
3617         Each  character has exactly one general category property, specified by         Phags_Pa,  Phoenician,  Rejang,  Runic, Samaritan, Saurashtra, Shavian,
3618         a two-letter abbreviation. For compatibility with Perl, negation can be         Sinhala, Sundanese, Syloti_Nagri, Syriac,  Tagalog,  Tagbanwa,  Tai_Le,
3619         specified  by  including a circumflex between the opening brace and the         Tai_Tham,  Tai_Viet,  Tamil,  Telugu,  Thaana, Thai, Tibetan, Tifinagh,
3620         property name. For example, \p{^Lu} is the same as \P{Lu}.         Ugaritic, Vai, Yi.
3621    
3622           Each character has exactly one Unicode general category property, spec-
3623           ified  by a two-letter abbreviation. For compatibility with Perl, nega-
3624           tion can be specified by including a  circumflex  between  the  opening
3625           brace  and  the  property  name.  For  example,  \p{^Lu} is the same as
3626           \P{Lu}.
3627    
3628         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-
3629         eral  category properties that start with that letter. In this case, in         eral  category properties that start with that letter. In this case, in
# Line 3593  BACKSLASH Line 3715  BACKSLASH
3715         Matching characters by Unicode property is not fast, because  PCRE  has         Matching characters by Unicode property is not fast, because  PCRE  has
3716         to  search  a  structure  that  contains data for over fifteen thousand         to  search  a  structure  that  contains data for over fifteen thousand
3717         characters. That is why the traditional escape sequences such as \d and         characters. That is why the traditional escape sequences such as \d and
3718         \w do not use Unicode properties in PCRE.         \w  do  not  use  Unicode properties in PCRE by default, though you can
3719           make them do so by setting the PCRE_UCP option for pcre_compile() or by
3720           starting the pattern with (*UCP).
3721    
3722       PCRE's additional properties
3723    
3724           As  well  as  the standard Unicode properties described in the previous
3725           section, PCRE supports four more that make it possible to convert  tra-
3726           ditional escape sequences such as \w and \s and POSIX character classes
3727           to use Unicode properties. PCRE uses these non-standard, non-Perl prop-
3728           erties internally when PCRE_UCP is set. They are:
3729    
3730             Xan   Any alphanumeric character
3731             Xps   Any POSIX space character
3732             Xsp   Any Perl space character
3733             Xwd   Any Perl "word" character
3734    
3735           Xan  matches  characters that have either the L (letter) or the N (num-
3736           ber) property. Xps matches the characters tab, linefeed, vertical  tab,
3737           formfeed,  or  carriage  return, and any other character that has the Z
3738           (separator) property.  Xsp is the same as Xps, except that vertical tab
3739           is excluded. Xwd matches the same characters as Xan, plus underscore.
3740    
3741     Resetting the match start     Resetting the match start
3742    
# Line 3614  BACKSLASH Line 3757  BACKSLASH
3757    
3758         matches "foobar", the first substring is still set to "foo".         matches "foobar", the first substring is still set to "foo".
3759    
3760           Perl documents that the use  of  \K  within  assertions  is  "not  well
3761           defined".  In  PCRE,  \K  is  acted upon when it occurs inside positive
3762           assertions, but is ignored in negative assertions.
3763    
3764     Simple assertions     Simple assertions
3765    
3766         The final use of backslash is for certain simple assertions. An  asser-         The final use of backslash is for certain simple assertions. An  asser-
# Line 3630  BACKSLASH Line 3777  BACKSLASH
3777           \z     matches only at the end of the subject           \z     matches only at the end of the subject
3778           \G     matches at the first matching position in the subject           \G     matches at the first matching position in the subject
3779    
3780         These assertions may not appear in character classes (but note that  \b         Inside a character class, \b has a different meaning;  it  matches  the
3781         has a different meaning, namely the backspace character, inside a char-         backspace  character.  If  any  other  of these assertions appears in a
3782         acter class).         character class, by default it matches the corresponding literal  char-
3783           acter  (for  example,  \B  matches  the  letter  B).  However,  if  the
3784         A word boundary is a position in the subject string where  the  current         PCRE_EXTRA option is set, an "invalid escape sequence" error is  gener-
3785         character  and  the previous character do not both match \w or \W (i.e.         ated instead.
3786         one matches \w and the other matches \W), or the start or  end  of  the  
3787         string if the first or last character matches \w, respectively. Neither         A  word  boundary is a position in the subject string where the current
3788         PCRE nor Perl has a separte "start of word" or "end  of  word"  metase-         character and the previous character do not both match \w or  \W  (i.e.
3789         quence.  However,  whatever follows \b normally determines which it is.         one  matches  \w  and the other matches \W), or the start or end of the
3790           string if the first or last  character  matches  \w,  respectively.  In
3791           UTF-8  mode,  the  meanings  of \w and \W can be changed by setting the
3792           PCRE_UCP option. When this is done, it also affects \b and \B.  Neither
3793           PCRE  nor  Perl has a separate "start of word" or "end of word" metase-
3794           quence. However, whatever follows \b normally determines which  it  is.
3795         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.
3796    
3797         The \A, \Z, and \z assertions differ from  the  traditional  circumflex         The  \A,  \Z,  and \z assertions differ from the traditional circumflex
3798         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
3799         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
3800         set.  Thus,  they are independent of multiline mode. These three asser-         set. Thus, they are independent of multiline mode. These  three  asser-
3801         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
3802         affect  only the behaviour of the circumflex and dollar metacharacters.         affect only the behaviour of the circumflex and dollar  metacharacters.
3803         However, if the startoffset argument of pcre_exec() is non-zero,  indi-         However,  if the startoffset argument of pcre_exec() is non-zero, indi-
3804         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
3805         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
3806         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
3807         the very end, whereas \z matches only at the end.         the very end, whereas \z matches only at the end.
3808    
3809         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
3810         the  start point of the match, as specified by the startoffset argument         the start point of the match, as specified by the startoffset  argument
3811         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
3812         non-zero.  By calling pcre_exec() multiple times with appropriate argu-         non-zero. By calling pcre_exec() multiple times with appropriate  argu-
3813         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-
3814         mentation where \G can be useful.         mentation where \G can be useful.
3815    
3816         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
3817         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
3818         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
3819         previously matched string was empty. Because PCRE does just  one  match         previously  matched  string was empty. Because PCRE does just one match
3820         at a time, it cannot reproduce this behaviour.         at a time, it cannot reproduce this behaviour.
3821    
3822         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
3823         anchored to the starting match position, and the "anchored" flag is set         anchored to the starting match position, and the "anchored" flag is set
3824         in the compiled regular expression.         in the compiled regular expression.
3825    
# Line 3675  BACKSLASH Line 3827  BACKSLASH
3827  CIRCUMFLEX AND DOLLAR  CIRCUMFLEX AND DOLLAR
3828    
3829         Outside a character class, in the default matching mode, the circumflex         Outside a character class, in the default matching mode, the circumflex
3830         character is an assertion that is true only  if  the  current  matching         character  is  an  assertion  that is true only if the current matching
3831         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-
3832         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
3833         PCRE_MULTILINE  option  is  unset. Inside a character class, circumflex         PCRE_MULTILINE option is unset. Inside a  character  class,  circumflex
3834         has an entirely different meaning (see below).         has an entirely different meaning (see below).
3835    
3836         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
3837         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
3838         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
3839         branch.  If all possible alternatives start with a circumflex, that is,         branch. If all possible alternatives start with a circumflex, that  is,
3840         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-
3841         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
3842         constructs that can cause a pattern to be anchored.)         constructs that can cause a pattern to be anchored.)
3843    
3844         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
3845         matching  point  is  at  the  end of the subject string, or immediately         matching point is at the end of  the  subject  string,  or  immediately
3846         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
3847         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
3848         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
3849         appears. Dollar has no special meaning in a character class.         appears. Dollar has no special meaning in a character class.
3850    
3851         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
3852         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
3853         compile time. This does not affect the \Z assertion.         compile time. This does not affect the \Z assertion.
3854    
3855         The meanings of the circumflex and dollar characters are changed if the         The meanings of the circumflex and dollar characters are changed if the
3856         PCRE_MULTILINE option is set. When  this  is  the  case,  a  circumflex         PCRE_MULTILINE  option  is  set.  When  this  is the case, a circumflex
3857         matches  immediately after internal newlines as well as at the start of         matches immediately after internal newlines as well as at the start  of
3858         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
3859         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
3860         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
3861         as  the  two-character  sequence CRLF, isolated CR and LF characters do         as the two-character sequence CRLF, isolated CR and  LF  characters  do
3862         not indicate newlines.         not indicate newlines.
3863    
3864         For example, the pattern /^abc$/ matches the subject string  "def\nabc"         For  example, the pattern /^abc$/ matches the subject string "def\nabc"
3865         (where  \n  represents a newline) in multiline mode, but not otherwise.         (where \n represents a newline) in multiline mode, but  not  otherwise.
3866         Consequently, patterns that are anchored in single  line  mode  because         Consequently,  patterns  that  are anchored in single line mode because
3867         all  branches  start  with  ^ are not anchored in multiline mode, and a         all branches start with ^ are not anchored in  multiline  mode,  and  a
3868         match for circumflex is  possible  when  the  startoffset  argument  of         match  for  circumflex  is  possible  when  the startoffset argument of
3869         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
3870         PCRE_MULTILINE is set.         PCRE_MULTILINE is set.
3871    
3872         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
3873         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
3874         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
3875         set.         set.
3876    
3877    
3878  FULL STOP (PERIOD, DOT)  FULL STOP (PERIOD, DOT) AND \N
3879    
3880         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-
3881         ter in the subject string except (by default) a character  that  signi-         ter  in  the subject string except (by default) a character that signi-
3882         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
3883         more than one byte long.         more than one byte long.
3884    
3885         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
3886         that  character; when the two-character sequence CRLF is used, dot does         that character; when the two-character sequence CRLF is used, dot  does
3887         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
3888         matches  all characters (including isolated CRs and LFs). When any Uni-         matches all characters (including isolated CRs and LFs). When any  Uni-
3889         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
3890         any of the other line ending characters.         any of the other line ending characters.
3891    
3892         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
3893         PCRE_DOTALL option is set, a dot matches  any  one  character,  without         PCRE_DOTALL  option  is  set,  a dot matches any one character, without
3894         exception. If the two-character sequence CRLF is present in the subject         exception. If the two-character sequence CRLF is present in the subject
3895         string, it takes two dots to match it.         string, it takes two dots to match it.
3896    
3897         The handling of dot is entirely independent of the handling of  circum-         The  handling of dot is entirely independent of the handling of circum-
3898         flex  and  dollar,  the  only relationship being that they both involve         flex and dollar, the only relationship being  that  they  both  involve
3899         newlines. Dot has no special meaning in a character class.         newlines. Dot has no special meaning in a character class.
3900    
3901           The escape sequence \N always behaves as a dot does when PCRE_DOTALL is
3902           not set. In other words, it matches any one character except  one  that
3903           signifies the end of a line.
3904    
3905    
3906  MATCHING A SINGLE BYTE  MATCHING A SINGLE BYTE
3907    
3908         Outside a character class, the escape sequence \C matches any one byte,         Outside a character class, the escape sequence \C matches any one byte,
3909         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
3910         line-ending characters. The feature is provided in  Perl  in  order  to         line-ending  characters.  The  feature  is provided in Perl in order to
3911         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-
3912         acters into individual bytes, what remains in the string may be a  mal-         acters  into individual bytes, what remains in the string may be a mal-
3913         formed  UTF-8  string.  For this reason, the \C escape sequence is best         formed UTF-8 string. For this reason, the \C escape  sequence  is  best
3914         avoided.         avoided.
3915    
3916         PCRE does not allow \C to appear in  lookbehind  assertions  (described         PCRE  does  not  allow \C to appear in lookbehind assertions (described
3917         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-
3918         late the length of the lookbehind.         late the length of the lookbehind.
3919    
3920    
# Line 3768  SQUARE BRACKETS AND CHARACTER CLASSES Line 3924  SQUARE BRACKETS AND CHARACTER CLASSES
3924         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-
3925         cial by default.  However, if the PCRE_JAVASCRIPT_COMPAT option is set,         cial by default.  However, if the PCRE_JAVASCRIPT_COMPAT option is set,
3926         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
3927         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
3928         first  data  character  in  the  class (after an initial circumflex, if         first data character in the class  (after  an  initial  circumflex,  if
3929         present) or escaped with a backslash.         present) or escaped with a backslash.
3930    
3931         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
3932         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
3933         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
3934         character  in  the  class definition is a circumflex, in which case the         character in the class definition is a circumflex, in  which  case  the
3935         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
3936         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
3937         not the first character, or escape it with a backslash.         not the first character, or escape it with a backslash.
3938    
3939         For example, the character class [aeiou] matches any lower case  vowel,         For  example, the character class [aeiou] matches any lower case vowel,
3940         while  [^aeiou]  matches  any character that is not a lower case vowel.         while [^aeiou] matches any character that is not a  lower  case  vowel.
3941         Note that a circumflex is just a convenient notation for specifying the         Note that a circumflex is just a convenient notation for specifying the
3942         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
3943         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-
3944         sumes  a  character  from the subject string, and therefore it fails if         sumes a character from the subject string, and therefore  it  fails  if
3945         the current pointer is at the end of the string.         the current pointer is at the end of the string.
3946    
3947         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
3948         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
3949         mechanism.         mechanism.
3950    
3951         When caseless matching is set, any letters in a  class  represent  both         When  caseless  matching  is set, any letters in a class represent both
3952         their  upper  case  and lower case versions, so for example, a caseless         their upper case and lower case versions, so for  example,  a  caseless
3953         [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
3954         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
3955         understands the concept of case for characters whose  values  are  less         understands  the  concept  of case for characters whose values are less
3956         than  128, so caseless matching is always possible. For characters with         than 128, so caseless matching is always possible. For characters  with
3957         higher values, the concept of case is supported  if  PCRE  is  compiled         higher  values,  the  concept  of case is supported if PCRE is compiled
3958         with  Unicode  property support, but not otherwise.  If you want to use         with Unicode property support, but not otherwise.  If you want  to  use
3959         caseless matching in UTF8-mode for characters 128 and above,  you  must         caseless  matching  in UTF8-mode for characters 128 and above, you must
3960         ensure  that  PCRE is compiled with Unicode property support as well as         ensure that PCRE is compiled with Unicode property support as  well  as
3961         with UTF-8 support.         with UTF-8 support.
3962    
3963         Characters that might indicate line breaks are  never  treated  in  any         Characters  that  might  indicate  line breaks are never treated in any
3964         special  way  when  matching  character  classes,  whatever line-ending         special way  when  matching  character  classes,  whatever  line-ending
3965         sequence is in  use,  and  whatever  setting  of  the  PCRE_DOTALL  and         sequence  is  in  use,  and  whatever  setting  of  the PCRE_DOTALL and
3966         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
3967         of these characters.         of these characters.
3968    
3969         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-
3970         ters  in  a  character  class.  For  example,  [d-m] matches any letter         ters in a character  class.  For  example,  [d-m]  matches  any  letter
3971         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
3972         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
3973         where it cannot be interpreted as indicating a range, typically as  the         where  it cannot be interpreted as indicating a range, typically as the
3974         first or last character in the class.         first or last character in the class.
3975    
3976         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-
3977         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
3978         two  characters ("W" and "-") followed by a literal string "46]", so it         two characters ("W" and "-") followed by a literal string "46]", so  it
3979         would match "W46]" or "-46]". However, if the "]"  is  escaped  with  a         would  match  "W46]"  or  "-46]". However, if the "]" is escaped with a
3980         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-
3981         preted as a class containing a range followed by two other  characters.         preted  as a class containing a range followed by two other characters.
3982         The  octal or hexadecimal representation of "]" can also be used to end         The octal or hexadecimal representation of "]" can also be used to  end
3983         a range.         a range.
3984    
3985         Ranges operate in the collating sequence of character values. They  can         Ranges  operate in the collating sequence of character values. They can
3986         also   be  used  for  characters  specified  numerically,  for  example         also  be  used  for  characters  specified  numerically,  for   example
3987         [\000-\037]. In UTF-8 mode, ranges can include characters whose  values         [\000-\037].  In UTF-8 mode, ranges can include characters whose values
3988         are greater than 255, for example [\x{100}-\x{2ff}].         are greater than 255, for example [\x{100}-\x{2ff}].
3989    
3990         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,
3991         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
3992         to  [][\\^_`wxyzabc],  matched  caselessly,  and  in non-UTF-8 mode, if         to [][\\^_`wxyzabc], matched caselessly,  and  in  non-UTF-8  mode,  if
3993         character tables for a French locale are in  use,  [\xc8-\xcb]  matches         character  tables  for  a French locale are in use, [\xc8-\xcb] matches
3994         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
3995         concept of case for characters with values greater than 128  only  when         concept  of  case for characters with values greater than 128 only when
3996         it is compiled with Unicode property support.         it is compiled with Unicode property support.
3997    
3998         The  character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear         The character types \d, \D, \h, \H, \p, \P, \s, \S, \v, \V, \w, and  \W
3999         in a character class, and add the characters that  they  match  to  the         may  also appear in a character class, and add the characters that they
4000         class. For example, [\dABCDEF] matches any hexadecimal digit. A circum-         match to the class. For example,  [\dABCDEF]  matches  any  hexadecimal
4001         flex can conveniently be used with the upper case  character  types  to         digit.  A circumflex can conveniently be used with the upper case char-
4002         specify  a  more  restricted  set of characters than the matching lower         acter types to specify a more restricted set  of  characters  than  the
4003         case type. For example, the class [^\W_] matches any letter  or  digit,         matching  lower  case  type.  For example, the class [^\W_] matches any
4004         but not underscore.         letter or digit, but not underscore.
4005    
4006         The  only  metacharacters  that are recognized in character classes are         The only metacharacters that are recognized in  character  classes  are
4007         backslash, hyphen (only where it can be  interpreted  as  specifying  a         backslash,  hyphen  (only  where  it can be interpreted as specifying a
4008         range),  circumflex  (only  at the start), opening square bracket (only         range), circumflex (only at the start), opening  square  bracket  (only
4009         when it can be interpreted as introducing a POSIX class name - see  the         when  it can be interpreted as introducing a POSIX class name - see the
4010         next  section),  and  the  terminating closing square bracket. However,         next section), and the terminating  closing  square  bracket.  However,
4011         escaping other non-alphanumeric characters does no harm.         escaping other non-alphanumeric characters does no harm.
4012    
4013    
4014  POSIX CHARACTER CLASSES  POSIX CHARACTER CLASSES
4015    
4016         Perl supports the POSIX notation for character classes. This uses names         Perl supports the POSIX notation for character classes. This uses names
4017         enclosed  by  [: and :] within the enclosing square brackets. PCRE also         enclosed by [: and :] within the enclosing square brackets.  PCRE  also
4018         supports this notation. For example,         supports this notation. For example,
4019    
4020           [01[:alpha:]%]           [01[:alpha:]%]
4021    
4022         matches "0", "1", any alphabetic character, or "%". The supported class         matches "0", "1", any alphabetic character, or "%". The supported class
4023         names are         names are:
4024    
4025           alnum    letters and digits           alnum    letters and digits
4026           alpha    letters           alpha    letters
# Line 3875  POSIX CHARACTER CLASSES Line 4031  POSIX CHARACTER CLASSES
4031           graph    printing characters, excluding space           graph    printing characters, excluding space
4032           lower    lower case letters           lower    lower case letters
4033           print    printing characters, including space           print    printing characters, including space
4034           punct    printing characters, excluding letters and digits           punct    printing characters, excluding letters and digits and space
4035           space    white space (not quite the same as \s)           space    white space (not quite the same as \s)
4036           upper    upper case letters           upper    upper case letters
4037           word     "word" characters (same as \w)           word     "word" characters (same as \w)
4038           xdigit   hexadecimal digits           xdigit   hexadecimal digits
4039    
4040         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),
4041         and space (32). Notice that this list includes the VT  character  (code         and  space  (32). Notice that this list includes the VT character (code
4042         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
4043         Perl compatibility).         Perl compatibility).
4044    
4045         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
4046         from  Perl  5.8. Another Perl extension is negation, which is indicated         from Perl 5.8. Another Perl extension is negation, which  is  indicated
4047         by a ^ character after the colon. For example,         by a ^ character after the colon. For example,
4048    
4049           [12[:^digit:]]           [12[:^digit:]]
4050    
4051         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
4052         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but
4053         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.
4054    
4055         In UTF-8 mode, characters with values greater than 128 do not match any         By  default,  in UTF-8 mode, characters with values greater than 128 do
4056         of the POSIX character classes.         not match any of the POSIX character classes. However, if the  PCRE_UCP
4057           option  is passed to pcre_compile(), some of the classes are changed so
4058           that Unicode character properties are used. This is achieved by replac-
4059           ing the POSIX classes by other sequences, as follows:
4060    
4061             [:alnum:]  becomes  \p{Xan}
4062             [:alpha:]  becomes  \p{L}
4063             [:blank:]  becomes  \h
4064             [:digit:]  becomes  \p{Nd}
4065             [:lower:]  becomes  \p{Ll}
4066             [:space:]  becomes  \p{Xps}
4067             [:upper:]  becomes  \p{Lu}
4068             [:word:]   becomes  \p{Xwd}
4069    
4070           Negated  versions,  such  as [:^alpha:] use \P instead of \p. The other
4071           POSIX classes are unchanged, and match only characters with code points
4072           less than 128.
4073    
4074    
4075  VERTICAL BAR  VERTICAL BAR
# Line 3968  INTERNAL OPTION SETTING Line 4140  INTERNAL OPTION SETTING
4140         cases the pattern can contain special leading sequences such as (*CRLF)         cases the pattern can contain special leading sequences such as (*CRLF)
4141         to  override  what  the application has set or what has been defaulted.         to  override  what  the application has set or what has been defaulted.
4142         Details are given in the section entitled  "Newline  sequences"  above.         Details are given in the section entitled  "Newline  sequences"  above.
4143         There  is  also  the  (*UTF8)  leading sequence that can be used to set         There  are  also  the  (*UTF8) and (*UCP) leading sequences that can be
4144         UTF-8 mode; this is equivalent to setting the PCRE_UTF8 option.         used to set UTF-8 and Unicode property modes; they  are  equivalent  to
4145           setting the PCRE_UTF8 and the PCRE_UCP options, respectively.
4146    
4147    
4148  SUBPATTERNS  SUBPATTERNS
# Line 3981  SUBPATTERNS Line 4154  SUBPATTERNS
4154    
4155           cat(aract|erpillar|)           cat(aract|erpillar|)
4156    
4157         matches  one  of the words "cat", "cataract", or "caterpillar". Without         matches one of the words "cat", "cataract", or  "caterpillar".  Without
4158         the parentheses, it would match  "cataract",  "erpillar"  or  an  empty         the  parentheses,  it  would  match  "cataract", "erpillar" or an empty
4159         string.         string.
4160    
4161         2.  It  sets  up  the  subpattern as a capturing subpattern. This means         2. It sets up the subpattern as  a  capturing  subpattern.  This  means
4162         that, when the whole pattern  matches,  that  portion  of  the  subject         that,  when  the  whole  pattern  matches,  that portion of the subject
4163         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
4164         ovector argument of pcre_exec(). Opening parentheses are  counted  from         ovector  argument  of pcre_exec(). Opening parentheses are counted from
4165         left  to  right  (starting  from 1) to obtain numbers for the capturing         left to right (starting from 1) to obtain  numbers  for  the  capturing
4166         subpatterns.         subpatterns.
4167    
4168         For example, if the string "the red king" is matched against  the  pat-         For  example,  if the string "the red king" is matched against the pat-
4169         tern         tern
4170    
4171           the ((red|white) (king|queen))           the ((red|white) (king|queen))
# Line 4000  SUBPATTERNS Line 4173  SUBPATTERNS
4173         the captured substrings are "red king", "red", and "king", and are num-         the captured substrings are "red king", "red", and "king", and are num-
4174         bered 1, 2, and 3, respectively.         bered 1, 2, and 3, respectively.
4175    
4176         The fact that plain parentheses fulfil  two  functions  is  not  always         The  fact  that  plain  parentheses  fulfil two functions is not always
4177         helpful.   There are often times when a grouping subpattern is required         helpful.  There are often times when a grouping subpattern is  required
4178         without a capturing requirement. If an opening parenthesis is  followed         without  a capturing requirement. If an opening parenthesis is followed
4179         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-
4180         ing, and is not counted when computing the  number  of  any  subsequent         ing,  and  is  not  counted when computing the number of any subsequent
4181         capturing  subpatterns. For example, if the string "the white queen" is         capturing subpatterns. For example, if the string "the white queen"  is
4182         matched against the pattern         matched against the pattern
4183    
4184           the ((?:red|white) (king|queen))           the ((?:red|white) (king|queen))
# Line 4013  SUBPATTERNS Line 4186  SUBPATTERNS
4186         the captured substrings are "white queen" and "queen", and are numbered         the captured substrings are "white queen" and "queen", and are numbered
4187         1 and 2. The maximum number of capturing subpatterns is 65535.         1 and 2. The maximum number of capturing subpatterns is 65535.
4188    
4189         As  a  convenient shorthand, if any option settings are required at the         As a convenient shorthand, if any option settings are required  at  the
4190         start of a non-capturing subpattern,  the  option  letters  may  appear         start  of  a  non-capturing  subpattern,  the option letters may appear
4191         between the "?" and the ":". Thus the two patterns         between the "?" and the ":". Thus the two patterns
4192    
4193           (?i:saturday|sunday)           (?i:saturday|sunday)
4194           (?:(?i)saturday|sunday)           (?:(?i)saturday|sunday)
4195    
4196         match exactly the same set of strings. Because alternative branches are         match exactly the same set of strings. Because alternative branches are
4197         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
4198         the  subpattern is reached, an option setting in one branch does affect         the subpattern is reached, an option setting in one branch does  affect
4199         subsequent branches, so the above patterns match "SUNDAY"  as  well  as         subsequent  branches,  so  the above patterns match "SUNDAY" as well as
4200         "Saturday".         "Saturday".
4201    
4202    
4203  DUPLICATE SUBPATTERN NUMBERS  DUPLICATE SUBPATTERN NUMBERS
4204    
4205         Perl 5.10 introduced a feature whereby each alternative in a subpattern         Perl 5.10 introduced a feature whereby each alternative in a subpattern
4206         uses the same numbers for its capturing parentheses. Such a  subpattern         uses  the same numbers for its capturing parentheses. Such a subpattern
4207         starts  with (?| and is itself a non-capturing subpattern. For example,         starts with (?| and is itself a non-capturing subpattern. For  example,
4208         consider this pattern:         consider this pattern:
4209    
4210           (?|(Sat)ur|(Sun))day           (?|(Sat)ur|(Sun))day
4211    
4212         Because the two alternatives are inside a (?| group, both sets of  cap-         Because  the two alternatives are inside a (?| group, both sets of cap-
4213         turing  parentheses  are  numbered one. Thus, when the pattern matches,         turing parentheses are numbered one. Thus, when  the  pattern  matches,
4214         you can look at captured substring number  one,  whichever  alternative         you  can  look  at captured substring number one, whichever alternative
4215         matched.  This  construct  is useful when you want to capture part, but         matched. This construct is useful when you want to  capture  part,  but
4216         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-
4217         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
4218         each branch. The numbers of any capturing buffers that follow the  sub-         each  branch. The numbers of any capturing buffers that follow the sub-
4219         pattern  start after the highest number used in any branch. The follow-         pattern start after the highest number used in any branch. The  follow-
4220         ing example is taken from the Perl documentation.  The  numbers  under-         ing  example  is taken from the Perl documentation.  The numbers under-
4221         neath show in which buffer the captured content will be stored.         neath show in which buffer the captured content will be stored.
4222    
4223           # before  ---------------branch-reset----------- after           # before  ---------------branch-reset----------- after
4224           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
4225           # 1            2         2  3        2     3     4           # 1            2         2  3        2     3     4
4226    
4227         A  backreference  to  a  numbered subpattern uses the most recent value         A back reference to a numbered subpattern uses the  most  recent  value
4228         that is set for that number by any subpattern.  The  following  pattern         that  is  set  for that number by any subpattern. The following pattern
4229         matches "abcabc" or "defdef":         matches "abcabc" or "defdef":
4230    
4231           /(?|(abc)|(def))\1/           /(?|(abc)|(def))\1/
4232    
4233         In  contrast, a recursive or "subroutine" call to a numbered subpattern         In contrast, a recursive or "subroutine" call to a numbered  subpattern
4234         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.
4235         The following pattern matches "abcabc" or "defabc":         The following pattern matches "abcabc" or "defabc":
4236    
4237           /(?|(abc)|(def))(?1)/           /(?|(abc)|(def))(?1)/
4238    
4239         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-
4240         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-
4241         ber have matched.         ber have matched.
4242    
4243         An  alternative approach to using this "branch reset" feature is to use         An alternative approach to using this "branch reset" feature is to  use
4244         duplicate named subpatterns, as described in the next section.         duplicate named subpatterns, as described in the next section.
4245    
4246    
4247  NAMED SUBPATTERNS  NAMED SUBPATTERNS
4248    
4249         Identifying capturing parentheses by number is simple, but  it  can  be         Identifying  capturing  parentheses  by number is simple, but it can be
4250         very  hard  to keep track of the numbers in complicated regular expres-         very hard to keep track of the numbers in complicated  regular  expres-
4251         sions. Furthermore, if an  expression  is  modified,  the  numbers  may         sions.  Furthermore,  if  an  expression  is  modified, the numbers may
4252         change.  To help with this difficulty, PCRE supports the naming of sub-         change. To help with this difficulty, PCRE supports the naming of  sub-
4253         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
4254         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
4255         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-
4256         tax.  Perl  allows  identically  numbered subpatterns to have different         tax. Perl allows identically numbered  subpatterns  to  have  different
4257         names, but PCRE does not.         names, but PCRE does not.
4258    
4259         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>...)
4260         or  (?'name'...)  as in Perl, or (?P<name>...) as in Python. References         or (?'name'...) as in Perl, or (?P<name>...) as in  Python.  References
4261         to capturing parentheses from other parts of the pattern, such as back-         to  capturing parentheses from other parts of the pattern, such as back
4262         references,  recursion,  and conditions, can be made by name as well as         references, recursion, and conditions, can be made by name as  well  as
4263         by number.         by number.
4264    
4265         Names consist of up to  32  alphanumeric  characters  and  underscores.         Names  consist  of  up  to  32 alphanumeric characters and underscores.
4266         Named  capturing  parentheses  are  still  allocated numbers as well as         Named capturing parentheses are still  allocated  numbers  as  well  as
4267         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
4268         function calls for extracting the name-to-number translation table from         function calls for extracting the name-to-number translation table from
4269         a compiled pattern. There is also a convenience function for extracting         a compiled pattern. There is also a convenience function for extracting
4270         a captured substring by name.         a captured substring by name.
4271    
4272         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
4273         to relax this constraint by setting the PCRE_DUPNAMES option at compile         to relax this constraint by setting the PCRE_DUPNAMES option at compile
4274         time.  (Duplicate  names are also always permitted for subpatterns with         time. (Duplicate names are also always permitted for  subpatterns  with
4275         the same number, set up as described in the previous  section.)  Dupli-         the  same  number, set up as described in the previous section.) Dupli-
4276         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
4277         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
4278         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
4279         both cases you want to extract the abbreviation. This pattern (ignoring         both cases you want to extract the abbreviation. This pattern (ignoring
4280         the line breaks) does the job:         the line breaks) does the job:
4281    
# Line 4112  NAMED SUBPATTERNS Line 4285  NAMED SUBPATTERNS
4285           (?<DN>Thu)(?:rsday)?|           (?<DN>Thu)(?:rsday)?|
4286           (?<DN>Sat)(?:urday)?           (?<DN>Sat)(?:urday)?
4287    
4288         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
4289         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
4290         reset" subpattern, as described in the previous section.)         reset" subpattern, as described in the previous section.)
4291    
4292         The  convenience  function  for extracting the data by name returns the         The convenience function for extracting the data by  name  returns  the
4293         substring for the first (and in this example, the only)  subpattern  of         substring  for  the first (and in this example, the only) subpattern of
4294         that  name  that  matched.  This saves searching to find which numbered         that name that matched. This saves searching  to  find  which  numbered
4295         subpattern it was.         subpattern it was.
4296    
4297         If you make a backreference to a non-unique named subpattern from else-         If  you  make  a  back  reference to a non-unique named subpattern from
4298         where  in the pattern, the one that corresponds to the first occurrence         elsewhere in the pattern, the one that corresponds to the first  occur-
4299         of the name is used. In the absence of duplicate numbers (see the  pre-         rence of the name is used. In the absence of duplicate numbers (see the
4300         vious  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
4301         named reference in a condition test (see the section  about  conditions         named  reference  in a condition test (see the section about conditions
4302         below),  either  to check whether a subpattern has matched, or to check         below), either to check whether a subpattern has matched, or  to  check
4303         for recursion, all subpatterns with the same name are  tested.  If  the         for  recursion,  all  subpatterns with the same name are tested. If the
4304         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.
4305         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
4306         the interfaces for handling named subpatterns, see the pcreapi documen-         the interfaces for handling named subpatterns, see the pcreapi documen-
4307         tation.         tation.
4308    
4309         Warning: You cannot use different names to distinguish between two sub-         Warning: You cannot use different names to distinguish between two sub-
4310         patterns  with  the same number because PCRE uses only the numbers when         patterns with the same number because PCRE uses only the  numbers  when
4311         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-
4312         ent  names  are given to subpatterns with the same number. However, you         ent names are given to subpatterns with the same number.  However,  you
4313         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
4314         PCRE_DUPNAMES is not set.         PCRE_DUPNAMES is not set.
4315    
4316    
4317  REPETITION  REPETITION
4318    
4319         Repetition  is  specified  by  quantifiers, which can follow any of the         Repetition is specified by quantifiers, which can  follow  any  of  the
4320         following items:         following items:
4321    
4322           a literal data character           a literal data character
# Line 4157  REPETITION Line 4330  REPETITION
4330           a parenthesized subpattern (unless it is an assertion)           a parenthesized subpattern (unless it is an assertion)
4331           a recursive or "subroutine" call to a subpattern           a recursive or "subroutine" call to a subpattern
4332    
4333         The general repetition quantifier specifies a minimum and maximum  num-         The  general repetition quantifier specifies a minimum and maximum num-
4334         ber  of  permitted matches, by giving the two numbers in curly brackets         ber of permitted matches, by giving the two numbers in  curly  brackets
4335         (braces), separated by a comma. The numbers must be  less  than  65536,         (braces),  separated  by  a comma. The numbers must be less than 65536,
4336         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:
4337    
4338           z{2,4}           z{2,4}
4339    
4340         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
4341         special character. If the second number is omitted, but  the  comma  is         special  character.  If  the second number is omitted, but the comma is
4342         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
4343         are both omitted, the quantifier specifies an exact number of  required         are  both omitted, the quantifier specifies an exact number of required
4344         matches. Thus         matches. Thus
4345    
4346           [aeiou]{3,}           [aeiou]{3,}
# Line 4176  REPETITION Line 4349  REPETITION
4349    
4350           \d{8}           \d{8}
4351    
4352         matches  exactly  8  digits. An opening curly bracket that appears in a         matches exactly 8 digits. An opening curly bracket that  appears  in  a
4353         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
4354         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-
4355         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.
4356    
4357         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
4358         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-
4359         acters, each of which is represented by a two-byte sequence. Similarly,         acters, each of which is represented by a two-byte sequence. Similarly,
4360         when Unicode property support is available, \X{3} matches three Unicode         when Unicode property support is available, \X{3} matches three Unicode
4361         extended sequences, each of which may be several bytes long  (and  they         extended  sequences,  each of which may be several bytes long (and they
4362         may be of different lengths).         may be of different lengths).
4363    
4364         The quantifier {0} is permitted, causing the expression to behave as if         The quantifier {0} is permitted, causing the expression to behave as if
4365         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-
4366         ful  for  subpatterns that are referenced as subroutines from elsewhere         ful for subpatterns that are referenced as subroutines  from  elsewhere
4367         in the pattern. Items other than subpatterns that have a {0} quantifier         in the pattern. Items other than subpatterns that have a {0} quantifier
4368         are omitted from the compiled pattern.         are omitted from the compiled pattern.
4369    
4370         For  convenience, the three most common quantifiers have single-charac-         For convenience, the three most common quantifiers have  single-charac-
4371         ter abbreviations:         ter abbreviations:
4372    
4373           *    is equivalent to {0,}           *    is equivalent to {0,}
4374           +    is equivalent to {1,}           +    is equivalent to {1,}
4375           ?    is equivalent to {0,1}           ?    is equivalent to {0,1}
4376    
4377         It is possible to construct infinite loops by  following  a  subpattern         It  is  possible  to construct infinite loops by following a subpattern
4378         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,
4379         for example:         for example:
4380    
4381           (a?)*           (a?)*
4382    
4383         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
4384         for  such  patterns. However, because there are cases where this can be         for such patterns. However, because there are cases where this  can  be
4385         useful, such patterns are now accepted, but if any  repetition  of  the         useful,  such  patterns  are now accepted, but if any repetition of the
4386         subpattern  does in fact match no characters, the loop is forcibly bro-         subpattern does in fact match no characters, the loop is forcibly  bro-
4387         ken.         ken.
4388    
4389         By default, the quantifiers are "greedy", that is, they match  as  much         By  default,  the quantifiers are "greedy", that is, they match as much
4390         as  possible  (up  to  the  maximum number of permitted times), without         as possible (up to the maximum  number  of  permitted  times),  without
4391         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
4392         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
4393         appear between /* and */ and within the comment,  individual  *  and  /         appear  between  /*  and  */ and within the comment, individual * and /
4394         characters  may  appear. An attempt to match C comments by applying the         characters may appear. An attempt to match C comments by  applying  the
4395         pattern         pattern
4396    
4397           /\*.*\*/           /\*.*\*/
# Line 4227  REPETITION Line 4400  REPETITION
4400    
4401           /* first comment */  not comment  /* second comment */           /* first comment */  not comment  /* second comment */
4402    
4403         fails, because it matches the entire string owing to the greediness  of         fails,  because it matches the entire string owing to the greediness of
4404         the .*  item.         the .*  item.
4405    
4406         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
4407         be greedy, and instead matches the minimum number of times possible, so         be greedy, and instead matches the minimum number of times possible, so
4408         the pattern         the pattern
4409    
4410           /\*.*?\*/           /\*.*?\*/
4411    
4412         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
4413         quantifiers is not otherwise changed,  just  the  preferred  number  of         quantifiers  is  not  otherwise  changed,  just the preferred number of
4414         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
4415         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
4416         appear doubled, as in         appear doubled, as in
4417    
4418           \d??\d           \d??\d
# Line 4247  REPETITION Line 4420  REPETITION
4420         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
4421         only way the rest of the pattern matches.         only way the rest of the pattern matches.
4422    
4423         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
4424         Perl),  the  quantifiers are not greedy by default, but individual ones         Perl), the quantifiers are not greedy by default, but  individual  ones
4425         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
4426         words, it inverts the default behaviour.         words, it inverts the default behaviour.
4427    
4428         When  a  parenthesized  subpattern  is quantified with a minimum repeat         When a parenthesized subpattern is quantified  with  a  minimum  repeat
4429         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
4430         required  for  the  compiled  pattern, in proportion to the size of the         required for the compiled pattern, in proportion to  the  size  of  the
4431         minimum or maximum.         minimum or maximum.
4432    
4433         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-
4434         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,
4435         the pattern is implicitly anchored, because whatever  follows  will  be         the  pattern  is  implicitly anchored, because whatever follows will be
4436         tried  against every character position in the subject string, so there         tried against every character position in the subject string, so  there
4437         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
4438         first.  PCRE  normally treats such a pattern as though it were preceded         first. PCRE normally treats such a pattern as though it  were  preceded
4439         by \A.         by \A.
4440    
4441         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-
4442         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-
4443         mization, or alternatively using ^ to indicate anchoring explicitly.         mization, or alternatively using ^ to indicate anchoring explicitly.
4444    
4445         However, there is one situation where the optimization cannot be  used.         However,  there is one situation where the optimization cannot be used.
4446         When  .*   is  inside  capturing  parentheses that are the subject of a         When .*  is inside capturing parentheses that are the subject of a back
4447         backreference elsewhere in the pattern, a match at the start  may  fail         reference elsewhere in the pattern, a match at the start may fail where
4448         where a later one succeeds. Consider, for example:         a later one succeeds. Consider, for example:
4449    
4450           (.*)abc\1           (.*)abc\1
4451    
4452         If  the subject is "xyz123abc123" the match point is the fourth charac-         If the subject is "xyz123abc123" the match point is the fourth  charac-
4453         ter. For this reason, such a pattern is not implicitly anchored.         ter. For this reason, such a pattern is not implicitly anchored.
4454    
4455         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 4285  REPETITION Line 4458  REPETITION
4458           (tweedle[dume]{3}\s*)+           (tweedle[dume]{3}\s*)+
4459    
4460         has matched "tweedledum tweedledee" the value of the captured substring         has matched "tweedledum tweedledee" the value of the captured substring
4461         is "tweedledee". However, if there are  nested  capturing  subpatterns,         is  "tweedledee".  However,  if there are nested capturing subpatterns,
4462         the  corresponding captured values may have been set in previous itera-         the corresponding captured values may have been set in previous  itera-
4463         tions. For example, after         tions. For example, after
4464    
4465           /(a|(b))+/           /(a|(b))+/
# Line 4296  REPETITION Line 4469  REPETITION
4469    
4470  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
4471    
4472         With both maximizing ("greedy") and minimizing ("ungreedy"  or  "lazy")         With  both  maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
4473         repetition,  failure  of what follows normally causes the repeated item         repetition, failure of what follows normally causes the  repeated  item
4474         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
4475         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,
4476         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
4477         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
4478         no point in carrying on.         no point in carrying on.
4479    
4480         Consider, for example, the pattern \d+foo when applied to  the  subject         Consider,  for  example, the pattern \d+foo when applied to the subject
4481         line         line
4482    
4483           123456bar           123456bar
4484    
4485         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
4486         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
4487         \d+  item,  and  then  with  4,  and  so on, before ultimately failing.         \d+ item, and then with  4,  and  so  on,  before  ultimately  failing.
4488         "Atomic grouping" (a term taken from Jeffrey  Friedl's  book)  provides         "Atomic  grouping"  (a  term taken from Jeffrey Friedl's book) provides
4489         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
4490         to be re-evaluated in this way.         to be re-evaluated in this way.
4491    
4492         If we use atomic grouping for the previous example, the  matcher  gives         If  we  use atomic grouping for the previous example, the matcher gives
4493         up  immediately  on failing to match "foo" the first time. The notation         up immediately on failing to match "foo" the first time.  The  notation
4494         is a kind of special parenthesis, starting with (?> as in this example:         is a kind of special parenthesis, starting with (?> as in this example:
4495    
4496           (?>\d+)foo           (?>\d+)foo
4497    
4498         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-
4499         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
4500         prevented from backtracking into it. Backtracking past it  to  previous         prevented  from  backtracking into it. Backtracking past it to previous
4501         items, however, works as normal.         items, however, works as normal.
4502    
4503         An  alternative  description  is that a subpattern of this type matches         An alternative description is that a subpattern of  this  type  matches
4504         the string of characters that an  identical  standalone  pattern  would         the  string  of  characters  that an identical standalone pattern would
4505         match, if anchored at the current point in the subject string.         match, if anchored at the current point in the subject string.
4506    
4507         Atomic grouping subpatterns are not capturing subpatterns. Simple cases         Atomic grouping subpatterns are not capturing subpatterns. Simple cases
4508         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
4509         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-
4510         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
4511         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
4512         digits.         digits.
4513    
4514         Atomic groups in general can of course contain arbitrarily  complicated         Atomic  groups in general can of course contain arbitrarily complicated
4515         subpatterns,  and  can  be  nested. However, when the subpattern for an         subpatterns, and can be nested. However, when  the  subpattern  for  an
4516         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
4517         simpler  notation,  called  a "possessive quantifier" can be used. This         simpler notation, called a "possessive quantifier" can  be  used.  This
4518         consists of an additional + character  following  a  quantifier.  Using         consists  of  an  additional  + character following a quantifier. Using
4519         this notation, the previous example can be rewritten as         this notation, the previous example can be rewritten as
4520    
4521           \d++foo           \d++foo
# Line 4352  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 4525  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
4525    
4526           (abc|xyz){2,3}+           (abc|xyz){2,3}+
4527    
4528         Possessive  quantifiers  are  always  greedy;  the   setting   of   the         Possessive   quantifiers   are   always  greedy;  the  setting  of  the
4529         PCRE_UNGREEDY option is ignored. They are a convenient notation for the         PCRE_UNGREEDY option is ignored. They are a convenient notation for the
4530         simpler forms of atomic group. However, there is no difference  in  the         simpler  forms  of atomic group. However, there is no difference in the
4531         meaning  of  a  possessive  quantifier and the equivalent atomic group,         meaning of a possessive quantifier and  the  equivalent  atomic  group,
4532         though there may be a performance  difference;  possessive  quantifiers         though  there  may  be a performance difference; possessive quantifiers
4533         should be slightly faster.         should be slightly faster.
4534    
4535         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-
4536         tax.  Jeffrey Friedl originated the idea (and the name)  in  the  first         tax.   Jeffrey  Friedl  originated the idea (and the name) in the first
4537         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
4538         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
4539         found its way into Perl at release 5.10.         found its way into Perl at release 5.10.
4540    
4541         PCRE has an optimization that automatically "possessifies" certain sim-         PCRE has an optimization that automatically "possessifies" certain sim-
4542         ple pattern constructs. For example, the sequence  A+B  is  treated  as         ple  pattern  constructs.  For  example, the sequence A+B is treated as
4543         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
4544         when B must follow.         when B must follow.
4545    
4546         When a pattern contains an unlimited repeat inside  a  subpattern  that         When  a  pattern  contains an unlimited repeat inside a subpattern that
4547         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
4548         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
4549         very long time indeed. The pattern         very long time indeed. The pattern
4550    
4551           (\D+|<\d+>)*[!?]           (\D+|<\d+>)*[!?]
4552    
4553         matches  an  unlimited number of substrings that either consist of non-         matches an unlimited number of substrings that either consist  of  non-
4554         digits, or digits enclosed in <>, followed by either ! or  ?.  When  it         digits,  or  digits  enclosed in <>, followed by either ! or ?. When it
4555         matches, it runs quickly. However, if it is applied to         matches, it runs quickly. However, if it is applied to
4556    
4557           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4558    
4559         it  takes  a  long  time  before reporting failure. This is because the         it takes a long time before reporting  failure.  This  is  because  the
4560         string can be divided between the internal \D+ repeat and the  external         string  can be divided between the internal \D+ repeat and the external
4561         *  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
4562         example uses [!?] rather than a single character at  the  end,  because         example  uses  [!?]  rather than a single character at the end, because
4563         both  PCRE  and  Perl have an optimization that allows for fast failure         both PCRE and Perl have an optimization that allows  for  fast  failure
4564         when a single character is used. They remember the last single  charac-         when  a single character is used. They remember the last single charac-
4565         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
4566         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
4567         group, like this:         group, like this:
4568    
4569           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
# Line 4402  BACK REFERENCES Line 4575  BACK REFERENCES
4575    
4576         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
4577         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-
4578         pattern  earlier  (that is, to its left) in the pattern, provided there         pattern earlier (that is, to its left) in the pattern,  provided  there
4579         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
4580    
4581         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
4582         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
4583         there are not that many capturing left parentheses in the  entire  pat-         there  are  not that many capturing left parentheses in the entire pat-
4584         tern.  In  other words, the parentheses that are referenced need not be         tern. In other words, the parentheses that are referenced need  not  be
4585         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
4586         reference"  of  this  type can make sense when a repetition is involved         reference" of this type can make sense when a  repetition  is  involved
4587         and the subpattern to the right has participated in an  earlier  itera-         and  the  subpattern to the right has participated in an earlier itera-
4588         tion.         tion.
4589    
4590         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
4591         subpattern whose number is 10 or  more  using  this  syntax  because  a         subpattern  whose  number  is  10  or  more using this syntax because a
4592         sequence  such  as  \50 is interpreted as a character defined in octal.         sequence such as \50 is interpreted as a character  defined  in  octal.
4593         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
4594         details  of  the  handling of digits following a backslash. There is no         details of the handling of digits following a backslash.  There  is  no
4595         such problem when named parentheses are used. A back reference  to  any         such  problem  when named parentheses are used. A back reference to any
4596         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
4597    
4598         Another  way  of  avoiding  the ambiguity inherent in the use of digits         Another way of avoiding the ambiguity inherent in  the  use  of  digits
4599         following a backslash is to use the \g escape sequence, which is a fea-         following a backslash is to use the \g escape sequence, which is a fea-
4600         ture  introduced  in  Perl  5.10.  This  escape  must be followed by an         ture introduced in Perl 5.10.  This  escape  must  be  followed  by  an
4601         unsigned number or a negative number, optionally  enclosed  in  braces.         unsigned  number  or  a negative number, optionally enclosed in braces.
4602         These examples are all identical:         These examples are all identical:
4603    
4604           (ring), \1           (ring), \1
4605           (ring), \g1           (ring), \g1
4606           (ring), \g{1}           (ring), \g{1}
4607    
4608         An  unsigned number specifies an absolute reference without the ambigu-         An unsigned number specifies an absolute reference without the  ambigu-
4609         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
4610         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
4611         Consider this example:         Consider this example:
# Line 4440  BACK REFERENCES Line 4613  BACK REFERENCES
4613           (abc(def)ghi)\g{-1}           (abc(def)ghi)\g{-1}
4614    
4615         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-
4616         ing  subpattern  before \g, that is, is it equivalent to \2. Similarly,         ing subpattern before \g, that is, is it equivalent to  \2.  Similarly,
4617         \g{-2} would be equivalent to \1. The use of relative references can be         \g{-2} would be equivalent to \1. The use of relative references can be
4618         helpful  in  long  patterns,  and  also in patterns that are created by         helpful in long patterns, and also in  patterns  that  are  created  by
4619         joining together fragments that contain references within themselves.         joining together fragments that contain references within themselves.
4620    
4621         A back reference matches whatever actually matched the  capturing  sub-         A  back  reference matches whatever actually matched the capturing sub-
4622         pattern  in  the  current subject string, rather than anything matching         pattern in the current subject string, rather  than  anything  matching
4623         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
4624         of doing that). So the pattern         of doing that). So the pattern
4625    
4626           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4627    
4628         matches  "sense and sensibility" and "response and responsibility", but         matches "sense and sensibility" and "response and responsibility",  but
4629         not "sense and responsibility". If caseful matching is in force at  the         not  "sense and responsibility". If caseful matching is in force at the
4630         time  of the back reference, the case of letters is relevant. For exam-         time of the back reference, the case of letters is relevant. For  exam-
4631         ple,         ple,
4632    
4633           ((?i)rah)\s+\1           ((?i)rah)\s+\1
4634    
4635         matches "rah rah" and "RAH RAH", but not "RAH  rah",  even  though  the         matches  "rah  rah"  and  "RAH RAH", but not "RAH rah", even though the
4636         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
4637    
4638         There  are  several  different ways of writing back references to named         There are several different ways of writing back  references  to  named
4639         subpatterns. The .NET syntax \k{name} and the Perl syntax  \k<name>  or         subpatterns.  The  .NET syntax \k{name} and the Perl syntax \k<name> or
4640         \k'name'  are supported, as is the Python syntax (?P=name). Perl 5.10's         \k'name' are supported, as is the Python syntax (?P=name). Perl  5.10's
4641         unified back reference syntax, in which \g can be used for both numeric         unified back reference syntax, in which \g can be used for both numeric
4642         and  named  references,  is  also supported. We could rewrite the above         and named references, is also supported. We  could  rewrite  the  above
4643         example in any of the following ways:         example in any of the following ways:
4644    
4645           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 4474  BACK REFERENCES Line 4647  BACK REFERENCES
4647           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4648           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
4649    
4650         A subpattern that is referenced by  name  may  appear  in  the  pattern         A  subpattern  that  is  referenced  by  name may appear in the pattern
4651         before or after the reference.         before or after the reference.
4652    
4653         There  may be more than one back reference to the same subpattern. If a         There may be more than one back reference to the same subpattern. If  a
4654         subpattern has not actually been used in a particular match,  any  back         subpattern  has  not actually been used in a particular match, any back
4655         references to it always fail by default. For example, the pattern         references to it always fail by default. For example, the pattern
4656    
4657           (a|(bc))\2           (a|(bc))\2
4658    
4659         always  fails  if  it starts to match "a" rather than "bc". However, if         always fails if it starts to match "a" rather than  "bc".  However,  if
4660         the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-         the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-
4661         ence to an unset value matches an empty string.         ence to an unset value matches an empty string.
4662    
4663         Because  there may be many capturing parentheses in a pattern, all dig-         Because there may be many capturing parentheses in a pattern, all  dig-
4664         its following a backslash are taken as part of a potential back  refer-         its  following a backslash are taken as part of a potential back refer-
4665         ence  number.   If  the  pattern continues with a digit character, some         ence number.  If the pattern continues with  a  digit  character,  some
4666         delimiter must  be  used  to  terminate  the  back  reference.  If  the         delimiter  must  be  used  to  terminate  the  back  reference.  If the
4667         PCRE_EXTENDED option is set, this can be whitespace. Otherwise, the \g{         PCRE_EXTENDED option is set, this can be whitespace. Otherwise, the \g{
4668         syntax or an empty comment (see "Comments" below) can be used.         syntax or an empty comment (see "Comments" below) can be used.
4669    
4670         A back reference that occurs inside the parentheses to which it  refers     Recursive back references
4671         fails  when  the subpattern is first used, so, for example, (a\1) never  
4672         matches.  However, such references can be useful inside  repeated  sub-         A  back reference that occurs inside the parentheses to which it refers
4673           fails when the subpattern is first used, so, for example,  (a\1)  never
4674           matches.   However,  such references can be useful inside repeated sub-
4675         patterns. For example, the pattern         patterns. For example, the pattern
4676    
4677           (a|b\1)+           (a|b\1)+
4678    
4679         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
4680         ation of the subpattern,  the  back  reference  matches  the  character         ation  of  the  subpattern,  the  back  reference matches the character
4681         string  corresponding  to  the previous iteration. In order for this to         string corresponding to the previous iteration. In order  for  this  to
4682         work, the pattern must be such that the first iteration does  not  need         work,  the  pattern must be such that the first iteration does not need
4683         to  match the back reference. This can be done using alternation, as in         to match the back reference. This can be done using alternation, as  in
4684         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
4685    
4686           Back  references of this type cause the group that they reference to be
4687           treated as an atomic group.  Once the whole group has been  matched,  a
4688           subsequent  matching  failure cannot cause backtracking into the middle
4689           of the group.
4690    
4691    
4692  ASSERTIONS  ASSERTIONS
4693    
# Line 4904  RECURSIVE PATTERNS Line 5084  RECURSIVE PATTERNS
5084         so many different ways the + and * repeats can carve  up  the  subject,         so many different ways the + and * repeats can carve  up  the  subject,
5085         and all have to be tested before failure can be reported.         and all have to be tested before failure can be reported.
5086    
5087         At the end of a match, the values set for any capturing subpatterns are         At  the  end  of a match, the values of capturing parentheses are those
5088         those from the outermost level of the recursion at which the subpattern         from the outermost level. If you want to obtain intermediate values,  a
5089         value  is  set.   If  you want to obtain intermediate values, a callout         callout  function can be used (see below and the pcrecallout documenta-
5090         function can be used (see below and the pcrecallout documentation).  If         tion). If the pattern above is matched against
        the pattern above is matched against  
5091    
5092           (ab(cd)ef)           (ab(cd)ef)
5093    
5094         the  value  for  the  capturing  parentheses is "ef", which is the last         the value for the inner capturing parentheses  (numbered  2)  is  "ef",
5095         value taken on at the top level. If additional parentheses  are  added,         which  is the last value taken on at the top level. If a capturing sub-
5096         giving         pattern is not matched at the top level, its final value is unset, even
5097           if it is (temporarily) set at a deeper level.
5098           \( ( ( [^()]++ | (?R) )* ) \)  
5099              ^                        ^         If  there are more than 15 capturing parentheses in a pattern, PCRE has
5100              ^                        ^         to obtain extra memory to store data during a recursion, which it  does
5101           by using pcre_malloc, freeing it via pcre_free afterwards. If no memory
5102         the  string  they  capture is "ab(cd)ef", the contents of the top level         can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
        parentheses. If there are more than 15 capturing parentheses in a  pat-  
        tern, PCRE has to obtain extra memory to store data during a recursion,  
        which it does by using pcre_malloc, freeing  it  via  pcre_free  after-  
        wards.  If  no  memory  can  be  obtained,  the  match  fails  with the  
        PCRE_ERROR_NOMEMORY error.  
5103    
5104         Do not confuse the (?R) item with the condition (R),  which  tests  for         Do not confuse the (?R) item with the condition (R),  which  tests  for
5105         recursion.   Consider  this pattern, which matches text in angle brack-         recursion.   Consider  this pattern, which matches text in angle brack-
# Line 5039  SUBPATTERNS AS SUBROUTINES Line 5213  SUBPATTERNS AS SUBROUTINES
5213         two strings. Another example is  given  in  the  discussion  of  DEFINE         two strings. Another example is  given  in  the  discussion  of  DEFINE
5214         above.         above.
5215    
5216         Like recursive subpatterns, a "subroutine" call is always treated as an         Like  recursive  subpatterns, a subroutine call is always treated as an
5217         atomic group. That is, once it has matched some of the subject  string,         atomic group. That is, once it has matched some of the subject  string,
5218         it  is  never  re-entered, even if it contains untried alternatives and         it  is  never  re-entered, even if it contains untried alternatives and
5219         there is a subsequent matching failure.         there is a subsequent matching failure. Any capturing parentheses  that
5220           are  set  during  the  subroutine  call revert to their previous values
5221           afterwards.
5222    
5223         When a subpattern is used as a subroutine, processing options  such  as         When a subpattern is used as a subroutine, processing options  such  as
5224         case-independence are fixed when the subpattern is defined. They cannot         case-independence are fixed when the subpattern is defined. They cannot
# Line 5125  BACKTRACKING CONTROL Line 5301  BACKTRACKING CONTROL
5301         (*FAIL), which behaves like a failing negative assertion, they cause an         (*FAIL), which behaves like a failing negative assertion, they cause an
5302         error if encountered by pcre_dfa_exec().         error if encountered by pcre_dfa_exec().
5303    
5304         If any of these verbs are used in an assertion subpattern, their effect         If any of these verbs are used in an assertion or subroutine subpattern
5305         is confined to that subpattern; it does not extend to  the  surrounding         (including recursive subpatterns), their effect  is  confined  to  that
5306         pattern.   Note that assertion subpatterns are processed as anchored at         subpattern;  it  does  not extend to the surrounding pattern. Note that
5307         the point where they are tested.         such subpatterns are processed as anchored at the point where they  are
5308           tested.
5309         The new verbs make use of what was previously invalid syntax: an  open-  
5310         ing parenthesis followed by an asterisk. In Perl, they are generally of         The  new verbs make use of what was previously invalid syntax: an open-
5311         the form (*VERB:ARG) but PCRE does not support the use of arguments, so         ing parenthesis followed by an asterisk. They are generally of the form
5312         its  general  form is just (*VERB). Any number of these verbs may occur         (*VERB)  or (*VERB:NAME). Some may take either form, with differing be-
5313         in a pattern. There are two kinds:         haviour, depending on whether or not an argument is present. An name is
5314           a  sequence  of letters, digits, and underscores. If the name is empty,
5315           that is, if the closing parenthesis immediately follows the colon,  the
5316           effect is as if the colon were not there. Any number of these verbs may
5317           occur in a pattern.
5318    
5319           PCRE contains some optimizations that are used to speed up matching  by
5320           running some checks at the start of each match attempt. For example, it
5321           may know the minimum length of matching subject, or that  a  particular
5322           character  must  be present. When one of these optimizations suppresses
5323           the running of a match, any included backtracking verbs  will  not,  of
5324           course, be processed. You can suppress the start-of-match optimizations
5325           by setting the PCRE_NO_START_OPTIMIZE option when calling pcre_exec().
5326    
5327     Verbs that act immediately     Verbs that act immediately
5328    
5329         The following verbs act as soon as they are encountered:         The following verbs act as soon as they are encountered. They  may  not
5330           be followed by a name.
5331    
5332            (*ACCEPT)            (*ACCEPT)
5333    
5334         This verb causes the match to end successfully, skipping the  remainder         This  verb causes the match to end successfully, skipping the remainder
5335         of  the pattern. When inside a recursion, only the innermost pattern is         of the pattern. When inside a recursion, only the innermost pattern  is
5336         ended immediately. If (*ACCEPT) is inside  capturing  parentheses,  the         ended  immediately.  If  (*ACCEPT) is inside capturing parentheses, the
5337         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
5338         8.00.) For example:         8.00.) For example:
5339    
5340           A((?:A|B(*ACCEPT)|C)D)           A((?:A|B(*ACCEPT)|C)D)
5341    
5342         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-
5343         tured by the outer parentheses.         tured by the outer parentheses.
5344    
5345           (*FAIL) or (*F)           (*FAIL) or (*F)
5346    
5347         This  verb  causes the match to fail, forcing backtracking to occur. It         This verb causes the match to fail, forcing backtracking to  occur.  It
5348         is equivalent to (?!) but easier to read. The Perl documentation  notes         is  equivalent to (?!) but easier to read. The Perl documentation notes
5349         that  it  is  probably  useful only when combined with (?{}) or (??{}).         that it is probably useful only when combined  with  (?{})  or  (??{}).
5350         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
5351         nearest  equivalent is the callout feature, as for example in this pat-         nearest equivalent is the callout feature, as for example in this  pat-
5352         tern:         tern:
5353    
5354           a+(?C)(*FAIL)           a+(?C)(*FAIL)
5355    
5356         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
5357         before each backtrack happens (in this example, 10 times).         before each backtrack happens (in this example, 10 times).
5358    
5359       Recording which path was taken
5360    
5361           There is one verb whose main purpose  is  to  track  how  a  match  was
5362           arrived  at,  though  it  also  has a secondary use in conjunction with
5363           advancing the match starting point (see (*SKIP) below).
5364    
5365             (*MARK:NAME) or (*:NAME)
5366    
5367           A name is always  required  with  this  verb.  There  may  be  as  many
5368           instances  of  (*MARK) as you like in a pattern, and their names do not
5369           have to be unique.
5370    
5371           When a match succeeds, the name  of  the  last-encountered  (*MARK)  is
5372           passed  back  to  the  caller  via  the  pcre_extra  data structure, as
5373           described in the section on pcre_extra in the pcreapi documentation. No
5374           data  is  returned  for a partial match. Here is an example of pcretest
5375           output, where the /K modifier requests the retrieval and outputting  of
5376           (*MARK) data:
5377    
5378             /X(*MARK:A)Y|X(*MARK:B)Z/K
5379             XY
5380              0: XY
5381             MK: A
5382             XZ
5383              0: XZ
5384             MK: B
5385    
5386           The (*MARK) name is tagged with "MK:" in this output, and in this exam-
5387           ple it indicates which of the two alternatives matched. This is a  more
5388           efficient  way of obtaining this information than putting each alterna-
5389           tive in its own capturing parentheses.
5390    
5391           A name may also be returned after a failed  match  if  the  final  path
5392           through  the  pattern involves (*MARK). However, unless (*MARK) used in
5393           conjunction with (*COMMIT), this is unlikely to  happen  for  an  unan-
5394           chored pattern because, as the starting point for matching is advanced,
5395           the final check is often with an empty string, causing a failure before
5396           (*MARK) is reached. For example:
5397    
5398             /X(*MARK:A)Y|X(*MARK:B)Z/K
5399             XP
5400             No match
5401    
5402           There are three potential starting points for this match (starting with
5403           X, starting with P, and with  an  empty  string).  If  the  pattern  is
5404           anchored, the result is different:
5405    
5406             /^X(*MARK:A)Y|^X(*MARK:B)Z/K
5407             XP
5408             No match, mark = B
5409    
5410           PCRE's  start-of-match  optimizations can also interfere with this. For
5411           example, if, as a result of a call to pcre_study(), it knows the  mini-
5412           mum  subject  length for a match, a shorter subject will not be scanned
5413           at all.
5414    
5415           Note that similar anomalies (though different in detail) exist in Perl,
5416           no  doubt  for the same reasons. The use of (*MARK) data after a failed
5417           match of an unanchored pattern is not recommended, unless (*COMMIT)  is
5418           involved.
5419    
5420     Verbs that act after backtracking     Verbs that act after backtracking
5421    
5422         The following verbs do nothing when they are encountered. Matching con-         The following verbs do nothing when they are encountered. Matching con-
5423         tinues with what follows, but if there is no subsequent match, a  fail-         tinues with what follows, but if there is no subsequent match,  causing
5424         ure  is  forced.   The  verbs  differ  in  exactly what kind of failure         a  backtrack  to  the  verb, a failure is forced. That is, backtracking
5425         occurs.         cannot pass to the left of the verb. However, when one of  these  verbs
5426           appears  inside  an atomic group, its effect is confined to that group,
5427           because once the group has been matched, there is never any  backtrack-
5428           ing  into  it.  In  this situation, backtracking can "jump back" to the
5429           left of the entire atomic group. (Remember also, as stated above,  that
5430           this localization also applies in subroutine calls and assertions.)
5431    
5432           These  verbs  differ  in exactly what kind of failure occurs when back-
5433           tracking reaches them.
5434    
5435           (*COMMIT)           (*COMMIT)
5436    
5437         This verb causes the whole match to fail outright if the  rest  of  the         This verb, which may not be followed by a name, causes the whole  match
5438         pattern  does  not match. Even if the pattern is unanchored, no further         to fail outright if the rest of the pattern does not match. Even if the
5439         attempts to find a match by advancing the starting  point  take  place.         pattern is unanchored, no further attempts to find a match by advancing
5440         Once  (*COMMIT)  has been passed, pcre_exec() is committed to finding a         the  starting  point  take  place.  Once  (*COMMIT)  has  been  passed,
5441         match at the current starting point, or not at all. For example:         pcre_exec() is committed to finding a match  at  the  current  starting
5442           point, or not at all. For example:
5443    
5444           a+(*COMMIT)b           a+(*COMMIT)b
5445    
5446         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
5447         of dynamic anchor, or "I've started, so I must finish."         of dynamic anchor, or "I've started, so I must finish." The name of the
5448           most  recently passed (*MARK) in the path is passed back when (*COMMIT)
5449           (*PRUNE)         forces a match failure.
5450    
5451           Note that (*COMMIT) at the start of a pattern is not  the  same  as  an
5452           anchor,  unless  PCRE's start-of-match optimizations are turned off, as
5453           shown in this pcretest example:
5454    
5455             /(*COMMIT)abc/
5456             xyzabc
5457              0: abc
5458             xyzabc\Y
5459             No match
5460    
5461         This  verb causes the match to fail at the current position if the rest         PCRE knows that any match must start  with  "a",  so  the  optimization
5462         of the pattern does not match. If the pattern is unanchored, the normal         skips  along the subject to "a" before running the first match attempt,
5463         "bumpalong"  advance to the next starting character then happens. Back-         which succeeds. When the optimization is disabled by the \Y  escape  in
5464         tracking can occur as usual to the left of (*PRUNE), or  when  matching         the second subject, the match starts at "x" and so the (*COMMIT) causes
5465         to  the right of (*PRUNE), but if there is no match to the right, back-         it to fail without trying any other starting points.
5466         tracking cannot cross (*PRUNE).  In simple cases, the use  of  (*PRUNE)  
5467         is just an alternative to an atomic group or possessive quantifier, but           (*PRUNE) or (*PRUNE:NAME)
5468         there are some uses of (*PRUNE) that cannot be expressed in  any  other  
5469         way.         This verb causes the match to fail at the current starting position  in
5470           the  subject  if the rest of the pattern does not match. If the pattern
5471           is unanchored, the normal "bumpalong"  advance  to  the  next  starting
5472           character  then happens. Backtracking can occur as usual to the left of
5473           (*PRUNE), before it is reached,  or  when  matching  to  the  right  of
5474           (*PRUNE),  but  if  there is no match to the right, backtracking cannot
5475           cross (*PRUNE). In simple cases, the use of (*PRUNE) is just an  alter-
5476           native  to an atomic group or possessive quantifier, but there are some
5477           uses of (*PRUNE) that cannot be expressed in any other way.  The behav-
5478           iour  of  (*PRUNE:NAME)  is  the  same as (*MARK:NAME)(*PRUNE) when the
5479           match fails completely; the name is passed back if this  is  the  final
5480           attempt.   (*PRUNE:NAME)  does  not  pass back a name if the match suc-
5481           ceeds. In an anchored pattern (*PRUNE) has the same  effect  as  (*COM-
5482           MIT).
5483    
5484           (*SKIP)           (*SKIP)
5485    
5486         This  verb  is like (*PRUNE), except that if the pattern is unanchored,         This  verb, when given without a name, is like (*PRUNE), except that if
5487         the "bumpalong" advance is not to the next character, but to the  posi-         the pattern is unanchored, the "bumpalong" advance is not to  the  next
5488         tion  in  the  subject where (*SKIP) was encountered. (*SKIP) signifies         character, but to the position in the subject where (*SKIP) was encoun-
5489         that whatever text was matched leading up to it cannot  be  part  of  a         tered. (*SKIP) signifies that whatever text was matched leading  up  to
5490         successful match. Consider:         it cannot be part of a successful match. Consider:
5491    
5492           a+(*SKIP)b           a+(*SKIP)b
5493    
# Line 5217  BACKTRACKING CONTROL Line 5499  BACKTRACKING CONTROL
5499         attempt would start at the second character instead of skipping  on  to         attempt would start at the second character instead of skipping  on  to
5500         "c".         "c".
5501    
5502           (*THEN)           (*SKIP:NAME)
5503    
5504           When  (*SKIP) has an associated name, its behaviour is modified. If the
5505           following pattern fails to match, the previous path through the pattern
5506           is  searched for the most recent (*MARK) that has the same name. If one
5507           is found, the "bumpalong" advance is to the subject position that  cor-
5508           responds  to  that (*MARK) instead of to where (*SKIP) was encountered.
5509           If no (*MARK) with a matching name is found, normal "bumpalong" of  one
5510           character happens (the (*SKIP) is ignored).
5511    
5512             (*THEN) or (*THEN:NAME)
5513    
5514         This verb causes a skip to the next alternation if the rest of the pat-         This verb causes a skip to the next alternation if the rest of the pat-
5515         tern does not match. That is, it cancels pending backtracking, but only         tern does not match. That is, it cancels pending backtracking, but only
# Line 5229  BACKTRACKING CONTROL Line 5521  BACKTRACKING CONTROL
5521         If the COND1 pattern matches, FOO is tried (and possibly further  items         If the COND1 pattern matches, FOO is tried (and possibly further  items
5522         after  the  end  of  the group if FOO succeeds); on failure the matcher         after  the  end  of  the group if FOO succeeds); on failure the matcher
5523         skips to the second alternative and tries COND2,  without  backtracking         skips to the second alternative and tries COND2,  without  backtracking
5524         into  COND1.  If  (*THEN)  is  used outside of any alternation, it acts         into  COND1.  The  behaviour  of  (*THEN:NAME)  is  exactly the same as
5525         exactly like (*PRUNE).         (*MARK:NAME)(*THEN) if the overall  match  fails.  If  (*THEN)  is  not
5526           directly inside an alternation, it acts like (*PRUNE).
5527    
5528    
5529  SEE ALSO  SEE ALSO
# Line 5247  AUTHOR Line 5540  AUTHOR
5540    
5541  REVISION  REVISION
5542    
5543         Last updated: 04 October 2009         Last updated: 18 May 2010
5544         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
5545  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5546    
5547    
# Line 5295  CHARACTER TYPES Line 5588  CHARACTER TYPES
5588           \D         a character that is not a decimal digit           \D         a character that is not a decimal digit
5589           \h         a horizontal whitespace character           \h         a horizontal whitespace character
5590           \H         a character that is not a horizontal whitespace character           \H         a character that is not a horizontal whitespace character
5591             \N         a character that is not a newline
5592           \p{xx}     a character with the xx property           \p{xx}     a character with the xx property
5593           \P{xx}     a character without the xx property           \P{xx}     a character without the xx property
5594           \R         a newline sequence           \R         a newline sequence
# Line 5306  CHARACTER TYPES Line 5600  CHARACTER TYPES
5600           \W         a "non-word" character           \W         a "non-word" character
5601           \X         an extended Unicode sequence           \X         an extended Unicode sequence
5602    
5603         In PCRE, \d, \D, \s, \S, \w, and \W recognize only ASCII characters.         In  PCRE,  by  default, \d, \D, \s, \S, \w, and \W recognize only ASCII
5604           characters, even in UTF-8 mode. However, this can be changed by setting
5605           the PCRE_UCP option.
5606    
5607    
5608  GENERAL CATEGORY PROPERTY CODES FOR \p and \P  GENERAL CATEGORY PROPERTIES FOR \p and \P
5609    
5610           C          Other           C          Other
5611           Cc         Control           Cc         Control
# Line 5357  GENERAL CATEGORY PROPERTY CODES FOR \p a Line 5653  GENERAL CATEGORY PROPERTY CODES FOR \p a
5653           Zs         Space separator           Zs         Space separator
5654    
5655    
5656    PCRE SPECIAL CATEGORY PROPERTIES FOR \p and \P
5657    
5658             Xan        Alphanumeric: union of properties L and N
5659             Xps        POSIX space: property Z or tab, NL, VT, FF, CR
5660             Xsp        Perl space: property Z or tab, NL, FF, CR
5661             Xwd        Perl word: property Xan or underscore
5662    
5663    
5664  SCRIPT NAMES FOR \p AND \P  SCRIPT NAMES FOR \p AND \P
5665    
5666         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,         Arabic, Armenian, Avestan, Balinese, Bamum, Bengali, Bopomofo, Braille,
5667         Buhid, Canadian_Aboriginal, Carian, Cham, Cherokee, Common, Coptic, Cu-         Buginese, Buhid, Canadian_Aboriginal, Carian, Cham,  Cherokee,  Common,
5668         neiform,  Cypriot,  Cyrillic,  Deseret, Devanagari, Ethiopic, Georgian,         Coptic,   Cuneiform,  Cypriot,  Cyrillic,  Deseret,  Devanagari,  Egyp-
5669         Glagolitic, Gothic, Greek, Gujarati, Gurmukhi,  Han,  Hangul,  Hanunoo,         tian_Hieroglyphs,  Ethiopic,  Georgian,  Glagolitic,   Gothic,   Greek,
5670         Hebrew,  Hiragana,  Inherited, Kannada, Katakana, Kayah_Li, Kharoshthi,         Gujarati,  Gurmukhi,  Han,  Hangul,  Hanunoo,  Hebrew,  Hiragana, Impe-
5671         Khmer, Lao, Latin, Lepcha, Limbu, Linear_B, Lycian, Lydian,  Malayalam,         rial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian,
5672         Mongolian,  Myanmar,  New_Tai_Lue, Nko, Ogham, Old_Italic, Old_Persian,         Javanese,  Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khmer, Lao,
5673         Ol_Chiki, Oriya, Osmanya, Phags_Pa, Phoenician, Rejang, Runic, Saurash-         Latin,  Lepcha,  Limbu,  Linear_B,  Lisu,  Lycian,  Lydian,  Malayalam,
5674         tra,  Shavian,  Sinhala,  Sudanese, Syloti_Nagri, Syriac, Tagalog, Tag-         Meetei_Mayek,  Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic,
5675         banwa,  Tai_Le,  Tamil,  Telugu,  Thaana,  Thai,   Tibetan,   Tifinagh,         Old_Persian, Old_South_Arabian, Old_Turkic, Ol_Chiki,  Oriya,  Osmanya,
5676           Phags_Pa,  Phoenician,  Rejang,  Runic, Samaritan, Saurashtra, Shavian,
5677           Sinhala, Sundanese, Syloti_Nagri, Syriac,  Tagalog,  Tagbanwa,  Tai_Le,
5678           Tai_Tham,  Tai_Viet,  Tamil,  Telugu,  Thaana, Thai, Tibetan, Tifinagh,
5679         Ugaritic, Vai, Yi.         Ugaritic, Vai, Yi.
5680    
5681    
# Line 5395  CHARACTER CLASSES Line 5702  CHARACTER CLASSES
5702           word        same as \w           word        same as \w
5703           xdigit      hexadecimal digit           xdigit      hexadecimal digit
5704    
5705         In PCRE, POSIX character set names recognize only ASCII characters. You         In PCRE, POSIX character set names recognize only ASCII  characters  by
5706         can use \Q...\E inside a character class.         default,  but  some  of them use Unicode properties if PCRE_UCP is set.
5707           You can use \Q...\E inside a character class.
5708    
5709    
5710  QUANTIFIERS  QUANTIFIERS
# Line 5421  QUANTIFIERS Line 5729  QUANTIFIERS
5729    
5730  ANCHORS AND SIMPLE ASSERTIONS  ANCHORS AND SIMPLE ASSERTIONS
5731    
5732           \b          word boundary (only ASCII letters recognized)           \b          word boundary
5733           \B          not a word boundary           \B          not a word boundary
5734           ^           start of subject           ^           start of subject
5735                        also after internal newline in multiline mode                        also after internal newline in multiline mode
# Line 5476  OPTION SETTING Line 5784  OPTION SETTING
5784           (?x)            extended (ignore white space)           (?x)            extended (ignore white space)
5785           (?-...)         unset option(s)           (?-...)         unset option(s)
5786    
5787         The following is recognized only at the start of a pattern or after one         The following are recognized only at the start of a  pattern  or  after
5788         of the newline-setting options with similar syntax:         one of the newline-setting options with similar syntax:
5789    
5790           (*UTF8)         set UTF-8 mode           (*UTF8)         set UTF-8 mode (PCRE_UTF8)
5791             (*UCP)          set PCRE_UCP (use Unicode properties for \d etc)
5792    
5793    
5794  LOOKAHEAD AND LOOKBEHIND ASSERTIONS  LOOKAHEAD AND LOOKBEHIND ASSERTIONS
# Line 5562  BACKTRACKING CONTROL Line 5871  BACKTRACKING CONTROL
5871  NEWLINE CONVENTIONS  NEWLINE CONVENTIONS
5872    
5873         These are recognized only at the very start of the pattern or  after  a         These are recognized only at the very start of the pattern or  after  a
5874         (*BSR_...) or (*UTF8) option.         (*BSR_...) or (*UTF8) or (*UCP) option.
5875    
5876           (*CR)           carriage return only           (*CR)           carriage return only
5877           (*LF)           linefeed only           (*LF)           linefeed only
# Line 5574  NEWLINE CONVENTIONS Line 5883  NEWLINE CONVENTIONS
5883  WHAT \R MATCHES  WHAT \R MATCHES
5884    
5885         These  are  recognized only at the very start of the pattern or after a         These  are  recognized only at the very start of the pattern or after a
5886         (*...) option that sets the newline convention or UTF-8 mode.         (*...) option that sets the newline convention or UTF-8 or UCP mode.
5887    
5888           (*BSR_ANYCRLF)  CR, LF, or CRLF           (*BSR_ANYCRLF)  CR, LF, or CRLF
5889           (*BSR_UNICODE)  any Unicode newline sequence           (*BSR_UNICODE)  any Unicode newline sequence
# Line 5600  AUTHOR Line 5909  AUTHOR
5909    
5910  REVISION  REVISION
5911    
5912         Last updated: 11 April 2009         Last updated: 12 May 2010
5913         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
5914  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5915    
5916    
# Line 5754  PARTIAL MATCHING USING pcre_dfa_exec() Line 6063  PARTIAL MATCHING USING pcre_dfa_exec()
6063    
6064  PARTIAL MATCHING AND WORD BOUNDARIES  PARTIAL MATCHING AND WORD BOUNDARIES
6065    
6066         If  a  pattern ends with one of sequences \w or \W, which test for word         If  a  pattern ends with one of sequences \b or \B, which test for word
6067         boundaries, partial matching with PCRE_PARTIAL_SOFT can  give  counter-         boundaries, partial matching with PCRE_PARTIAL_SOFT can  give  counter-
6068         intuitive results. Consider this pattern:         intuitive results. Consider this pattern:
6069    
# Line 5861  MULTI-SEGMENT MATCHING WITH pcre_exec() Line 6170  MULTI-SEGMENT MATCHING WITH pcre_exec()
6170           data> The date is 23ja\P           data> The date is 23ja\P
6171           Partial match: 23ja           Partial match: 23ja
6172    
6173         The this stage, an application could discard the text preceding "23ja",         At  this stage, an application could discard the text preceding "23ja",
6174         add on text from the next segment, and call pcre_exec()  again.  Unlike         add on text from the next segment, and call pcre_exec()  again.  Unlike
6175         pcre_dfa_exec(),  the  entire matching string must always be available,         pcre_dfa_exec(),  the  entire matching string must always be available,
6176         and the complete matching process occurs for each call, so more  memory         and the complete matching process occurs for each call, so more  memory
# Line 5938  ISSUES WITH MULTI-SEGMENT MATCHING Line 6247  ISSUES WITH MULTI-SEGMENT MATCHING
6247    
6248         4. Patterns that contain alternatives at the top level which do not all         4. Patterns that contain alternatives at the top level which do not all
6249         start with the  same  pattern  item  may  not  work  as  expected  when         start with the  same  pattern  item  may  not  work  as  expected  when
6250         pcre_dfa_exec() is used. For example, consider this pattern:         PCRE_DFA_RESTART  is  used  with pcre_dfa_exec(). For example, consider
6251           this pattern:
6252    
6253           1234|3789           1234|3789
6254    
6255         If  the  first  part of the subject is "ABC123", a partial match of the         If the first part of the subject is "ABC123", a partial  match  of  the
6256         first alternative is found at offset 3. There is no partial  match  for         first  alternative  is found at offset 3. There is no partial match for
6257         the second alternative, because such a match does not start at the same         the second alternative, because such a match does not start at the same
6258         point in the subject string. Attempting to  continue  with  the  string         point  in  the  subject  string. Attempting to continue with the string
6259         "7890"  does  not  yield  a  match because only those alternatives that         "7890" does not yield a match  because  only  those  alternatives  that
6260         match at one point in the subject are remembered.  The  problem  arises         match  at  one  point in the subject are remembered. The problem arises
6261         because  the  start  of the second alternative matches within the first         because the start of the second alternative matches  within  the  first
6262         alternative. There is no problem with  anchored  patterns  or  patterns         alternative.  There  is  no  problem with anchored patterns or patterns
6263         such as:         such as:
6264    
6265           1234|ABCD           1234|ABCD
6266    
6267         where  no  string can be a partial match for both alternatives. This is         where no string can be a partial match for both alternatives.  This  is
6268         not a problem if pcre_exec() is used, because the entire match  has  to         not  a  problem if pcre_exec() is used, because the entire match has to
6269         be rerun each time:         be rerun each time:
6270    
6271             re> /1234|3789/             re> /1234|3789/
# Line 5964  ISSUES WITH MULTI-SEGMENT MATCHING Line 6274  ISSUES WITH MULTI-SEGMENT MATCHING
6274           data> 1237890           data> 1237890
6275            0: 3789            0: 3789
6276    
6277           Of course, instead of using PCRE_DFA_PARTIAL, the same technique of re-
6278           running the entire match can also be used with pcre_dfa_exec(). Another
6279           possibility is to work with two buffers. If a partial match at offset n
6280           in  the first buffer is followed by "no match" when PCRE_DFA_RESTART is
6281           used on the second buffer, you can then try a  new  match  starting  at
6282           offset n+1 in the first buffer.
6283    
6284    
6285  AUTHOR  AUTHOR
6286    
# Line 5974  AUTHOR Line 6291  AUTHOR
6291    
6292  REVISION  REVISION
6293    
6294         Last updated: 29 September 2009         Last updated: 19 October 2009
6295         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
6296  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6297    
# Line 6117  PCRE PERFORMANCE Line 6434  PCRE PERFORMANCE
6434         can affect both of them.         can affect both of them.
6435    
6436    
6437  MEMORY USAGE  COMPILED PATTERN MEMORY USAGE
6438    
6439         Patterns are compiled by PCRE into a reasonably efficient byte code, so         Patterns are compiled by PCRE into a reasonably efficient byte code, so
6440         that most simple patterns do not use much memory. However, there is one         that most simple patterns do not use much memory. However, there is one
6441         case where memory usage can be unexpectedly large. When a parenthesized         case  where  the memory usage of a compiled pattern can be unexpectedly
6442         subpattern has a quantifier with a minimum greater than 1 and/or a lim-         large. If a parenthesized subpattern has a quantifier  with  a  minimum
6443         ited  maximum,  the  whole subpattern is repeated in the compiled code.         greater  than  1  and/or  a  limited  maximum,  the whole subpattern is
6444         For example, the pattern         repeated in the compiled code. For example, the pattern
6445    
6446           (abc|def){2,4}           (abc|def){2,4}
6447    
# Line 6166  MEMORY USAGE Line 6483  MEMORY USAGE
6483         otherwise handle.         otherwise handle.
6484    
6485    
6486    STACK USAGE AT RUN TIME
6487    
6488           When  pcre_exec()  is  used  for matching, certain kinds of pattern can
6489           cause it to use large amounts of the process stack.  In  some  environ-
6490           ments  the default process stack is quite small, and if it runs out the
6491           result is often SIGSEGV.  This issue is probably  the  most  frequently
6492           raised  problem  with  PCRE. Rewriting your pattern can often help. The
6493           pcrestack documentation discusses this issue in detail.
6494    
6495    
6496  PROCESSING TIME  PROCESSING TIME
6497    
6498         Certain  items  in regular expression patterns are processed more effi-         Certain items in regular expression patterns are processed  more  effi-
6499         ciently than others. It is more efficient to use a character class like         ciently than others. It is more efficient to use a character class like
6500         [aeiou]   than   a   set   of  single-character  alternatives  such  as         [aeiou]  than  a  set  of   single-character   alternatives   such   as
6501         (a|e|i|o|u). In general, the simplest construction  that  provides  the         (a|e|i|o|u).  In  general,  the simplest construction that provides the
6502         required behaviour is usually the most efficient. Jeffrey Friedl's book         required behaviour is usually the most efficient. Jeffrey Friedl's book
6503         contains a lot of useful general discussion  about  optimizing  regular         contains  a  lot  of useful general discussion about optimizing regular
6504         expressions  for  efficient  performance.  This document contains a few         expressions for efficient performance. This  document  contains  a  few
6505         observations about PCRE.         observations about PCRE.
6506    
6507         Using Unicode character properties (the \p,  \P,  and  \X  escapes)  is         Using  Unicode  character  properties  (the  \p, \P, and \X escapes) is
6508         slow,  because PCRE has to scan a structure that contains data for over         slow, because PCRE has to scan a structure that contains data for  over
6509         fifteen thousand characters whenever it needs a  character's  property.         fifteen  thousand  characters whenever it needs a character's property.
6510         If  you  can  find  an  alternative pattern that does not use character         If you can find an alternative pattern  that  does  not  use  character
6511         properties, it will probably be faster.         properties, it will probably be faster.
6512    
6513         When a pattern begins with .* not in  parentheses,  or  in  parentheses         By  default,  the  escape  sequences  \b, \d, \s, and \w, and the POSIX
6514           character classes such as [:alpha:]  do  not  use  Unicode  properties,
6515           partly for backwards compatibility, and partly for performance reasons.
6516           However, you can set PCRE_UCP if you want Unicode character  properties
6517           to  be  used.  This  can double the matching time for items such as \d,
6518           when matched with  pcre_exec();  the  performance  loss  is  less  with
6519           pcre_dfa_exec(), and in both cases there is not much difference for \b.
6520    
6521           When  a  pattern  begins  with .* not in parentheses, or in parentheses
6522         that are not the subject of a backreference, and the PCRE_DOTALL option         that are not the subject of a backreference, and the PCRE_DOTALL option
6523         is set, the pattern is implicitly anchored by PCRE, since it can  match         is  set, the pattern is implicitly anchored by PCRE, since it can match
6524         only  at  the start of a subject string. However, if PCRE_DOTALL is not         only at the start of a subject string. However, if PCRE_DOTALL  is  not
6525         set, PCRE cannot make this optimization, because  the  .  metacharacter         set,  PCRE  cannot  make this optimization, because the . metacharacter
6526         does  not then match a newline, and if the subject string contains new-         does not then match a newline, and if the subject string contains  new-
6527         lines, the pattern may match from the character  immediately  following         lines,  the  pattern may match from the character immediately following
6528         one of them instead of from the very start. For example, the pattern         one of them instead of from the very start. For example, the pattern
6529    
6530           .*second           .*second
6531    
6532         matches  the subject "first\nand second" (where \n stands for a newline         matches the subject "first\nand second" (where \n stands for a  newline
6533         character), with the match starting at the seventh character. In  order         character),  with the match starting at the seventh character. In order
6534         to do this, PCRE has to retry the match starting after every newline in         to do this, PCRE has to retry the match starting after every newline in
6535         the subject.         the subject.
6536    
6537         If you are using such a pattern with subject strings that do  not  con-         If  you  are using such a pattern with subject strings that do not con-
6538         tain newlines, the best performance is obtained by setting PCRE_DOTALL,         tain newlines, the best performance is obtained by setting PCRE_DOTALL,
6539         or starting the pattern with ^.* or ^.*? to indicate  explicit  anchor-         or  starting  the pattern with ^.* or ^.*? to indicate explicit anchor-
6540         ing.  That saves PCRE from having to scan along the subject looking for         ing. That saves PCRE from having to scan along the subject looking  for
6541         a newline to restart at.         a newline to restart at.
6542    
6543         Beware of patterns that contain nested indefinite  repeats.  These  can         Beware  of  patterns  that contain nested indefinite repeats. These can
6544         take  a  long time to run when applied to a string that does not match.         take a long time to run when applied to a string that does  not  match.
6545         Consider the pattern fragment         Consider the pattern fragment
6546    
6547           ^(a+)*           ^(a+)*
6548    
6549         This can match "aaaa" in 16 different ways, and this  number  increases         This  can  match "aaaa" in 16 different ways, and this number increases
6550         very  rapidly  as the string gets longer. (The * repeat can match 0, 1,         very rapidly as the string gets longer. (The * repeat can match  0,  1,
6551         2, 3, or 4 times, and for each of those cases other than 0 or 4, the  +         2,  3, or 4 times, and for each of those cases other than 0 or 4, the +
6552         repeats  can  match  different numbers of times.) When the remainder of         repeats can match different numbers of times.) When  the  remainder  of
6553         the pattern is such that the entire match is going to fail, PCRE has in         the pattern is such that the entire match is going to fail, PCRE has in
6554         principle  to  try  every  possible  variation,  and  this  can take an         principle to try  every  possible  variation,  and  this  can  take  an
6555         extremely long time, even for relatively short strings.         extremely long time, even for relatively short strings.
6556    
6557         An optimization catches some of the more simple cases such as         An optimization catches some of the more simple cases such as
6558    
6559           (a+)*b           (a+)*b
6560    
6561         where a literal character follows. Before  embarking  on  the  standard         where  a  literal  character  follows. Before embarking on the standard
6562         matching  procedure,  PCRE checks that there is a "b" later in the sub-         matching procedure, PCRE checks that there is a "b" later in  the  sub-
6563         ject string, and if there is not, it fails the match immediately.  How-         ject  string, and if there is not, it fails the match immediately. How-
6564         ever,  when  there  is no following literal this optimization cannot be         ever, when there is no following literal this  optimization  cannot  be
6565         used. You can see the difference by comparing the behaviour of         used. You can see the difference by comparing the behaviour of
6566    
6567           (a+)*\d           (a+)*\d
6568    
6569         with the pattern above. The former gives  a  failure  almost  instantly         with  the  pattern  above.  The former gives a failure almost instantly
6570         when  applied  to  a  whole  line of "a" characters, whereas the latter         when applied to a whole line of  "a"  characters,  whereas  the  latter
6571         takes an appreciable time with strings longer than about 20 characters.         takes an appreciable time with strings longer than about 20 characters.
6572    
6573         In many cases, the solution to this kind of performance issue is to use         In many cases, the solution to this kind of performance issue is to use
# Line 6248  AUTHOR Line 6583  AUTHOR
6583    
6584  REVISION  REVISION
6585    
6586         Last updated: 06 March 2007         Last updated: 16 May 2010
6587         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
6588  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6589    
6590    
# Line 6354  COMPILING A PATTERN Line 6689  COMPILING A PATTERN
6689         ing,  the  nmatch  and  pmatch  arguments  are ignored, and no captured         ing,  the  nmatch  and  pmatch  arguments  are ignored, and no captured
6690         strings are returned.         strings are returned.
6691    
6692             REG_UCP
6693    
6694           The PCRE_UCP option is set when the regular expression  is  passed  for
6695           compilation  to  the  native  function. This causes PCRE to use Unicode
6696           properties when matchine \d, \w,  etc.,  instead  of  just  recognizing
6697           ASCII values. Note that REG_UTF8 is not part of the POSIX standard.
6698    
6699           REG_UNGREEDY           REG_UNGREEDY
6700    
6701         The PCRE_UNGREEDY option is set when the regular expression  is  passed         The  PCRE_UNGREEDY  option is set when the regular expression is passed
6702         for  compilation  to the native function. Note that REG_UNGREEDY is not         for compilation to the native function. Note that REG_UNGREEDY  is  not
6703         part of the POSIX standard.         part of the POSIX standard.
6704    
6705           REG_UTF8           REG_UTF8
6706    
6707         The PCRE_UTF8 option is set when the regular expression is  passed  for         The  PCRE_UTF8  option is set when the regular expression is passed for
6708         compilation  to the native function. This causes the pattern itself and         compilation to the native function. This causes the pattern itself  and
6709         all data strings used for matching it to be treated as  UTF-8  strings.         all  data  strings used for matching it to be treated as UTF-8 strings.
6710         Note that REG_UTF8 is not part of the POSIX standard.         Note that REG_UTF8 is not part of the POSIX standard.
6711    
6712         In  the  absence  of  these  flags, no options are passed to the native         In the absence of these flags, no options  are  passed  to  the  native
6713         function.  This means the the  regex  is  compiled  with  PCRE  default         function.   This  means  the  the  regex  is compiled with PCRE default
6714         semantics.  In particular, the way it handles newline characters in the         semantics. In particular, the way it handles newline characters in  the
6715         subject string is the Perl way, not the POSIX way.  Note  that  setting         subject  string  is  the Perl way, not the POSIX way. Note that setting
6716         PCRE_MULTILINE  has only some of the effects specified for REG_NEWLINE.         PCRE_MULTILINE has only some of the effects specified for  REG_NEWLINE.
6717         It does not affect the way newlines are matched by . (they are not)  or         It  does not affect the way newlines are matched by . (they are not) or
6718         by a negative class such as [^a] (they are).         by a negative class such as [^a] (they are).
6719    
6720         The  yield of regcomp() is zero on success, and non-zero otherwise. The         The yield of regcomp() is zero on success, and non-zero otherwise.  The
6721         preg structure is filled in on success, and one member of the structure         preg structure is filled in on success, and one member of the structure
6722         is  public: re_nsub contains the number of capturing subpatterns in the         is public: re_nsub contains the number of capturing subpatterns in  the
6723         regular expression. Various error codes are defined in the header file.         regular expression. Various error codes are defined in the header file.
6724    
6725         NOTE: If the yield of regcomp() is non-zero, you must  not  attempt  to         NOTE:  If  the  yield of regcomp() is non-zero, you must not attempt to
6726         use the contents of the preg structure. If, for example, you pass it to         use the contents of the preg structure. If, for example, you pass it to
6727         regexec(), the result is undefined and your program is likely to crash.         regexec(), the result is undefined and your program is likely to crash.
6728    
# Line 6388  COMPILING A PATTERN Line 6730  COMPILING A PATTERN
6730  MATCHING NEWLINE CHARACTERS  MATCHING NEWLINE CHARACTERS
6731    
6732         This area is not simple, because POSIX and Perl take different views of         This area is not simple, because POSIX and Perl take different views of
6733         things.   It  is  not possible to get PCRE to obey POSIX semantics, but         things.  It is not possible to get PCRE to obey  POSIX  semantics,  but
6734         then PCRE was never intended to be a POSIX engine. The following  table         then  PCRE was never intended to be a POSIX engine. The following table
6735         lists  the  different  possibilities for matching newline characters in         lists the different possibilities for matching  newline  characters  in
6736         PCRE:         PCRE:
6737    
6738                                   Default   Change with                                   Default   Change with
# Line 6412  MATCHING NEWLINE CHARACTERS Line 6754  MATCHING NEWLINE CHARACTERS
6754           ^ matches \n in middle     no     REG_NEWLINE           ^ matches \n in middle     no     REG_NEWLINE
6755    
6756         PCRE's behaviour is the same as Perl's, except that there is no equiva-         PCRE's behaviour is the same as Perl's, except that there is no equiva-
6757         lent  for  PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is         lent for PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl,  there  is
6758         no way to stop newline from matching [^a].         no way to stop newline from matching [^a].
6759    
6760         The  default  POSIX  newline  handling  can  be  obtained  by   setting         The   default  POSIX  newline  handling  can  be  obtained  by  setting
6761         PCRE_DOTALL  and  PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE         PCRE_DOTALL and PCRE_DOLLAR_ENDONLY, but there is no way to  make  PCRE
6762         behave exactly as for the REG_NEWLINE action.         behave exactly as for the REG_NEWLINE action.
6763    
6764    
6765  MATCHING A PATTERN  MATCHING A PATTERN
6766    
6767         The function regexec() is called  to  match  a  compiled  pattern  preg         The  function  regexec()  is  called  to  match a compiled pattern preg
6768         against  a  given string, which is by default terminated by a zero byte         against a given string, which is by default terminated by a  zero  byte
6769         (but see REG_STARTEND below), subject to the options in  eflags.  These         (but  see  REG_STARTEND below), subject to the options in eflags. These
6770         can be:         can be:
6771    
6772           REG_NOTBOL           REG_NOTBOL
# Line 6446  MATCHING A PATTERN Line 6788  MATCHING A PATTERN
6788    
6789           REG_STARTEND           REG_STARTEND
6790    
6791         The string is considered to start at string +  pmatch[0].rm_so  and  to         The  string  is  considered to start at string + pmatch[0].rm_so and to
6792         have  a terminating NUL located at string + pmatch[0].rm_eo (there need         have a terminating NUL located at string + pmatch[0].rm_eo (there  need
6793         not actually be a NUL at that location), regardless  of  the  value  of         not  actually  be  a  NUL at that location), regardless of the value of
6794         nmatch.  This  is a BSD extension, compatible with but not specified by         nmatch. This is a BSD extension, compatible with but not  specified  by
6795         IEEE Standard 1003.2 (POSIX.2), and should  be  used  with  caution  in         IEEE  Standard  1003.2  (POSIX.2),  and  should be used with caution in
6796         software intended to be portable to other systems. Note that a non-zero         software intended to be portable to other systems. Note that a non-zero
6797         rm_so does not imply REG_NOTBOL; REG_STARTEND affects only the location         rm_so does not imply REG_NOTBOL; REG_STARTEND affects only the location
6798         of the string, not how it is matched.         of the string, not how it is matched.
6799    
6800         If  the pattern was compiled with the REG_NOSUB flag, no data about any         If the pattern was compiled with the REG_NOSUB flag, no data about  any
6801         matched strings  is  returned.  The  nmatch  and  pmatch  arguments  of         matched  strings  is  returned.  The  nmatch  and  pmatch  arguments of
6802         regexec() are ignored.         regexec() are ignored.
6803    
6804         If the value of nmatch is zero, or if the value pmatch is NULL, no data         If the value of nmatch is zero, or if the value pmatch is NULL, no data
# Line 6464  MATCHING A PATTERN Line 6806  MATCHING A PATTERN
6806    
6807         Otherwise,the portion of the string that was matched, and also any cap-         Otherwise,the portion of the string that was matched, and also any cap-
6808         tured substrings, are returned via the pmatch argument, which points to         tured substrings, are returned via the pmatch argument, which points to
6809         an array of nmatch structures of type regmatch_t, containing  the  mem-         an  array  of nmatch structures of type regmatch_t, containing the mem-
6810         bers  rm_so  and rm_eo. These contain the offset to the first character         bers rm_so and rm_eo. These contain the offset to the  first  character
6811         of each substring and the offset to the first character after  the  end         of  each  substring and the offset to the first character after the end
6812         of  each substring, respectively. The 0th element of the vector relates         of each substring, respectively. The 0th element of the vector  relates
6813         to the entire portion of string that was matched;  subsequent  elements         to  the  entire portion of string that was matched; subsequent elements
6814         relate  to  the capturing subpatterns of the regular expression. Unused         relate to the capturing subpatterns of the regular  expression.  Unused
6815         entries in the array have both structure members set to -1.         entries in the array have both structure members set to -1.
6816    
6817         A successful match yields  a  zero  return;  various  error  codes  are         A  successful  match  yields  a  zero  return;  various error codes are
6818         defined  in  the  header  file,  of which REG_NOMATCH is the "expected"         defined in the header file, of  which  REG_NOMATCH  is  the  "expected"
6819         failure code.         failure code.
6820    
6821    
6822  ERROR MESSAGES  ERROR MESSAGES
6823    
6824         The regerror() function maps a non-zero errorcode from either regcomp()         The regerror() function maps a non-zero errorcode from either regcomp()
6825         or  regexec()  to  a  printable message. If preg is not NULL, the error         or regexec() to a printable message. If preg is  not  NULL,  the  error
6826         should have arisen from the use of that structure. A message terminated         should have arisen from the use of that structure. A message terminated
6827         by  a  binary  zero  is  placed  in  errbuf. The length of the message,         by a binary zero is placed  in  errbuf.  The  length  of  the  message,
6828         including the zero, is limited to errbuf_size. The yield of  the  func-         including  the  zero, is limited to errbuf_size. The yield of the func-
6829         tion is the size of buffer needed to hold the whole message.         tion is the size of buffer needed to hold the whole message.
6830    
6831    
6832  MEMORY USAGE  MEMORY USAGE
6833    
6834         Compiling  a regular expression causes memory to be allocated and asso-         Compiling a regular expression causes memory to be allocated and  asso-
6835         ciated with the preg structure. The function regfree() frees  all  such         ciated  with  the preg structure. The function regfree() frees all such
6836         memory,  after  which  preg may no longer be used as a compiled expres-         memory, after which preg may no longer be used as  a  compiled  expres-
6837         sion.         sion.
6838    
6839    
# Line 6504  AUTHOR Line 6846  AUTHOR
6846    
6847  REVISION  REVISION
6848    
6849         Last updated: 02 September 2009         Last updated: 16 May 2010
6850         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
6851  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6852    
6853    
# Line 6891  PCRE SAMPLE PROGRAM Line 7233  PCRE SAMPLE PROGRAM
7233           gcc -o pcredemo -I/usr/local/include pcredemo.c \           gcc -o pcredemo -I/usr/local/include pcredemo.c \
7234               -L/usr/local/lib -lpcre               -L/usr/local/lib -lpcre
7235    
7236         Once you have compiled the demonstration program, you  can  run  simple         In a Windows environment, if you want to statically  link  the  program
7237         tests like this:         against a non-dll pcre.a file, you must uncomment the line that defines
7238           PCRE_STATIC before including pcre.h, because  otherwise  the  pcre_mal-