1PCRE2PARTIAL(3)            Library Functions Manual            PCRE2PARTIAL(3)
2
3
4

NAME

6       PCRE2 - Perl-compatible regular expressions
7

PARTIAL MATCHING IN PCRE2

9
10       In  normal use of PCRE2, if there is a match up to the end of a subject
11       string, but more characters are needed to  match  the  entire  pattern,
12       PCRE2_ERROR_NOMATCH  is  returned,  just  like any other failing match.
13       There are circumstances where it might be helpful to  distinguish  this
14       "partial match" case.
15
16       One  example  is  an application where the subject string is very long,
17       and not all available at once. The requirement here is to be able to do
18       the  matching  segment  by segment, but special action is needed when a
19       matched substring spans the boundary between two segments.
20
21       Another example is checking a user input string  as  it  is  typed,  to
22       ensure that it conforms to a required format. Invalid characters can be
23       immediately diagnosed and rejected, giving instant feedback.
24
25       Partial matching is a PCRE2-specific feature; it is  not  Perl-compati‐
26       ble.  It  is  requested  by  setting  one  of the PCRE2_PARTIAL_HARD or
27       PCRE2_PARTIAL_SOFT options when calling a matching function.  The  dif‐
28       ference  between  the  two options is whether or not a partial match is
29       preferred to an alternative complete match, though the  details  differ
30       between  the  two  types of matching function. If both options are set,
31       PCRE2_PARTIAL_HARD takes precedence.
32
33       If you want to use partial matching with just-in-time  optimized  code,
34       as  well  as  setting a partial match option for the matching function,
35       you must also call  pcre2_jit_compile()  with  one  or  both  of  these
36       options:
37
38         PCRE2_JIT_PARTIAL_HARD
39         PCRE2_JIT_PARTIAL_SOFT
40
41       PCRE2_JIT_COMPLETE  should also be set if you are going to run non-par‐
42       tial matches on the same pattern. Separate code is  compiled  for  each
43       mode.  If  the appropriate JIT mode has not been compiled, interpretive
44       matching code is used.
45
46       Setting a partial matching option  disables  two  of  PCRE2's  standard
47       optimization  hints.  PCRE2  remembers  the last literal code unit in a
48       pattern, and abandons matching immediately if it is not present in  the
49       subject  string.  This optimization cannot be used for a subject string
50       that might match only partially. PCRE2 also remembers a minimum  length
51       of  a matching string, and does not bother to run the matching function
52       on shorter strings. This optimization  is  also  disabled  for  partial
53       matching.
54

REQUIREMENTS FOR A PARTIAL MATCH

56
57       A  possible  partial  match  occurs during matching when the end of the
58       subject string is reached successfully, but either more characters  are
59       needed  to complete the match, or the addition of more characters might
60       change what is matched.
61
62       Example 1: if the pattern is /abc/ and the subject is "ab", more  char‐
63       acters  are  definitely  needed  to complete a match. In this case both
64       hard and soft matching options yield a partial match.
65
66       Example 2: if the pattern is /ab+/ and the subject is "ab", a  complete
67       match  can  be  found, but the addition of more characters might change
68       what is matched. In this case, only PCRE2_PARTIAL_HARD returns  a  par‐
69       tial match; PCRE2_PARTIAL_SOFT returns the complete match.
70
71       On  reaching the end of the subject, when PCRE2_PARTIAL_HARD is set, if
72       the next pattern item is \z, \Z, \b, \B, or $ there is always a partial
73       match.   Otherwise, for both options, the next pattern item must be one
74       that inspects a character, and at least one of the  following  must  be
75       true:
76
77       (1)  At  least  one  character has already been inspected. An inspected
78       character need not form part of the final  matched  string;  lookbehind
79       assertions  and the \K escape sequence provide ways of inspecting char‐
80       acters before the start of a matched string.
81
82       (2) The pattern contains one or more lookbehind assertions. This condi‐
83       tion  exists  in  case  there  is a lookbehind that inspects characters
84       before the start of the match.
85
86       (3) There is a special case when the whole pattern can match  an  empty
87       string.   When  the  starting  point  is at the end of the subject, the
88       empty string match is a possibility, and if PCRE2_PARTIAL_SOFT  is  set
89       and  neither  of the above conditions is true, it is returned. However,
90       because adding more characters  might  result  in  a  non-empty  match,
91       PCRE2_PARTIAL_HARD  returns  a  partial match, which in this case means
92       "there is going to be a match at this point, but until some more  char‐
93       acters are added, we do not know if it will be an empty string or some‐
94       thing longer".
95

