| 4 |
.SH "PARTIAL MATCHING IN PCRE" |
.SH "PARTIAL MATCHING IN PCRE" |
| 5 |
.rs |
.rs |
| 6 |
.sp |
.sp |
| 7 |
In normal use of PCRE, if the subject string that is passed to |
In normal use of PCRE, if the subject string that is passed to a matching |
| 8 |
\fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP matches as far as it goes, but is |
function matches as far as it goes, but is too short to match the entire |
| 9 |
too short to match the entire pattern, PCRE_ERROR_NOMATCH is returned. There |
pattern, PCRE_ERROR_NOMATCH is returned. There are circumstances where it might |
| 10 |
are circumstances where it might be helpful to distinguish this case from other |
be helpful to distinguish this case from other cases in which there is no |
| 11 |
cases in which there is no match. |
match. |
| 12 |
.P |
.P |
| 13 |
Consider, for example, an application where a human is required to type in data |
Consider, for example, an application where a human is required to type in data |
| 14 |
for a field with specific formatting requirements. An example might be a date |
for a field with specific formatting requirements. An example might be a date |
| 21 |
as soon as a mistake is made, by beeping and not reflecting the character that |
as soon as a mistake is made, by beeping and not reflecting the character that |
| 22 |
has been typed, for example. This immediate feedback is likely to be a better |
has been typed, for example. This immediate feedback is likely to be a better |
| 23 |
user interface than a check that is delayed until the entire string has been |
user interface than a check that is delayed until the entire string has been |
| 24 |
entered. Partial matching can also sometimes be useful when the subject string |
entered. Partial matching can also be useful when the subject string is very |
| 25 |
is very long and is not all available at once. |
long and is not all available at once. |
| 26 |
.P |
.P |
| 27 |
PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT and |
PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT and |
| 28 |
PCRE_PARTIAL_HARD options, which can be set when calling \fBpcre_exec()\fP or |
PCRE_PARTIAL_HARD options, which can be set when calling any of the matching |
| 29 |
\fBpcre_dfa_exec()\fP. For backwards compatibility, PCRE_PARTIAL is a synonym |
functions. For backwards compatibility, PCRE_PARTIAL is a synonym for |
| 30 |
for PCRE_PARTIAL_SOFT. The essential difference between the two options is |
PCRE_PARTIAL_SOFT. The essential difference between the two options is whether |
| 31 |
whether or not a partial match is preferred to an alternative complete match, |
or not a partial match is preferred to an alternative complete match, though |
| 32 |
though the details differ between the two matching functions. If both options |
the details differ between the two types of matching function. If both options |
| 33 |
are set, PCRE_PARTIAL_HARD takes precedence. |
are set, PCRE_PARTIAL_HARD takes precedence. |
| 34 |
.P |
.P |
| 35 |
Setting a partial matching option disables two of PCRE's optimizations. PCRE |
Setting a partial matching option disables the use of any just-in-time code |
| 36 |
remembers the last literal byte in a pattern, and abandons matching immediately |
that was set up by studying the compiled pattern with the |
| 37 |
if such a byte is not present in the subject string. This optimization cannot |
PCRE_STUDY_JIT_COMPILE option. It also disables two of PCRE's standard |
| 38 |
be used for a subject string that might match only partially. If the pattern |
optimizations. PCRE remembers the last literal data unit in a pattern, and |
| 39 |
was studied, PCRE knows the minimum length of a matching string, and does not |
abandons matching immediately if it is not present in the subject string. This |
| 40 |
bother to run the matching function on shorter strings. This optimization is |
optimization cannot be used for a subject string that might match only |
| 41 |
also disabled for partial matching. |
partially. If the pattern was studied, PCRE knows the minimum length of a |
| 42 |
. |
matching string, and does not bother to run the matching function on shorter |
| 43 |
. |
strings. This optimization is also disabled for partial matching. |
| 44 |
.SH "PARTIAL MATCHING USING pcre_exec()" |
. |
| 45 |
.rs |
. |
| 46 |
.sp |
.SH "PARTIAL MATCHING USING pcre_exec() OR pcre16_exec()" |
| 47 |
A partial match occurs during a call to \fBpcre_exec()\fP whenever the end of |
.rs |
| 48 |
the subject string is reached successfully, but matching cannot continue |
.sp |
| 49 |
because more characters are needed. However, at least one character must have |
A partial match occurs during a call to \fBpcre_exec()\fP or |
| 50 |
been matched. (In other words, a partial match can never be an empty string.) |
\fBpcre16_exec()\fP when the end of the subject string is reached successfully, |
| 51 |
.P |
but matching cannot continue because more characters are needed. However, at |
| 52 |
If PCRE_PARTIAL_SOFT is set, the partial match is remembered, but matching |
least one character in the subject must have been inspected. This character |
| 53 |
continues as normal, and other alternatives in the pattern are tried. If no |
need not form part of the final matched string; lookbehind assertions and the |
| 54 |
complete match can be found, \fBpcre_exec()\fP returns PCRE_ERROR_PARTIAL |
\eK escape sequence provide ways of inspecting characters before the start of a |
| 55 |
instead of PCRE_ERROR_NOMATCH. If there are at least two slots in the offsets |
matched substring. The requirement for inspecting at least one character exists |
| 56 |
vector, the first of them is set to the offset of the earliest character that |
because an empty string can always be matched; without such a restriction there |
| 57 |
was inspected when the partial match was found. For convenience, the second |
would always be a partial match of an empty string at the end of the subject. |
| 58 |
offset points to the end of the string so that a substring can easily be |
.P |
| 59 |
identified. |
If there are at least two slots in the offsets vector when a partial match is |
| 60 |
|
returned, the first slot is set to the offset of the earliest character that |
| 61 |
|
was inspected. For convenience, the second offset points to the end of the |
| 62 |
|
subject so that a substring can easily be identified. |
| 63 |
.P |
.P |
| 64 |
For the majority of patterns, the first offset identifies the start of the |
For the majority of patterns, the first offset identifies the start of the |
| 65 |
partially matched string. However, for patterns that contain lookbehind |
partially matched string. However, for patterns that contain lookbehind |
| 71 |
This pattern matches "123", but only if it is preceded by "abc". If the subject |
This pattern matches "123", but only if it is preceded by "abc". If the subject |
| 72 |
string is "xyzabc12", the offsets after a partial match are for the substring |
string is "xyzabc12", the offsets after a partial match are for the substring |
| 73 |
"abc12", because all these characters are needed if another match is tried |
"abc12", because all these characters are needed if another match is tried |
| 74 |
with extra characters added. |
with extra characters added to the subject. |
| 75 |
|
.P |
| 76 |
|
What happens when a partial match is identified depends on which of the two |
| 77 |
|
partial matching options are set. |
| 78 |
|
. |
| 79 |
|
. |
| 80 |
|
.SS "PCRE_PARTIAL_SOFT WITH pcre_exec() OR pcre16_exec()" |
| 81 |
|
.rs |
| 82 |
|
.sp |
| 83 |
|
If PCRE_PARTIAL_SOFT is set when \fBpcre_exec()\fP or \fBpcre16_exec()\fP |
| 84 |
|
identifies a partial match, the partial match is remembered, but matching |
| 85 |
|
continues as normal, and other alternatives in the pattern are tried. If no |
| 86 |
|
complete match can be found, PCRE_ERROR_PARTIAL is returned instead of |
| 87 |
|
PCRE_ERROR_NOMATCH. |
| 88 |
|
.P |
| 89 |
|
This option is "soft" because it prefers a complete match over a partial match. |
| 90 |
|
All the various matching items in a pattern behave as if the subject string is |
| 91 |
|
potentially complete. For example, \ez, \eZ, and $ match at the end of the |
| 92 |
|
subject, as normal, and for \eb and \eB the end of the subject is treated as a |
| 93 |
|
non-alphanumeric. |
| 94 |
.P |
.P |
| 95 |
If there is more than one partial match, the first one that was found provides |
If there is more than one partial match, the first one that was found provides |
| 96 |
the data that is returned. Consider this pattern: |
the data that is returned. Consider this pattern: |
| 99 |
.sp |
.sp |
| 100 |
If this is matched against the subject string "abc123dog", both |
If this is matched against the subject string "abc123dog", both |
| 101 |
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 |
| 102 |
matching, so PCRE_ERROR_PARTIAL is returned instead of PCRE_ERROR_NOMATCH. The |
matching, so PCRE_ERROR_PARTIAL is returned. The offsets are set to 3 and 9, |
| 103 |
offsets are set to 3 and 9, identifying "123dog" as the first partial match |
identifying "123dog" as the first partial match that was found. (In this |
| 104 |
that was found. (In this example, there are two partial matches, because "dog" |
example, there are two partial matches, because "dog" on its own partially |
| 105 |
on its own partially matches the second alternative.) |
matches the second alternative.) |
| 106 |
.P |
. |
| 107 |
If PCRE_PARTIAL_HARD is set for \fBpcre_exec()\fP, it returns |
. |
| 108 |
PCRE_ERROR_PARTIAL as soon as a partial match is found, without continuing to |
.SS "PCRE_PARTIAL_HARD WITH pcre_exec() OR pcre16_exec()" |
| 109 |
search for possible complete matches. The difference between the two options |
.rs |
| 110 |
can be illustrated by a pattern such as: |
.sp |
| 111 |
|
If PCRE_PARTIAL_HARD is set for \fBpcre_exec()\fP or \fBpcre16_exec()\fP, |
| 112 |
|
PCRE_ERROR_PARTIAL is returned as soon as a partial match is found, without |
| 113 |
|
continuing to search for possible complete matches. This option is "hard" |
| 114 |
|
because it prefers an earlier partial match over a later complete match. For |
| 115 |
|
this reason, the assumption is made that the end of the supplied subject string |
| 116 |
|
may not be the true end of the available data, and so, if \ez, \eZ, \eb, \eB, |
| 117 |
|
or $ are encountered at the end of the subject, the result is |
| 118 |
|
PCRE_ERROR_PARTIAL. |
| 119 |
|
.P |
| 120 |
|
Setting PCRE_PARTIAL_HARD also affects the way UTF-8 and UTF-16 |
| 121 |
|
subject strings are checked for validity. Normally, an invalid sequence |
| 122 |
|
causes the error PCRE_ERROR_BADUTF8 or PCRE_ERROR_BADUTF16. However, in the |
| 123 |
|
special case of a truncated character at the end of the subject, |
| 124 |
|
PCRE_ERROR_SHORTUTF8 or PCRE_ERROR_SHORTUTF16 is returned when |
| 125 |
|
PCRE_PARTIAL_HARD is set. |
| 126 |
|
. |
| 127 |
|
. |
| 128 |
|
.SS "Comparing hard and soft partial matching" |
| 129 |
|
.rs |
| 130 |
|
.sp |
| 131 |
|
The difference between the two partial matching options can be illustrated by a |
| 132 |
|
pattern such as: |
| 133 |
.sp |
.sp |
| 134 |
/dog(sbody)?/ |
/dog(sbody)?/ |
| 135 |
.sp |
.sp |
| 141 |
.sp |
.sp |
| 142 |
/dog(sbody)??/ |
/dog(sbody)??/ |
| 143 |
.sp |
.sp |
| 144 |
In this case the result is always a complete match because \fBpcre_exec()\fP |
In this case the result is always a complete match because that is found first, |
| 145 |
finds that first, and it never continues after finding a match. It might be |
and matching never continues after finding a complete match. It might be easier |
| 146 |
easier to follow this explanation by thinking of the two patterns like this: |
to follow this explanation by thinking of the two patterns like this: |
| 147 |
.sp |
.sp |
| 148 |
/dog(sbody)?/ is the same as /dogsbody|dog/ |
/dog(sbody)?/ is the same as /dogsbody|dog/ |
| 149 |
/dog(sbody)??/ is the same as /dog|dogsbody/ |
/dog(sbody)??/ is the same as /dog|dogsbody/ |
| 150 |
.sp |
.sp |
| 151 |
The second pattern will never match "dogsbody" when \fBpcre_exec()\fP is |
The second pattern will never match "dogsbody", because it will always find the |
| 152 |
used, because it will always find the shorter match first. |
shorter match first. |
| 153 |
. |
. |
| 154 |
. |
. |
| 155 |
.SH "PARTIAL MATCHING USING pcre_dfa_exec()" |
.SH "PARTIAL MATCHING USING pcre_dfa_exec() OR pcre16_dfa_exec()" |
| 156 |
.rs |
.rs |
| 157 |
.sp |
.sp |
| 158 |
The \fBpcre_dfa_exec()\fP function moves along the subject string character by |
The DFA functions move along the subject string character by character, without |
| 159 |
character, without backtracking, searching for all possible matches |
backtracking, searching for all possible matches simultaneously. If the end of |
| 160 |
simultaneously. If the end of the subject is reached before the end of the |
the subject is reached before the end of the pattern, there is the possibility |
| 161 |
pattern, there is the possibility of a partial match, again provided that at |
of a partial match, again provided that at least one character has been |
| 162 |
least one character has matched. |
inspected. |
| 163 |
.P |
.P |
| 164 |
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 |
| 165 |
have been no complete matches. Otherwise, the complete matches are returned. |
have been no complete matches. Otherwise, the complete matches are returned. |
| 168 |
partial match was found is set as the first matching string, provided there are |
partial match was found is set as the first matching string, provided there are |
| 169 |
at least two slots in the offsets vector. |
at least two slots in the offsets vector. |
| 170 |
.P |
.P |
| 171 |
Because \fBpcre_dfa_exec()\fP always searches for all possible matches, and |
Because the DFA functions always search for all possible matches, and there is |
| 172 |
there is no difference between greedy and ungreedy repetition, its behaviour is |
no difference between greedy and ungreedy repetition, their behaviour is |
| 173 |
different from \fBpcre_exec\fP when PCRE_PARTIAL_HARD is set. Consider the |
different from the standard functions when PCRE_PARTIAL_HARD is set. Consider |
| 174 |
string "dog" matched against the ungreedy pattern shown above: |
the string "dog" matched against the ungreedy pattern shown above: |
| 175 |
.sp |
.sp |
| 176 |
/dog(sbody)??/ |
/dog(sbody)??/ |
| 177 |
.sp |
.sp |
| 178 |
Whereas \fBpcre_exec()\fP stops as soon as it finds the complete match for |
Whereas the standard functions stop as soon as they find the complete match for |
| 179 |
"dog", \fBpcre_dfa_exec()\fP also finds the partial match for "dogsbody", and |
"dog", the DFA functions also find the partial match for "dogsbody", and so |
| 180 |
so returns that when PCRE_PARTIAL_HARD is set. |
return that when PCRE_PARTIAL_HARD is set. |
| 181 |
. |
. |
| 182 |
. |
. |
| 183 |
.SH "PARTIAL MATCHING AND WORD BOUNDARIES" |
.SH "PARTIAL MATCHING AND WORD BOUNDARIES" |
| 191 |
.sp |
.sp |
| 192 |
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 |
| 193 |
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 |
| 194 |
character cannot take place, so a partial match is found. However, |
character cannot take place, so a partial match is found. However, normal |
| 195 |
\fBpcre_exec()\fP carries on with normal matching, which matches \eb at the end |
matching carries on, and \eb matches at the end of the subject when the last |
| 196 |
of the subject when the last character is a letter, thus finding a complete |
character is a letter, so a complete match is found. The result, therefore, is |
| 197 |
match. The result, therefore, is \fInot\fP PCRE_ERROR_PARTIAL. The same thing |
\fInot\fP PCRE_ERROR_PARTIAL. Using PCRE_PARTIAL_HARD in this case does yield |
| 198 |
happens with \fBpcre_dfa_exec()\fP, because it also finds the complete match. |
PCRE_ERROR_PARTIAL, because then the partial match takes precedence. |
|
.P |
|
|
Using PCRE_PARTIAL_HARD in this case does yield PCRE_ERROR_PARTIAL, because |
|
|
then the partial match takes precedence. |
|
| 199 |
. |
. |
| 200 |
. |
. |
| 201 |
.SH "FORMERLY RESTRICTED PATTERNS" |
.SH "FORMERLY RESTRICTED PATTERNS" |
| 205 |
optimizations were implemented in the \fBpcre_exec()\fP function, the |
optimizations were implemented in the \fBpcre_exec()\fP function, the |
| 206 |
PCRE_PARTIAL option (predecessor of PCRE_PARTIAL_SOFT) could not be used with |
PCRE_PARTIAL option (predecessor of PCRE_PARTIAL_SOFT) could not be used with |
| 207 |
all patterns. From release 8.00 onwards, the restrictions no longer apply, and |
all patterns. From release 8.00 onwards, the restrictions no longer apply, and |
| 208 |
partial matching with \fBpcre_exec()\fP can be requested for any pattern. |
partial matching with can be requested for any pattern. |
| 209 |
.P |
.P |
| 210 |
Items that were formerly restricted were repeated single characters and |
Items that were formerly restricted were repeated single characters and |
| 211 |
repeated metasequences. If PCRE_PARTIAL was set for a pattern that did not |
repeated metasequences. If PCRE_PARTIAL was set for a pattern that did not |
| 238 |
The first data string is matched completely, so \fBpcretest\fP shows the |
The first data string is matched completely, so \fBpcretest\fP shows the |
| 239 |
matched substrings. The remaining four strings do not match the complete |
matched substrings. The remaining four strings do not match the complete |
| 240 |
pattern, but the first two are partial matches. Similar output is obtained |
pattern, but the first two are partial matches. Similar output is obtained |
| 241 |
when \fBpcre_dfa_exec()\fP is used. |
if DFA matching is used. |
| 242 |
.P |
.P |
| 243 |
If the escape sequence \eP is present more than once in a \fBpcretest\fP data |
If the escape sequence \eP is present more than once in a \fBpcretest\fP data |
| 244 |
line, the PCRE_PARTIAL_HARD option is set for the match. |
line, the PCRE_PARTIAL_HARD option is set for the match. |
| 245 |
. |
. |
| 246 |
. |
. |
| 247 |
.SH "MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()" |
.SH "MULTI-SEGMENT MATCHING WITH pcre_dfa_exec() OR pcre16_dfa_exec()" |
| 248 |
.rs |
.rs |
| 249 |
.sp |
.sp |
| 250 |
When a partial match has been found using \fBpcre_dfa_exec()\fP, it is possible |
When a partial match has been found using a DFA matching function, it is |
| 251 |
to continue the match by providing additional subject data and calling |
possible to continue the match by providing additional subject data and calling |
| 252 |
\fBpcre_dfa_exec()\fP again with the same compiled regular expression, this |
the function again with the same compiled regular expression, this time setting |
| 253 |
time setting the PCRE_DFA_RESTART option. You must pass the same working |
the PCRE_DFA_RESTART option. You must pass the same working space as before, |
| 254 |
space as before, because this is where details of the previous partial match |
because this is where details of the previous partial match are stored. Here is |
| 255 |
are stored. Here is an example using \fBpcretest\fP, using the \eR escape |
an example using \fBpcretest\fP, using the \eR escape sequence to set the |
| 256 |
sequence to set the PCRE_DFA_RESTART option (\eD specifies the use of |
PCRE_DFA_RESTART option (\eD specifies the use of the DFA matching function): |
|
\fBpcre_dfa_exec()\fP): |
|
| 257 |
.sp |
.sp |
| 258 |
re> /^\ed?\ed(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ed\ed$/ |
re> /^\ed?\ed(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ed\ed$/ |
| 259 |
data> 23ja\eP\eD |
data> 23ja\eP\eD |
| 269 |
.P |
.P |
| 270 |
You can set the PCRE_PARTIAL_SOFT or PCRE_PARTIAL_HARD options with |
You can set the PCRE_PARTIAL_SOFT or PCRE_PARTIAL_HARD options with |
| 271 |
PCRE_DFA_RESTART to continue partial matching over multiple segments. This |
PCRE_DFA_RESTART to continue partial matching over multiple segments. This |
| 272 |
facility can be used to pass very long subject strings to |
facility can be used to pass very long subject strings to the DFA matching |
| 273 |
\fBpcre_dfa_exec()\fP. |
functions. |
| 274 |
. |
. |
| 275 |
. |
. |
| 276 |
.SH "MULTI-SEGMENT MATCHING WITH pcre_exec()" |
.SH "MULTI-SEGMENT MATCHING WITH pcre_exec() OR pcre16_exec()" |
| 277 |
.rs |
.rs |
| 278 |
.sp |
.sp |
| 279 |
From release 8.00, \fBpcre_exec()\fP can also be used to do multi-segment |
From release 8.00, the standard matching functions can also be used to do |
| 280 |
matching. Unlike \fBpcre_dfa_exec()\fP, it is not possible to restart the |
multi-segment matching. Unlike the DFA functions, it is not possible to |
| 281 |
previous match with a new segment of data. Instead, new data must be added to |
restart the previous match with a new segment of data. Instead, new data must |
| 282 |
the previous subject string, and the entire match re-run, starting from the |
be added to the previous subject string, and the entire match re-run, starting |
| 283 |
point where the partial match occurred. Earlier data can be discarded. |
from the point where the partial match occurred. Earlier data can be discarded. |
| 284 |
Consider an unanchored pattern that matches dates: |
.P |
| 285 |
|
It is best to use PCRE_PARTIAL_HARD in this situation, because it does not |
| 286 |
|
treat the end of a segment as the end of the subject when matching \ez, \eZ, |
| 287 |
|
\eb, \eB, and $. Consider an unanchored pattern that matches dates: |
| 288 |
.sp |
.sp |
| 289 |
re> /\ed?\ed(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ed\ed/ |
re> /\ed?\ed(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ed\ed/ |
| 290 |
data> The date is 23ja\eP |
data> The date is 23ja\eP\eP |
| 291 |
Partial match: 23ja |
Partial match: 23ja |
| 292 |
.sp |
.sp |
| 293 |
At this stage, an application could discard the text preceding "23ja", add on |
At this stage, an application could discard the text preceding "23ja", add on |
| 294 |
text from the next segment, and call \fBpcre_exec()\fP again. Unlike |
text from the next segment, and call the matching function again. Unlike the |
| 295 |
\fBpcre_dfa_exec()\fP, the entire matching string must always be available, and |
DFA matching functions the entire matching string must always be available, and |
| 296 |
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 |
| 297 |
processing time is needed. |
processing time is needed. |
| 298 |
.P |
.P |
| 299 |
\fBNote:\fP If the pattern contains lookbehind assertions, or \eK, or starts |
\fBNote:\fP If the pattern contains lookbehind assertions, or \eK, or starts |
| 300 |
with \eb or \eB, the string that is returned for a partial match will include |
with \eb or \eB, the string that is returned for a partial match includes |
| 301 |
characters that precede the partially matched string itself, because these must |
characters that precede the partially matched string itself, because these must |
| 302 |
be retained when adding on more characters for a subsequent matching attempt. |
be retained when adding on more characters for a subsequent matching attempt. |
| 303 |
. |
. |
| 308 |
Certain types of pattern may give problems with multi-segment matching, |
Certain types of pattern may give problems with multi-segment matching, |
| 309 |
whichever matching function is used. |
whichever matching function is used. |
| 310 |
.P |
.P |
| 311 |
1. If the pattern contains tests for the beginning or end of a line, you need |
1. If the pattern contains a test for the beginning of a line, you need to pass |
| 312 |
to pass the PCRE_NOTBOL or PCRE_NOTEOL options, as appropriate, when the |
the PCRE_NOTBOL option when the subject string for any call does start at the |
| 313 |
subject string for any call does not contain the beginning or end of a line. |
beginning of a line. There is also a PCRE_NOTEOL option, but in practice when |
| 314 |
|
doing multi-segment matching you should be using PCRE_PARTIAL_HARD, which |
| 315 |
|
includes the effect of PCRE_NOTEOL. |
| 316 |
.P |
.P |
| 317 |
2. Lookbehind assertions at the start of a pattern are catered for in the |
2. Lookbehind assertions at the start of a pattern are catered for in the |
| 318 |
offsets that are returned for a partial match. However, in theory, a lookbehind |
offsets that are returned for a partial match. However, in theory, a lookbehind |
| 326 |
especially when PCRE_PARTIAL_SOFT is used. The section "Partial Matching and |
especially when PCRE_PARTIAL_SOFT is used. The section "Partial Matching and |
| 327 |
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 |
| 328 |
\eb or \eB. Another kind of difference may occur when there are multiple |
\eb or \eB. Another kind of difference may occur when there are multiple |
| 329 |
matching possibilities, because a partial match result is given only when there |
matching possibilities, because (for PCRE_PARTIAL_SOFT) a partial match result |
| 330 |
are no completed matches. This means that as soon as the shortest match has |
is given only when there are no completed matches. This means that as soon as |
| 331 |
been found, continuation to a new subject segment is no longer possible. |
the shortest match has been found, continuation to a new subject segment is no |
| 332 |
Consider again this \fBpcretest\fP example: |
longer possible. Consider again this \fBpcretest\fP example: |
| 333 |
.sp |
.sp |
| 334 |
re> /dog(sbody)?/ |
re> /dog(sbody)?/ |
| 335 |
data> dogsb\eP |
data> dogsb\eP |
| 342 |
0: dogsbody |
0: dogsbody |
| 343 |
1: dog |
1: dog |
| 344 |
.sp |
.sp |
| 345 |
The first data line passes the string "dogsb" to \fBpcre_exec()\fP, setting the |
The first data line passes the string "dogsb" to a standard matching function, |
| 346 |
PCRE_PARTIAL_SOFT option. Although the string is a partial match for |
setting the PCRE_PARTIAL_SOFT option. Although the string is a partial match |
| 347 |
"dogsbody", the result is not PCRE_ERROR_PARTIAL, because the shorter string |
for "dogsbody", the result is not PCRE_ERROR_PARTIAL, because the shorter |
| 348 |
"dog" is a complete match. Similarly, when the subject is presented to |
string "dog" is a complete match. Similarly, when the subject is presented to |
| 349 |
\fBpcre_dfa_exec()\fP in several parts ("do" and "gsb" being the first two) the |
a DFA matching function in several parts ("do" and "gsb" being the first two) |
| 350 |
match stops when "dog" has been found, and it is not possible to continue. On |
the match stops when "dog" has been found, and it is not possible to continue. |
| 351 |
the other hand, if "dogsbody" is presented as a single string, |
On the other hand, if "dogsbody" is presented as a single string, a DFA |
| 352 |
\fBpcre_dfa_exec()\fP finds both matches. |
matching function finds both matches. |
| 353 |
.P |
.P |
| 354 |
Because of these problems, it is probably best to use PCRE_PARTIAL_HARD when |
Because of these problems, it is best to use PCRE_PARTIAL_HARD when matching |
| 355 |
matching multi-segment data. The example above then behaves differently: |
multi-segment data. The example above then behaves differently: |
| 356 |
.sp |
.sp |
| 357 |
re> /dog(sbody)?/ |
re> /dog(sbody)?/ |
| 358 |
data> dogsb\eP\eP |
data> dogsb\eP\eP |
| 362 |
data> gsb\eR\eP\eP\eD |
data> gsb\eR\eP\eP\eD |
| 363 |
Partial match: gsb |
Partial match: gsb |
| 364 |
.sp |
.sp |
| 365 |
.P |
4. Patterns that contain alternatives at the top level which do not all start |
| 366 |
4. Patterns that contain alternatives at the top level which do not all |
with the same pattern item may not work as expected when PCRE_DFA_RESTART is |
| 367 |
start with the same pattern item may not work as expected when |
used. For example, consider this pattern: |
|
PCRE_DFA_RESTART is used with \fBpcre_dfa_exec()\fP. For example, consider this |
|
|
pattern: |
|
| 368 |
.sp |
.sp |
| 369 |
1234|3789 |
1234|3789 |
| 370 |
.sp |
.sp |
| 380 |
1234|ABCD |
1234|ABCD |
| 381 |
.sp |
.sp |
| 382 |
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 |
| 383 |
problem if \fBpcre_exec()\fP is used, because the entire match has to be rerun |
problem if a standard matching function is used, because the entire match has |
| 384 |
each time: |
to be rerun each time: |
| 385 |
.sp |
.sp |
| 386 |
re> /1234|3789/ |
re> /1234|3789/ |
| 387 |
data> ABC123\eP |
data> ABC123\eP\eP |
| 388 |
Partial match: 123 |
Partial match: 123 |
| 389 |
data> 1237890 |
data> 1237890 |
| 390 |
0: 3789 |
0: 3789 |
| 391 |
.sp |
.sp |
| 392 |
Of course, instead of using PCRE_DFA_PARTIAL, the same technique of re-running |
Of course, instead of using PCRE_DFA_RESTART, the same technique of re-running |
| 393 |
the entire match can also be used with \fBpcre_dfa_exec()\fP. Another |
the entire match can also be used with the DFA matching functions. Another |
| 394 |
possibility is to work with two buffers. If a partial match at offset \fIn\fP |
possibility is to work with two buffers. If a partial match at offset \fIn\fP |
| 395 |
in the first buffer is followed by "no match" when PCRE_DFA_RESTART is used on |
in the first buffer is followed by "no match" when PCRE_DFA_RESTART is used on |
| 396 |
the second buffer, you can then try a new match starting at offset \fIn+1\fP in |
the second buffer, you can then try a new match starting at offset \fIn+1\fP in |
| 411 |
.rs |
.rs |
| 412 |
.sp |
.sp |
| 413 |
.nf |
.nf |
| 414 |
Last updated: 19 October 2009 |
Last updated: 08 January 2012 |
| 415 |
Copyright (c) 1997-2009 University of Cambridge. |
Copyright (c) 1997-2012 University of Cambridge. |
| 416 |
.fi |
.fi |