ViewVC logotype

Contents of /code/trunk/doc/pcrepartial.3

Parent Directory Parent Directory | Revision Log Revision Log

Revision 572 - (hide annotations) (download)
Wed Nov 17 17:55:57 2010 UTC (4 years, 4 months ago) by ph10
File size: 18350 byte(s)
Documentation updates and tidies.

1 nigel 79 .TH PCREPARTIAL 3
2 nigel 75 .SH NAME
3     PCRE - Perl-compatible regular expressions
5     .rs
6     .sp
7     In normal use of PCRE, if the subject string that is passed to
8 nigel 77 \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP matches as far as it goes, but is
9     too short to match the entire pattern, PCRE_ERROR_NOMATCH is returned. There
10     are circumstances where it might be helpful to distinguish this case from other
11     cases in which there is no match.
12 nigel 75 .P
13     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
15     in the form \fIddmmmyy\fP, defined by this pattern:
16     .sp
17     ^\ed?\ed(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ed\ed$
18     .sp
19     If the application sees the user's keystrokes one by one, and can check that
20     what has been typed so far is potentially valid, it is able to raise an error
21 ph10 428 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
23 nigel 75 user interface than a check that is delayed until the entire string has been
24 ph10 553 entered. Partial matching can also be useful when the subject string is very
25     long and is not all available at once.
26 nigel 75 .P
27 ph10 428 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
29 ph10 435 \fBpcre_dfa_exec()\fP. For backwards compatibility, PCRE_PARTIAL is a synonym
30     for PCRE_PARTIAL_SOFT. The essential difference between the two options is
31     whether or not a partial match is preferred to an alternative complete match,
32     though the details differ between the two matching functions. If both options
33 ph10 428 are set, PCRE_PARTIAL_HARD takes precedence.
34 nigel 75 .P
35 ph10 456 Setting a partial matching option disables two of PCRE's optimizations. PCRE
36 ph10 428 remembers the last literal byte in a pattern, and abandons matching immediately
37     if such a byte is not present in the subject string. This optimization cannot
38 ph10 461 be used for a subject string that might match only partially. If the pattern
39     was studied, PCRE knows the minimum length of a matching string, and does not
40     bother to run the matching function on shorter strings. This optimization is
41 ph10 456 also disabled for partial matching.
42 ph10 428 .
43     .
44     .SH "PARTIAL MATCHING USING pcre_exec()"
45     .rs
46 ph10 426 .sp
47 ph10 553 A partial match occurs during a call to \fBpcre_exec()\fP when the end of the
48     subject string is reached successfully, but matching cannot continue because
49     more characters are needed. However, at least one character in the subject must
50     have been inspected. This character need not form part of the final matched
51     string; lookbehind assertions and the \eK escape sequence provide ways of
52     inspecting characters before the start of a matched substring. The requirement
53     for inspecting at least one character exists because an empty string can always
54     be matched; without such a restriction there would always be a partial match of
55     an empty string at the end of the subject.
56 ph10 428 .P
57 ph10 553 If there are at least two slots in the offsets vector when \fBpcre_exec()\fP
58     returns with a partial match, the first slot is set to the offset of the
59     earliest character that was inspected when the partial match was found. For
60     convenience, the second offset points to the end of the subject so that a
61     substring can easily be identified.
62 ph10 435 .P
63     For the majority of patterns, the first offset identifies the start of the
64     partially matched string. However, for patterns that contain lookbehind
65     assertions, or \eK, or begin with \eb or \eB, earlier characters have been
66     inspected while carrying out the match. For example:
67 ph10 428 .sp
68 ph10 435 /(?<=abc)123/
69     .sp
70     This pattern matches "123", but only if it is preceded by "abc". If the subject
71     string is "xyzabc12", the offsets after a partial match are for the substring
72     "abc12", because all these characters are needed if another match is tried
73 ph10 553 with extra characters added to the subject.
74 ph10 435 .P
75 ph10 553 What happens when a partial match is identified depends on which of the two
76     partial matching options are set.
77     .
78     .
79     .SS "PCRE_PARTIAL_SOFT with pcre_exec()"
80     .rs
81     .sp
82     If PCRE_PARTIAL_SOFT is set when \fBpcre_exec()\fP identifies a partial match,
83     the partial match is remembered, but matching continues as normal, and other
84     alternatives in the pattern are tried. If no complete match can be found,
85     \fBpcre_exec()\fP returns PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH.
86     .P
87     This option is "soft" because it prefers a complete match over a partial match.
88     All the various matching items in a pattern behave as if the subject string is
89     potentially complete. For example, \ez, \eZ, and $ match at the end of the
90     subject, as normal, and for \eb and \eB the end of the subject is treated as a
91     non-alphanumeric.
92     .P
93 ph10 435 If there is more than one partial match, the first one that was found provides
94     the data that is returned. Consider this pattern:
95     .sp
96 ph10 426 /123\ew+X|dogY/
97     .sp
98     If this is matched against the subject string "abc123dog", both
99 ph10 435 alternatives fail to match, but the end of the subject is reached during
100 ph10 553 matching, so PCRE_ERROR_PARTIAL is returned. The offsets are set to 3 and 9,
101     identifying "123dog" as the first partial match that was found. (In this
102     example, there are two partial matches, because "dog" on its own partially
103     matches the second alternative.)
104     .
105     .
106     .SS "PCRE_PARTIAL_HARD with pcre_exec()"
107     .rs
108     .sp
109 ph10 435 If PCRE_PARTIAL_HARD is set for \fBpcre_exec()\fP, it returns
110 ph10 428 PCRE_ERROR_PARTIAL as soon as a partial match is found, without continuing to
111 ph10 553 search for possible complete matches. This option is "hard" because it prefers
112     an earlier partial match over a later complete match. For this reason, the
113     assumption is made that the end of the supplied subject string may not be the
114     true end of the available data, and so, if \ez, \eZ, \eb, \eB, or $ are
115     encountered at the end of the subject, the result is PCRE_ERROR_PARTIAL.
116 ph10 569 .P
117     Setting PCRE_PARTIAL_HARD also affects the way \fBpcre_exec()\fP checks UTF-8
118     subject strings for validity. Normally, an invalid UTF-8 sequence causes the
119     error PCRE_ERROR_BADUTF8. However, in the special case of a truncated UTF-8
120     character at the end of the subject, PCRE_ERROR_SHORTUTF8 is returned when
121     PCRE_PARTIAL_HARD is set.
122 ph10 553 .
123     .
124     .SS "Comparing hard and soft partial matching"
125     .rs
126 ph10 428 .sp
127 ph10 553 The difference between the two partial matching options can be illustrated by a
128     pattern such as:
129     .sp
130 ph10 428 /dog(sbody)?/
131     .sp
132 ph10 435 This matches either "dog" or "dogsbody", greedily (that is, it prefers the
133 ph10 428 longer string if possible). If it is matched against the string "dog" with
134 ph10 435 PCRE_PARTIAL_SOFT, it yields a complete match for "dog". However, if
135     PCRE_PARTIAL_HARD is set, the result is PCRE_ERROR_PARTIAL. On the other hand,
136 ph10 428 if the pattern is made ungreedy the result is different:
137     .sp
138     /dog(sbody)??/
139     .sp
140 ph10 435 In this case the result is always a complete match because \fBpcre_exec()\fP
141     finds that first, and it never continues after finding a match. It might be
142 ph10 428 easier to follow this explanation by thinking of the two patterns like this:
143     .sp
144     /dog(sbody)?/ is the same as /dogsbody|dog/
145     /dog(sbody)??/ is the same as /dog|dogsbody/
146     .sp
147 ph10 435 The second pattern will never match "dogsbody" when \fBpcre_exec()\fP is
148 ph10 428 used, because it will always find the shorter match first.
149     .
150     .
151     .SH "PARTIAL MATCHING USING pcre_dfa_exec()"
152     .rs
153     .sp
154 ph10 435 The \fBpcre_dfa_exec()\fP function moves along the subject string character by
155     character, without backtracking, searching for all possible matches
156     simultaneously. If the end of the subject is reached before the end of the
157 ph10 428 pattern, there is the possibility of a partial match, again provided that at
158 ph10 553 least one character has been inspected.
159 nigel 77 .P
160 ph10 428 When PCRE_PARTIAL_SOFT is set, PCRE_ERROR_PARTIAL is returned only if there
161     have been no complete matches. Otherwise, the complete matches are returned.
162     However, if PCRE_PARTIAL_HARD is set, a partial match takes precedence over any
163 ph10 435 complete matches. The portion of the string that was inspected when the longest
164     partial match was found is set as the first matching string, provided there are
165     at least two slots in the offsets vector.
166 ph10 428 .P
167 ph10 435 Because \fBpcre_dfa_exec()\fP always searches for all possible matches, and
168 ph10 428 there is no difference between greedy and ungreedy repetition, its behaviour is
169 ph10 435 different from \fBpcre_exec\fP when PCRE_PARTIAL_HARD is set. Consider the
170 ph10 428 string "dog" matched against the ungreedy pattern shown above:
171     .sp
172     /dog(sbody)??/
173     .sp
174 ph10 435 Whereas \fBpcre_exec()\fP stops as soon as it finds the complete match for
175 ph10 428 "dog", \fBpcre_dfa_exec()\fP also finds the partial match for "dogsbody", and
176     so returns that when PCRE_PARTIAL_HARD is set.
177 nigel 75 .
178     .
180 nigel 75 .rs
181     .sp
182 ph10 468 If a pattern ends with one of sequences \eb or \eB, which test for word
183 ph10 435 boundaries, partial matching with PCRE_PARTIAL_SOFT can give counter-intuitive
184 ph10 428 results. Consider this pattern:
185     .sp
186     /\ebcat\eb/
187     .sp
188     This matches "cat", provided there is a word boundary at either end. If the
189     subject string is "the cat", the comparison of the final "t" with a following
190 ph10 435 character cannot take place, so a partial match is found. However,
191     \fBpcre_exec()\fP carries on with normal matching, which matches \eb at the end
192     of the subject when the last character is a letter, thus finding a complete
193     match. The result, therefore, is \fInot\fP PCRE_ERROR_PARTIAL. The same thing
194 ph10 428 happens with \fBpcre_dfa_exec()\fP, because it also finds the complete match.
195     .P
196 ph10 435 Using PCRE_PARTIAL_HARD in this case does yield PCRE_ERROR_PARTIAL, because
197 ph10 428 then the partial match takes precedence.
198     .
199     .
201     .rs
202     .sp
203 ph10 426 For releases of PCRE prior to 8.00, because of the way certain internal
204     optimizations were implemented in the \fBpcre_exec()\fP function, the
205 ph10 428 PCRE_PARTIAL option (predecessor of PCRE_PARTIAL_SOFT) could not be used with
206     all patterns. From release 8.00 onwards, the restrictions no longer apply, and
207     partial matching with \fBpcre_exec()\fP can be requested for any pattern.
208 nigel 75 .P
209 ph10 426 Items that were formerly restricted were repeated single characters and
210     repeated metasequences. If PCRE_PARTIAL was set for a pattern that did not
211     conform to the restrictions, \fBpcre_exec()\fP returned the error code
212     PCRE_ERROR_BADPARTIAL (-13). This error code is no longer in use. The
213     PCRE_INFO_OKPARTIAL call to \fBpcre_fullinfo()\fP to find out if a compiled
214     pattern can be used for partial matching now always returns 1.
215 nigel 75 .
216     .
218     .rs
219     .sp
220     If the escape sequence \eP is present in a \fBpcretest\fP data line, the
221 ph10 428 PCRE_PARTIAL_SOFT option is used for the match. Here is a run of \fBpcretest\fP
222     that uses the date example quoted above:
223 nigel 75 .sp
224     re> /^\ed?\ed(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ed\ed$/
225 nigel 87 data> 25jun04\eP
226 nigel 75 0: 25jun04
227     1: jun
228 nigel 87 data> 25dec3\eP
229 ph10 426 Partial match: 23dec3
230 nigel 87 data> 3ju\eP
231 ph10 426 Partial match: 3ju
232 nigel 87 data> 3juj\eP
233 nigel 75 No match
234 nigel 87 data> j\eP
235 nigel 75 No match
236     .sp
237     The first data string is matched completely, so \fBpcretest\fP shows the
238     matched substrings. The remaining four strings do not match the complete
239 ph10 426 pattern, but the first two are partial matches. Similar output is obtained
240     when \fBpcre_dfa_exec()\fP is used.
241 ph10 428 .P
242     If the escape sequence \eP is present more than once in a \fBpcretest\fP data
243     line, the PCRE_PARTIAL_HARD option is set for the match.
244 ph10 426 .
245 ph10 435 .
246 nigel 77 .SH "MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()"
247     .rs
248     .sp
249     When a partial match has been found using \fBpcre_dfa_exec()\fP, it is possible
250     to continue the match by providing additional subject data and calling
251 nigel 93 \fBpcre_dfa_exec()\fP again with the same compiled regular expression, this
252 ph10 428 time setting the PCRE_DFA_RESTART option. You must pass the same working
253 nigel 93 space as before, because this is where details of the previous partial match
254     are stored. Here is an example using \fBpcretest\fP, using the \eR escape
255 ph10 428 sequence to set the PCRE_DFA_RESTART option (\eD specifies the use of
256     \fBpcre_dfa_exec()\fP):
257 nigel 77 .sp
258 ph10 155 re> /^\ed?\ed(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ed\ed$/
259 nigel 77 data> 23ja\eP\eD
260     Partial match: 23ja
261     data> n05\eR\eD
262     0: n05
263     .sp
264     The first call has "23ja" as the subject, and requests partial matching; the
265     second call has "n05" as the subject for the continued (restarted) match.
266     Notice that when the match is complete, only the last part is shown; PCRE does
267     not retain the previously partially-matched string. It is up to the calling
268     program to do that if it needs to.
269 nigel 75 .P
270 ph10 428 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
272     facility can be used to pass very long subject strings to
273     \fBpcre_dfa_exec()\fP.
274 ph10 426 .
275     .
276     .SH "MULTI-SEGMENT MATCHING WITH pcre_exec()"
277     .rs
278     .sp
279 ph10 435 From release 8.00, \fBpcre_exec()\fP can also be used to do multi-segment
280     matching. Unlike \fBpcre_dfa_exec()\fP, it is not possible to restart the
281     previous match with a new segment of data. Instead, new data must be added to
282     the previous subject string, and the entire match re-run, starting from the
283 ph10 553 point where the partial match occurred. Earlier data can be discarded. It is
284     best to use PCRE_PARTIAL_HARD in this situation, because it does not treat the
285     end of a segment as the end of the subject when matching \ez, \eZ, \eb, \eB,
286     and $. Consider an unanchored pattern that matches dates:
287 ph10 426 .sp
288     re> /\ed?\ed(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\ed\ed/
289 ph10 553 data> The date is 23ja\eP\eP
290 ph10 426 Partial match: 23ja
291     .sp
292 ph10 468 At this stage, an application could discard the text preceding "23ja", add on
293 ph10 435 text from the next segment, and call \fBpcre_exec()\fP again. Unlike
294     \fBpcre_dfa_exec()\fP, the entire matching string must always be available, and
295     the complete matching process occurs for each call, so more memory and more
296 ph10 426 processing time is needed.
297 ph10 435 .P
298     \fBNote:\fP If the pattern contains lookbehind assertions, or \eK, or starts
299     with \eb or \eB, the string that is returned for a partial match will include
300     characters that precede the partially matched string itself, because these must
301     be retained when adding on more characters for a subsequent matching attempt.
302 ph10 426 .
303 ph10 435 .
305     .rs
306     .sp
307 ph10 435 Certain types of pattern may give problems with multi-segment matching,
308 ph10 426 whichever matching function is used.
309 nigel 77 .P
310 ph10 553 1. If the pattern contains a test for the beginning of a line, you need to pass
311     the PCRE_NOTBOL option when the subject string for any call does start at the
312     beginning of a line. There is also a PCRE_NOTEOL option, but in practice when
313     doing multi-segment matching you should be using PCRE_PARTIAL_HARD, which
314     includes the effect of PCRE_NOTEOL.
315 nigel 77 .P
316 ph10 435 2. Lookbehind assertions at the start of a pattern are catered for in the
317     offsets that are returned for a partial match. However, in theory, a lookbehind
318     assertion later in the pattern could require even earlier characters to be
319     inspected, and it might not have been reached when a partial match occurs. This
320     is probably an extremely unlikely case; you could guard against it to a certain
321     extent by always including extra characters at the start.
322 nigel 77 .P
323 ph10 428 3. Matching a subject string that is split into multiple segments may not
324     always produce exactly the same result as matching over one single long string,
325 ph10 435 especially when PCRE_PARTIAL_SOFT is used. The section "Partial Matching and
326     Word Boundaries" above describes an issue that arises if the pattern ends with
327 ph10 428 \eb or \eB. Another kind of difference may occur when there are multiple
328 ph10 553 matching possibilities, because (for PCRE_PARTIAL_SOFT) a partial match result
329     is given only when there are no completed matches. This means that as soon as
330     the shortest match has been found, continuation to a new subject segment is no
331     longer possible. Consider again this \fBpcretest\fP example:
332 nigel 77 .sp
333     re> /dog(sbody)?/
334 ph10 426 data> dogsb\eP
335 ph10 435 0: dog
336 nigel 77 data> do\eP\eD
337     Partial match: do
338     data> gsb\eR\eP\eD
339     0: g
340     data> dogsbody\eD
341     0: dogsbody
342     1: dog
343     .sp
344 ph10 428 The first data line passes the string "dogsb" to \fBpcre_exec()\fP, setting the
345     PCRE_PARTIAL_SOFT option. Although the string is a partial match for
346     "dogsbody", the result is not PCRE_ERROR_PARTIAL, because the shorter string
347     "dog" is a complete match. Similarly, when the subject is presented to
348     \fBpcre_dfa_exec()\fP in several parts ("do" and "gsb" being the first two) the
349     match stops when "dog" has been found, and it is not possible to continue. On
350     the other hand, if "dogsbody" is presented as a single string,
351     \fBpcre_dfa_exec()\fP finds both matches.
352 nigel 77 .P
353 ph10 553 Because of these problems, it is best to use PCRE_PARTIAL_HARD when matching
354     multi-segment data. The example above then behaves differently:
355 ph10 428 .sp
356     re> /dog(sbody)?/
357     data> dogsb\eP\eP
358 ph10 435 Partial match: dogsb
359 ph10 428 data> do\eP\eD
360     Partial match: do
361     data> gsb\eR\eP\eP\eD
362 ph10 435 Partial match: gsb
363 ph10 428 .sp
364 nigel 87 4. Patterns that contain alternatives at the top level which do not all
365 ph10 435 start with the same pattern item may not work as expected when
366 ph10 463 PCRE_DFA_RESTART is used with \fBpcre_dfa_exec()\fP. For example, consider this
367     pattern:
368 nigel 87 .sp
369     1234|3789
370     .sp
371     If the first part of the subject is "ABC123", a partial match of the first
372     alternative is found at offset 3. There is no partial match for the second
373     alternative, because such a match does not start at the same point in the
374 ph10 426 subject string. Attempting to continue with the string "7890" does not yield a
375 nigel 87 match because only those alternatives that match at one point in the subject
376     are remembered. The problem arises because the start of the second alternative
377     matches within the first alternative. There is no problem with anchored
378     patterns or patterns such as:
379     .sp
380     1234|ABCD
381     .sp
382 ph10 426 where no string can be a partial match for both alternatives. This is not a
383 ph10 468 problem if \fBpcre_exec()\fP is used, because the entire match has to be rerun
384 ph10 426 each time:
385     .sp
386     re> /1234|3789/
387 ph10 553 data> ABC123\eP\eP
388 ph10 426 Partial match: 123
389     data> 1237890
390     0: 3789
391 ph10 435 .sp
392 ph10 553 Of course, instead of using PCRE_DFA_RESTART, the same technique of re-running
393 ph10 469 the entire match can also be used with \fBpcre_dfa_exec()\fP. Another
394     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
396     the second buffer, you can then try a new match starting at offset \fIn+1\fP in
397 ph10 468 the first buffer.
398 nigel 77 .
399     .
400 ph10 99 .SH AUTHOR
401     .rs
402     .sp
403     .nf
404     Philip Hazel
405     University Computing Service
406     Cambridge CB2 3QH, England.
407     .fi
408     .
409     .
410     .SH REVISION
411     .rs
412     .sp
413     .nf
414 ph10 569 Last updated: 07 November 2010
415 ph10 553 Copyright (c) 1997-2010 University of Cambridge.
416 ph10 99 .fi


Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

ViewVC Help
Powered by ViewVC 1.1.12