PARTIAL MATCHING USING pcre2_match()

97
98       When  a  partial  matching  option  is  set,  the  result  of   calling
99       pcre2_match() can be one of the following:
100
101       A successful match
102         A complete match has been found, starting and ending within this sub‐
103         ject.
104
105       PCRE2_ERROR_NOMATCH
106         No match can start anywhere in this subject.
107
108       PCRE2_ERROR_PARTIAL
109         Adding more characters may result in a complete match that  uses  one
110         or more characters from the end of this subject.
111
112       When a partial match is returned, the first two elements in the ovector
113       point to the portion of the subject that was matched, but the values in
114       the rest of the ovector are undefined. The appearance of \K in the pat‐
115       tern has no effect for a partial match. Consider this pattern:
116
117         /abc\K123/
118
119       If it is matched against "456abc123xyz" the result is a complete match,
120       and  the ovector defines the matched string as "123", because \K resets
121       the "start of match" point. However, if a partial  match  is  requested
122       and  the subject string is "456abc12", a partial match is found for the
123       string "abc12", because all these characters are needed  for  a  subse‐
124       quent re-match with additional characters.
125
126       If  there  is more than one partial match, the first one that was found
127       provides the data that is returned. Consider this pattern:
128
129         /123\w+X|dogY/
130
131       If this is matched against the subject string "abc123dog", both  alter‐
132       natives  fail  to  match,  but the end of the subject is reached during
133       matching, so PCRE2_ERROR_PARTIAL is returned. The offsets are set to  3
134       and  9, identifying "123dog" as the first partial match. (In this exam‐
135       ple, there are two partial matches, because "dog" on its own  partially
136       matches the second alternative.)
137
138   How a partial match is processed by pcre2_match()
139
140       What happens when a partial match is identified depends on which of the
141       two partial matching options is set.
142
143       If PCRE2_PARTIAL_HARD is set, PCRE2_ERROR_PARTIAL is returned  as  soon
144       as  a partial match is found, without continuing to search for possible
145       complete matches. This option is "hard" because it prefers  an  earlier
146       partial match over a later complete match. For this reason, the assump‐
147       tion is made that the end of the supplied subject  string  is  not  the
148       true  end  of  the  available  data, which is why \z, \Z, \b, \B, and $
149       always give a partial match.
150
151       If PCRE2_PARTIAL_SOFT is set, the  partial  match  is  remembered,  but
152       matching continues as normal, and other alternatives in the pattern are
153       tried. If no  complete  match  can  be  found,  PCRE2_ERROR_PARTIAL  is
154       returned  instead of PCRE2_ERROR_NOMATCH. This option is "soft" because
155       it prefers a complete match over  a  partial  match.  All  the  various
156       matching  items  in a pattern behave as if the subject string is poten‐
157       tially complete; \z, \Z, and $ match at the end of the subject, as nor‐
158       mal,  and  for  \b  and  \B the end of the subject is treated as a non-
159       alphanumeric.
160
161       The difference between the two partial matching options can  be  illus‐
162       trated by a pattern such as:
163
164         /dog(sbody)?/
165
166       This  matches either "dog" or "dogsbody", greedily (that is, it prefers
167       the longer string if possible). If it is  matched  against  the  string
168       "dog"  with  PCRE2_PARTIAL_SOFT,  it yields a complete match for "dog".
169       However, if PCRE2_PARTIAL_HARD is set, the result  is  PCRE2_ERROR_PAR‐
170       TIAL.  On the other hand, if the pattern is made ungreedy the result is
171       different:
172
173         /dog(sbody)??/
174
175       In this case the result is always a  complete  match  because  that  is
176       found  first,  and  matching  never  continues after finding a complete
177       match. It might be easier to follow this explanation by thinking of the
178       two patterns like this:
179
180         /dog(sbody)?/    is the same as  /dogsbody|dog/
181         /dog(sbody)??/   is the same as  /dog|dogsbody/
182
183       The  second pattern will never match "dogsbody", because it will always
184       find the shorter match first.
185
186   Example of partial matching using pcre2test
187
188       The pcre2test data modifiers partial_hard (or ph) and partial_soft  (or
189       ps)  set  PCRE2_PARTIAL_HARD and PCRE2_PARTIAL_SOFT, respectively, when
190       calling pcre2_match(). Here is a run of pcre2test using a pattern  that
191       matches the whole subject in the form of a date:
192
193           re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
194         data> 25dec3\=ph
195         Partial match: 23dec3
196         data> 3ju\=ph
197         Partial match: 3ju
198         data> 3juj\=ph
199         No match
200
201       This  example  gives  the  same  results for both hard and soft partial
202       matching options. Here is an example where there is a difference:
203
204           re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
205         data> 25jun04\=ps
206          0: 25jun04
207          1: jun
208         data> 25jun04\=ph
209         Partial match: 25jun04
210
211       With  PCRE2_PARTIAL_SOFT,  the  subject  is  matched  completely.   For
212       PCRE2_PARTIAL_HARD, however, the subject is assumed not to be complete,
213       so there is only a partial match.
214

