/[pcre]/code/trunk/doc/html/pcrepartial.html
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 429 - (show annotations) (download) (as text)
Tue Sep 1 16:10:16 2009 UTC (5 years, 1 month ago) by ph10
File MIME type: text/html
File size: 16348 byte(s)
Add pcredemo man page, containing a listing of pcredemo.c.

1 <html>
2 <head>
3 <title>pcrepartial specification</title>
4 </head>
5 <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6 <h1>pcrepartial man page</h1>
7 <p>
8 Return to the <a href="index.html">PCRE index page</a>.
9 </p>
10 <p>
11 This page is part of the PCRE HTML documentation. It was generated automatically
12 from the original man page. If there is any nonsense in it, please consult the
13 man page, in case the conversion went wrong.
14 <br>
15 <ul>
16 <li><a name="TOC1" href="#SEC1">PARTIAL MATCHING IN PCRE</a>
17 <li><a name="TOC2" href="#SEC2">PARTIAL MATCHING USING pcre_exec()</a>
18 <li><a name="TOC3" href="#SEC3">PARTIAL MATCHING USING pcre_dfa_exec()</a>
19 <li><a name="TOC4" href="#SEC4">PARTIAL MATCHING AND WORD BOUNDARIES</a>
20 <li><a name="TOC5" href="#SEC5">FORMERLY RESTRICTED PATTERNS</a>
21 <li><a name="TOC6" href="#SEC6">EXAMPLE OF PARTIAL MATCHING USING PCRETEST</a>
22 <li><a name="TOC7" href="#SEC7">MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()</a>
23 <li><a name="TOC8" href="#SEC8">MULTI-SEGMENT MATCHING WITH pcre_exec()</a>
24 <li><a name="TOC9" href="#SEC9">ISSUES WITH MULTI-SEGMENT MATCHING</a>
25 <li><a name="TOC10" href="#SEC10">AUTHOR</a>
26 <li><a name="TOC11" href="#SEC11">REVISION</a>
27 </ul>
28 <br><a name="SEC1" href="#TOC1">PARTIAL MATCHING IN PCRE</a><br>
29 <P>
30 In normal use of PCRE, if the subject string that is passed to
31 <b>pcre_exec()</b> or <b>pcre_dfa_exec()</b> matches as far as it goes, but is
32 too short to match the entire pattern, PCRE_ERROR_NOMATCH is returned. There
33 are circumstances where it might be helpful to distinguish this case from other
34 cases in which there is no match.
35 </P>
36 <P>
37 Consider, for example, an application where a human is required to type in data
38 for a field with specific formatting requirements. An example might be a date
39 in the form <i>ddmmmyy</i>, defined by this pattern:
40 <pre>
41 ^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$
42 </pre>
43 If the application sees the user's keystrokes one by one, and can check that
44 what has been typed so far is potentially valid, it is able to raise an error
45 as soon as a mistake is made, by beeping and not reflecting the character that
46 has been typed, for example. This immediate feedback is likely to be a better
47 user interface than a check that is delayed until the entire string has been
48 entered. Partial matching can also sometimes be useful when the subject string
49 is very long and is not all available at once.
50 </P>
51 <P>
52 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
54 <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
56 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
58 are set, PCRE_PARTIAL_HARD takes precedence.
59 </P>
60 <P>
61 Setting a partial matching option disables one of PCRE's optimizations. PCRE
62 remembers the last literal byte in a pattern, and abandons matching immediately
63 if such a byte is not present in the subject string. This optimization cannot
64 be used for a subject string that might match only partially.
65 </P>
66 <br><a name="SEC2" href="#TOC1">PARTIAL MATCHING USING pcre_exec()</a><br>
67 <P>
68 A partial match occurs during a call to <b>pcre_exec()</b> whenever the end of
69 the subject string is reached successfully, but matching cannot continue
70 because more characters are needed. However, at least one character must have
71 been matched. (In other words, a partial match can never be an empty string.)
72 </P>
73 <P>
74 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
76 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
78 offsets vector, they are filled in with the offsets of the longest string that
79 partially matched. Consider this pattern:
80 <pre>
81 /123\w+X|dogY/
82 </pre>
83 If this is matched against the subject string "abc123dog", both
84 alternatives fail to match, but the end of the subject is reached during
85 matching, so PCRE_ERROR_PARTIAL is returned instead of PCRE_ERROR_NOMATCH. The
86 offsets are set to 3 and 9, identifying "123dog" as the longest partial match
87 that was found. (In this example, there are two partial matches, because "dog"
88 on its own partially matches the second alternative.)
89 </P>
90 <P>
91 If PCRE_PARTIAL_HARD is set for <b>pcre_exec()</b>, it returns
92 PCRE_ERROR_PARTIAL as soon as a partial match is found, without continuing to
93 search for possible complete matches. The difference between the two options
94 can be illustrated by a pattern such as:
95 <pre>
96 /dog(sbody)?/
97 </pre>
98 This matches either "dog" or "dogsbody", greedily (that is, it prefers the
99 longer string if possible). If it is matched against the string "dog" with
100 PCRE_PARTIAL_SOFT, it yields a complete match for "dog". However, if
101 PCRE_PARTIAL_HARD is set, the result is PCRE_ERROR_PARTIAL. On the other hand,
102 if the pattern is made ungreedy the result is different:
103 <pre>
104 /dog(sbody)??/
105 </pre>
106 In this case the result is always a complete match because <b>pcre_exec()</b>
107 finds that first, and it never continues after finding a match. It might be
108 easier to follow this explanation by thinking of the two patterns like this:
109 <pre>
110 /dog(sbody)?/ is the same as /dogsbody|dog/
111 /dog(sbody)??/ is the same as /dog|dogsbody/
112 </pre>
113 The second pattern will never match "dogsbody" when <b>pcre_exec()</b> is
114 used, because it will always find the shorter match first.
115 </P>
116 <br><a name="SEC3" href="#TOC1">PARTIAL MATCHING USING pcre_dfa_exec()</a><br>
117 <P>
118 The <b>pcre_dfa_exec()</b> function moves along the subject string character by
119 character, without backtracking, searching for all possible matches
120 simultaneously. If the end of the subject is reached before the end of the
121 pattern, there is the possibility of a partial match, again provided that at
122 least one character has matched.
123 </P>
124 <P>
125 When PCRE_PARTIAL_SOFT is set, PCRE_ERROR_PARTIAL is returned only if there
126 have been no complete matches. Otherwise, the complete matches are returned.
127 However, if PCRE_PARTIAL_HARD is set, a partial match takes precedence over any
128 complete matches. The portion of the string that provided the longest partial
129 match is set as the first matching string, provided there are at least two
130 slots in the offsets vector.
131 </P>
132 <P>
133 Because <b>pcre_dfa_exec()</b> always searches for all possible matches, and
134 there is no difference between greedy and ungreedy repetition, its behaviour is
135 different from <b>pcre_exec</b> when PCRE_PARTIAL_HARD is set. Consider the
136 string "dog" matched against the ungreedy pattern shown above:
137 <pre>
138 /dog(sbody)??/
139 </pre>
140 Whereas <b>pcre_exec()</b> stops as soon as it finds the complete match for
141 "dog", <b>pcre_dfa_exec()</b> also finds the partial match for "dogsbody", and
142 so returns that when PCRE_PARTIAL_HARD is set.
143 </P>
144 <br><a name="SEC4" href="#TOC1">PARTIAL MATCHING AND WORD BOUNDARIES</a><br>
145 <P>
146 If a pattern ends with one of sequences \w or \W, which test for word
147 boundaries, partial matching with PCRE_PARTIAL_SOFT can give counter-intuitive
148 results. Consider this pattern:
149 <pre>
150 /\bcat\b/
151 </pre>
152 This matches "cat", provided there is a word boundary at either end. If the
153 subject string is "the cat", the comparison of the final "t" with a following
154 character cannot take place, so a partial match is found. However,
155 <b>pcre_exec()</b> carries on with normal matching, which matches \b at the end
156 of the subject when the last character is a letter, thus finding a complete
157 match. The result, therefore, is <i>not</i> PCRE_ERROR_PARTIAL. The same thing
158 happens with <b>pcre_dfa_exec()</b>, because it also finds the complete match.
159 </P>
160 <P>
161 Using PCRE_PARTIAL_HARD in this case does yield PCRE_ERROR_PARTIAL, because
162 then the partial match takes precedence.
163 </P>
164 <br><a name="SEC5" href="#TOC1">FORMERLY RESTRICTED PATTERNS</a><br>
165 <P>
166 For releases of PCRE prior to 8.00, because of the way certain internal
167 optimizations were implemented in the <b>pcre_exec()</b> function, the
168 PCRE_PARTIAL option (predecessor of PCRE_PARTIAL_SOFT) could not be used with
169 all patterns. From release 8.00 onwards, the restrictions no longer apply, and
170 partial matching with <b>pcre_exec()</b> can be requested for any pattern.
171 </P>
172 <P>
173 Items that were formerly restricted were repeated single characters and
174 repeated metasequences. If PCRE_PARTIAL was set for a pattern that did not
175 conform to the restrictions, <b>pcre_exec()</b> returned the error code
176 PCRE_ERROR_BADPARTIAL (-13). This error code is no longer in use. The
177 PCRE_INFO_OKPARTIAL call to <b>pcre_fullinfo()</b> to find out if a compiled
178 pattern can be used for partial matching now always returns 1.
179 </P>
180 <br><a name="SEC6" href="#TOC1">EXAMPLE OF PARTIAL MATCHING USING PCRETEST</a><br>
181 <P>
182 If the escape sequence \P is present in a <b>pcretest</b> data line, the
183 PCRE_PARTIAL_SOFT option is used for the match. Here is a run of <b>pcretest</b>
184 that uses the date example quoted above:
185 <pre>
186 re&#62; /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
187 data&#62; 25jun04\P
188 0: 25jun04
189 1: jun
190 data&#62; 25dec3\P
191 Partial match: 23dec3
192 data&#62; 3ju\P
193 Partial match: 3ju
194 data&#62; 3juj\P
195 No match
196 data&#62; j\P
197 No match
198 </pre>
199 The first data string is matched completely, so <b>pcretest</b> shows the
200 matched substrings. The remaining four strings do not match the complete
201 pattern, but the first two are partial matches. Similar output is obtained
202 when <b>pcre_dfa_exec()</b> is used.
203 </P>
204 <P>
205 If the escape sequence \P is present more than once in a <b>pcretest</b> data
206 line, the PCRE_PARTIAL_HARD option is set for the match.
207 </P>
208 <br><a name="SEC7" href="#TOC1">MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()</a><br>
209 <P>
210 When a partial match has been found using <b>pcre_dfa_exec()</b>, it is possible
211 to continue the match by providing additional subject data and calling
212 <b>pcre_dfa_exec()</b> again with the same compiled regular expression, this
213 time setting the PCRE_DFA_RESTART option. You must pass the same working
214 space as before, because this is where details of the previous partial match
215 are stored. Here is an example using <b>pcretest</b>, using the \R escape
216 sequence to set the PCRE_DFA_RESTART option (\D specifies the use of
217 <b>pcre_dfa_exec()</b>):
218 <pre>
219 re&#62; /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
220 data&#62; 23ja\P\D
221 Partial match: 23ja
222 data&#62; n05\R\D
223 0: n05
224 </pre>
225 The first call has "23ja" as the subject, and requests partial matching; the
226 second call has "n05" as the subject for the continued (restarted) match.
227 Notice that when the match is complete, only the last part is shown; PCRE does
228 not retain the previously partially-matched string. It is up to the calling
229 program to do that if it needs to.
230 </P>
231 <P>
232 You can set the PCRE_PARTIAL_SOFT or PCRE_PARTIAL_HARD options with
233 PCRE_DFA_RESTART to continue partial matching over multiple segments. This
234 facility can be used to pass very long subject strings to
235 <b>pcre_dfa_exec()</b>.
236 </P>
237 <br><a name="SEC8" href="#TOC1">MULTI-SEGMENT MATCHING WITH pcre_exec()</a><br>
238 <P>
239 From release 8.00, <b>pcre_exec()</b> can also be used to do multi-segment
240 matching. Unlike <b>pcre_dfa_exec()</b>, it is not possible to restart the
241 previous match with a new segment of data. Instead, new data must be added to
242 the previous subject string, and the entire match re-run, starting from the
243 point where the partial match occurred. Earlier data can be discarded.
244 Consider an unanchored pattern that matches dates:
245 <pre>
246 re&#62; /\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d/
247 data&#62; The date is 23ja\P
248 Partial match: 23ja
249 </pre>
250 The this stage, an application could discard the text preceding "23ja", add on
251 text from the next segment, and call <b>pcre_exec()</b> again. Unlike
252 <b>pcre_dfa_exec()</b>, the entire matching string must always be available, and
253 the complete matching process occurs for each call, so more memory and more
254 processing time is needed.
255 </P>
256 <br><a name="SEC9" href="#TOC1">ISSUES WITH MULTI-SEGMENT MATCHING</a><br>
257 <P>
258 Certain types of pattern may give problems with multi-segment matching,
259 whichever matching function is used.
260 </P>
261 <P>
262 1. If the pattern contains tests for the beginning or end of a line, you need
263 to pass the PCRE_NOTBOL or PCRE_NOTEOL options, as appropriate, when the
264 subject string for any call does not contain the beginning or end of a line.
265 </P>
266 <P>
267 2. If the pattern contains backward assertions (including \b or \B), you need
268 to arrange for some overlap in the subject strings to allow for them to be
269 correctly tested at the start of each substring. For example, using
270 <b>pcre_dfa_exec()</b>, you could pass the subject in chunks that are 500 bytes
271 long, but in a buffer of 700 bytes, with the starting offset set to 200 and the
272 previous 200 bytes at the start of the buffer.
273 </P>
274 <P>
275 3. Matching a subject string that is split into multiple segments may not
276 always produce exactly the same result as matching over one single long string,
277 especially when PCRE_PARTIAL_SOFT is used. The section "Partial Matching and
278 Word Boundaries" above describes an issue that arises if the pattern ends with
279 \b or \B. Another kind of difference may occur when there are multiple
280 matching possibilities, because a partial match result is given only when there
281 are no completed matches. This means that as soon as the shortest match has
282 been found, continuation to a new subject segment is no longer possible.
283 Consider again this <b>pcretest</b> example:
284 <pre>
285 re&#62; /dog(sbody)?/
286 data&#62; dogsb\P
287 0: dog
288 data&#62; do\P\D
289 Partial match: do
290 data&#62; gsb\R\P\D
291 0: g
292 data&#62; dogsbody\D
293 0: dogsbody
294 1: dog
295 </pre>
296 The first data line passes the string "dogsb" to <b>pcre_exec()</b>, setting the
297 PCRE_PARTIAL_SOFT option. Although the string is a partial match for
298 "dogsbody", the result is not PCRE_ERROR_PARTIAL, because the shorter string
299 "dog" is a complete match. Similarly, when the subject is presented to
300 <b>pcre_dfa_exec()</b> in several parts ("do" and "gsb" being the first two) the
301 match stops when "dog" has been found, and it is not possible to continue. On
302 the other hand, if "dogsbody" is presented as a single string,
303 <b>pcre_dfa_exec()</b> finds both matches.
304 </P>
305 <P>
306 Because of these problems, it is probably best to use PCRE_PARTIAL_HARD when
307 matching multi-segment data. The example above then behaves differently:
308 <pre>
309 re&#62; /dog(sbody)?/
310 data&#62; dogsb\P\P
311 Partial match: dogsb
312 data&#62; do\P\D
313 Partial match: do
314 data&#62; gsb\R\P\P\D
315 Partial match: gsb
316
317 </PRE>
318 </P>
319 <P>
320 4. Patterns that contain alternatives at the top level which do not all
321 start with the same pattern item may not work as expected when
322 <b>pcre_dfa_exec()</b> is used. For example, consider this pattern:
323 <pre>
324 1234|3789
325 </pre>
326 If the first part of the subject is "ABC123", a partial match of the first
327 alternative is found at offset 3. There is no partial match for the second
328 alternative, because such a match does not start at the same point in the
329 subject string. Attempting to continue with the string "7890" does not yield a
330 match because only those alternatives that match at one point in the subject
331 are remembered. The problem arises because the start of the second alternative
332 matches within the first alternative. There is no problem with anchored
333 patterns or patterns such as:
334 <pre>
335 1234|ABCD
336 </pre>
337 where no string can be a partial match for both alternatives. This is not a
338 problem if \fPpcre_exec()\fP is used, because the entire match has to be rerun
339 each time:
340 <pre>
341 re&#62; /1234|3789/
342 data&#62; ABC123\P
343 Partial match: 123
344 data&#62; 1237890
345 0: 3789
346
347 </PRE>
348 </P>
349 <br><a name="SEC10" href="#TOC1">AUTHOR</a><br>
350 <P>
351 Philip Hazel
352 <br>
353 University Computing Service
354 <br>
355 Cambridge CB2 3QH, England.
356 <br>
357 </P>
358 <br><a name="SEC11" href="#TOC1">REVISION</a><br>
359 <P>
360 Last updated: 31 August 2009
361 <br>
362 Copyright &copy; 1997-2009 University of Cambridge.
363 <br>
364 <p>
365 Return to the <a href="index.html">PCRE index page</a>.
366 </p>

Properties

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12