summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorph10 <ph10@2f5784b3-3f2a-0410-8824-cb99058d5e15>2007-06-05 10:40:13 +0000
committerph10 <ph10@2f5784b3-3f2a-0410-8824-cb99058d5e15>2007-06-05 10:40:13 +0000
commitbcfc903ca01ecbdc1c9852aab9b2d6fdcf4d9d2d (patch)
tree65e5bea0d6be410f1c71262c6e6321bb3cccc92f
parent2713a25161c167b2339b245e124932e6e3820ba7 (diff)
downloadpcre-bcfc903ca01ecbdc1c9852aab9b2d6fdcf4d9d2d.tar.gz
Drastically reduce workspace used for alternatives in groups; also some
trailing space removals for a test release. git-svn-id: svn://vcs.exim.org/pcre/code/trunk@172 2f5784b3-3f2a-0410-8824-cb99058d5e15
-rw-r--r--ChangeLog45
-rw-r--r--NEWS10
-rw-r--r--configure.ac2
-rw-r--r--doc/html/pcreapi.html16
-rw-r--r--doc/html/pcrecallout.html12
-rw-r--r--doc/html/pcrematching.html11
-rw-r--r--doc/html/pcrepartial.html4
-rw-r--r--doc/html/pcrepattern.html111
-rw-r--r--doc/pcre.txt1267
-rw-r--r--doc/pcreapi.34
-rw-r--r--doc/pcrepartial.32
-rw-r--r--doc/pcrepattern.332
-rw-r--r--pcre_compile.c132
-rw-r--r--pcre_dfa_exec.c2
-rw-r--r--pcre_exec.c18
-rw-r--r--pcre_fullinfo.c8
-rw-r--r--pcre_internal.h4
-rw-r--r--pcretest.c8
18 files changed, 947 insertions, 741 deletions
diff --git a/ChangeLog b/ChangeLog
index 46689b0..c135fb2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,7 @@
ChangeLog for PCRE
------------------
-Version 7.2 01-May-07
+Version 7.2 05-June-07
---------------------
1. If the fr_FR locale cannot be found for test 3, try the "french" locale,
@@ -21,33 +21,40 @@ Version 7.2 01-May-07
stack recursion. This gives a massive performance boost under BSD, but just
a small improvement under Linux. However, it saves one field in the frame
in all cases.
-
+
6. Added more features from the forthcoming Perl 5.10:
-
+
(a) (?-n) (where n is a string of digits) is a relative subroutine or
recursion call. It refers to the nth most recently opened parentheses.
-
+
(b) (?+n) is also a relative subroutine call; it refers to the nth next
- to be opened parentheses.
-
- (c) Conditions that refer to capturing parentheses can be specified
+ to be opened parentheses.
+
+ (c) Conditions that refer to capturing parentheses can be specified
relatively, for example, (?(-2)... or (?(+3)...
-
+
(d) \K resets the start of the current match so that everything before
- is not part of it.
-
+ is not part of it.
+
(e) \k{name} is synonymous with \k<name> and \k'name' (.NET compatible).
-
+
(f) \g{name} is another synonym - part of Perl 5.10's unification of
- reference syntax.
-
- 7. Added two new calls to pcre_fullinfo(): PCRE_INFO_OKPARTIAL and
- PCRE_INFO_JCHANGED.
-
- 8. A pattern such as (.*(.)?)* caused pcre_exec() to fail by either not
- terminating or by crashing. Diagnosed by Viktor Griph; it was in the code
+ reference syntax.
+
+ 7. Added two new calls to pcre_fullinfo(): PCRE_INFO_OKPARTIAL and
+ PCRE_INFO_JCHANGED.
+
+ 8. A pattern such as (.*(.)?)* caused pcre_exec() to fail by either not
+ terminating or by crashing. Diagnosed by Viktor Griph; it was in the code
for detecting groups that can match an empty string.
-
+
+ 9. A pattern with a very large number of alternatives (more than several
+ hundred) was running out of internal workspace during the pre-compile
+ phase, where pcre_compile() figures out how much memory will be needed. A
+ bit of new cunning has reduced the workspace needed for groups with
+ alternatives. The 1000-alternative test pattern now uses 12 bytes of
+ workspace instead of running out of the 4096 that are available.
+
Version 7.1 24-Apr-07
---------------------
diff --git a/NEWS b/NEWS
index b3ccb69..7709e9f 100644
--- a/NEWS
+++ b/NEWS
@@ -5,7 +5,7 @@ News about PCRE releases
Release 7.2 30-Apr-07
---------------------
-WARNING: saved patterns that were compiled by earlier versions of PCRE must be
+WARNING: saved patterns that were compiled by earlier versions of PCRE must be
recompiled for use with 7.2 (necessitated by the addition of \K).
Correction to the notes for 7.1: the note about shared libraries for Windows is
@@ -19,12 +19,12 @@ the basic pcre library.
Some more features from Perl 5.10 have been added:
(?-n) and (?+n) relative references for recursion and subroutines.
-
+
(Not sure if this one is actually in Perl 5.10)
- (?(-n) and (?(+n) relative references as conditions.
-
+ (?(-n) and (?(+n) relative references as conditions.
+
\K to reset the start of the matched string; for example, (foo)\Kbar
- matches bar preceded by foo, but only sets bar as the matched string
+ matches bar preceded by foo, but only sets bar as the matched string
Release 7.1 24-Apr-07
diff --git a/configure.ac b/configure.ac
index 6c2fe9d..ef7505d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -9,7 +9,7 @@ dnl empty.
m4_define(pcre_major, [7])
m4_define(pcre_minor, [2])
m4_define(pcre_prerelease, [-RC2])
-m4_define(pcre_date, [2007-05-09])
+m4_define(pcre_date, [2007-06-05])
# Libtool shared library interface versions (current:revision:age)
m4_define(libpcre_version, [0:1:0])
diff --git a/doc/html/pcreapi.html b/doc/html/pcreapi.html
index 263d26b..7830ef8 100644
--- a/doc/html/pcreapi.html
+++ b/doc/html/pcreapi.html
@@ -888,6 +888,12 @@ table indicating a fixed set of bytes for the first byte in any matching
string, a pointer to the table is returned. Otherwise NULL is returned. The
fourth argument should point to an <b>unsigned char *</b> variable.
<pre>
+ PCRE_INFO_JCHANGED
+</pre>
+Return 1 if the (?J) option setting is used in the pattern, otherwise 0. The
+fourth argument should point to an <b>int</b> variable. The (?J) internal option
+setting changes the local PCRE_DUPNAMES value.
+<pre>
PCRE_INFO_LASTLITERAL
</pre>
Return the value of the rightmost literal byte that must exist in any matched
@@ -939,6 +945,14 @@ When writing code to extract data from named subpatterns using the
name-to-number map, remember that the length of the entries is likely to be
different for each compiled pattern.
<pre>
+ PCRE_INFO_OKPARTIAL
+</pre>
+Return 1 if the pattern can be used for partial matching, otherwise 0. The
+fourth argument should point to an <b>int</b> variable. The
+<a href="pcrepartial.html"><b>pcrepartial</b></a>
+documentation lists the restrictions that apply to patterns when partial
+matching is used.
+<pre>
PCRE_INFO_OPTIONS
</pre>
Return a copy of the options with which the pattern was compiled. The fourth
@@ -1859,7 +1873,7 @@ Cambridge CB2 3QH, England.
</P>
<br><a name="SEC22" href="#TOC1">REVISION</a><br>
<P>
-Last updated: 24 April 2007
+Last updated: 04 June 2007
<br>
Copyright &copy; 1997-2007 University of Cambridge.
<br>
diff --git a/doc/html/pcrecallout.html b/doc/html/pcrecallout.html
index c87acf2..f8b5e2e 100644
--- a/doc/html/pcrecallout.html
+++ b/doc/html/pcrecallout.html
@@ -115,10 +115,12 @@ The <i>subject</i> and <i>subject_length</i> fields contain copies of the values
that were passed to <b>pcre_exec()</b>.
</P>
<P>
-The <i>start_match</i> field contains the offset within the subject at which the
-current match attempt started. If the pattern is not anchored, the callout
-function may be called several times from the same point in the pattern for
-different starting points in the subject.
+The <i>start_match</i> field normally contains the offset within the subject at
+which the current match attempt started. However, if the escape sequence \K
+has been encountered, this value is changed to reflect the modified starting
+point. If the pattern is not anchored, the callout function may be called
+several times from the same point in the pattern for different starting points
+in the subject.
</P>
<P>
The <i>current_position</i> field contains the offset within the subject of the
@@ -190,7 +192,7 @@ Cambridge CB2 3QH, England.
</P>
<br><a name="SEC6" href="#TOC1">REVISION</a><br>
<P>
-Last updated: 06 March 2007
+Last updated: 29 May 2007
<br>
Copyright &copy; 1997-2007 University of Cambridge.
<br>
diff --git a/doc/html/pcrematching.html b/doc/html/pcrematching.html
index 8ba2bbb..ed89faa 100644
--- a/doc/html/pcrematching.html
+++ b/doc/html/pcrematching.html
@@ -148,11 +148,16 @@ not supported, and cause errors if encountered.
condition or test for a specific group recursion are not supported.
</P>
<P>
-5. Callouts are supported, but the value of the <i>capture_top</i> field is
+5. Because many paths through the tree may be active, the \K escape sequence,
+which resets the start of the match when encountered (but may be on some paths
+and not on others), is not supported. It causes an error if encountered.
+</P>
+<P>
+6. Callouts are supported, but the value of the <i>capture_top</i> field is
always 1, and the value of the <i>capture_last</i> field is always -1.
</P>
<P>
-6.
+7.
The \C escape sequence, which (in the standard algorithm) matches a single
byte, even in UTF-8 mode, is not supported because the alternative algorithm
moves through the subject string one character at a time, for all active paths
@@ -207,7 +212,7 @@ Cambridge CB2 3QH, England.
</P>
<br><a name="SEC8" href="#TOC1">REVISION</a><br>
<P>
-Last updated: 06 March 2007
+Last updated: 29 May 2007
<br>
Copyright &copy; 1997-2007 University of Cambridge.
<br>
diff --git a/doc/html/pcrepartial.html b/doc/html/pcrepartial.html
index d54ed23..1fab23c 100644
--- a/doc/html/pcrepartial.html
+++ b/doc/html/pcrepartial.html
@@ -92,6 +92,8 @@ envisaged for this facility, this is not felt to be a major restriction.
<P>
If PCRE_PARTIAL is set for a pattern that does not conform to the restrictions,
<b>pcre_exec()</b> returns the error code PCRE_ERROR_BADPARTIAL (-13).
+You can use the PCRE_INFO_OKPARTIAL call to <b>pcre_fullinfo()</b> to find out
+if a compiled pattern can be used for partial matching.
</P>
<br><a name="SEC3" href="#TOC1">EXAMPLE OF PARTIAL MATCHING USING PCRETEST</a><br>
<P>
@@ -231,7 +233,7 @@ Cambridge CB2 3QH, England.
</P>
<br><a name="SEC6" href="#TOC1">REVISION</a><br>
<P>
-Last updated: 06 March 2007
+Last updated: 04 June 2007
<br>
Copyright &copy; 1997-2007 University of Cambridge.
<br>
diff --git a/doc/html/pcrepattern.html b/doc/html/pcrepattern.html
index 9e0b4cd..15ccf85 100644
--- a/doc/html/pcrepattern.html
+++ b/doc/html/pcrepattern.html
@@ -63,8 +63,10 @@ The remainder of this document discusses the patterns that are supported by
PCRE when its main matching function, <b>pcre_exec()</b>, is used.
From release 6.0, PCRE offers a second matching function,
<b>pcre_dfa_exec()</b>, which matches using a different algorithm that is not
-Perl-compatible. The advantages and disadvantages of the alternative function,
-and how it differs from the normal function, are discussed in the
+Perl-compatible. Some of the features discussed below are not available when
+<b>pcre_dfa_exec()</b> is used. The advantages and disadvantages of the
+alternative function, and how it differs from the normal function, are
+discussed in the
<a href="pcrematching.html"><b>pcrematching</b></a>
page.
</P>
@@ -253,8 +255,8 @@ Absolute and relative back references
</b><br>
<P>
The sequence \g followed by a positive or negative number, optionally enclosed
-in braces, is an absolute or relative back reference. Back references are
-discussed
+in braces, is an absolute or relative back reference. A named back reference
+can be coded as \g{name}. Back references are discussed
<a href="#backreferences">later,</a>
following the discussion of
<a href="#subpattern">parenthesized subpatterns.</a>
@@ -528,6 +530,29 @@ Matching characters by Unicode property is not fast, because PCRE has to search
a structure that contains data for over fifteen thousand characters. That is
why the traditional escape sequences such as \d and \w do not use Unicode
properties in PCRE.
+<a name="resetmatchstart"></a></P>
+<br><b>
+Resetting the match start
+</b><br>
+<P>
+The escape sequence \K, which is a Perl 5.10 feature, causes any previously
+matched characters not to be included in the final matched sequence. For
+example, the pattern:
+<pre>
+ foo\Kbar
+</pre>
+matches "foobar", but reports that it has matched "bar". This feature is
+similar to a lookbehind assertion
+<a href="#lookbehind">(described below).</a>
+However, in this case, the part of the subject before the real match does not
+have to be of fixed length, as lookbehind assertions do. The use of \K does
+not interfere with the setting of
+<a href="#subpattern">captured substrings.</a>
+For example, when the pattern
+<pre>
+ (foo)\Kbar
+</pre>
+matches "foobar", the first substring is still set to "foo".
<a name="smallassertions"></a></P>
<br><b>
Simple assertions
@@ -1309,12 +1334,17 @@ matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
capturing subpattern is matched caselessly.
</P>
<P>
-Back references to named subpatterns use the Perl syntax \k&#60;name&#62; or \k'name'
-or the Python syntax (?P=name). We could rewrite the above example in either of
+There are several different ways of writing back references to named
+subpatterns. The .NET syntax \k{name} and the Perl syntax \k&#60;name&#62; or
+\k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified
+back reference syntax, in which \g can be used for both numeric and named
+references, is also supported. We could rewrite the above example in any of
the following ways:
<pre>
(?&#60;p1&#62;(?i)rah)\s+\k&#60;p1&#62;
+ (?'p1'(?i)rah)\s+\k{p1}
(?P&#60;p1&#62;(?i)rah)\s+(?P=p1)
+ (?&#60;p1&#62;(?i)rah)\s+\g{p1}
</pre>
A subpattern that is referenced by name may appear in the pattern before or
after the reference.
@@ -1432,6 +1462,12 @@ lengths, but it is acceptable if rewritten to use two top-level branches:
<pre>
(?&#60;=abc|abde)
</pre>
+In some cases, the Perl 5.10 escape sequence \K
+<a href="#resetmatchstart">(see above)</a>
+can be used instead of a lookbehind assertion; this is not restricted to a
+fixed-length.
+</P>
+<P>
The implementation of lookbehind assertions is, for each alternative, to
temporarily move the current position back by the fixed length and then try to
match. If there are insufficient characters before the current position, the
@@ -1528,7 +1564,11 @@ Checking for a used subpattern by number
<P>
If the text between the parentheses consists of a sequence of digits, the
condition is true if the capturing subpattern of that number has previously
-matched.
+matched. An alternative notation is to precede the digits with a plus or minus
+sign. In this case, the subpattern number is relative rather than absolute.
+The most recently opened parentheses can be referenced by (?(-1), the next most
+recent by (?(-2), and so on. In looping constructs it can also make sense to
+refer to subsequent groups with constructs such as (?(+2).
</P>
<P>
Consider the following pattern, which contains non-significant white space to
@@ -1547,6 +1587,14 @@ parenthesis is required. Otherwise, since no-pattern is not present, the
subpattern matches nothing. In other words, this pattern matches a sequence of
non-parentheses, optionally enclosed in parentheses.
</P>
+<P>
+If you were embedding this pattern in a larger one, you could use a relative
+reference:
+<pre>
+ ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ...
+</pre>
+This makes the fragment independent of the parentheses in the larger pattern.
+</P>
<br><b>
Checking for a used subpattern by name
</b><br>
@@ -1697,19 +1745,37 @@ pattern, so instead you could use this:
( \( ( (?&#62;[^()]+) | (?1) )* \) )
</pre>
We have put the pattern into parentheses, and caused the recursion to refer to
-them instead of the whole pattern. In a larger pattern, keeping track of
-parenthesis numbers can be tricky. It may be more convenient to use named
-parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier
-syntax (?P&#62;name) is also supported. We could rewrite the above example as
-follows:
+them instead of the whole pattern.
+</P>
+<P>
+In a larger pattern, keeping track of parenthesis numbers can be tricky. This
+is made easier by the use of relative references. (A Perl 5.10 feature.)
+Instead of (?1) in the pattern above you can write (?-2) to refer to the second
+most recently opened parentheses preceding the recursion. In other words, a
+negative number counts capturing parentheses leftwards from the point at which
+it is encountered.
+</P>
+<P>
+It is also possible to refer to subsequently opened parentheses, by writing
+references such as (?+2). However, these cannot be recursive because the
+reference is not inside the parentheses that are referenced. They are always
+"subroutine" calls, as described in the next section.
+</P>
+<P>
+An alternative approach is to use named parentheses instead. The Perl syntax
+for this is (?&name); PCRE's earlier syntax (?P&#62;name) is also supported. We
+could rewrite the above example as follows:
<pre>
(?&#60;pn&#62; \( ( (?&#62;[^()]+) | (?&pn) )* \) )
</pre>
If there is more than one subpattern with the same name, the earliest one is
-used. This particular example pattern contains nested unlimited repeats, and so
-the use of atomic grouping for matching strings of non-parentheses is important
-when applying the pattern to strings that do not match. For example, when this
-pattern is applied to
+used.
+</P>
+<P>
+This particular example pattern that we have been looking at contains nested
+unlimited repeats, and so the use of atomic grouping for matching strings of
+non-parentheses is important when applying the pattern to strings that do not
+match. For example, when this pattern is applied to
<pre>
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
</pre>
@@ -1758,7 +1824,14 @@ is the actual recursive call.
If the syntax for a recursive subpattern reference (either by number or by
name) is used outside the parentheses to which it refers, it operates like a
subroutine in a programming language. The "called" subpattern may be defined
-before or after the reference. An earlier example pointed out that the pattern
+before or after the reference. A numbered reference can be absolute or
+relative, as in these examples:
+<pre>
+ (...(absolute)...)...(?2)...
+ (...(relative)...)...(?-1)...
+ (...(?+1)...(relative)...
+</pre>
+An earlier example pointed out that the pattern
<pre>
(sens|respons)e and \1ibility
</pre>
@@ -1781,7 +1854,7 @@ When a subpattern is used as a subroutine, processing options such as
case-independence are fixed when the subpattern is defined. They cannot be
changed for different calls. For example, consider this pattern:
<pre>
- (abc)(?i:(?1))
+ (abc)(?i:(?-1))
</pre>
It matches "abcabc". It does not match "abcABC" because the change of
processing option does not affect the called subpattern.
@@ -1836,7 +1909,7 @@ Cambridge CB2 3QH, England.
</P>
<br><a name="SEC24" href="#TOC1">REVISION</a><br>
<P>
-Last updated: 06 March 2007
+Last updated: 29 May 2007
<br>
Copyright &copy; 1997-2007 University of Cambridge.
<br>
diff --git a/doc/pcre.txt b/doc/pcre.txt
index f50b1d3..601812c 100644
--- a/doc/pcre.txt
+++ b/doc/pcre.txt
@@ -610,34 +610,39 @@ THE ALTERNATIVE MATCHING ALGORITHM
ence as the condition or test for a specific group recursion are not
supported.
- 5. Callouts are supported, but the value of the capture_top field is
+ 5. Because many paths through the tree may be active, the \K escape
+ sequence, which resets the start of the match when encountered (but may
+ be on some paths and not on others), is not supported. It causes an
+ error if encountered.
+
+ 6. Callouts are supported, but the value of the capture_top field is
always 1, and the value of the capture_last field is always -1.
- 6. The \C escape sequence, which (in the standard algorithm) matches a
- single byte, even in UTF-8 mode, is not supported because the alterna-
- tive algorithm moves through the subject string one character at a
+ 7. The \C escape sequence, which (in the standard algorithm) matches a
+ single byte, even in UTF-8 mode, is not supported because the alterna-
+ tive algorithm moves through the subject string one character at a
time, for all active paths through the tree.
ADVANTAGES OF THE ALTERNATIVE ALGORITHM
- Using the alternative matching algorithm provides the following advan-
+ Using the alternative matching algorithm provides the following advan-
tages:
1. All possible matches (at a single point in the subject) are automat-
- ically found, and in particular, the longest match is found. To find
+ ically found, and in particular, the longest match is found. To find
more than one match using the standard algorithm, you have to do kludgy
things with callouts.
- 2. There is much better support for partial matching. The restrictions
- on the content of the pattern that apply when using the standard algo-
- rithm for partial matching do not apply to the alternative algorithm.
- For non-anchored patterns, the starting position of a partial match is
+ 2. There is much better support for partial matching. The restrictions
+ on the content of the pattern that apply when using the standard algo-
+ rithm for partial matching do not apply to the alternative algorithm.
+ For non-anchored patterns, the starting position of a partial match is
available.
- 3. Because the alternative algorithm scans the subject string just
- once, and never needs to backtrack, it is possible to pass very long
- subject strings to the matching function in several pieces, checking
+ 3. Because the alternative algorithm scans the subject string just
+ once, and never needs to backtrack, it is possible to pass very long
+ subject strings to the matching function in several pieces, checking
for partial matching each time.
@@ -645,8 +650,8 @@ DISADVANTAGES OF THE ALTERNATIVE ALGORITHM
The alternative algorithm suffers from a number of disadvantages:
- 1. It is substantially slower than the standard algorithm. This is
- partly because it has to search for all possible matches, but is also
+ 1. It is substantially slower than the standard algorithm. This is
+ partly because it has to search for all possible matches, but is also
because it is less susceptible to optimization.
2. Capturing parentheses and back references are not supported.
@@ -664,7 +669,7 @@ AUTHOR
REVISION
- Last updated: 06 March 2007
+ Last updated: 29 May 2007
Copyright (c) 1997-2007 University of Cambridge.
------------------------------------------------------------------------------
@@ -1471,6 +1476,12 @@ INFORMATION ABOUT A PATTERN
returned. The fourth argument should point to an unsigned char * vari-
able.
+ PCRE_INFO_JCHANGED
+
+ Return 1 if the (?J) option setting is used in the pattern, otherwise
+ 0. The fourth argument should point to an int variable. The (?J) inter-
+ nal option setting changes the local PCRE_DUPNAMES value.
+
PCRE_INFO_LASTLITERAL
Return the value of the rightmost literal byte that must exist in any
@@ -1525,14 +1536,21 @@ INFORMATION ABOUT A PATTERN
name-to-number map, remember that the length of the entries is likely
to be different for each compiled pattern.
+ PCRE_INFO_OKPARTIAL
+
+ Return 1 if the pattern can be used for partial matching, otherwise 0.
+ The fourth argument should point to an int variable. The pcrepartial
+ documentation lists the restrictions that apply to patterns when par-
+ tial matching is used.
+
PCRE_INFO_OPTIONS
- Return a copy of the options with which the pattern was compiled. The
- fourth argument should point to an unsigned long int variable. These
+ Return a copy of the options with which the pattern was compiled. The
+ fourth argument should point to an unsigned long int variable. These
option bits are those specified in the call to pcre_compile(), modified
by any top-level option settings within the pattern itself.
- 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
alternatives begin with one of the following:
^ unless PCRE_MULTILINE is set
@@ -1546,7 +1564,7 @@ INFORMATION ABOUT A PATTERN
PCRE_INFO_SIZE
- 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
passed as the argument to pcre_malloc() when PCRE was getting memory in
which to place the compiled data. The fourth argument should point to a
size_t variable.
@@ -1554,9 +1572,9 @@ INFORMATION ABOUT A PATTERN
PCRE_INFO_STUDYSIZE
Return the size of the data block pointed to by the study_data field in
- 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
pcre_malloc() when PCRE was getting memory into which to place the data
- created by pcre_study(). The fourth argument should point to a size_t
+ created by pcre_study(). The fourth argument should point to a size_t
variable.
@@ -1564,21 +1582,21 @@ OBSOLETE INFO FUNCTION
int pcre_info(const pcre *code, int *optptr, int *firstcharptr);
- The pcre_info() function is now obsolete because its interface is too
- restrictive to return all the available data about a compiled pattern.
- New programs should use pcre_fullinfo() instead. The yield of
- pcre_info() is the number of capturing subpatterns, or one of the fol-
+ The pcre_info() function is now obsolete because its interface is too
+ restrictive to return all the available data about a compiled pattern.
+ New programs should use pcre_fullinfo() instead. The yield of
+ pcre_info() is the number of capturing subpatterns, or one of the fol-
lowing negative numbers:
PCRE_ERROR_NULL the argument code was NULL
PCRE_ERROR_BADMAGIC the "magic number" was not found
- If the optptr argument is not NULL, a copy of the options with which
- the pattern was compiled is placed in the integer it points to (see
+ If the optptr argument is not NULL, a copy of the options with which
+ the pattern was compiled is placed in the integer it points to (see
PCRE_INFO_OPTIONS above).
- If the pattern is not anchored and the firstcharptr argument is not
- NULL, it is used to pass back information about the first character of
+ If the pattern is not anchored and the firstcharptr argument is not
+ NULL, it is used to pass back information about the first character of
any matched string (see PCRE_INFO_FIRSTBYTE above).
@@ -1586,21 +1604,21 @@ REFERENCE COUNTS
int pcre_refcount(pcre *code, int adjust);
- The pcre_refcount() function is used to maintain a reference count in
+ The pcre_refcount() function is used to maintain a reference count in
the data block that contains a compiled pattern. It is provided for the
- benefit of applications that operate in an object-oriented manner,
+ benefit of applications that operate in an object-oriented manner,
where different parts of the application may be using the same compiled
pattern, but you want to free the block when they are all done.
When a pattern is compiled, the reference count field is initialized to
- zero. It is changed only by calling this function, whose action is to
- add the adjust value (which may be positive or negative) to it. The
+ zero. It is changed only by calling this function, whose action is to
+ add the adjust value (which may be positive or negative) to it. The
yield of the function is the new value. However, the value of the count
- 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
is outside these limits, it is forced to the appropriate limit value.
- Except when it is zero, the reference count is not correctly preserved
- if a pattern is compiled on one host and then transferred to a host
+ Except when it is zero, the reference count is not correctly preserved
+ if a pattern is compiled on one host and then transferred to a host
whose byte-order is different. (This seems a highly unlikely scenario.)
@@ -1610,18 +1628,18 @@ MATCHING A PATTERN: THE TRADITIONAL FUNCTION
const char *subject, int length, int startoffset,
int options, int *ovector, int ovecsize);
- The function pcre_exec() is called to match a subject string against a
- compiled pattern, which is passed in the code argument. If the pattern
+ The function pcre_exec() is called to match a subject string against a
+ compiled pattern, which is passed in the code argument. If the pattern
has been studied, the result of the study should be passed in the extra
- argument. This function is the main matching facility of the library,
+ argument. This function is the main matching facility of the library,
and it operates in a Perl-like manner. For specialist use there is also
- an alternative matching function, which is described below in the sec-
+ an alternative matching function, which is described below in the sec-
tion about the pcre_dfa_exec() function.
- In most applications, the pattern will have been compiled (and option-
- ally studied) in the same process that calls pcre_exec(). However, it
+ In most applications, the pattern will have been compiled (and option-
+ ally studied) in the same process that calls pcre_exec(). However, it
is possible to save compiled patterns and study data, and then use them
- later in different processes, possibly even on different hosts. For a
+ later in different processes, possibly even on different hosts. For a
discussion about this, see the pcreprecompile documentation.
Here is an example of a simple call to pcre_exec():
@@ -1640,10 +1658,10 @@ MATCHING A PATTERN: THE TRADITIONAL FUNCTION
Extra data for pcre_exec()
- If the extra argument is not NULL, it must point to a pcre_extra data
- block. The pcre_study() function returns such a block (when it doesn't
- return NULL), but you can also create one for yourself, and pass addi-
- tional information in it. The pcre_extra block contains the following
+ If the extra argument is not NULL, it must point to a pcre_extra data
+ block. The pcre_study() function returns such a block (when it doesn't
+ return NULL), but you can also create one for yourself, and pass addi-
+ tional information in it. The pcre_extra block contains the following
fields (not necessarily in this order):
unsigned long int flags;
@@ -1653,7 +1671,7 @@ MATCHING A PATTERN: THE TRADITIONAL FUNCTION
void *callout_data;
const unsigned char *tables;
- 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
are set. The flag bits are:
PCRE_EXTRA_STUDY_DATA
@@ -1662,75 +1680,75 @@ MATCHING A PATTERN: THE TRADITIONAL FUNCTION
PCRE_EXTRA_CALLOUT_DATA
PCRE_EXTRA_TABLES
- Other flag bits should be set to zero. The study_data field is set in
- the pcre_extra block that is returned by pcre_study(), together with
+ Other flag bits should be set to zero. The study_data field is set in
+ the pcre_extra block that is returned by pcre_study(), together with
the appropriate flag bit. You should not set this yourself, but you may
- add to the block by setting the other fields and their corresponding
+ add to the block by setting the other fields and their corresponding
flag bits.
The match_limit field provides a means of preventing PCRE from using up
- a vast amount of resources when running patterns that are not going to
- match, but which have a very large number of possibilities in their
- search trees. The classic example is the use of nested unlimited
+ a vast amount of resources when running patterns that are not going to
+ match, but which have a very large number of possibilities in their
+ search trees. The classic example is the use of nested unlimited
repeats.
- Internally, PCRE uses a function called match() which it calls repeat-
- edly (sometimes recursively). The limit set by match_limit is imposed
- on the number of times this function is called during a match, which
- has the effect of limiting the amount of backtracking that can take
+ Internally, PCRE uses a function called match() which it calls repeat-
+ edly (sometimes recursively). The limit set by match_limit is imposed
+ on the number of times this function is called during a match, which
+ has the effect of limiting the amount of backtracking that can take
place. For patterns that are not anchored, the count restarts from zero
for each position in the subject string.
- The default value for the limit can be set when PCRE is built; the
- default default is 10 million, which handles all but the most extreme
- cases. You can override the default by suppling pcre_exec() with a
- pcre_extra block in which match_limit is set, and
- PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is
+ The default value for the limit can be set when PCRE is built; the
+ default default is 10 million, which handles all but the most extreme
+ cases. You can override the default by suppling pcre_exec() with a
+ pcre_extra block in which match_limit is set, and
+ PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is
exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.
- The match_limit_recursion field is similar to match_limit, but instead
+ The match_limit_recursion field is similar to match_limit, but instead
of limiting the total number of times that match() is called, it limits
- the depth of recursion. The recursion depth is a smaller number than
- the total number of calls, because not all calls to match() are recur-
+ the depth of recursion. The recursion depth is a smaller number than
+ the total number of calls, because not all calls to match() are recur-
sive. This limit is of use only if it is set smaller than match_limit.
- Limiting the recursion depth limits the amount of stack that can be
+ Limiting the recursion depth limits the amount of stack that can be
used, or, when PCRE has been compiled to use memory on the heap instead
of the stack, the amount of heap memory that can be used.
- The default value for match_limit_recursion can be set when PCRE is
- built; the default default is the same value as the default for
- match_limit. You can override the default by suppling pcre_exec() with
- a pcre_extra block in which match_limit_recursion is set, and
- PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the
+ The default value for match_limit_recursion can be set when PCRE is
+ built; the default default is the same value as the default for
+ match_limit. You can override the default by suppling pcre_exec() with
+ a pcre_extra block in which match_limit_recursion is set, and
+ PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the
limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.
- The pcre_callout field is used in conjunction with the "callout" fea-
+ The pcre_callout field is used in conjunction with the "callout" fea-
ture, which is described in the pcrecallout documentation.
- The tables field is used to pass a character tables pointer to
- pcre_exec(); this overrides the value that is stored with the compiled
- pattern. A non-NULL value is stored with the compiled pattern only if
- custom tables were supplied to pcre_compile() via its tableptr argu-
+ The tables field is used to pass a character tables pointer to
+ pcre_exec(); this overrides the value that is stored with the compiled
+ pattern. A non-NULL value is stored with the compiled pattern only if
+ custom tables were supplied to pcre_compile() via its tableptr argu-
ment. If NULL is passed to pcre_exec() using this mechanism, it forces
- PCRE's internal tables to be used. This facility is helpful when re-
- using patterns that have been saved after compiling with an external
- set of tables, because the external tables might be at a different
- address when pcre_exec() is called. See the pcreprecompile documenta-
+ PCRE's internal tables to be used. This facility is helpful when re-
+ using patterns that have been saved after compiling with an external
+ set of tables, because the external tables might be at a different
+ address when pcre_exec() is called. See the pcreprecompile documenta-
tion for a discussion of saving compiled patterns for later use.
Option bits for pcre_exec()
- The unused bits of the options argument for pcre_exec() must be zero.
- The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,
+ The unused bits of the options argument for pcre_exec() must be zero.
+ The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,
PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK and
PCRE_PARTIAL.
PCRE_ANCHORED
- The PCRE_ANCHORED option limits pcre_exec() to matching at the first
- matching position. If a pattern was compiled with PCRE_ANCHORED, or
- turned out to be anchored by virtue of its contents, it cannot be made
+ The PCRE_ANCHORED option limits pcre_exec() to matching at the first
+ matching position. If a pattern was compiled with PCRE_ANCHORED, or
+ turned out to be anchored by virtue of its contents, it cannot be made
unachored at matching time.
PCRE_NEWLINE_CR
@@ -1739,196 +1757,196 @@ MATCHING A PATTERN: THE TRADITIONAL FUNCTION
PCRE_NEWLINE_ANYCRLF
PCRE_NEWLINE_ANY
- These options override the newline definition that was chosen or
- defaulted when the pattern was compiled. For details, see the descrip-
- tion of pcre_compile() above. During matching, the newline choice
- affects the behaviour of the dot, circumflex, and dollar metacharac-
- ters. It may also alter the way the match position is advanced after a
+ These options override the newline definition that was chosen or
+ defaulted when the pattern was compiled. For details, see the descrip-
+ tion of pcre_compile() above. During matching, the newline choice
+ affects the behaviour of the dot, circumflex, and dollar metacharac-
+ ters. It may also alter the way the match position is advanced after a
match failure for an unanchored pattern. When PCRE_NEWLINE_CRLF,
- PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is set, and a match attempt
- fails when the current position is at a CRLF sequence, the match posi-
- tion is advanced by two characters instead of one, in other words, to
+ PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is set, and a match attempt
+ fails when the current position is at a CRLF sequence, the match posi-
+ tion is advanced by two characters instead of one, in other words, to
after the CRLF.
PCRE_NOTBOL
This option specifies that first character of the subject string is not
- the beginning of a line, so the circumflex metacharacter should not
- match before it. Setting this without PCRE_MULTILINE (at compile time)
- causes circumflex never to match. This option affects only the behav-
+ the beginning of a line, so the circumflex metacharacter should not
+ match before it. Setting this without PCRE_MULTILINE (at compile time)
+ causes circumflex never to match. This option affects only the behav-
iour of the circumflex metacharacter. It does not affect \A.
PCRE_NOTEOL
This option specifies that the end of the subject string is not the end
- of a line, so the dollar metacharacter should not match it nor (except
- in multiline mode) a newline immediately before it. Setting this with-
+ of a line, so the dollar metacharacter should not match it nor (except
+ in multiline mode) a newline immediately before it. Setting this with-
out PCRE_MULTILINE (at compile time) causes dollar never to match. This
- option affects only the behaviour of the dollar metacharacter. It does
+ option affects only the behaviour of the dollar metacharacter. It does
not affect \Z or \z.
PCRE_NOTEMPTY
An empty string is not considered to be a valid match if this option is
- set. If there are alternatives in the pattern, they are tried. If all
- the alternatives match the empty string, the entire match fails. For
+ set. If there are alternatives in the pattern, they are tried. If all
+ the alternatives match the empty string, the entire match fails. For
example, if the pattern
a?b?
- is applied to a string not beginning with "a" or "b", it matches the
- empty string at the start of the subject. With PCRE_NOTEMPTY set, this
+ is applied to a string not beginning with "a" or "b", it matches the
+ empty string at the start of the subject. With PCRE_NOTEMPTY set, this
match is not valid, so PCRE searches further into the string for occur-
rences of "a" or "b".
Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe-
- cial case of a pattern match of the empty string within its split()
- function, and when using the /g modifier. It is possible to emulate
+ cial case of a pattern match of the empty string within its split()
+ function, and when using the /g modifier. It is possible to emulate
Perl's behaviour after matching a null string by first trying the match
again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then
- if that fails by advancing the starting offset (see below) and trying
+ if that fails by advancing the starting offset (see below) and trying
an ordinary match again. There is some code that demonstrates how to do
this in the pcredemo.c sample program.
PCRE_NO_UTF8_CHECK
When PCRE_UTF8 is set at compile time, the validity of the subject as a
- UTF-8 string is automatically checked when pcre_exec() is subsequently
- called. The value of startoffset is also checked to ensure that it
- points to the start of a UTF-8 character. If an invalid UTF-8 sequence
+ UTF-8 string is automatically checked when pcre_exec() is subsequently
+ called. The value of startoffset is also checked to ensure that it
+ points to the start of a UTF-8 character. If an invalid UTF-8 sequence
of bytes is found, pcre_exec() returns the error PCRE_ERROR_BADUTF8. If
- startoffset contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is
+ startoffset contains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is
returned.
- If you already know that your subject is valid, and you want to skip
- these checks for performance reasons, you can set the
- PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to
- do this for the second and subsequent calls to pcre_exec() if you are
- making repeated calls to find all the matches in a single subject
- string. However, you should be sure that the value of startoffset
- points to the start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is
- set, the effect of passing an invalid UTF-8 string as a subject, or a
- value of startoffset that does not point to the start of a UTF-8 char-
+ If you already know that your subject is valid, and you want to skip
+ these checks for performance reasons, you can set the
+ PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to
+ do this for the second and subsequent calls to pcre_exec() if you are
+ making repeated calls to find all the matches in a single subject
+ string. However, you should be sure that the value of startoffset
+ points to the start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is
+ set, the effect of passing an invalid UTF-8 string as a subject, or a
+ value of startoffset that does not point to the start of a UTF-8 char-
acter, is undefined. Your program may crash.
PCRE_PARTIAL
- This option turns on the partial matching feature. If the subject
- string fails to match the pattern, but at some point during the match-
- ing process the end of the subject was reached (that is, the subject
- partially matches the pattern and the failure to match occurred only
- because there were not enough subject characters), pcre_exec() returns
- PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is
- used, there are restrictions on what may appear in the pattern. These
+ This option turns on the partial matching feature. If the subject
+ string fails to match the pattern, but at some point during the match-
+ ing process the end of the subject was reached (that is, the subject
+ partially matches the pattern and the failure to match occurred only
+ because there were not enough subject characters), pcre_exec() returns
+ PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is
+ used, there are restrictions on what may appear in the pattern. These
are discussed in the pcrepartial documentation.
The string to be matched by pcre_exec()
- The subject string is passed to pcre_exec() as a pointer in subject, a
- length in length, and a starting byte offset in startoffset. In UTF-8
- mode, the byte offset must point to the start of a UTF-8 character.
- Unlike the pattern string, the subject may contain binary zero bytes.
- When the starting offset is zero, the search for a match starts at the
+ The subject string is passed to pcre_exec() as a pointer in subject, a
+ length in length, and a starting byte offset in startoffset. In UTF-8
+ mode, the byte offset must point to the start of a UTF-8 character.
+ Unlike the pattern string, the subject may contain binary zero bytes.
+ When the starting offset is zero, the search for a match starts at the
beginning of the subject, and this is by far the most common case.
- A non-zero starting offset is useful when searching for another match
- in the same subject by calling pcre_exec() again after a previous suc-
- cess. Setting startoffset differs from just passing over a shortened
- string and setting PCRE_NOTBOL in the case of a pattern that begins
+ A non-zero starting offset is useful when searching for another match
+ in the same subject by calling pcre_exec() again after a previous suc-
+ cess. Setting startoffset differs from just passing over a shortened
+ string and setting PCRE_NOTBOL in the case of a pattern that begins
with any kind of lookbehind. For example, consider the pattern
\Biss\B
- which finds occurrences of "iss" in the middle of words. (\B matches
- only if the current position in the subject is not a word boundary.)
- When applied to the string "Mississipi" the first call to pcre_exec()
- finds the first occurrence. If pcre_exec() is called again with just
- the remainder of the subject, namely "issipi", it does not match,
+ which finds occurrences of "iss" in the middle of words. (\B matches
+ only if the current position in the subject is not a word boundary.)
+ When applied to the string "Mississipi" the first call to pcre_exec()
+ finds the first occurrence. If pcre_exec() is called again with just
+ the remainder of the subject, namely "issipi", it does not match,
because \B is always false at the start of the subject, which is deemed
- to be a word boundary. However, if pcre_exec() is passed the entire
+ to be a word boundary. However, if pcre_exec() is passed the entire
string again, but with startoffset set to 4, it finds the second occur-
- rence of "iss" because it is able to look behind the starting point to
+ rence of "iss" because it is able to look behind the starting point to
discover that it is preceded by a letter.
- If a non-zero starting offset is passed when the pattern is anchored,
+ If a non-zero starting offset is passed when the pattern is anchored,
one attempt to match at the given offset is made. This can only succeed
- if the pattern does not require the match to be at the start of the
+ if the pattern does not require the match to be at the start of the
subject.
How pcre_exec() returns captured substrings
- In general, a pattern matches a certain portion of the subject, and in
- addition, further substrings from the subject may be picked out by
- parts of the pattern. Following the usage in Jeffrey Friedl's book,
- this is called "capturing" in what follows, and the phrase "capturing
- subpattern" is used for a fragment of a pattern that picks out a sub-
- string. PCRE supports several other kinds of parenthesized subpattern
+ In general, a pattern matches a certain portion of the subject, and in
+ addition, further substrings from the subject may be picked out by
+ parts of the pattern. Following the usage in Jeffrey Friedl's book,
+ this is called "capturing" in what follows, and the phrase "capturing
+ subpattern" is used for a fragment of a pattern that picks out a sub-
+ string. PCRE supports several other kinds of parenthesized subpattern
that do not cause substrings to be captured.
- Captured substrings are returned to the caller via a vector of integer
- offsets whose address is passed in ovector. The number of elements in
- the vector is passed in ovecsize, which must be a non-negative number.
+ Captured substrings are returned to the caller via a vector of integer
+ offsets whose address is passed in ovector. The number of elements in
+ the vector is passed in ovecsize, which must be a non-negative number.
Note: this argument is NOT the size of ovector in bytes.
- The first two-thirds of the vector is used to pass back captured sub-
- strings, each substring using a pair of integers. The remaining third
- of the vector is used as workspace by pcre_exec() while matching cap-
- turing subpatterns, and is not available for passing back information.
- The length passed in ovecsize should always be a multiple of three. If
+ The first two-thirds of the vector is used to pass back captured sub-
+ strings, each substring using a pair of integers. The remaining third
+ of the vector is used as workspace by pcre_exec() while matching cap-
+ turing subpatterns, and is not available for passing back information.
+ The length passed in ovecsize should always be a multiple of three. If
it is not, it is rounded down.
- When a match is successful, information about captured substrings is
- returned in pairs of integers, starting at the beginning of ovector,
- and continuing up to two-thirds of its length at the most. The first
+ When a match is successful, information about captured substrings is
+ returned in pairs of integers, starting at the beginning of ovector,
+ and continuing up to two-thirds of its length at the most. The first
element of a pair is set to the offset of the first character in a sub-
- string, and the second is set to the offset of the first character
- after the end of a substring. The first pair, ovector[0] and ovec-
- tor[1], identify the portion of the subject string matched by the
- entire pattern. The next pair is used for the first capturing subpat-
+ string, and the second is set to the offset of the first character
+ after the end of a substring. The first pair, ovector[0] and ovec-
+ tor[1], identify the portion of the subject string matched by the
+ entire pattern. The next pair is used for the first capturing subpat-
tern, and so on. The value returned by pcre_exec() is one more than the
highest numbered pair that has been set. For example, if two substrings
- have been captured, the returned value is 3. If there are no capturing
- subpatterns, the return value from a successful match is 1, indicating
+ have been captured, the returned value is 3. If there are no capturing
+ subpatterns, the return value from a successful match is 1, indicating
that just the first pair of offsets has been set.
If a capturing subpattern is matched repeatedly, it is the last portion
of the string that it matched that is returned.
- If the vector is too small to hold all the captured substring offsets,
+ If the vector is too small to hold all the captured substring offsets,
it is used as far as possible (up to two-thirds of its length), and the
- function returns a value of zero. In particular, if the substring off-
+ function returns a value of zero. In particular, if the substring off-
sets are not of interest, pcre_exec() may be called with ovector passed
- as NULL and ovecsize as zero. However, if the pattern contains back
- references and the ovector is not big enough to remember the related
- substrings, PCRE has to get additional memory for use during matching.
+ as NULL and ovecsize as zero. However, if the pattern contains back
+ references and the ovector is not big enough to remember the related
+ substrings, PCRE has to get additional memory for use during matching.
Thus it is usually advisable to supply an ovector.
- The pcre_info() function can be used to find out how many capturing
- subpatterns there are in a compiled pattern. The smallest size for
- ovector that will allow for n captured substrings, in addition to the
+ The pcre_info() function can be used to find out how many capturing
+ subpatterns there are in a compiled pattern. The smallest size for
+ ovector that will allow for n captured substrings, in addition to the
offsets of the substring matched by the whole pattern, is (n+1)*3.
- It is possible for capturing subpattern number n+1 to match some part
+ It is possible for capturing subpattern number n+1 to match some part
of the subject when subpattern n has not been used at all. For example,
- if the string "abc" is matched against the pattern (a|(z))(bc) the
+ if the string "abc" is matched against the pattern (a|(z))(bc) the
return from the function is 4, and subpatterns 1 and 3 are matched, but
- 2 is not. When this happens, both values in the offset pairs corre-
+ 2 is not. When this happens, both values in the offset pairs corre-
sponding to unused subpatterns are set to -1.
- Offset values that correspond to unused subpatterns at the end of the
- expression are also set to -1. For example, if the string "abc" is
- matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not
- matched. The return from the function is 2, because the highest used
+ Offset values that correspond to unused subpatterns at the end of the
+ expression are also set to -1. For example, if the string "abc" is
+ matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not
+ matched. The return from the function is 2, because the highest used
capturing subpattern number is 1. However, you can refer to the offsets
- for the second and third capturing subpatterns if you wish (assuming
+ for the second and third capturing subpatterns if you wish (assuming
the vector is large enough, of course).
- Some convenience functions are provided for extracting the captured
+ Some convenience functions are provided for extracting the captured
substrings as separate strings. These are described below.
Error return values from pcre_exec()
- If pcre_exec() fails, it returns a negative number. The following are
+ If pcre_exec() fails, it returns a negative number. The following are
defined in the header file:
PCRE_ERROR_NOMATCH (-1)
@@ -1937,7 +1955,7 @@ MATCHING A PATTERN: THE TRADITIONAL FUNCTION
PCRE_ERROR_NULL (-2)
- Either code or subject was passed as NULL, or ovector was NULL and
+ Either code or subject was passed as NULL, or ovector was NULL and
ovecsize was not zero.
PCRE_ERROR_BADOPTION (-3)
@@ -1946,87 +1964,87 @@ MATCHING A PATTERN: THE TRADITIONAL FUNCTION
PCRE_ERROR_BADMAGIC (-4)
- PCRE stores a 4-byte "magic number" at the start of the compiled code,
+ PCRE stores a 4-byte "magic number" at the start of the compiled code,
to catch the case when it is passed a junk pointer and to detect when a
pattern that was compiled in an environment of one endianness is run in
- an environment with the other endianness. This is the error that PCRE
+ an environment with the other endianness. This is the error that PCRE
gives when the magic number is not present.
PCRE_ERROR_UNKNOWN_OPCODE (-5)
While running the pattern match, an unknown item was encountered in the
- compiled pattern. This error could be caused by a bug in PCRE or by
+ compiled pattern. This error could be caused by a bug in PCRE or by
overwriting of the compiled pattern.
PCRE_ERROR_NOMEMORY (-6)
- If a pattern contains back references, but the ovector that is passed
+ If a pattern contains back references, but the ovector that is passed
to pcre_exec() is not big enough to remember the referenced substrings,
- PCRE gets a block of memory at the start of matching to use for this
- purpose. If the call via pcre_malloc() fails, this error is given. The
+ PCRE gets a block of memory at the start of matching to use for this
+ purpose. If the call via pcre_malloc() fails, this error is given. The
memory is automatically freed at the end of matching.
PCRE_ERROR_NOSUBSTRING (-7)
- This error is used by the pcre_copy_substring(), pcre_get_substring(),
+ This error is used by the pcre_copy_substring(), pcre_get_substring(),
and pcre_get_substring_list() functions (see below). It is never
returned by pcre_exec().
PCRE_ERROR_MATCHLIMIT (-8)
- The backtracking limit, as specified by the match_limit field in a
- pcre_extra structure (or defaulted) was reached. See the description
+ The backtracking limit, as specified by the match_limit field in a
+ pcre_extra structure (or defaulted) was reached. See the description
above.
PCRE_ERROR_CALLOUT (-9)
This error is never generated by pcre_exec() itself. It is provided for
- use by callout functions that want to yield a distinctive error code.
+ use by callout functions that want to yield a distinctive error code.
See the pcrecallout documentation for details.
PCRE_ERROR_BADUTF8 (-10)
- A string that contains an invalid UTF-8 byte sequence was passed as a
+ A string that contains an invalid UTF-8 byte sequence was passed as a
subject.
PCRE_ERROR_BADUTF8_OFFSET (-11)
The UTF-8 byte sequence that was passed as a subject was valid, but the
- value of startoffset did not point to the beginning of a UTF-8 charac-
+ value of startoffset did not point to the beginning of a UTF-8 charac-
ter.
PCRE_ERROR_PARTIAL (-12)
- The subject string did not match, but it did match partially. See the
+ The subject string did not match, but it did match partially. See the
pcrepartial documentation for details of partial matching.
PCRE_ERROR_BADPARTIAL (-13)
- The PCRE_PARTIAL option was used with a compiled pattern containing
- items that are not supported for partial matching. See the pcrepartial
+ The PCRE_PARTIAL option was used with a compiled pattern containing
+ items that are not supported for partial matching. See the pcrepartial
documentation for details of partial matching.
PCRE_ERROR_INTERNAL (-14)
- An unexpected internal error has occurred. This error could be caused
+ An unexpected internal error has occurred. This error could be caused
by a bug in PCRE or by overwriting of the compiled pattern.
PCRE_ERROR_BADCOUNT (-15)
- This error is given if the value of the ovecsize argument is negative.
+ This error is given if the value of the ovecsize argument is negative.
PCRE_ERROR_RECURSIONLIMIT (-21)
The internal recursion limit, as specified by the match_limit_recursion
- field in a pcre_extra structure (or defaulted) was reached. See the
+ field in a pcre_extra structure (or defaulted) was reached. See the
description above.
PCRE_ERROR_NULLWSLIMIT (-22)
- When a group that can match an empty substring is repeated with an
- unbounded upper limit, the subject position at the start of the group
+ When a group that can match an empty substring is repeated with an
+ unbounded upper limit, the subject position at the start of the group
must be remembered, so that a test for an empty string can be made when
- the end of the group is reached. Some workspace is required for this;
+ the end of the group is reached. Some workspace is required for this;
if it runs out, this error is given.
PCRE_ERROR_BADNEWLINE (-23)
@@ -2049,78 +2067,78 @@ EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
int pcre_get_substring_list(const char *subject,
int *ovector, int stringcount, const char ***listptr);
- Captured substrings can be accessed directly by using the offsets
- returned by pcre_exec() in ovector. For convenience, the functions
+ Captured substrings can be accessed directly by using the offsets
+ returned by pcre_exec() in ovector. For convenience, the functions
pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub-
- string_list() are provided for extracting captured substrings as new,
- separate, zero-terminated strings. These functions identify substrings
- by number. The next section describes functions for extracting named
+ string_list() are provided for extracting captured substrings as new,
+ separate, zero-terminated strings. These functions identify substrings
+ by number. The next section describes functions for extracting named
substrings.
- A substring that contains a binary zero is correctly extracted and has
- a further zero added on the end, but the result is not, of course, a C
- string. However, you can process such a string by referring to the
- length that is returned by pcre_copy_substring() and pcre_get_sub-
+ A substring that contains a binary zero is correctly extracted and has
+ a further zero added on the end, but the result is not, of course, a C
+ string. However, you can process such a string by referring to the
+ length that is returned by pcre_copy_substring() and pcre_get_sub-
string(). Unfortunately, the interface to pcre_get_substring_list() is
- not adequate for handling strings containing binary zeros, because the
+ not adequate for handling strings containing binary zeros, because the
end of the final string is not independently indicated.
- The first three arguments are the same for all three of these func-
- tions: subject is the subject string that has just been successfully
+ The first three arguments are the same for all three of these func-
+ tions: subject is the subject string that has just been successfully
matched, ovector is a pointer to the vector of integer offsets that was
passed to pcre_exec(), and stringcount is the number of substrings that
- were captured by the match, including the substring that matched the
+ were captured by the match, including the substring that matched the
entire regular expression. This is the value returned by pcre_exec() if
- it is greater than zero. If pcre_exec() returned zero, indicating that
- it ran out of space in ovector, the value passed as stringcount should
+ it is greater than zero. If pcre_exec() returned zero, indicating that
+ it ran out of space in ovector, the value passed as stringcount should
be the number of elements in the vector divided by three.
- The functions pcre_copy_substring() and pcre_get_substring() extract a
- single substring, whose number is given as stringnumber. A value of
- zero extracts the substring that matched the entire pattern, whereas
- higher values extract the captured substrings. For pcre_copy_sub-
- string(), the string is placed in buffer, whose length is given by
- buffersize, while for pcre_get_substring() a new block of memory is
- obtained via pcre_malloc, and its address is returned via stringptr.
- The yield of the function is the length of the string, not including
+ The functions pcre_copy_substring() and pcre_get_substring() extract a
+ single substring, whose number is given as stringnumber. A value of
+ zero extracts the substring that matched the entire pattern, whereas
+ higher values extract the captured substrings. For pcre_copy_sub-
+ string(), the string is placed in buffer, whose length is given by
+ buffersize, while for pcre_get_substring() a new block of memory is
+ obtained via pcre_malloc, and its address is returned via stringptr.
+ The yield of the function is the length of the string, not including
the terminating zero, or one of these error codes:
PCRE_ERROR_NOMEMORY (-6)
- The buffer was too small for pcre_copy_substring(), or the attempt to
+ The buffer was too small for pcre_copy_substring(), or the attempt to
get memory failed for pcre_get_substring().
PCRE_ERROR_NOSUBSTRING (-7)
There is no substring whose number is stringnumber.
- The pcre_get_substring_list() function extracts all available sub-
- strings and builds a list of pointers to them. All this is done in a
+ The pcre_get_substring_list() function extracts all available sub-
+ strings and builds a list of pointers to them. All this is done in a
single block of memory that is obtained via pcre_malloc. The address of
- the memory block is returned via listptr, which is also the start of
- the list of string pointers. The end of the list is marked by a NULL
- pointer. The yield of the function is zero if all went well, or the
+ the memory block is returned via listptr, which is also the start of
+ the list of string pointers. The end of the list is marked by a NULL
+ pointer. The yield of the function is zero if all went well, or the
error code
PCRE_ERROR_NOMEMORY (-6)
if the attempt to get the memory block failed.
- When any of these functions encounter a substring that is unset, which
- can happen when capturing subpattern number n+1 matches some part of
- the subject, but subpattern n has not been used at all, they return an
+ When any of these functions encounter a substring that is unset, which
+ can happen when capturing subpattern number n+1 matches some part of
+ the subject, but subpattern n has not been used at all, they return an
empty string. This can be distinguished from a genuine zero-length sub-
- string by inspecting the appropriate offset in ovector, which is nega-
+ string by inspecting the appropriate offset in ovector, which is nega-
tive for unset substrings.
- The two convenience functions pcre_free_substring() and pcre_free_sub-
- string_list() can be used to free the memory returned by a previous
+ The two convenience functions pcre_free_substring() and pcre_free_sub-
+ string_list() can be used to free the memory returned by a previous
call of pcre_get_substring() or pcre_get_substring_list(), respec-
- tively. They do nothing more than call the function pointed to by
- pcre_free, which of course could be called directly from a C program.
- However, PCRE is used in some situations where it is linked via a spe-
- cial interface to another programming language that cannot use
- pcre_free directly; it is for these cases that the functions are pro-
+ tively. They do nothing more than call the function pointed to by
+ pcre_free, which of course could be called directly from a C program.
+ However, PCRE is used in some situations where it is linked via a spe-
+ cial interface to another programming language that cannot use
+ pcre_free directly; it is for these cases that the functions are pro-
vided.
@@ -2139,7 +2157,7 @@ EXTRACTING CAPTURED SUBSTRINGS BY NAME
int stringcount, const char *stringname,
const char **stringptr);
- To extract a substring by name, you first have to find associated num-
+ To extract a substring by name, you first have to find associated num-
ber. For example, for this pattern
(a+)b(?<xxx>\d+)...
@@ -2148,27 +2166,27 @@ EXTRACTING CAPTURED SUBSTRINGS BY NAME
be unique (PCRE_DUPNAMES was not set), you can find the number from the
name by calling pcre_get_stringnumber(). The first argument is the com-
piled pattern, and the second is the name. The yield of the function is
- the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no
+ the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no
subpattern of that name.
Given the number, you can extract the substring directly, or use one of
the functions described in the previous section. For convenience, there
are also two functions that do the whole job.
- Most of the arguments of pcre_copy_named_substring() and
- pcre_get_named_substring() are the same as those for the similarly
- named functions that extract by number. As these are described in the
- previous section, they are not re-described here. There are just two
+ Most of the arguments of pcre_copy_named_substring() and
+ pcre_get_named_substring() are the same as those for the similarly
+ named functions that extract by number. As these are described in the
+ previous section, they are not re-described here. There are just two
differences:
- First, instead of a substring number, a substring name is given. Sec-
+ First, instead of a substring number, a substring name is given. Sec-
ond, there is an extra argument, given at the start, which is a pointer
- to the compiled pattern. This is needed in order to gain access to the
+ to the compiled pattern. This is needed in order to gain access to the
name-to-number translation table.
- These functions call pcre_get_stringnumber(), and if it succeeds, they
- then call pcre_copy_substring() or pcre_get_substring(), as appropri-
- ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate names, the
+ These functions call pcre_get_stringnumber(), and if it succeeds, they
+ then call pcre_copy_substring() or pcre_get_substring(), as appropri-
+ ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate names, the
behaviour may not be what you want (see the next section).
@@ -2177,45 +2195,45 @@ DUPLICATE SUBPATTERN NAMES
int pcre_get_stringtable_entries(const pcre *code,
const char *name, char **first, char **last);
- When a pattern is compiled with the PCRE_DUPNAMES option, names for
- subpatterns are not required to be unique. Normally, patterns with
- duplicate names are such that in any one match, only one of the named
- subpatterns participates. An example is shown in the pcrepattern docu-
+ When a pattern is compiled with the PCRE_DUPNAMES option, names for
+ subpatterns are not required to be unique. Normally, patterns with
+ duplicate names are such that in any one match, only one of the named
+ subpatterns participates. An example is shown in the pcrepattern docu-
mentation. When duplicates are present, pcre_copy_named_substring() and
- pcre_get_named_substring() return the first substring corresponding to
- the given name that is set. If none are set, an empty string is
+ pcre_get_named_substring() return the first substring corresponding to
+ the given name that is set. If none are set, an empty string is
returned. The pcre_get_stringnumber() function returns one of the num-
- bers that are associated with the name, but it is not defined which it
+ bers that are associated with the name, but it is not defined which it
is.
- If you want to get full details of all captured substrings for a given
- name, you must use the pcre_get_stringtable_entries() function. The
+ If you want to get full details of all captured substrings for a given
+ name, you must use the pcre_get_stringtable_entries() function. The
first argument is the compiled pattern, and the second is the name. The
- third and fourth are pointers to variables which are updated by the
+ third and fourth are pointers to variables which are updated by the
function. After it has run, they point to the first and last entries in
- the name-to-number table for the given name. The function itself
- returns the length of each entry, or PCRE_ERROR_NOSUBSTRING (-7) if
- there are none. The format of the table is described above in the sec-
- tion entitled Information about a pattern. Given all the relevant
- entries for the name, you can extract each of their numbers, and hence
+ the name-to-number table for the given name. The function itself
+ returns the length of each entry, or PCRE_ERROR_NOSUBSTRING (-7) if
+ there are none. The format of the table is described above in the sec-
+ tion entitled Information about a pattern. Given all the relevant
+ entries for the name, you can extract each of their numbers, and hence
the captured data, if any.
FINDING ALL POSSIBLE MATCHES
- The traditional matching function uses a similar algorithm to Perl,
+ The traditional matching function uses a similar algorithm to Perl,
which stops when it finds the first match, starting at a given point in
- the subject. If you want to find all possible matches, or the longest
- possible match, consider using the alternative matching function (see
- below) instead. If you cannot use the alternative function, but still
- need to find all possible matches, you can kludge it up by making use
+ the subject. If you want to find all possible matches, or the longest
+ possible match, consider using the alternative matching function (see
+ below) instead. If you cannot use the alternative function, but still
+ need to find all possible matches, you can kludge it up by making use
of the callout facility, which is described in the pcrecallout documen-
tation.
What you have to do is to insert a callout right at the end of the pat-
- tern. When your callout function is called, extract and save the cur-
- rent matched substring. Then return 1, which forces pcre_exec() to
- backtrack and try other alternatives. Ultimately, when it runs out of
+ tern. When your callout function is called, extract and save the cur-
+ rent matched substring. Then return 1, which forces pcre_exec() to
+ backtrack and try other alternatives. Ultimately, when it runs out of
matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.
@@ -2226,25 +2244,25 @@ MATCHING A PATTERN: THE ALTERNATIVE FUNCTION
int options, int *ovector, int ovecsize,
int *workspace, int wscount);
- The function pcre_dfa_exec() is called to match a subject string
- against a compiled pattern, using a matching algorithm that scans the
- subject string just once, and does not backtrack. This has different
- characteristics to the normal algorithm, and is not compatible with
- Perl. Some of the features of PCRE patterns are not supported. Never-
- theless, there are times when this kind of matching can be useful. For
+ The function pcre_dfa_exec() is called to match a subject string
+ against a compiled pattern, using a matching algorithm that scans the
+ subject string just once, and does not backtrack. This has different
+ characteristics to the normal algorithm, and is not compatible with
+ Perl. Some of the features of PCRE patterns are not supported. Never-
+ theless, there are times when this kind of matching can be useful. For
a discussion of the two matching algorithms, see the pcrematching docu-
mentation.
- The arguments for the pcre_dfa_exec() function are the same as for
+ The arguments for the pcre_dfa_exec() function are the same as for
pcre_exec(), plus two extras. The ovector argument is used in a differ-
- ent way, and this is described below. The other common arguments are
- used in the same way as for pcre_exec(), so their description is not
+ ent way, and this is described below. The other common arguments are
+ used in the same way as for pcre_exec(), so their description is not
repeated here.
- The two additional arguments provide workspace for the function. The
- workspace vector should contain at least 20 elements. It is used for
+ The two additional arguments provide workspace for the function. The
+ workspace vector should contain at least 20 elements. It is used for
keeping track of multiple paths through the pattern tree. More
- workspace will be needed for patterns and subjects where there are a
+ workspace will be needed for patterns and subjects where there are a
lot of potential matches.
Here is an example of a simple call to pcre_dfa_exec():
@@ -2266,47 +2284,47 @@ MATCHING A PATTERN: THE ALTERNATIVE FUNCTION
Option bits for pcre_dfa_exec()
- The unused bits of the options argument for pcre_dfa_exec() must be
- zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEW-
- LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK,
+ The unused bits of the options argument for pcre_dfa_exec() must be
+ zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEW-
+ LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK,
PCRE_PARTIAL, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last
three of these are the same as for pcre_exec(), so their description is
not repeated here.
PCRE_PARTIAL
- This has the same general effect as it does for pcre_exec(), but the
- details are slightly different. When PCRE_PARTIAL is set for
- pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is converted into
- PCRE_ERROR_PARTIAL if the end of the subject is reached, there have
+ This has the same general effect as it does for pcre_exec(), but the
+ details are slightly different. When PCRE_PARTIAL is set for
+ pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is converted into
+ PCRE_ERROR_PARTIAL if the end of the subject is reached, there have
been no complete matches, but there is still at least one matching pos-
- sibility. The portion of the string that provided the partial match is
+ sibility. The portion of the string that provided the partial match is
set as the first matching string.
PCRE_DFA_SHORTEST
- Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to
+ Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to
stop as soon as it has found one match. Because of the way the alterna-
- tive algorithm works, this is necessarily the shortest possible match
+ tive algorithm works, this is necessarily the shortest possible match
at the first possible matching point in the subject string.
PCRE_DFA_RESTART
- When pcre_dfa_exec() is called with the PCRE_PARTIAL option, and
- returns a partial match, it is possible to call it again, with addi-
- tional subject characters, and have it continue with the same match.
- The PCRE_DFA_RESTART option requests this action; when it is set, the
- workspace and wscount options must reference the same vector as before
- because data about the match so far is left in them after a partial
- match. There is more discussion of this facility in the pcrepartial
+ When pcre_dfa_exec() is called with the PCRE_PARTIAL option, and
+ returns a partial match, it is possible to call it again, with addi-
+ tional subject characters, and have it continue with the same match.
+ The PCRE_DFA_RESTART option requests this action; when it is set, the
+ workspace and wscount options must reference the same vector as before
+ because data about the match so far is left in them after a partial
+ match. There is more discussion of this facility in the pcrepartial
documentation.
Successful returns from pcre_dfa_exec()
- 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-
string in the subject. Note, however, that all the matches from one run
- of the function start at the same point in the subject. The shorter
- matches are all initial substrings of the longer matches. For example,
+ of the function start at the same point in the subject. The shorter
+ matches are all initial substrings of the longer matches. For example,
if the pattern
<.*>
@@ -2321,62 +2339,62 @@ MATCHING A PATTERN: THE ALTERNATIVE FUNCTION
<something> <something else>
<something> <something else> <something further>
- On success, the yield of the function is a number greater than zero,
- which is the number of matched substrings. The substrings themselves
- are returned in ovector. Each string uses two elements; the first is
- the offset to the start, and the second is the offset to the end. In
- fact, all the strings have the same start offset. (Space could have
- been saved by giving this only once, but it was decided to retain some
- compatibility with the way pcre_exec() returns data, even though the
+ On success, the yield of the function is a number greater than zero,
+ which is the number of matched substrings. The substrings themselves
+ are returned in ovector. Each string uses two elements; the first is
+ the offset to the start, and the second is the offset to the end. In
+ fact, all the strings have the same start offset. (Space could have
+ been saved by giving this only once, but it was decided to retain some
+ compatibility with the way pcre_exec() returns data, even though the
meaning of the strings is different.)
The strings are returned in reverse order of length; that is, the long-
- est matching string is given first. If there were too many matches to
- fit into ovector, the yield of the function is zero, and the vector is
+ est matching string is given first. If there were too many matches to
+ fit into ovector, the yield of the function is zero, and the vector is
filled with the longest matches.
Error returns from pcre_dfa_exec()
- The pcre_dfa_exec() function returns a negative number when it fails.
- Many of the errors are the same as for pcre_exec(), and these are
- described above. There are in addition the following errors that are
+ The pcre_dfa_exec() function returns a negative number when it fails.
+ Many of the errors are the same as for pcre_exec(), and these are
+ described above. There are in addition the following errors that are
specific to pcre_dfa_exec():
PCRE_ERROR_DFA_UITEM (-16)
- This return is given if pcre_dfa_exec() encounters an item in the pat-
- tern that it does not support, for instance, the use of \C or a back
+ This return is given if pcre_dfa_exec() encounters an item in the pat-
+ tern that it does not support, for instance, the use of \C or a back
reference.
PCRE_ERROR_DFA_UCOND (-17)
- This return is given if pcre_dfa_exec() encounters a condition item
- that uses a back reference for the condition, or a test for recursion
+ This return is given if pcre_dfa_exec() encounters a condition item
+ that uses a back reference for the condition, or a test for recursion
in a specific group. These are not supported.
PCRE_ERROR_DFA_UMLIMIT (-18)
- 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
that contains a setting of the match_limit field. This is not supported
(it is meaningless).
PCRE_ERROR_DFA_WSSIZE (-19)
- 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
workspace vector.
PCRE_ERROR_DFA_RECURSE (-20)
- When a recursive subpattern is processed, the matching function calls
- itself recursively, using private vectors for ovector and workspace.
- This error is given if the output vector is not large enough. This
+ When a recursive subpattern is processed, the matching function calls
+ itself recursively, using private vectors for ovector and workspace.
+ This error is given if the output vector is not large enough. This
should be extremely rare, as a vector of size 1000 is used.
SEE ALSO
- pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-
- tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
+ pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-
+ tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
AUTHOR
@@ -2388,7 +2406,7 @@ AUTHOR
REVISION
- Last updated: 24 April 2007
+ Last updated: 04 June 2007
Copyright (c) 1997-2007 University of Cambridge.
------------------------------------------------------------------------------
@@ -2491,10 +2509,12 @@ THE CALLOUT INTERFACE
The subject and subject_length fields contain copies of the values that
were passed to pcre_exec().
- The start_match field contains the offset within the subject at which
- the current match attempt started. If the pattern is not anchored, the
- callout function may be called several times from the same point in the
- pattern for different starting points in the subject.
+ The start_match field normally contains the offset within the subject
+ at which the current match attempt started. However, if the escape
+ sequence \K has been encountered, this value is changed to reflect the
+ modified starting point. If the pattern is not anchored, the callout
+ function may be called several times from the same point in the pattern
+ for different starting points in the subject.
The current_position field contains the offset within the subject of
the current match pointer.
@@ -2557,7 +2577,7 @@ AUTHOR
REVISION
- Last updated: 06 March 2007
+ Last updated: 29 May 2007
Copyright (c) 1997-2007 University of Cambridge.
------------------------------------------------------------------------------
@@ -2718,38 +2738,39 @@ PCRE REGULAR EXPRESSION DETAILS
ported by PCRE when its main matching function, pcre_exec(), is used.
From release 6.0, PCRE offers a second matching function,
pcre_dfa_exec(), which matches using a different algorithm that is not
- Perl-compatible. The advantages and disadvantages of the alternative
- function, and how it differs from the normal function, are discussed in
- the pcrematching page.
+ Perl-compatible. Some of the features discussed below are not available
+ when pcre_dfa_exec() is used. The advantages and disadvantages of the
+ alternative function, and how it differs from the normal function, are
+ discussed in the pcrematching page.
CHARACTERS AND METACHARACTERS
- A regular expression is a pattern that is matched against a subject
- string from left to right. Most characters stand for themselves in a
- pattern, and match the corresponding characters in the subject. As a
+ A regular expression is a pattern that is matched against a subject
+ string from left to right. Most characters stand for themselves in a
+ pattern, and match the corresponding characters in the subject. As a
trivial example, the pattern
The quick brown fox
matches a portion of a subject string that is identical to itself. When
- caseless matching is specified (the PCRE_CASELESS option), letters are
- matched independently of case. In UTF-8 mode, PCRE always understands
- the concept of case for characters whose values are less than 128, so
- caseless matching is always possible. For characters with higher val-
- ues, the concept of case is supported if PCRE is compiled with Unicode
- property support, but not otherwise. If you want to use caseless
- matching for characters 128 and above, you must ensure that PCRE is
+ caseless matching is specified (the PCRE_CASELESS option), letters are
+ matched independently of case. In UTF-8 mode, PCRE always understands
+ the concept of case for characters whose values are less than 128, so
+ caseless matching is always possible. For characters with higher val-
+ ues, the concept of case is supported if PCRE is compiled with Unicode
+ property support, but not otherwise. If you want to use caseless
+ matching for characters 128 and above, you must ensure that PCRE is
compiled with Unicode property support as well as with UTF-8 support.
- The power of regular expressions comes from the ability to include
- alternatives and repetitions in the pattern. These are encoded in the
+ The power of regular expressions comes from the ability to include
+ alternatives and repetitions in the pattern. These are encoded in the
pattern by the use of metacharacters, which do not stand for themselves
but instead are interpreted in some special way.
- There are two different sets of metacharacters: those that are recog-
- nized anywhere in the pattern except within square brackets, and those
- that are recognized within square brackets. Outside square brackets,
+ There are two different sets of metacharacters: those that are recog-
+ nized anywhere in the pattern except within square brackets, and those
+ that are recognized within square brackets. Outside square brackets,
the metacharacters are as follows:
\ general escape character with several uses
@@ -2768,7 +2789,7 @@ CHARACTERS AND METACHARACTERS
also "possessive quantifier"
{ start min/max quantifier
- Part of a pattern that is in square brackets is called a "character
+ Part of a pattern that is in square brackets is called a "character
class". In a character class the only metacharacters are:
\ general escape character
@@ -2778,33 +2799,33 @@ CHARACTERS AND METACHARACTERS
syntax)
] terminates the character class
- The following sections describe the use of each of the metacharacters.
+ The following sections describe the use of each of the metacharacters.
BACKSLASH
The backslash character has several uses. Firstly, if it is followed by
- a non-alphanumeric character, it takes away any special meaning that
- character may have. This use of backslash as an escape character
+ a non-alphanumeric character, it takes away any special meaning that
+ character may have. This use of backslash as an escape character
applies both inside and outside character classes.
- For example, if you want to match a * character, you write \* in the
- pattern. This escaping action applies whether or not the following
- character would otherwise be interpreted as a metacharacter, so it is
- always safe to precede a non-alphanumeric with backslash to specify
- that it stands for itself. In particular, if you want to match a back-
+ For example, if you want to match a * character, you write \* in the
+ pattern. This escaping action applies whether or not the following
+ character would otherwise be interpreted as a metacharacter, so it is
+ always safe to precede a non-alphanumeric with backslash to specify
+ that it stands for itself. In particular, if you want to match a back-
slash, you write \\.
- If a pattern is compiled with the PCRE_EXTENDED option, whitespace in
- the pattern (other than in a character class) and characters between a
+ If a pattern is compiled with the PCRE_EXTENDED option, whitespace in
+ the pattern (other than in a character class) and characters between a
# outside a character class and the next newline are ignored. An escap-
- ing backslash can be used to include a whitespace or # character as
+ ing backslash can be used to include a whitespace or # character as
part of the pattern.
- If you want to remove the special meaning from a sequence of charac-
- ters, you can do so by putting them between \Q and \E. This is differ-
- ent from Perl in that $ and @ are handled as literals in \Q...\E
- sequences in PCRE, whereas in Perl, $ and @ cause variable interpola-
+ If you want to remove the special meaning from a sequence of charac-
+ ters, you can do so by putting them between \Q and \E. This is differ-
+ ent from Perl in that $ and @ are handled as literals in \Q...\E
+ sequences in PCRE, whereas in Perl, $ and @ cause variable interpola-
tion. Note the following examples:
Pattern PCRE matches Perl matches
@@ -2814,16 +2835,16 @@ BACKSLASH
\Qabc\$xyz\E abc\$xyz abc\$xyz
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz
- The \Q...\E sequence is recognized both inside and outside character
+ The \Q...\E sequence is recognized both inside and outside character
classes.
Non-printing characters
A second use of backslash provides a way of encoding non-printing char-
- acters in patterns in a visible manner. There is no restriction on the
- appearance of non-printing characters, apart from the binary zero that
- terminates a pattern, but when a pattern is being prepared by text
- editing, it is usually easier to use one of the following escape
+ acters in patterns in a visible manner. There is no restriction on the
+ appearance of non-printing characters, apart from the binary zero that
+ terminates a pattern, but when a pattern is being prepared by text
+ editing, it is usually easier to use one of the following escape
sequences than the binary character it represents:
\a alarm, that is, the BEL character (hex 07)
@@ -2837,45 +2858,45 @@ BACKSLASH
\xhh character with hex code hh
\x{hhh..} character with hex code hhh..
- The precise effect of \cx is as follows: if x is a lower case letter,
- it is converted to upper case. Then bit 6 of the character (hex 40) is
- inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c;
+ The precise effect of \cx is as follows: if x is a lower case letter,
+ it is converted to upper case. Then bit 6 of the character (hex 40) is
+ inverted. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c;
becomes hex 7B.
- After \x, from zero to two hexadecimal digits are read (letters can be
- in upper or lower case). Any number of hexadecimal digits may appear
- between \x{ and }, but the value of the character code must be less
+ After \x, from zero to two hexadecimal digits are read (letters can be
+ in upper or lower case). Any number of hexadecimal digits may appear
+ between \x{ and }, but the value of the character code must be less
than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode (that is,
- the maximum hexadecimal value is 7FFFFFFF). If characters other than
- hexadecimal digits appear between \x{ and }, or if there is no termi-
- nating }, this form of escape is not recognized. Instead, the initial
+ the maximum hexadecimal value is 7FFFFFFF). If characters other than
+ hexadecimal digits appear between \x{ and }, or if there is no termi-
+ nating }, this form of escape is not recognized. Instead, the initial
\x will be interpreted as a basic hexadecimal escape, with no following
digits, giving a character whose value is zero.
Characters whose value is less than 256 can be defined by either of the
- two syntaxes for \x. There is no difference in the way they are han-
+ two syntaxes for \x. There is no difference in the way they are han-
dled. For example, \xdc is exactly the same as \x{dc}.
- After \0 up to two further octal digits are read. If there are fewer
- than two digits, just those that are present are used. Thus the
+ After \0 up to two further octal digits are read. If there are fewer
+ than two digits, just those that are present are used. Thus the
sequence \0\x\07 specifies two binary zeros followed by a BEL character
- (code value 7). Make sure you supply two digits after the initial zero
+ (code value 7). Make sure you supply two digits after the initial zero
if the pattern character that follows is itself an octal digit.
The handling of a backslash followed by a digit other than 0 is compli-
cated. Outside a character class, PCRE reads it and any following dig-
- its as a decimal number. If the number is less than 10, or if there
+ its as a decimal number. If the number is less than 10, or if there
have been at least that many previous capturing left parentheses in the
- expression, the entire sequence is taken as a back reference. A
- description of how this works is given later, following the discussion
+ expression, the entire sequence is taken as a back reference. A
+ description of how this works is given later, following the discussion
of parenthesized subpatterns.
- Inside a character class, or if the decimal number is greater than 9
- and there have not been that many capturing subpatterns, PCRE re-reads
+ Inside a character class, or if the decimal number is greater than 9
+ and there have not been that many capturing subpatterns, PCRE re-reads
up to three octal digits following the backslash, and uses them to gen-
- erate a data character. Any subsequent digits stand for themselves. In
- non-UTF-8 mode, the value of a character specified in octal must be
- less than \400. In UTF-8 mode, values up to \777 are permitted. For
+ erate a data character. Any subsequent digits stand for themselves. In
+ non-UTF-8 mode, the value of a character specified in octal must be
+ less than \400. In UTF-8 mode, values up to \777 are permitted. For
example:
\040 is another way of writing a space
@@ -2893,22 +2914,22 @@ BACKSLASH
\81 is either a back reference, or a binary zero
followed by the two characters "8" and "1"
- Note that octal values of 100 or greater must not be introduced by a
+ Note that octal values of 100 or greater must not be introduced by a
leading zero, because no more than three octal digits are ever read.
All the sequences that define a single character value can be used both
- inside and outside character classes. In addition, inside a character
- class, the sequence \b is interpreted as the backspace character (hex
- 08), and the sequences \R and \X are interpreted as the characters "R"
- and "X", respectively. Outside a character class, these sequences have
+ inside and outside character classes. In addition, inside a character
+ class, the sequence \b is interpreted as the backspace character (hex
+ 08), and the sequences \R and \X are interpreted as the characters "R"
+ and "X", respectively. Outside a character class, these sequences have
different meanings (see below).
Absolute and relative back references
- The sequence \g followed by a positive or negative number, optionally
- enclosed in braces, is an absolute or relative back reference. Back
- references are discussed later, following the discussion of parenthe-
- sized subpatterns.
+ The sequence \g followed by a positive or negative number, optionally
+ enclosed in braces, is an absolute or relative back reference. A named
+ back reference can be coded as \g{name}. Back references are discussed
+ later, following the discussion of parenthesized subpatterns.
Generic character types
@@ -2923,51 +2944,51 @@ BACKSLASH
\W any "non-word" character
Each pair of escape sequences partitions the complete set of characters
- into two disjoint sets. Any given character matches one, and only one,
+ into two disjoint sets. Any given character matches one, and only one,
of each pair.
These character type sequences can appear both inside and outside char-
- acter classes. They each match one character of the appropriate type.
- If the current matching point is at the end of the subject string, all
+ acter classes. They each match one character of the appropriate type.
+ If the current matching point is at the end of the subject string, all
of them fail, since there is no character to match.
- For compatibility with Perl, \s does not match the VT character (code
- 11). This makes it different from the the POSIX "space" class. The \s
- characters are HT (9), LF (10), FF (12), CR (13), and space (32). (If
+ For compatibility with Perl, \s does not match the VT character (code
+ 11). This makes it different from the the POSIX "space" class. The \s
+ characters are HT (9), LF (10), FF (12), CR (13), and space (32). (If
"use locale;" is included in a Perl script, \s may match the VT charac-
ter. In PCRE, it never does.)
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
+ 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.
- In UTF-8 mode, characters with values greater than 128 never match \d,
+ In UTF-8 mode, characters with values greater than 128 never match \d,
\s, or \w, and always match \D, \S, and \W. This is true even when Uni-
- code character property support is available. The use of locales with
+ code character property support is available. The use of locales with
Unicode is discouraged.
Newline sequences
- Outside a character class, the escape sequence \R matches any Unicode
+ Outside a character class, the escape sequence \R matches any Unicode
newline sequence. This is an extension to Perl. In non-UTF-8 mode \R is
equivalent to the following:
(?>\r\n|\n|\x0b|\f|\r|\x85)
- This is an example of an "atomic group", details of which are given
+ This is an example of an "atomic group", details of which are given
below. This particular group matches either the two-character sequence
- CR followed by LF, or one of the single characters LF (linefeed,
+ CR followed by LF, or one of the single characters LF (linefeed,
U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage
return, U+000D), or NEL (next line, U+0085). The two-character sequence
is treated as a single unit that cannot be split.
- In UTF-8 mode, two additional characters whose codepoints are greater
+ In UTF-8 mode, two additional characters whose codepoints are greater
than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-
- rator, U+2029). Unicode character property support is not needed for
+ rator, U+2029). Unicode character property support is not needed for
these characters to be recognized.
Inside a character class, \R matches the letter "R".
@@ -2975,47 +2996,47 @@ BACKSLASH
Unicode character properties
When PCRE is built with Unicode character property support, three addi-
- tional escape sequences to match character properties are available
+ tional escape sequences to match character properties are available
when UTF-8 mode is selected. They are:
\p{xx} a character with the xx property
\P{xx} a character without the xx property
\X an extended Unicode sequence
- The property names represented by xx above are limited to the Unicode
+ The property names represented by xx above are limited to the Unicode
script names, the general category properties, and "Any", which matches
any character (including newline). Other properties such as "InMusical-
- Symbols" are not currently supported by PCRE. Note that \P{Any} does
+ Symbols" are not currently supported by PCRE. Note that \P{Any} does
not match any characters, so always causes a match failure.
Sets of Unicode characters are defined as belonging to certain scripts.
- 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.
For example:
\p{Greek}
\P{Han}
- 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
"Common". The current list of scripts is:
Arabic, Armenian, Balinese, Bengali, Bopomofo, Braille, Buginese,
- Buhid, Canadian_Aboriginal, Cherokee, Common, Coptic, Cuneiform,
+ Buhid, Canadian_Aboriginal, Cherokee, Common, Coptic, Cuneiform,
Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,
- Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira-
- gana, Inherited, Kannada, Katakana, Kharoshthi, Khmer, Lao, Latin,
+ Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira-
+ gana, Inherited, Kannada, Katakana, Kharoshthi, Khmer, Lao, Latin,
Limbu, Linear_B, Malayalam, Mongolian, Myanmar, New_Tai_Lue, Nko,
- Ogham, Old_Italic, Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,
+ Ogham, Old_Italic, Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,
Runic, Shavian, Sinhala, Syloti_Nagri, Syriac, Tagalog, Tagbanwa,
Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.
- Each character has exactly one general category property, specified by
+ Each character has exactly one general category property, specified by
a two-letter abbreviation. For compatibility with Perl, negation can be
- specified by including a circumflex between the opening brace and the
+ specified by including a circumflex between the opening brace and the
property name. For example, \p{^Lu} is the same as \P{Lu}.
If only one letter is specified with \p or \P, it includes all the gen-
- eral category properties that start with that letter. In this case, in
- the absence of negation, the curly brackets in the escape sequence are
+ eral category properties that start with that letter. In this case, in
+ the absence of negation, the curly brackets in the escape sequence are
optional; these two examples have the same effect:
\p{L}
@@ -3067,36 +3088,55 @@ BACKSLASH
Zp Paragraph separator
Zs Space separator
- The special property L& is also supported: it matches a character that
- has the Lu, Ll, or Lt property, in other words, a letter that is not
+ The special property L& is also supported: it matches a character that
+ has the Lu, Ll, or Lt property, in other words, a letter that is not
classified as a modifier or "other".
- The long synonyms for these properties that Perl supports (such as
- \p{Letter}) are not supported by PCRE, nor is it permitted to prefix
+ The long synonyms for these properties that Perl supports (such as
+ \p{Letter}) are not supported by PCRE, nor is it permitted to prefix
any of these properties with "Is".
No character that is in the Unicode table has the Cn (unassigned) prop-
erty. Instead, this property is assumed for any code point that is not
in the Unicode table.
- Specifying caseless matching does not affect these escape sequences.
+ Specifying caseless matching does not affect these escape sequences.
For example, \p{Lu} always matches only upper case letters.
- The \X escape matches any number of Unicode characters that form an
+ The \X escape matches any number of Unicode characters that form an
extended Unicode sequence. \X is equivalent to
(?>\PM\pM*)
- That is, it matches a character without the "mark" property, followed
- by zero or more characters with the "mark" property, and treats the
- sequence as an atomic group (see below). Characters with the "mark"
+ That is, it matches a character without the "mark" property, followed
+ by zero or more characters with the "mark" property, and treats the
+ sequence as an atomic group (see below). Characters with the "mark"
property are typically accents that affect the preceding character.
- Matching characters by Unicode property is not fast, because PCRE has
- to search a structure that contains data for over fifteen thousand
+ Matching characters by Unicode property is not fast, because PCRE has
+ to search a structure that contains data for over fifteen thousand
characters. That is why the traditional escape sequences such as \d and
\w do not use Unicode properties in PCRE.
+ Resetting the match start
+
+ The escape sequence \K, which is a Perl 5.10 feature, causes any previ-
+ ously matched characters not to be included in the final matched
+ sequence. For example, the pattern:
+
+ foo\Kbar
+
+ matches "foobar", but reports that it has matched "bar". This feature
+ is similar to a lookbehind assertion (described below). However, in
+ this case, the part of the subject before the real match does not have
+ to be of fixed length, as lookbehind assertions do. The use of \K does
+ not interfere with the setting of captured substrings. For example,
+ when the pattern
+
+ (foo)\Kbar
+
+ matches "foobar", the first substring is still set to "foo".
+
Simple assertions
The final use of backslash is for certain simple assertions. An asser-
@@ -3858,64 +3898,69 @@ BACK REFERENCES
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the
original capturing subpattern is matched caselessly.
- Back references to named subpatterns use the Perl syntax \k<name> or
- \k'name' or the Python syntax (?P=name). We could rewrite the above
- example in either of the following ways:
+ There are several different ways of writing back references to named
+ subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or
+ \k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's
+ unified back reference syntax, in which \g can be used for both numeric
+ and named references, is also supported. We could rewrite the above
+ example in any of the following ways:
(?<p1>(?i)rah)\s+\k<p1>
+ (?'p1'(?i)rah)\s+\k{p1}
(?P<p1>(?i)rah)\s+(?P=p1)
+ (?<p1>(?i)rah)\s+\g{p1}
- A subpattern that is referenced by name may appear in the pattern
+ A subpattern that is referenced by name may appear in the pattern
before or after the reference.
- There may be more than one back reference to the same subpattern. If a
- subpattern has not actually been used in a particular match, any back
+ There may be more than one back reference to the same subpattern. If a
+ subpattern has not actually been used in a particular match, any back
references to it always fail. For example, the pattern
(a|(bc))\2
- always fails if it starts to match "a" rather than "bc". Because there
- may be many capturing parentheses in a pattern, all digits following
- the backslash are taken as part of a potential back reference number.
+ always fails if it starts to match "a" rather than "bc". Because there
+ may be many capturing parentheses in a pattern, all digits following
+ the backslash are taken as part of a potential back reference number.
If the pattern continues with a digit character, some delimiter must be
- used to terminate the back reference. If the PCRE_EXTENDED option is
- set, this can be whitespace. Otherwise an empty comment (see "Com-
+ used to terminate the back reference. If the PCRE_EXTENDED option is
+ set, this can be whitespace. Otherwise an empty comment (see "Com-
ments" below) can be used.
- A back reference that occurs inside the parentheses to which it refers
- fails when the subpattern is first used, so, for example, (a\1) never
- matches. However, such references can be useful inside repeated sub-
+ A back reference that occurs inside the parentheses to which it refers
+ fails when the subpattern is first used, so, for example, (a\1) never
+ matches. However, such references can be useful inside repeated sub-
patterns. For example, the pattern
(a|b\1)+
matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
- ation of the subpattern, the back reference matches the character
- string corresponding to the previous iteration. In order for this to
- work, the pattern must be such that the first iteration does not need
- to match the back reference. This can be done using alternation, as in
+ ation of the subpattern, the back reference matches the character
+ string corresponding to the previous iteration. In order for this to
+ work, the pattern must be such that the first iteration does not need
+ to match the back reference. This can be done using alternation, as in
the example above, or by a quantifier with a minimum of zero.
ASSERTIONS
- An assertion is a test on the characters following or preceding the
- current matching point that does not actually consume any characters.
- The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are
+ An assertion is a test on the characters following or preceding the
+ current matching point that does not actually consume any characters.
+ The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are
described above.
- More complicated assertions are coded as subpatterns. There are two
- kinds: those that look ahead of the current position in the subject
- string, and those that look behind it. An assertion subpattern is
- matched in the normal way, except that it does not cause the current
+ More complicated assertions are coded as subpatterns. There are two
+ kinds: those that look ahead of the current position in the subject
+ string, and those that look behind it. An assertion subpattern is
+ matched in the normal way, except that it does not cause the current
matching position to be changed.
- Assertion subpatterns are not capturing subpatterns, and may not be
- repeated, because it makes no sense to assert the same thing several
- times. If any kind of assertion contains capturing subpatterns within
- it, these are counted for the purposes of numbering the capturing sub-
+ Assertion subpatterns are not capturing subpatterns, and may not be
+ repeated, because it makes no sense to assert the same thing several
+ times. If any kind of assertion contains capturing subpatterns within
+ it, these are counted for the purposes of numbering the capturing sub-
patterns in the whole pattern. However, substring capturing is carried
- out only for positive assertions, because it does not make sense for
+ out only for positive assertions, because it does not make sense for
negative assertions.
Lookahead assertions
@@ -3925,37 +3970,37 @@ ASSERTIONS
\w+(?=;)
- matches a word followed by a semicolon, but does not include the semi-
+ matches a word followed by a semicolon, but does not include the semi-
colon in the match, and
foo(?!bar)
- matches any occurrence of "foo" that is not followed by "bar". Note
+ matches any occurrence of "foo" that is not followed by "bar". Note
that the apparently similar pattern
(?!foo)bar
- does not find an occurrence of "bar" that is preceded by something
- other than "foo"; it finds any occurrence of "bar" whatsoever, because
+ does not find an occurrence of "bar" that is preceded by something
+ other than "foo"; it finds any occurrence of "bar" whatsoever, because
the assertion (?!foo) is always true when the next three characters are
"bar". A lookbehind assertion is needed to achieve the other effect.
If you want to force a matching failure at some point in a pattern, the
- most convenient way to do it is with (?!) because an empty string
- always matches, so an assertion that requires there not to be an empty
+ most convenient way to do it is with (?!) because an empty string
+ always matches, so an assertion that requires there not to be an empty
string must always fail.
Lookbehind assertions
- Lookbehind assertions start with (?<= for positive assertions and (?<!
+ Lookbehind assertions start with (?<= for positive assertions and (?<!
for negative assertions. For example,
(?<!foo)bar
- does find an occurrence of "bar" that is not preceded by "foo". The
- contents of a lookbehind assertion are restricted such that all the
+ does find an occurrence of "bar" that is not preceded by "foo". The
+ contents of a lookbehind assertion are restricted such that all the
strings it matches must have a fixed length. However, if there are sev-
- eral top-level alternatives, they do not all have to have the same
+ eral top-level alternatives, they do not all have to have the same
fixed length. Thus
(?<=bullock|donkey)
@@ -3964,55 +4009,59 @@ ASSERTIONS
(?<!dogs?|cats?)
- causes an error at compile time. Branches that match different length
- strings are permitted only at the top level of a lookbehind assertion.
- This is an extension compared with Perl (at least for 5.8), which
- requires all branches to match the same length of string. An assertion
+ causes an error at compile time. Branches that match different length
+ strings are permitted only at the top level of a lookbehind assertion.
+ This is an extension compared with Perl (at least for 5.8), which
+ requires all branches to match the same length of string. An assertion
such as
(?<=ab(c|de))
- is not permitted, because its single top-level branch can match two
- different lengths, but it is acceptable if rewritten to use two top-
+ is not permitted, because its single top-level branch can match two
+ different lengths, but it is acceptable if rewritten to use two top-
level branches:
(?<=abc|abde)
- The implementation of lookbehind assertions is, for each alternative,
- to temporarily move the current position back by the fixed length and
+ In some cases, the Perl 5.10 escape sequence \K (see above) can be used
+ instead of a lookbehind assertion; this is not restricted to a fixed-
+ length.
+
+ The implementation of lookbehind assertions is, for each alternative,
+ to temporarily move the current position back by the fixed length and
then try to match. If there are insufficient characters before the cur-
rent position, the assertion fails.
PCRE does not allow the \C escape (which matches a single byte in UTF-8
- mode) to appear in lookbehind assertions, because it makes it impossi-
- ble to calculate the length of the lookbehind. The \X and \R escapes,
+ mode) to appear in lookbehind assertions, because it makes it impossi-
+ ble to calculate the length of the lookbehind. The \X and \R escapes,
which can match different numbers of bytes, are also not permitted.
- Possessive quantifiers can be used in conjunction with lookbehind
- assertions to specify efficient matching at the end of the subject
+ Possessive quantifiers can be used in conjunction with lookbehind
+ assertions to specify efficient matching at the end of the subject
string. Consider a simple pattern such as
abcd$
- when applied to a long string that does not match. Because matching
+ when applied to a long string that does not match. Because matching
proceeds from left to right, PCRE will look for each "a" in the subject
- and then see if what follows matches the rest of the pattern. If the
+ and then see if what follows matches the rest of the pattern. If the
pattern is specified as
^.*abcd$
- the initial .* matches the entire string at first, but when this fails
+ the initial .* matches the entire string at first, but when this fails
(because there is no following "a"), it backtracks to match all but the
- last character, then all but the last two characters, and so on. Once
- again the search for "a" covers the entire string, from right to left,
+ last character, then all but the last two characters, and so on. Once
+ again the search for "a" covers the entire string, from right to left,
so we are no better off. However, if the pattern is written as
^.*+(?<=abcd)
- there can be no backtracking for the .*+ item; it can match only the
- entire string. The subsequent lookbehind assertion does a single test
- on the last four characters. If it fails, the match fails immediately.
- For long strings, this approach makes a significant difference to the
+ there can be no backtracking for the .*+ item; it can match only the
+ entire string. The subsequent lookbehind assertion does a single test
+ on the last four characters. If it fails, the match fails immediately.
+ For long strings, this approach makes a significant difference to the
processing time.
Using multiple assertions
@@ -4021,18 +4070,18 @@ ASSERTIONS
(?<=\d{3})(?<!999)foo
- matches "foo" preceded by three digits that are not "999". Notice that
- each of the assertions is applied independently at the same point in
- the subject string. First there is a check that the previous three
- characters are all digits, and then there is a check that the same
+ matches "foo" preceded by three digits that are not "999". Notice that
+ each of the assertions is applied independently at the same point in
+ the subject string. First there is a check that the previous three
+ characters are all digits, and then there is a check that the same
three characters are not "999". This pattern does not match "foo" pre-
- ceded by six characters, the first of which are digits and the last
- three of which are not "999". For example, it doesn't match "123abc-
+ ceded by six characters, the first of which are digits and the last
+ three of which are not "999". For example, it doesn't match "123abc-
foo". A pattern to do that is
(?<=\d{3}...)(?<!999)foo
- This time the first assertion looks at the preceding six characters,
+ This time the first assertion looks at the preceding six characters,
checking that the first three are digits, and then the second assertion
checks that the preceding three characters are not "999".
@@ -4040,38 +4089,43 @@ ASSERTIONS
(?<=(?<!foo)bar)baz
- matches an occurrence of "baz" that is preceded by "bar" which in turn
+ matches an occurrence of "baz" that is preceded by "bar" which in turn
is not preceded by "foo", while
(?<=\d{3}(?!999)...)foo
- is another pattern that matches "foo" preceded by three digits and any
+ is another pattern that matches "foo" preceded by three digits and any
three characters that are not "999".
CONDITIONAL SUBPATTERNS
- It is possible to cause the matching process to obey a subpattern con-
- ditionally or to choose between two alternative subpatterns, depending
- on the result of an assertion, or whether a previous capturing subpat-
- tern matched or not. The two possible forms of conditional subpattern
+ It is possible to cause the matching process to obey a subpattern con-
+ ditionally or to choose between two alternative subpatterns, depending
+ on the result of an assertion, or whether a previous capturing subpat-
+ tern matched or not. The two possible forms of conditional subpattern
are
(?(condition)yes-pattern)
(?(condition)yes-pattern|no-pattern)
- If the condition is satisfied, the yes-pattern is used; otherwise the
- no-pattern (if present) is used. If there are more than two alterna-
+ If the condition is satisfied, the yes-pattern is used; otherwise the
+ no-pattern (if present) is used. If there are more than two alterna-
tives in the subpattern, a compile-time error occurs.
- There are four kinds of condition: references to subpatterns, refer-
+ There are four kinds of condition: references to subpatterns, refer-
ences to recursion, a pseudo-condition called DEFINE, and assertions.
Checking for a used subpattern by number
- If the text between the parentheses consists of a sequence of digits,
- the condition is true if the capturing subpattern of that number has
- previously matched.
+ If the text between the parentheses consists of a sequence of digits,
+ the condition is true if the capturing subpattern of that number has
+ previously matched. An alternative notation is to precede the digits
+ with a plus or minus sign. In this case, the subpattern number is rela-
+ tive rather than absolute. The most recently opened parentheses can be
+ referenced by (?(-1), the next most recent by (?(-2), and so on. In
+ looping constructs it can also make sense to refer to subsequent groups
+ with constructs such as (?(+2).
Consider the following pattern, which contains non-significant white
space to make it more readable (assume the PCRE_EXTENDED option) and to
@@ -4090,6 +4144,14 @@ CONDITIONAL SUBPATTERNS
other words, this pattern matches a sequence of non-parentheses,
optionally enclosed in parentheses.
+ If you were embedding this pattern in a larger one, you could use a
+ relative reference:
+
+ ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ...
+
+ This makes the fragment independent of the parentheses in the larger
+ pattern.
+
Checking for a used subpattern by name
Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a
@@ -4231,19 +4293,35 @@ RECURSIVE PATTERNS
( \( ( (?>[^()]+) | (?1) )* \) )
We have put the pattern into parentheses, and caused the recursion to
- refer to them instead of the whole pattern. In a larger pattern, keep-
- ing track of parenthesis numbers can be tricky. It may be more conve-
- nient to use named parentheses instead. The Perl syntax for this is
- (?&name); PCRE's earlier syntax (?P>name) is also supported. We could
- rewrite the above example as follows:
+ refer to them instead of the whole pattern.
+
+ In a larger pattern, keeping track of parenthesis numbers can be
+ tricky. This is made easier by the use of relative references. (A Perl
+ 5.10 feature.) Instead of (?1) in the pattern above you can write
+ (?-2) to refer to the second most recently opened parentheses preceding
+ the recursion. In other words, a negative number counts capturing
+ parentheses leftwards from the point at which it is encountered.
+
+ It is also possible to refer to subsequently opened parentheses, by
+ writing references such as (?+2). However, these cannot be recursive
+ because the reference is not inside the parentheses that are refer-
+ enced. They are always "subroutine" calls, as described in the next
+ section.
+
+ An alternative approach is to use named parentheses instead. The Perl
+ syntax for this is (?&name); PCRE's earlier syntax (?P>name) is also
+ supported. We could rewrite the above example as follows:
(?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )
- If there is more than one subpattern with the same name, the earliest
- one is used. This particular example pattern contains nested unlimited
- repeats, and so the use of atomic grouping for matching strings of non-
- parentheses is important when applying the pattern to strings that do
- not match. For example, when this pattern is applied to
+ If there is more than one subpattern with the same name, the earliest
+ one is used.
+
+ This particular example pattern that we have been looking at contains
+ nested unlimited repeats, and so the use of atomic grouping for match-
+ ing strings of non-parentheses is important when applying the pattern
+ to strings that do not match. For example, when this pattern is applied
+ to
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
@@ -4293,8 +4371,14 @@ SUBPATTERNS AS SUBROUTINES
If the syntax for a recursive subpattern reference (either by number or
by name) is used outside the parentheses to which it refers, it oper-
ates like a subroutine in a programming language. The "called" subpat-
- tern may be defined before or after the reference. An earlier example
- pointed out that the pattern
+ tern may be defined before or after the reference. A numbered reference
+ can be absolute or relative, as in these examples:
+
+ (...(absolute)...)...(?2)...
+ (...(relative)...)...(?-1)...
+ (...(?+1)...(relative)...
+
+ An earlier example pointed out that the pattern
(sens|respons)e and \1ibility
@@ -4316,7 +4400,7 @@ SUBPATTERNS AS SUBROUTINES
case-independence are fixed when the subpattern is defined. They cannot
be changed for different calls. For example, consider this pattern:
- (abc)(?i:(?1))
+ (abc)(?i:(?-1))
It matches "abcabc". It does not match "abcABC" because the change of
processing option does not affect the called subpattern.
@@ -4371,7 +4455,7 @@ AUTHOR
REVISION
- Last updated: 06 March 2007
+ Last updated: 29 May 2007
Copyright (c) 1997-2007 University of Cambridge.
------------------------------------------------------------------------------
@@ -4452,12 +4536,13 @@ RESTRICTED PATTERNS FOR PCRE_PARTIAL
If PCRE_PARTIAL is set for a pattern that does not conform to the
restrictions, pcre_exec() returns the error code PCRE_ERROR_BADPARTIAL
- (-13).
+ (-13). You can use the PCRE_INFO_OKPARTIAL call to pcre_fullinfo() to
+ find out if a compiled pattern can be used for partial matching.
EXAMPLE OF PARTIAL MATCHING USING PCRETEST
- If the escape sequence \P is present in a pcretest data line, the
+ If the escape sequence \P is present in a pcretest data line, the
PCRE_PARTIAL flag is used for the match. Here is a run of pcretest that
uses the date example quoted above:
@@ -4474,10 +4559,10 @@ EXAMPLE OF PARTIAL MATCHING USING PCRETEST
data> j\P
No match
- The first data string is matched completely, so pcretest shows the
- matched substrings. The remaining four strings do not match the com-
- plete pattern, but the first two are partial matches. The same test,
- using pcre_dfa_exec() matching (by means of the \D escape sequence),
+ The first data string is matched completely, so pcretest shows the
+ matched substrings. The remaining four strings do not match the com-
+ plete pattern, but the first two are partial matches. The same test,
+ using pcre_dfa_exec() matching (by means of the \D escape sequence),
produces the following output:
re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
@@ -4492,18 +4577,18 @@ EXAMPLE OF PARTIAL MATCHING USING PCRETEST
data> j\P\D
No match
- Notice that in this case the portion of the string that was matched is
+ Notice that in this case the portion of the string that was matched is
made available.
MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()
When a partial match has been found using pcre_dfa_exec(), it is possi-
- ble to continue the match by providing additional subject data and
- calling pcre_dfa_exec() again with the same compiled regular expres-
+ ble to continue the match by providing additional subject data and
+ calling pcre_dfa_exec() again with the same compiled regular expres-
sion, this time setting the PCRE_DFA_RESTART option. You must also pass
- the same working space as before, because this is where details of the
- previous partial match are stored. Here is an example using pcretest,
+ the same working space as before, because this is where details of the
+ previous partial match are stored. Here is an example using pcretest,
using the \R escape sequence to set the PCRE_DFA_RESTART option (\P and
\D are as above):
@@ -4513,35 +4598,35 @@ MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()
data> n05\R\D
0: n05
- The first call has "23ja" as the subject, and requests partial match-
- ing; the second call has "n05" as the subject for the continued
- (restarted) match. Notice that when the match is complete, only the
- last part is shown; PCRE does not retain the previously partially-
- matched string. It is up to the calling program to do that if it needs
+ The first call has "23ja" as the subject, and requests partial match-
+ ing; the second call has "n05" as the subject for the continued
+ (restarted) match. Notice that when the match is complete, only the
+ last part is shown; PCRE does not retain the previously partially-
+ matched string. It is up to the calling program to do that if it needs
to.
- You can set PCRE_PARTIAL with PCRE_DFA_RESTART to continue partial
+ You can set PCRE_PARTIAL with PCRE_DFA_RESTART to continue partial
matching over multiple segments. This facility can be used to pass very
- long subject strings to pcre_dfa_exec(). However, some care is needed
+ long subject strings to pcre_dfa_exec(). However, some care is needed
for certain types of pattern.
- 1. If the pattern contains tests for the beginning or end of a line,
- you need to pass the PCRE_NOTBOL or PCRE_NOTEOL options, as appropri-
- ate, when the subject string for any call does not contain the begin-
+ 1. If the pattern contains tests for the beginning or end of a line,
+ you need to pass the PCRE_NOTBOL or PCRE_NOTEOL options, as appropri-
+ ate, when the subject string for any call does not contain the begin-
ning or end of a line.
- 2. If the pattern contains backward assertions (including \b or \B),
- you need to arrange for some overlap in the subject strings to allow
- for this. For example, you could pass the subject in chunks that are
- 500 bytes long, but in a buffer of 700 bytes, with the starting offset
+ 2. If the pattern contains backward assertions (including \b or \B),
+ you need to arrange for some overlap in the subject strings to allow
+ for this. For example, you could pass the subject in chunks that are
+ 500 bytes long, but in a buffer of 700 bytes, with the starting offset
set to 200 and the previous 200 bytes at the start of the buffer.
- 3. Matching a subject string that is split into multiple segments does
- not always produce exactly the same result as matching over one single
- long string. The difference arises when there are multiple matching
- possibilities, because a partial match result is given only when there
- are no completed matches in a call to pcre_dfa_exec(). This means that
- as soon as the shortest match has been found, continuation to a new
+ 3. Matching a subject string that is split into multiple segments does
+ not always produce exactly the same result as matching over one single
+ long string. The difference arises when there are multiple matching
+ possibilities, because a partial match result is given only when there
+ are no completed matches in a call to pcre_dfa_exec(). This means that
+ as soon as the shortest match has been found, continuation to a new
subject segment is no longer possible. Consider this pcretest example:
re> /dog(sbody)?/
@@ -4553,13 +4638,13 @@ MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()
0: dogsbody
1: dog
- The pattern matches the words "dog" or "dogsbody". When the subject is
- presented in several parts ("do" and "gsb" being the first two) the
- match stops when "dog" has been found, and it is not possible to con-
- tinue. On the other hand, if "dogsbody" is presented as a single
+ The pattern matches the words "dog" or "dogsbody". When the subject is
+ presented in several parts ("do" and "gsb" being the first two) the
+ match stops when "dog" has been found, and it is not possible to con-
+ tinue. On the other hand, if "dogsbody" is presented as a single
string, both matches are found.
- Because of this phenomenon, it does not usually make sense to end a
+ Because of this phenomenon, it does not usually make sense to end a
pattern that is going to be matched in this way with a variable repeat.
4. Patterns that contain alternatives at the top level which do not all
@@ -4568,13 +4653,13 @@ MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()
1234|3789
- If the first part of the subject is "ABC123", a partial match of the
- first alternative is found at offset 3. There is no partial match for
+ If the first part of the subject is "ABC123", a partial match of the
+ first alternative is found at offset 3. There is no partial match for
the second alternative, because such a match does not start at the same
- point in the subject string. Attempting to continue with the string
+ point in the subject string. Attempting to continue with the string
"789" does not yield a match because only those alternatives that match
- at one point in the subject are remembered. The problem arises because
- the start of the second alternative matches within the first alterna-
+ at one point in the subject are remembered. The problem arises because
+ the start of the second alternative matches within the first alterna-
tive. There is no problem with anchored patterns or patterns such as:
1234|ABCD
@@ -4591,7 +4676,7 @@ AUTHOR
REVISION
- Last updated: 06 March 2007
+ Last updated: 04 June 2007
Copyright (c) 1997-2007 University of Cambridge.
------------------------------------------------------------------------------
diff --git a/doc/pcreapi.3 b/doc/pcreapi.3
index 94684df..f36e9af 100644
--- a/doc/pcreapi.3
+++ b/doc/pcreapi.3
@@ -873,7 +873,7 @@ fourth argument should point to an \fBunsigned char *\fP variable.
.sp
PCRE_INFO_JCHANGED
.sp
-Return 1 if the (?J) option setting is used in the pattern, otherwise 0. The
+Return 1 if the (?J) option setting is used in the pattern, otherwise 0. The
fourth argument should point to an \fBint\fP variable. The (?J) internal option
setting changes the local PCRE_DUPNAMES value.
.sp
@@ -931,7 +931,7 @@ different for each compiled pattern.
.sp
PCRE_INFO_OKPARTIAL
.sp
-Return 1 if the pattern can be used for partial matching, otherwise 0. The
+Return 1 if the pattern can be used for partial matching, otherwise 0. The
fourth argument should point to an \fBint\fP variable. The
.\" HREF
\fBpcrepartial\fP
diff --git a/doc/pcrepartial.3 b/doc/pcrepartial.3
index 6b88b7f..e418734 100644
--- a/doc/pcrepartial.3
+++ b/doc/pcrepartial.3
@@ -71,7 +71,7 @@ envisaged for this facility, this is not felt to be a major restriction.
.P
If PCRE_PARTIAL is set for a pattern that does not conform to the restrictions,
\fBpcre_exec()\fP returns the error code PCRE_ERROR_BADPARTIAL (-13).
-You can use the PCRE_INFO_OKPARTIAL call to \fBpcre_fullinfo()\fP to find out
+You can use the PCRE_INFO_OKPARTIAL call to \fBpcre_fullinfo()\fP to find out
if a compiled pattern can be used for partial matching.
.
.
diff --git a/doc/pcrepattern.3 b/doc/pcrepattern.3
index 4d8b943..6dcd161 100644
--- a/doc/pcrepattern.3
+++ b/doc/pcrepattern.3
@@ -30,7 +30,7 @@ The remainder of this document discusses the patterns that are supported by
PCRE when its main matching function, \fBpcre_exec()\fP, is used.
From release 6.0, PCRE offers a second matching function,
\fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not
-Perl-compatible. Some of the features discussed below are not available when
+Perl-compatible. Some of the features discussed below are not available when
\fBpcre_dfa_exec()\fP is used. The advantages and disadvantages of the
alternative function, and how it differs from the normal function, are
discussed in the
@@ -241,7 +241,7 @@ meanings
.rs
.sp
The sequence \eg followed by a positive or negative number, optionally enclosed
-in braces, is an absolute or relative back reference. A named back reference
+in braces, is an absolute or relative back reference. A named back reference
can be coded as \eg{name}. Back references are discussed
.\" HTML <a href="#backreferences">
.\" </a>
@@ -525,30 +525,30 @@ properties in PCRE.
.SS "Resetting the match start"
.rs
.sp
-The escape sequence \eK, which is a Perl 5.10 feature, causes any previously
-matched characters not to be included in the final matched sequence. For
+The escape sequence \eK, which is a Perl 5.10 feature, causes any previously
+matched characters not to be included in the final matched sequence. For
example, the pattern:
.sp
foo\eKbar
.sp
-matches "foobar", but reports that it has matched "bar". This feature is
+matches "foobar", but reports that it has matched "bar". This feature is
similar to a lookbehind assertion
.\" HTML <a href="#lookbehind">
.\" </a>
(described below).
.\"
-However, in this case, the part of the subject before the real match does not
-have to be of fixed length, as lookbehind assertions do. The use of \eK does
+However, in this case, the part of the subject before the real match does not
+have to be of fixed length, as lookbehind assertions do. The use of \eK does
not interfere with the setting of
.\" HTML <a href="#subpattern">
.\" </a>
captured substrings.
-.\"
+.\"
For example, when the pattern
.sp
(foo)\eKbar
.sp
-matches "foobar", the first substring is still set to "foo".
+matches "foobar", the first substring is still set to "foo".
.
.
.\" HTML <a name="smallassertions"></a>
@@ -1458,7 +1458,7 @@ lengths, but it is acceptable if rewritten to use two top-level branches:
.sp
(?<=abc|abde)
.sp
-In some cases, the Perl 5.10 escape sequence \eK
+In some cases, the Perl 5.10 escape sequence \eK
.\" HTML <a href="#resetmatchstart">
.\" </a>
(see above)
@@ -1560,9 +1560,9 @@ recursion, a pseudo-condition called DEFINE, and assertions.
.sp
If the text between the parentheses consists of a sequence of digits, the
condition is true if the capturing subpattern of that number has previously
-matched. An alternative notation is to precede the digits with a plus or minus
+matched. An alternative notation is to precede the digits with a plus or minus
sign. In this case, the subpattern number is relative rather than absolute.
-The most recently opened parentheses can be referenced by (?(-1), the next most
+The most recently opened parentheses can be referenced by (?(-1), the next most
recent by (?(-2), and so on. In looping constructs it can also make sense to
refer to subsequent groups with constructs such as (?(+2).
.P
@@ -1582,7 +1582,7 @@ parenthesis is required. Otherwise, since no-pattern is not present, the
subpattern matches nothing. In other words, this pattern matches a sequence of
non-parentheses, optionally enclosed in parentheses.
.P
-If you were embedding this pattern in a larger one, you could use a relative
+If you were embedding this pattern in a larger one, you could use a relative
reference:
.sp
...other stuff... ( \e( )? [^()]+ (?(-1) \e) ) ...
@@ -1730,7 +1730,7 @@ pattern, so instead you could use this:
( \e( ( (?>[^()]+) | (?1) )* \e) )
.sp
We have put the pattern into parentheses, and caused the recursion to refer to
-them instead of the whole pattern.
+them instead of the whole pattern.
.P
In a larger pattern, keeping track of parenthesis numbers can be tricky. This
is made easier by the use of relative references. (A Perl 5.10 feature.)
@@ -1751,7 +1751,7 @@ could rewrite the above example as follows:
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) )
.sp
If there is more than one subpattern with the same name, the earliest one is
-used.
+used.
.P
This particular example pattern that we have been looking at contains nested
unlimited repeats, and so the use of atomic grouping for matching strings of
@@ -1813,7 +1813,7 @@ relative, as in these examples:
.sp
(...(absolute)...)...(?2)...
(...(relative)...)...(?-1)...
- (...(?+1)...(relative)...
+ (...(?+1)...(relative)...
.sp
An earlier example pointed out that the pattern
.sp
diff --git a/pcre_compile.c b/pcre_compile.c
index 3fd5432..92b52df 100644
--- a/pcre_compile.c
+++ b/pcre_compile.c
@@ -243,7 +243,7 @@ static const char *error_texts[] = {
"repeating a DEFINE group is not allowed",
"inconsistent NEWLINE options",
"\\g is not followed by a braced name or an optionally braced non-zero number",
- "(?+ or (?- or (?(+ or (?(- must be followed by a non-zero number"
+ "(?+ or (?- or (?(+ or (?(- must be followed by a non-zero number"
};
@@ -453,9 +453,9 @@ else
/* \g must be followed by a number, either plain or braced. If positive, it
is an absolute backreference. If negative, it is a relative backreference.
- This is a Perl 5.10 feature. Perl 5.10 also supports \g{name} as a
- reference to a named group. This is part of Perl's movement towards a
- unified syntax for back references. As this is synonymous with \k{name}, we
+ This is a Perl 5.10 feature. Perl 5.10 also supports \g{name} as a
+ reference to a named group. This is part of Perl's movement towards a
+ unified syntax for back references. As this is synonymous with \k{name}, we
fudge it up by pretending it really was \k. */
case 'g':
@@ -464,11 +464,11 @@ else
const uschar *p;
for (p = ptr+2; *p != 0 && *p != '}'; p++)
if (*p != '-' && (digitab[*p] & ctype_digit) == 0) break;
- if (*p != 0 && *p != '}')
+ if (*p != 0 && *p != '}')
{
c = -ESC_k;
break;
- }
+ }
braced = TRUE;
ptr++;
}
@@ -1381,18 +1381,19 @@ for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE
const uschar *ccode;
c = *code;
-
+
/* Groups with zero repeats can of course be empty; skip them. */
if (c == OP_BRAZERO || c == OP_BRAMINZERO)
{
+ code += _pcre_OP_lengths[c];
do code += GET(code, 1); while (*code == OP_ALT);
c = *code;
continue;
}
/* For other groups, scan the branches. */
-
+
if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE)
{
BOOL empty_branch;
@@ -1409,7 +1410,7 @@ for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE
}
while (*code == OP_ALT);
if (!empty_branch) return FALSE; /* All branches are non-empty */
- c = *code;
+ c = *code;
continue;
}
@@ -2113,7 +2114,7 @@ for (;; ptr++)
int class_lastchar;
int newoptions;
int recno;
- int refsign;
+ int refsign;
int skipbytes;
int subreqbyte;
int subfirstbyte;
@@ -3640,7 +3641,7 @@ for (;; ptr++)
code[1+LINK_SIZE] = OP_CREF;
skipbytes = 3;
- refsign = -1;
+ refsign = -1;
/* Check for a test for recursion in a named group. */
@@ -3664,11 +3665,11 @@ for (;; ptr++)
terminator = '\'';
ptr++;
}
- else
+ else
{
terminator = 0;
- if (ptr[1] == '-' || ptr[1] == '+') refsign = *(++ptr);
- }
+ if (ptr[1] == '-' || ptr[1] == '+') refsign = *(++ptr);
+ }
/* We now expect to read a name; any thing else is an error */
@@ -3706,31 +3707,31 @@ for (;; ptr++)
/* In the real compile we do the work of looking for the actual
reference. If the string started with "+" or "-" we require the rest to
be digits, in which case recno will be set. */
-
+
if (refsign > 0)
{
if (recno <= 0)
{
*errorcodeptr = ERR58;
goto FAILED;
- }
+ }
if (refsign == '-')
{
- recno = cd->bracount - recno + 1;
+ recno = cd->bracount - recno + 1;
if (recno <= 0)
{
*errorcodeptr = ERR15;
goto FAILED;
- }
+ }
}
- else recno += cd->bracount;
+ else recno += cd->bracount;
PUT2(code, 2+LINK_SIZE, recno);
break;
- }
+ }
/* Otherwise (did not start with "+" or "-"), start by looking for the
name. */
-
+
slot = cd->name_table;
for (i = 0; i < cd->names_found; i++)
{
@@ -4055,13 +4056,13 @@ for (;; ptr++)
const uschar *called;
if ((refsign = *ptr) == '+') ptr++;
- else if (refsign == '-')
+ else if (refsign == '-')
{
if ((digitab[ptr[1]] & ctype_digit) == 0)
goto OTHER_CHAR_AFTER_QUERY;
- ptr++;
- }
-
+ ptr++;
+ }
+
recno = 0;
while((digitab[*ptr] & ctype_digit) != 0)
recno = recno * 10 + *ptr++ - '0';
@@ -4071,20 +4072,20 @@ for (;; ptr++)
*errorcodeptr = ERR29;
goto FAILED;
}
-
+
if (refsign == '-')
{
if (recno == 0)
{
*errorcodeptr = ERR58;
goto FAILED;
- }
- recno = cd->bracount - recno + 1;
+ }
+ recno = cd->bracount - recno + 1;
if (recno <= 0)
{
*errorcodeptr = ERR15;
goto FAILED;
- }
+ }
}
else if (refsign == '+')
{
@@ -4092,9 +4093,9 @@ for (;; ptr++)
{
*errorcodeptr = ERR58;
goto FAILED;
- }
- recno += cd->bracount;
- }
+ }
+ recno += cd->bracount;
+ }
/* Come here from code above that handles a named recursion */
@@ -4168,7 +4169,7 @@ for (;; ptr++)
/* ------------------------------------------------------------ */
default: /* Other characters: check option setting */
- OTHER_CHAR_AFTER_QUERY:
+ OTHER_CHAR_AFTER_QUERY:
set = unset = 0;
optset = &set;
@@ -4319,9 +4320,11 @@ for (;; ptr++)
is on the bracket. */
/* If this is a conditional bracket, check that there are no more than
- two branches in the group, or just one if it's a DEFINE group. */
+ two branches in the group, or just one if it's a DEFINE group. We do this
+ in the real compile phase, not in the pre-pass, where the whole group may
+ not be available. */
- if (bravalue == OP_COND)
+ if (bravalue == OP_COND && lengthptr == NULL)
{
uschar *tc = code;
int condcount = 0;
@@ -4653,7 +4656,7 @@ This function is used during the pre-compile phase when we are trying to find
out the amount of memory needed, as well as during the real compile phase. The
value of lengthptr distinguishes the two phases.
-Argument:
+Arguments:
options option bits, including any changes for this subpattern
oldims previous settings of ims option bits
codeptr -> the address of the current code pointer
@@ -4806,26 +4809,29 @@ for (;;)
}
}
- /* Reached end of expression, either ')' or end of pattern. Go back through
- the alternative branches and reverse the chain of offsets, with the field in
- the BRA item now becoming an offset to the first alternative. If there are
- no alternatives, it points to the end of the group. The length in the
- terminating ket is always the length of the whole bracketed item. If any of
- the ims options were changed inside the group, compile a resetting op-code
- following, except at the very end of the pattern. Return leaving the pointer
- at the terminating char. */
+ /* Reached end of expression, either ')' or end of pattern. In the real
+ compile phase, go back through the alternative branches and reverse the chain
+ of offsets, with the field in the BRA item now becoming an offset to the
+ first alternative. If there are no alternatives, it points to the end of the
+ group. The length in the terminating ket is always the length of the whole
+ bracketed item. If any of the ims options were changed inside the group,
+ compile a resetting op-code following, except at the very end of the pattern.
+ Return leaving the pointer at the terminating char. */
if (*ptr != '|')
{
- int branch_length = code - last_branch;
- do
+ if (lengthptr == NULL)
{
- int prev_length = GET(last_branch, 1);
- PUT(last_branch, 1, branch_length);
- branch_length = prev_length;
- last_branch -= branch_length;
+ int branch_length = code - last_branch;
+ do
+ {
+ int prev_length = GET(last_branch, 1);
+ PUT(last_branch, 1, branch_length);
+ branch_length = prev_length;
+ last_branch -= branch_length;
+ }
+ while (branch_length > 0);
}
- while (branch_length > 0);
/* Fill in the ket */
@@ -4852,17 +4858,29 @@ for (;;)
return TRUE;
}
- /* Another branch follows; insert an "or" node. Its length field points back
+ /* Another branch follows. In the pre-compile phase, we can move the code
+ pointer back to where it was for the start of the first branch. (That is,
+ pretend that each branch is the only one.)
+
+ In the real compile phase, insert an ALT node. Its length field points back
to the previous branch while the bracket remains open. At the end the chain
is reversed. It's done like this so that the start of the bracket has a
zero offset until it is closed, making it possible to detect recursion. */
- *code = OP_ALT;
- PUT(code, 1, code - last_branch);
- bc.current = last_branch = code;
- code += 1 + LINK_SIZE;
+ if (lengthptr != NULL)
+ {
+ code = *codeptr + 1 + LINK_SIZE + skipbytes;
+ length += 1 + LINK_SIZE;
+ }
+ else
+ {
+ *code = OP_ALT;
+ PUT(code, 1, code - last_branch);
+ bc.current = last_branch = code;
+ code += 1 + LINK_SIZE;
+ }
+
ptr++;
- length += 1 + LINK_SIZE;
}
/* Control never reaches here */
}
diff --git a/pcre_dfa_exec.c b/pcre_dfa_exec.c
index 472a907..01dab00 100644
--- a/pcre_dfa_exec.c
+++ b/pcre_dfa_exec.c
@@ -74,7 +74,7 @@ enough. */
character that is to be tested in some way. This makes is possible to
centralize the loading of these characters. In the case of Type * etc, the
"character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a
-small value. ***NOTE*** If the start of this table is modified, the two tables
+small value. ***NOTE*** If the start of this table is modified, the two tables
that follow must also be modified. */
static uschar coptable[] = {
diff --git a/pcre_exec.c b/pcre_exec.c
index 4066d09..faac023 100644
--- a/pcre_exec.c
+++ b/pcre_exec.c
@@ -292,7 +292,7 @@ typedef struct heapframe {
const uschar *Xeptr;
const uschar *Xecode;
- const uschar *Xmstart;
+ const uschar *Xmstart;
int Xoffset_top;
long int Xims;
eptrblock *Xeptrb;
@@ -374,7 +374,7 @@ Arguments:
eptr pointer to current character in subject
ecode pointer to current position in compiled code
mstart pointer to the current match start position (can be modified
- by encountering \K)
+ by encountering \K)
offset_top current top pointer
md pointer to "static" info for the match
ims current /i, /m, and /s options
@@ -394,7 +394,7 @@ Returns: MATCH_MATCH if matched ) these values are >= 0
*/
static int
-match(REGISTER USPTR eptr, REGISTER const uschar *ecode, const uschar *mstart,
+match(REGISTER USPTR eptr, REGISTER const uschar *ecode, const uschar *mstart,
int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,
int flags, unsigned int rdepth)
{
@@ -1266,13 +1266,13 @@ for (;;)
if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);
ecode++;
break;
-
+
/* Reset the start of match point */
-
+
case OP_SET_SOM:
mstart = eptr;
- ecode++;
- break;
+ ecode++;
+ break;
/* Assert before internal newline if multiline, or before a terminating
newline unless endonly is set, else end of subject unless noteol is set. */
@@ -4181,7 +4181,7 @@ for(;;)
md->start_match_ptr = start_match; /* Insurance */
md->match_call_count = 0;
md->eptrn = 0; /* Next free eptrchain slot */
- rc = match(start_match, md->start_code, start_match, 2, md,
+ rc = match(start_match, md->start_code, start_match, 2, md,
ims, NULL, 0, 0);
/* Any return other than MATCH_NOMATCH breaks the loop. */
@@ -4263,7 +4263,7 @@ if (rc == MATCH_MATCH)
rc = md->offset_overflow? 0 : md->end_offset_top/2;
/* If there is space, set up the whole thing as substring 0. The value of
- md->start_match_ptr might be modified if \K was encountered on the success
+ md->start_match_ptr might be modified if \K was encountered on the success
matching path. */
if (offsetcount < 2) rc = 0; else
diff --git a/pcre_fullinfo.c b/pcre_fullinfo.c
index 7682435..797edde 100644
--- a/pcre_fullinfo.c
+++ b/pcre_fullinfo.c
@@ -139,14 +139,14 @@ switch (what)
case PCRE_INFO_DEFAULT_TABLES:
*((const uschar **)where) = (const uschar *)(_pcre_default_tables);
break;
-
+
case PCRE_INFO_OKPARTIAL:
- *((int *)where) = (re->options & PCRE_NOPARTIAL) == 0;
+ *((int *)where) = (re->options & PCRE_NOPARTIAL) == 0;
break;
-
+
case PCRE_INFO_JCHANGED:
*((int *)where) = (re->options & PCRE_JCHANGED) != 0;
- break;
+ break;
default: return PCRE_ERROR_BADOPTION;
}
diff --git a/pcre_internal.h b/pcre_internal.h
index 7bab088..5ff4b15 100644
--- a/pcre_internal.h
+++ b/pcre_internal.h
@@ -605,7 +605,7 @@ ESC_Z to detect the types that may be repeated. These are the types that
consume characters. If any new escapes are put in between that don't consume a
character, that code will have to change. */
-enum { ESC_A = 1, ESC_G, ESC_K, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s,
+enum { ESC_A = 1, ESC_G, ESC_K, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s,
ESC_W, ESC_w, ESC_dum1, ESC_C, ESC_P, ESC_p, ESC_R, ESC_X, ESC_Z, ESC_z,
ESC_E, ESC_Q, ESC_k, ESC_REF };
@@ -629,7 +629,7 @@ enum {
OP_SOD, /* 1 Start of data: \A */
OP_SOM, /* 2 Start of match (subject + offset): \G */
- OP_SET_SOM, /* 3 Set start of match (\K) */
+ OP_SET_SOM, /* 3 Set start of match (\K) */
OP_NOT_WORD_BOUNDARY, /* 4 \B */
OP_WORD_BOUNDARY, /* 5 \b */
OP_NOT_DIGIT, /* 6 \D */
diff --git a/pcretest.c b/pcretest.c
index f3c24f4..9cd07ee 100644
--- a/pcretest.c
+++ b/pcretest.c
@@ -1365,8 +1365,8 @@ while (!done)
new_info(re, NULL, PCRE_INFO_NAMEENTRYSIZE, &nameentrysize);
new_info(re, NULL, PCRE_INFO_NAMECOUNT, &namecount);
new_info(re, NULL, PCRE_INFO_NAMETABLE, (void *)&nametable);
- new_info(re, NULL, PCRE_INFO_OKPARTIAL, &okpartial);
- new_info(re, NULL, PCRE_INFO_JCHANGED, &jchanged);
+ new_info(re, NULL, PCRE_INFO_OKPARTIAL, &okpartial);
+ new_info(re, NULL, PCRE_INFO_JCHANGED, &jchanged);
#if !defined NOINFOCHECK
old_count = pcre_info(re, &old_options, &old_first_char);
@@ -1407,7 +1407,7 @@ while (!done)
nametable += nameentrysize;
}
}
-
+
if (!okpartial) fprintf(outfile, "Partial matching not supported\n");
all_options = ((real_pcre *)re)->options;
@@ -1428,7 +1428,7 @@ while (!done)
((get_options & PCRE_UTF8) != 0)? " utf8" : "",
((get_options & PCRE_NO_UTF8_CHECK) != 0)? " no_utf8_check" : "",
((get_options & PCRE_DUPNAMES) != 0)? " dupnames" : "");
-
+
if (jchanged) fprintf(outfile, "Duplicate name status changes\n");
switch (get_options & PCRE_NEWLINE_BITS)