MULTI-SEGMENT MATCHING WITH pcre2_match()

216
217       PCRE was not originally designed with multi-segment matching  in  mind.
218       However,  over  time,  features  (including partial matching) that make
219       multi-segment matching possible have been added. A very long string can
220       be  searched  segment  by  segment by calling pcre2_match() repeatedly,
221       with the aim of achieving the same results that  would  happen  if  the
222       entire  string  was available for searching all the time. Normally, the
223       strings that are being sought are much  shorter  than  each  individual
224       segment,  and are in the middle of very long strings, so the pattern is
225       normally not anchored.
226
227       Special logic must be implemented to handle a  matched  substring  that
228       spans a segment boundary. PCRE2_PARTIAL_HARD should be used, because it
229       returns a partial match at the end of a segment whenever there  is  the
230       possibility  of  changing  the  match  by  adding  more characters. The
231       PCRE2_NOTBOL option should also be set for all but the first segment.
232
233       When a partial match occurs, the next segment must be added to the cur‐
234       rent  subject  and  the match re-run, using the startoffset argument of
235       pcre2_match() to begin at the point where the  partial  match  started.
236       For example:
237
238           re> /\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d/
239         data> ...the date is 23ja\=ph
240         Partial match: 23ja
241         data> ...the date is 23jan19 and on that day...\=offset=15
242          0: 23jan19
243          1: jan
244
245       Note  the  use  of the offset modifier to start the new match where the
246       partial match was found. In this example, the next segment was added to
247       the  one  in  which  the  partial  match  was  found.  This is the most
248       straightforward approach, typically using a memory buffer that is twice
249       the  size of each segment. After a partial match, the first half of the
250       buffer is discarded, the second half is moved to the start of the  buf‐
251       fer,  and  a  new segment is added before repeating the match as in the
252       example above. After a no match, the entire buffer can be discarded.
253
254       If there are memory constraints, you may want to discard text that pre‐
255       cedes  a  partial  match before adding the next segment. Unfortunately,
256       this is not at present straightforward. In cases  such  as  the  above,
257       where the pattern does not contain any lookbehinds, it is sufficient to
258       retain only the partially matched substring. However,  if  the  pattern
259       contains  a  lookbehind assertion, characters that precede the start of
260       the partial match may have been inspected during the matching  process.
261       When  pcre2test displays a partial match, it indicates these characters
262       with '<' if the allusedtext modifier is set:
263
264           re> "(?<=123)abc"
265         data> xx123ab\=ph,allusedtext
266         Partial match: 123ab
267                        <<<
268
269       However, the allusedtext modifier is not available  for  JIT  matching,
270       because  JIT  matching  does  not  record the first (or last) consulted
271       characters.  For this reason, this information is not available via the
272       API. It is therefore not possible in general to obtain the exact number
273       of characters that must be retained in order to  get  the  right  match
274       result.  If  you  cannot  retain the entire segment, you must find some
275       heuristic way of choosing.
276
277       If you know the approximate length of the matching substrings, you  can
278       use  that to decide how much text to retain. The only lookbehind infor‐
279       mation that is currently available via the API is  the  length  of  the
280       longest  individual lookbehind in a pattern, but this can be misleading
281       if  there  are  nested  lookbehinds.  The  value  returned  by  calling
282       pcre2_pattern_info()  with  the  PCRE2_INFO_MAXLOOKBEHIND option is the
283       maximum number of characters (not code units) that any individual look‐
284       behind   moves   back   when   it  is  processed.  A  pattern  such  as
285       "(?<=(?<!b)a)" has a maximum lookbehind value of one, but inspects  two
286       characters before its starting point.
287
288       In  a  non-UTF or a 32-bit case, moving back is just a subtraction, but
289       in UTF-8 or UTF-16 you have  to  count  characters  while  moving  back
290       through the code units.
291

