| 51 |
<P> |
<P> |
| 52 |
PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT and |
PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT and |
| 53 |
PCRE_PARTIAL_HARD options, which can be set when calling <b>pcre_exec()</b> or |
PCRE_PARTIAL_HARD options, which can be set when calling <b>pcre_exec()</b> or |
| 54 |
<b>pcre_dfa_exec()</b>. For backwards compatibility, PCRE_PARTIAL is a synonym |
<b>pcre_dfa_exec()</b>. For backwards compatibility, PCRE_PARTIAL is a synonym |
| 55 |
for PCRE_PARTIAL_SOFT. The essential difference between the two options is |
for PCRE_PARTIAL_SOFT. The essential difference between the two options is |
| 56 |
whether or not a partial match is preferred to an alternative complete match, |
whether or not a partial match is preferred to an alternative complete match, |
| 57 |
though the details differ between the two matching functions. If both options |
though the details differ between the two matching functions. If both options |
| 58 |
are set, PCRE_PARTIAL_HARD takes precedence. |
are set, PCRE_PARTIAL_HARD takes precedence. |
| 59 |
</P> |
</P> |
| 60 |
<P> |
<P> |
| 74 |
If PCRE_PARTIAL_SOFT is set, the partial match is remembered, but matching |
If PCRE_PARTIAL_SOFT is set, the partial match is remembered, but matching |
| 75 |
continues as normal, and other alternatives in the pattern are tried. If no |
continues as normal, and other alternatives in the pattern are tried. If no |
| 76 |
complete match can be found, <b>pcre_exec()</b> returns PCRE_ERROR_PARTIAL |
complete match can be found, <b>pcre_exec()</b> returns PCRE_ERROR_PARTIAL |
| 77 |
instead of PCRE_ERROR_NOMATCH, and if there are at least two slots in the |
instead of PCRE_ERROR_NOMATCH. If there are at least two slots in the offsets |
| 78 |
offsets vector, they are filled in with the offsets of the longest string that |
vector, the first of them is set to the offset of the earliest character that |
| 79 |
partially matched. Consider this pattern: |
was inspected when the partial match was found. For convenience, the second |
| 80 |
|
offset points to the end of the string so that a substring can easily be |
| 81 |
|
extracted. |
| 82 |
|
</P> |
| 83 |
|
<P> |
| 84 |
|
For the majority of patterns, the first offset identifies the start of the |
| 85 |
|
partially matched string. However, for patterns that contain lookbehind |
| 86 |
|
assertions, or \K, or begin with \b or \B, earlier characters have been |
| 87 |
|
inspected while carrying out the match. For example: |
| 88 |
|
<pre> |
| 89 |
|
/(?<=abc)123/ |
| 90 |
|
</pre> |
| 91 |
|
This pattern matches "123", but only if it is preceded by "abc". If the subject |
| 92 |
|
string is "xyzabc12", the offsets after a partial match are for the substring |
| 93 |
|
"abc12", because all these characters are needed if another match is tried |
| 94 |
|
with extra characters added. |
| 95 |
|
</P> |
| 96 |
|
<P> |
| 97 |
|
If there is more than one partial match, the first one that was found provides |
| 98 |
|
the data that is returned. Consider this pattern: |
| 99 |
<pre> |
<pre> |
| 100 |
/123\w+X|dogY/ |
/123\w+X|dogY/ |
| 101 |
</pre> |
</pre> |
| 102 |
If this is matched against the subject string "abc123dog", both |
If this is matched against the subject string "abc123dog", both |
| 103 |
alternatives fail to match, but the end of the subject is reached during |
alternatives fail to match, but the end of the subject is reached during |
| 104 |
matching, so PCRE_ERROR_PARTIAL is returned instead of PCRE_ERROR_NOMATCH. The |
matching, so PCRE_ERROR_PARTIAL is returned instead of PCRE_ERROR_NOMATCH. The |
| 105 |
offsets are set to 3 and 9, identifying "123dog" as the longest partial match |
offsets are set to 3 and 9, identifying "123dog" as the first partial match |
| 106 |
that was found. (In this example, there are two partial matches, because "dog" |
that was found. (In this example, there are two partial matches, because "dog" |
| 107 |
on its own partially matches the second alternative.) |
on its own partially matches the second alternative.) |
| 108 |
</P> |
</P> |
| 109 |
<P> |
<P> |
| 110 |
If PCRE_PARTIAL_HARD is set for <b>pcre_exec()</b>, it returns |
If PCRE_PARTIAL_HARD is set for <b>pcre_exec()</b>, it returns |
| 111 |
PCRE_ERROR_PARTIAL as soon as a partial match is found, without continuing to |
PCRE_ERROR_PARTIAL as soon as a partial match is found, without continuing to |
| 112 |
search for possible complete matches. The difference between the two options |
search for possible complete matches. The difference between the two options |
| 113 |
can be illustrated by a pattern such as: |
can be illustrated by a pattern such as: |
| 114 |
<pre> |
<pre> |
| 115 |
/dog(sbody)?/ |
/dog(sbody)?/ |
| 116 |
</pre> |
</pre> |
| 117 |
This matches either "dog" or "dogsbody", greedily (that is, it prefers the |
This matches either "dog" or "dogsbody", greedily (that is, it prefers the |
| 118 |
longer string if possible). If it is matched against the string "dog" with |
longer string if possible). If it is matched against the string "dog" with |
| 119 |
PCRE_PARTIAL_SOFT, it yields a complete match for "dog". However, if |
PCRE_PARTIAL_SOFT, it yields a complete match for "dog". However, if |
| 120 |
PCRE_PARTIAL_HARD is set, the result is PCRE_ERROR_PARTIAL. On the other hand, |
PCRE_PARTIAL_HARD is set, the result is PCRE_ERROR_PARTIAL. On the other hand, |
| 121 |
if the pattern is made ungreedy the result is different: |
if the pattern is made ungreedy the result is different: |
| 122 |
<pre> |
<pre> |
| 123 |
/dog(sbody)??/ |
/dog(sbody)??/ |
| 124 |
</pre> |
</pre> |
| 125 |
In this case the result is always a complete match because <b>pcre_exec()</b> |
In this case the result is always a complete match because <b>pcre_exec()</b> |
| 126 |
finds that first, and it never continues after finding a match. It might be |
finds that first, and it never continues after finding a match. It might be |
| 127 |
easier to follow this explanation by thinking of the two patterns like this: |
easier to follow this explanation by thinking of the two patterns like this: |
| 128 |
<pre> |
<pre> |
| 129 |
/dog(sbody)?/ is the same as /dogsbody|dog/ |
/dog(sbody)?/ is the same as /dogsbody|dog/ |
| 130 |
/dog(sbody)??/ is the same as /dog|dogsbody/ |
/dog(sbody)??/ is the same as /dog|dogsbody/ |
| 131 |
</pre> |
</pre> |
| 132 |
The second pattern will never match "dogsbody" when <b>pcre_exec()</b> is |
The second pattern will never match "dogsbody" when <b>pcre_exec()</b> is |
| 133 |
used, because it will always find the shorter match first. |
used, because it will always find the shorter match first. |
| 134 |
</P> |
</P> |
| 135 |
<br><a name="SEC3" href="#TOC1">PARTIAL MATCHING USING pcre_dfa_exec()</a><br> |
<br><a name="SEC3" href="#TOC1">PARTIAL MATCHING USING pcre_dfa_exec()</a><br> |
| 136 |
<P> |
<P> |
| 137 |
The <b>pcre_dfa_exec()</b> function moves along the subject string character by |
The <b>pcre_dfa_exec()</b> function moves along the subject string character by |
| 138 |
character, without backtracking, searching for all possible matches |
character, without backtracking, searching for all possible matches |
| 139 |
simultaneously. If the end of the subject is reached before the end of the |
simultaneously. If the end of the subject is reached before the end of the |
| 140 |
pattern, there is the possibility of a partial match, again provided that at |
pattern, there is the possibility of a partial match, again provided that at |
| 141 |
least one character has matched. |
least one character has matched. |
| 142 |
</P> |
</P> |
| 144 |
When PCRE_PARTIAL_SOFT is set, PCRE_ERROR_PARTIAL is returned only if there |
When PCRE_PARTIAL_SOFT is set, PCRE_ERROR_PARTIAL is returned only if there |
| 145 |
have been no complete matches. Otherwise, the complete matches are returned. |
have been no complete matches. Otherwise, the complete matches are returned. |
| 146 |
However, if PCRE_PARTIAL_HARD is set, a partial match takes precedence over any |
However, if PCRE_PARTIAL_HARD is set, a partial match takes precedence over any |
| 147 |
complete matches. The portion of the string that provided the longest partial |
complete matches. The portion of the string that was inspected when the longest |
| 148 |
match is set as the first matching string, provided there are at least two |
partial match was found is set as the first matching string, provided there are |
| 149 |
slots in the offsets vector. |
at least two slots in the offsets vector. |
| 150 |
</P> |
</P> |
| 151 |
<P> |
<P> |
| 152 |
Because <b>pcre_dfa_exec()</b> always searches for all possible matches, and |
Because <b>pcre_dfa_exec()</b> always searches for all possible matches, and |
| 153 |
there is no difference between greedy and ungreedy repetition, its behaviour is |
there is no difference between greedy and ungreedy repetition, its behaviour is |
| 154 |
different from <b>pcre_exec</b> when PCRE_PARTIAL_HARD is set. Consider the |
different from <b>pcre_exec</b> when PCRE_PARTIAL_HARD is set. Consider the |
| 155 |
string "dog" matched against the ungreedy pattern shown above: |
string "dog" matched against the ungreedy pattern shown above: |
| 156 |
<pre> |
<pre> |
| 157 |
/dog(sbody)??/ |
/dog(sbody)??/ |
| 158 |
</pre> |
</pre> |
| 159 |
Whereas <b>pcre_exec()</b> stops as soon as it finds the complete match for |
Whereas <b>pcre_exec()</b> stops as soon as it finds the complete match for |
| 160 |
"dog", <b>pcre_dfa_exec()</b> also finds the partial match for "dogsbody", and |
"dog", <b>pcre_dfa_exec()</b> also finds the partial match for "dogsbody", and |
| 161 |
so returns that when PCRE_PARTIAL_HARD is set. |
so returns that when PCRE_PARTIAL_HARD is set. |
| 162 |
</P> |
</P> |
| 163 |
<br><a name="SEC4" href="#TOC1">PARTIAL MATCHING AND WORD BOUNDARIES</a><br> |
<br><a name="SEC4" href="#TOC1">PARTIAL MATCHING AND WORD BOUNDARIES</a><br> |
| 164 |
<P> |
<P> |
| 165 |
If a pattern ends with one of sequences \w or \W, which test for word |
If a pattern ends with one of sequences \w or \W, which test for word |
| 166 |
boundaries, partial matching with PCRE_PARTIAL_SOFT can give counter-intuitive |
boundaries, partial matching with PCRE_PARTIAL_SOFT can give counter-intuitive |
| 167 |
results. Consider this pattern: |
results. Consider this pattern: |
| 168 |
<pre> |
<pre> |
| 169 |
/\bcat\b/ |
/\bcat\b/ |
| 170 |
</pre> |
</pre> |
| 171 |
This matches "cat", provided there is a word boundary at either end. If the |
This matches "cat", provided there is a word boundary at either end. If the |
| 172 |
subject string is "the cat", the comparison of the final "t" with a following |
subject string is "the cat", the comparison of the final "t" with a following |
| 173 |
character cannot take place, so a partial match is found. However, |
character cannot take place, so a partial match is found. However, |
| 174 |
<b>pcre_exec()</b> carries on with normal matching, which matches \b at the end |
<b>pcre_exec()</b> carries on with normal matching, which matches \b at the end |
| 175 |
of the subject when the last character is a letter, thus finding a complete |
of the subject when the last character is a letter, thus finding a complete |
| 176 |
match. The result, therefore, is <i>not</i> PCRE_ERROR_PARTIAL. The same thing |
match. The result, therefore, is <i>not</i> PCRE_ERROR_PARTIAL. The same thing |
| 177 |
happens with <b>pcre_dfa_exec()</b>, because it also finds the complete match. |
happens with <b>pcre_dfa_exec()</b>, because it also finds the complete match. |
| 178 |
</P> |
</P> |
| 179 |
<P> |
<P> |
| 180 |
Using PCRE_PARTIAL_HARD in this case does yield PCRE_ERROR_PARTIAL, because |
Using PCRE_PARTIAL_HARD in this case does yield PCRE_ERROR_PARTIAL, because |
| 181 |
then the partial match takes precedence. |
then the partial match takes precedence. |
| 182 |
</P> |
</P> |
| 183 |
<br><a name="SEC5" href="#TOC1">FORMERLY RESTRICTED PATTERNS</a><br> |
<br><a name="SEC5" href="#TOC1">FORMERLY RESTRICTED PATTERNS</a><br> |
| 255 |
</P> |
</P> |
| 256 |
<br><a name="SEC8" href="#TOC1">MULTI-SEGMENT MATCHING WITH pcre_exec()</a><br> |
<br><a name="SEC8" href="#TOC1">MULTI-SEGMENT MATCHING WITH pcre_exec()</a><br> |
| 257 |
<P> |
<P> |
| 258 |
From release 8.00, <b>pcre_exec()</b> can also be used to do multi-segment |
From release 8.00, <b>pcre_exec()</b> can also be used to do multi-segment |
| 259 |
matching. Unlike <b>pcre_dfa_exec()</b>, it is not possible to restart the |
matching. Unlike <b>pcre_dfa_exec()</b>, it is not possible to restart the |
| 260 |
previous match with a new segment of data. Instead, new data must be added to |
previous match with a new segment of data. Instead, new data must be added to |
| 261 |
the previous subject string, and the entire match re-run, starting from the |
the previous subject string, and the entire match re-run, starting from the |
| 262 |
point where the partial match occurred. Earlier data can be discarded. |
point where the partial match occurred. Earlier data can be discarded. |
| 263 |
Consider an unanchored pattern that matches dates: |
Consider an unanchored pattern that matches dates: |
| 264 |
<pre> |
<pre> |
| 266 |
data> The date is 23ja\P |
data> The date is 23ja\P |
| 267 |
Partial match: 23ja |
Partial match: 23ja |
| 268 |
</pre> |
</pre> |
| 269 |
The this stage, an application could discard the text preceding "23ja", add on |
The this stage, an application could discard the text preceding "23ja", add on |
| 270 |
text from the next segment, and call <b>pcre_exec()</b> again. Unlike |
text from the next segment, and call <b>pcre_exec()</b> again. Unlike |
| 271 |
<b>pcre_dfa_exec()</b>, the entire matching string must always be available, and |
<b>pcre_dfa_exec()</b>, the entire matching string must always be available, and |
| 272 |
the complete matching process occurs for each call, so more memory and more |
the complete matching process occurs for each call, so more memory and more |
| 273 |
processing time is needed. |
processing time is needed. |
| 274 |
</P> |
</P> |
| 275 |
|
<P> |
| 276 |
|
<b>Note:</b> If the pattern contains lookbehind assertions, or \K, or starts |
| 277 |
|
with \b or \B, the string that is returned for a partial match will include |
| 278 |
|
characters that precede the partially matched string itself, because these must |
| 279 |
|
be retained when adding on more characters for a subsequent matching attempt. |
| 280 |
|
</P> |
| 281 |
<br><a name="SEC9" href="#TOC1">ISSUES WITH MULTI-SEGMENT MATCHING</a><br> |
<br><a name="SEC9" href="#TOC1">ISSUES WITH MULTI-SEGMENT MATCHING</a><br> |
| 282 |
<P> |
<P> |
| 283 |
Certain types of pattern may give problems with multi-segment matching, |
Certain types of pattern may give problems with multi-segment matching, |
| 284 |
whichever matching function is used. |
whichever matching function is used. |
| 285 |
</P> |
</P> |
| 286 |
<P> |
<P> |
| 289 |
subject string for any call does not contain the beginning or end of a line. |
subject string for any call does not contain the beginning or end of a line. |
| 290 |
</P> |
</P> |
| 291 |
<P> |
<P> |
| 292 |
2. If the pattern contains backward assertions (including \b or \B), you need |
2. Lookbehind assertions at the start of a pattern are catered for in the |
| 293 |
to arrange for some overlap in the subject strings to allow for them to be |
offsets that are returned for a partial match. However, in theory, a lookbehind |
| 294 |
correctly tested at the start of each substring. For example, using |
assertion later in the pattern could require even earlier characters to be |
| 295 |
<b>pcre_dfa_exec()</b>, you could pass the subject in chunks that are 500 bytes |
inspected, and it might not have been reached when a partial match occurs. This |
| 296 |
long, but in a buffer of 700 bytes, with the starting offset set to 200 and the |
is probably an extremely unlikely case; you could guard against it to a certain |
| 297 |
previous 200 bytes at the start of the buffer. |
extent by always including extra characters at the start. |
| 298 |
</P> |
</P> |
| 299 |
<P> |
<P> |
| 300 |
3. Matching a subject string that is split into multiple segments may not |
3. Matching a subject string that is split into multiple segments may not |
| 301 |
always produce exactly the same result as matching over one single long string, |
always produce exactly the same result as matching over one single long string, |
| 302 |
especially when PCRE_PARTIAL_SOFT is used. The section "Partial Matching and |
especially when PCRE_PARTIAL_SOFT is used. The section "Partial Matching and |
| 303 |
Word Boundaries" above describes an issue that arises if the pattern ends with |
Word Boundaries" above describes an issue that arises if the pattern ends with |
| 304 |
\b or \B. Another kind of difference may occur when there are multiple |
\b or \B. Another kind of difference may occur when there are multiple |
| 305 |
matching possibilities, because a partial match result is given only when there |
matching possibilities, because a partial match result is given only when there |
| 306 |
are no completed matches. This means that as soon as the shortest match has |
are no completed matches. This means that as soon as the shortest match has |
| 309 |
<pre> |
<pre> |
| 310 |
re> /dog(sbody)?/ |
re> /dog(sbody)?/ |
| 311 |
data> dogsb\P |
data> dogsb\P |
| 312 |
0: dog |
0: dog |
| 313 |
data> do\P\D |
data> do\P\D |
| 314 |
Partial match: do |
Partial match: do |
| 315 |
data> gsb\R\P\D |
data> gsb\R\P\D |
| 333 |
<pre> |
<pre> |
| 334 |
re> /dog(sbody)?/ |
re> /dog(sbody)?/ |
| 335 |
data> dogsb\P\P |
data> dogsb\P\P |
| 336 |
Partial match: dogsb |
Partial match: dogsb |
| 337 |
data> do\P\D |
data> do\P\D |
| 338 |
Partial match: do |
Partial match: do |
| 339 |
data> gsb\R\P\P\D |
data> gsb\R\P\P\D |
| 340 |
Partial match: gsb |
Partial match: gsb |
| 341 |
|
|
| 342 |
</PRE> |
</PRE> |
| 343 |
</P> |
</P> |
| 344 |
<P> |
<P> |
| 345 |
4. Patterns that contain alternatives at the top level which do not all |
4. Patterns that contain alternatives at the top level which do not all |
| 346 |
start with the same pattern item may not work as expected when |
start with the same pattern item may not work as expected when |
| 347 |
<b>pcre_dfa_exec()</b> is used. For example, consider this pattern: |
<b>pcre_dfa_exec()</b> is used. For example, consider this pattern: |
| 348 |
<pre> |
<pre> |
| 349 |
1234|3789 |
1234|3789 |
| 360 |
1234|ABCD |
1234|ABCD |
| 361 |
</pre> |
</pre> |
| 362 |
where no string can be a partial match for both alternatives. This is not a |
where no string can be a partial match for both alternatives. This is not a |
| 363 |
problem if \fPpcre_exec()\fP is used, because the entire match has to be rerun |
problem if \fPpcre_exec()\fP is used, because the entire match has to be rerun |
| 364 |
each time: |
each time: |
| 365 |
<pre> |
<pre> |
| 366 |
re> /1234|3789/ |
re> /1234|3789/ |
| 382 |
</P> |
</P> |
| 383 |
<br><a name="SEC11" href="#TOC1">REVISION</a><br> |
<br><a name="SEC11" href="#TOC1">REVISION</a><br> |
| 384 |
<P> |
<P> |
| 385 |
Last updated: 31 August 2009 |
Last updated: 05 September 2009 |
| 386 |
<br> |
<br> |
| 387 |
Copyright © 1997-2009 University of Cambridge. |
Copyright © 1997-2009 University of Cambridge. |
| 388 |
<br> |
<br> |