PARTIAL MATCHING USING pcre2_dfa_match()

293
294       The DFA function moves along the subject string character by character,
295       without backtracking, searching for  all  possible  matches  simultane‐
296       ously.  If the end of the subject is reached before the end of the pat‐
297       tern, there is the possibility of a partial match.
298
299       When PCRE2_PARTIAL_SOFT is set, PCRE2_ERROR_PARTIAL is returned only if
300       there  have  been  no complete matches. Otherwise, the complete matches
301       are returned.  If PCRE2_PARTIAL_HARD is  set,  a  partial  match  takes
302       precedence  over  any  complete matches. The portion of the string that
303       was matched when the longest partial match was  found  is  set  as  the
304       first matching string.
305
306       Because  the DFA function always searches for all possible matches, and
307       there is no difference between greedy and ungreedy repetition, its  be‐
308       haviour  is different from the pcre2_match(). Consider the string "dog"
309       matched against this ungreedy pattern:
310
311         /dog(sbody)??/
312
313       Whereas the standard function stops as soon as it  finds  the  complete
314       match  for  "dog",  the  DFA  function also finds the partial match for
315       "dogsbody", and so returns that when PCRE2_PARTIAL_HARD is set.
316

MULTI-SEGMENT MATCHING WITH pcre2_dfa_match()

318
319       When a partial match has been found using the DFA matching function, it
320       is  possible to continue the match by providing additional subject data
321       and calling the function again with the same compiled  regular  expres‐
322       sion, this time setting the PCRE2_DFA_RESTART option. You must pass the
323       same working space as before, because this is where details of the pre‐
324       vious  partial  match are stored. You can set the PCRE2_PARTIAL_SOFT or
325       PCRE2_PARTIAL_HARD options with PCRE2_DFA_RESTART to  continue  partial
326       matching over multiple segments. Here is an example using pcre2test:
327
328           re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
329         data> 23ja\=dfa,ps
330         Partial match: 23ja
331         data> n05\=dfa,dfa_restart
332          0: n05
333
334       The  first  call has "23ja" as the subject, and requests partial match‐
335       ing; the second call  has  "n05"  as  the  subject  for  the  continued
336       (restarted)  match.   Notice  that when the match is complete, only the
337       last part is shown; PCRE2 does not  retain  the  previously  partially-
338       matched  string. It is up to the calling program to do that if it needs
339       to. This means that, for an unanchored pattern, if  a  continued  match
340       fails,  it  is  not  possible to try again at a new starting point. All
341       this facility is capable of doing is continuing with the previous match
342       attempt. For example, consider this pattern:
343
344         1234|3789
345
346       If  the  first  part of the subject is "ABC123", a partial match of the
347       first alternative is found at offset 3. There is no partial  match  for
348       the second alternative, because such a match does not start at the same
349       point in the subject string. Attempting to  continue  with  the  string
350       "7890"  does  not  yield  a  match because only those alternatives that
351       match at one point in the subject  are  remembered.  Depending  on  the
352       application, this may or may not be what you want.
353
354       If  you  do want to allow for starting again at the next character, one
355       way of doing it is to retain some or all of the segment and try  a  new
356       complete match, as described for pcre2_match() above. Another possibil‐
357       ity is to work with two buffers. If a partial match at offset n in  the
358       first  buffer  is followed by "no match" when PCRE2_DFA_RESTART is used
359       on the second buffer, you can then try a new match starting  at  offset
360       n+1 in the first buffer.
361

AUTHOR

363
364       Philip Hazel
365       University Computing Service
366       Cambridge, England.
367

REVISION

369
370       Last updated: 04 September 2019
371       Copyright (c) 1997-2019 University of Cambridge.
372
373
374
375PCRE2 10.34                    04 September 2019               PCRE2PARTIAL(3)
Impressum