1<html> 2<head> 3<title>pcre2partial specification</title> 4</head> 5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB"> 6<h1>pcre2partial man page</h1> 7<p> 8Return to the <a href="index.html">PCRE2 index page</a>. 9</p> 10<p> 11This page is part of the PCRE2 HTML documentation. It was generated 12automatically from the original man page. If there is any nonsense in it, 13please consult the man page, in case the conversion went wrong. 14<br> 15<ul> 16<li><a name="TOC1" href="#SEC1">PARTIAL MATCHING IN PCRE2</a> 17<li><a name="TOC2" href="#SEC2">REQUIREMENTS FOR A PARTIAL MATCH</a> 18<li><a name="TOC3" href="#SEC3">PARTIAL MATCHING USING pcre2_match()</a> 19<li><a name="TOC4" href="#SEC4">MULTI-SEGMENT MATCHING WITH pcre2_match()</a> 20<li><a name="TOC5" href="#SEC5">PARTIAL MATCHING USING pcre2_dfa_match()</a> 21<li><a name="TOC6" href="#SEC6">MULTI-SEGMENT MATCHING WITH pcre2_dfa_match()</a> 22<li><a name="TOC7" href="#SEC7">AUTHOR</a> 23<li><a name="TOC8" href="#SEC8">REVISION</a> 24</ul> 25<br><a name="SEC1" href="#TOC1">PARTIAL MATCHING IN PCRE2</a><br> 26<P> 27In normal use of PCRE2, if there is a match up to the end of a subject string, 28but more characters are needed to match the entire pattern, PCRE2_ERROR_NOMATCH 29is returned, just like any other failing match. There are circumstances where 30it might be helpful to distinguish this "partial match" case. 31</P> 32<P> 33One example is an application where the subject string is very long, and not 34all available at once. The requirement here is to be able to do the matching 35segment by segment, but special action is needed when a matched substring spans 36the boundary between two segments. 37</P> 38<P> 39Another example is checking a user input string as it is typed, to ensure that 40it conforms to a required format. Invalid characters can be immediately 41diagnosed and rejected, giving instant feedback. 42</P> 43<P> 44Partial matching is a PCRE2-specific feature; it is not Perl-compatible. It is 45requested by setting one of the PCRE2_PARTIAL_HARD or PCRE2_PARTIAL_SOFT 46options when calling a matching function. The difference between the two 47options is whether or not a partial match is preferred to an alternative 48complete match, though the details differ between the two types of matching 49function. If both options are set, PCRE2_PARTIAL_HARD takes precedence. 50</P> 51<P> 52If you want to use partial matching with just-in-time optimized code, as well 53as setting a partial match option for the matching function, you must also call 54<b>pcre2_jit_compile()</b> with one or both of these options: 55<pre> 56 PCRE2_JIT_PARTIAL_HARD 57 PCRE2_JIT_PARTIAL_SOFT 58</pre> 59PCRE2_JIT_COMPLETE should also be set if you are going to run non-partial 60matches on the same pattern. Separate code is compiled for each mode. If the 61appropriate JIT mode has not been compiled, interpretive matching code is used. 62</P> 63<P> 64Setting a partial matching option disables two of PCRE2's standard 65optimization hints. PCRE2 remembers the last literal code unit in a pattern, 66and abandons matching immediately if it is not present in the subject string. 67This optimization cannot be used for a subject string that might match only 68partially. PCRE2 also remembers a minimum length of a matching string, and does 69not bother to run the matching function on shorter strings. This optimization 70is also disabled for partial matching. 71</P> 72<br><a name="SEC2" href="#TOC1">REQUIREMENTS FOR A PARTIAL MATCH</a><br> 73<P> 74A possible partial match occurs during matching when the end of the subject 75string is reached successfully, but either more characters are needed to 76complete the match, or the addition of more characters might change what is 77matched. 78</P> 79<P> 80Example 1: if the pattern is /abc/ and the subject is "ab", more characters are 81definitely needed to complete a match. In this case both hard and soft matching 82options yield a partial match. 83</P> 84<P> 85Example 2: if the pattern is /ab+/ and the subject is "ab", a complete match 86can be found, but the addition of more characters might change what is 87matched. In this case, only PCRE2_PARTIAL_HARD returns a partial match; 88PCRE2_PARTIAL_SOFT returns the complete match. 89</P> 90<P> 91On reaching the end of the subject, when PCRE2_PARTIAL_HARD is set, if the next 92pattern item is \z, \Z, \b, \B, or $ there is always a partial match. 93Otherwise, for both options, the next pattern item must be one that inspects a 94character, and at least one of the following must be true: 95</P> 96<P> 97(1) At least one character has already been inspected. An inspected character 98need not form part of the final matched string; lookbehind assertions and the 99\K escape sequence provide ways of inspecting characters before the start of a 100matched string. 101</P> 102<P> 103(2) The pattern contains one or more lookbehind assertions. This condition 104exists in case there is a lookbehind that inspects characters before the start 105of the match. 106</P> 107<P> 108(3) There is a special case when the whole pattern can match an empty string. 109When the starting point is at the end of the subject, the empty string match is 110a possibility, and if PCRE2_PARTIAL_SOFT is set and neither of the above 111conditions is true, it is returned. However, because adding more characters 112might result in a non-empty match, PCRE2_PARTIAL_HARD returns a partial match, 113which in this case means "there is going to be a match at this point, but until 114some more characters are added, we do not know if it will be an empty string or 115something longer". 116</P> 117<br><a name="SEC3" href="#TOC1">PARTIAL MATCHING USING pcre2_match()</a><br> 118<P> 119When a partial matching option is set, the result of calling 120<b>pcre2_match()</b> can be one of the following: 121</P> 122<P> 123<b>A successful match</b> 124A complete match has been found, starting and ending within this subject. 125</P> 126<P> 127<b>PCRE2_ERROR_NOMATCH</b> 128No match can start anywhere in this subject. 129</P> 130<P> 131<b>PCRE2_ERROR_PARTIAL</b> 132Adding more characters may result in a complete match that uses one or more 133characters from the end of this subject. 134</P> 135<P> 136When a partial match is returned, the first two elements in the ovector point 137to the portion of the subject that was matched, but the values in the rest of 138the ovector are undefined. The appearance of \K in the pattern has no effect 139for a partial match. Consider this pattern: 140<pre> 141 /abc\K123/ 142</pre> 143If it is matched against "456abc123xyz" the result is a complete match, and the 144ovector defines the matched string as "123", because \K resets the "start of 145match" point. However, if a partial match is requested and the subject string 146is "456abc12", a partial match is found for the string "abc12", because all 147these characters are needed for a subsequent re-match with additional 148characters. 149</P> 150<P> 151If there is more than one partial match, the first one that was found provides 152the data that is returned. Consider this pattern: 153<pre> 154 /123\w+X|dogY/ 155</pre> 156If this is matched against the subject string "abc123dog", both alternatives 157fail to match, but the end of the subject is reached during matching, so 158PCRE2_ERROR_PARTIAL is returned. The offsets are set to 3 and 9, identifying 159"123dog" as the first partial match. (In this example, there are two partial 160matches, because "dog" on its own partially matches the second alternative.) 161</P> 162<br><b> 163How a partial match is processed by pcre2_match() 164</b><br> 165<P> 166What happens when a partial match is identified depends on which of the two 167partial matching options is set. 168</P> 169<P> 170If PCRE2_PARTIAL_HARD is set, PCRE2_ERROR_PARTIAL is returned as soon as a 171partial match is found, without continuing to search for possible complete 172matches. This option is "hard" because it prefers an earlier partial match over 173a later complete match. For this reason, the assumption is made that the end of 174the supplied subject string is not the true end of the available data, which is 175why \z, \Z, \b, \B, and $ always give a partial match. 176</P> 177<P> 178If PCRE2_PARTIAL_SOFT is set, the partial match is remembered, but matching 179continues as normal, and other alternatives in the pattern are tried. If no 180complete match can be found, PCRE2_ERROR_PARTIAL is returned instead of 181PCRE2_ERROR_NOMATCH. This option is "soft" because it prefers a complete match 182over a partial match. All the various matching items in a pattern behave as if 183the subject string is potentially complete; \z, \Z, and $ match at the end of 184the subject, as normal, and for \b and \B the end of the subject is treated 185as a non-alphanumeric. 186</P> 187<P> 188The difference between the two partial matching options can be illustrated by a 189pattern such as: 190<pre> 191 /dog(sbody)?/ 192</pre> 193This matches either "dog" or "dogsbody", greedily (that is, it prefers the 194longer string if possible). If it is matched against the string "dog" with 195PCRE2_PARTIAL_SOFT, it yields a complete match for "dog". However, if 196PCRE2_PARTIAL_HARD is set, the result is PCRE2_ERROR_PARTIAL. On the other 197hand, if the pattern is made ungreedy the result is different: 198<pre> 199 /dog(sbody)??/ 200</pre> 201In this case the result is always a complete match because that is found first, 202and matching never continues after finding a complete match. It might be easier 203to follow this explanation by thinking of the two patterns like this: 204<pre> 205 /dog(sbody)?/ is the same as /dogsbody|dog/ 206 /dog(sbody)??/ is the same as /dog|dogsbody/ 207</pre> 208The second pattern will never match "dogsbody", because it will always find the 209shorter match first. 210</P> 211<br><b> 212Example of partial matching using pcre2test 213</b><br> 214<P> 215The <b>pcre2test</b> data modifiers <b>partial_hard</b> (or <b>ph</b>) and 216<b>partial_soft</b> (or <b>ps</b>) set PCRE2_PARTIAL_HARD and PCRE2_PARTIAL_SOFT, 217respectively, when calling <b>pcre2_match()</b>. Here is a run of 218<b>pcre2test</b> using a pattern that matches the whole subject in the form of a 219date: 220<pre> 221 re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/ 222 data> 25dec3\=ph 223 Partial match: 23dec3 224 data> 3ju\=ph 225 Partial match: 3ju 226 data> 3juj\=ph 227 No match 228</pre> 229This example gives the same results for both hard and soft partial matching 230options. Here is an example where there is a difference: 231<pre> 232 re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/ 233 data> 25jun04\=ps 234 0: 25jun04 235 1: jun 236 data> 25jun04\=ph 237 Partial match: 25jun04 238</pre> 239With PCRE2_PARTIAL_SOFT, the subject is matched completely. For 240PCRE2_PARTIAL_HARD, however, the subject is assumed not to be complete, so 241there is only a partial match. 242</P> 243<br><a name="SEC4" href="#TOC1">MULTI-SEGMENT MATCHING WITH pcre2_match()</a><br> 244<P> 245PCRE was not originally designed with multi-segment matching in mind. However, 246over time, features (including partial matching) that make multi-segment 247matching possible have been added. A very long string can be searched segment 248by segment by calling <b>pcre2_match()</b> repeatedly, with the aim of achieving 249the same results that would happen if the entire string was available for 250searching all the time. Normally, the strings that are being sought are much 251shorter than each individual segment, and are in the middle of very long 252strings, so the pattern is normally not anchored. 253</P> 254<P> 255Special logic must be implemented to handle a matched substring that spans a 256segment boundary. PCRE2_PARTIAL_HARD should be used, because it returns a 257partial match at the end of a segment whenever there is the possibility of 258changing the match by adding more characters. The PCRE2_NOTBOL option should 259also be set for all but the first segment. 260</P> 261<P> 262When a partial match occurs, the next segment must be added to the current 263subject and the match re-run, using the <i>startoffset</i> argument of 264<b>pcre2_match()</b> to begin at the point where the partial match started. 265For example: 266<pre> 267 re> /\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d/ 268 data> ...the date is 23ja\=ph 269 Partial match: 23ja 270 data> ...the date is 23jan19 and on that day...\=offset=15 271 0: 23jan19 272 1: jan 273</pre> 274Note the use of the <b>offset</b> modifier to start the new match where the 275partial match was found. In this example, the next segment was added to the one 276in which the partial match was found. This is the most straightforward 277approach, typically using a memory buffer that is twice the size of each 278segment. After a partial match, the first half of the buffer is discarded, the 279second half is moved to the start of the buffer, and a new segment is added 280before repeating the match as in the example above. After a no match, the 281entire buffer can be discarded. 282</P> 283<P> 284If there are memory constraints, you may want to discard text that precedes a 285partial match before adding the next segment. Unfortunately, this is not at 286present straightforward. In cases such as the above, where the pattern does not 287contain any lookbehinds, it is sufficient to retain only the partially matched 288substring. However, if the pattern contains a lookbehind assertion, characters 289that precede the start of the partial match may have been inspected during the 290matching process. When <b>pcre2test</b> displays a partial match, it indicates 291these characters with '<' if the <b>allusedtext</b> modifier is set: 292<pre> 293 re> "(?<=123)abc" 294 data> xx123ab\=ph,allusedtext 295 Partial match: 123ab 296 <<< 297</pre> 298However, the <b>allusedtext</b> modifier is not available for JIT matching, 299because JIT matching does not record the first (or last) consulted characters. 300For this reason, this information is not available via the API. It is therefore 301not possible in general to obtain the exact number of characters that must be 302retained in order to get the right match result. If you cannot retain the 303entire segment, you must find some heuristic way of choosing. 304</P> 305<P> 306If you know the approximate length of the matching substrings, you can use that 307to decide how much text to retain. The only lookbehind information that is 308currently available via the API is the length of the longest individual 309lookbehind in a pattern, but this can be misleading if there are nested 310lookbehinds. The value returned by calling <b>pcre2_pattern_info()</b> with the 311PCRE2_INFO_MAXLOOKBEHIND option is the maximum number of characters (not code 312units) that any individual lookbehind moves back when it is processed. A 313pattern such as "(?<=(?<!b)a)" has a maximum lookbehind value of one, but 314inspects two characters before its starting point. 315</P> 316<P> 317In a non-UTF or a 32-bit case, moving back is just a subtraction, but in 318UTF-8 or UTF-16 you have to count characters while moving back through the code 319units. 320</P> 321<br><a name="SEC5" href="#TOC1">PARTIAL MATCHING USING pcre2_dfa_match()</a><br> 322<P> 323The DFA function moves along the subject string character by character, without 324backtracking, searching for all possible matches simultaneously. If the end of 325the subject is reached before the end of the pattern, there is the possibility 326of a partial match. 327</P> 328<P> 329When PCRE2_PARTIAL_SOFT is set, PCRE2_ERROR_PARTIAL is returned only if there 330have been no complete matches. Otherwise, the complete matches are returned. 331If PCRE2_PARTIAL_HARD is set, a partial match takes precedence over any 332complete matches. The portion of the string that was matched when the longest 333partial match was found is set as the first matching string. 334</P> 335<P> 336Because the DFA function always searches for all possible matches, and there is 337no difference between greedy and ungreedy repetition, its behaviour is 338different from the <b>pcre2_match()</b>. Consider the string "dog" matched 339against this ungreedy pattern: 340<pre> 341 /dog(sbody)??/ 342</pre> 343Whereas the standard function stops as soon as it finds the complete match for 344"dog", the DFA function also finds the partial match for "dogsbody", and so 345returns that when PCRE2_PARTIAL_HARD is set. 346</P> 347<br><a name="SEC6" href="#TOC1">MULTI-SEGMENT MATCHING WITH pcre2_dfa_match()</a><br> 348<P> 349When a partial match has been found using the DFA matching function, it is 350possible to continue the match by providing additional subject data and calling 351the function again with the same compiled regular expression, this time setting 352the PCRE2_DFA_RESTART option. You must pass the same working space as before, 353because this is where details of the previous partial match are stored. You can 354set the PCRE2_PARTIAL_SOFT or PCRE2_PARTIAL_HARD options with PCRE2_DFA_RESTART 355to continue partial matching over multiple segments. Here is an example using 356<b>pcre2test</b>: 357<pre> 358 re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/ 359 data> 23ja\=dfa,ps 360 Partial match: 23ja 361 data> n05\=dfa,dfa_restart 362 0: n05 363</pre> 364The first call has "23ja" as the subject, and requests partial matching; the 365second call has "n05" as the subject for the continued (restarted) match. 366Notice that when the match is complete, only the last part is shown; PCRE2 does 367not retain the previously partially-matched string. It is up to the calling 368program to do that if it needs to. This means that, for an unanchored pattern, 369if a continued match fails, it is not possible to try again at a new starting 370point. All this facility is capable of doing is continuing with the previous 371match attempt. For example, consider this pattern: 372<pre> 373 1234|3789 374</pre> 375If the first part of the subject is "ABC123", a partial match of the first 376alternative is found at offset 3. There is no partial match for the second 377alternative, because such a match does not start at the same point in the 378subject string. Attempting to continue with the string "7890" does not yield a 379match because only those alternatives that match at one point in the subject 380are remembered. Depending on the application, this may or may not be what you 381want. 382</P> 383<P> 384If you do want to allow for starting again at the next character, one way of 385doing it is to retain some or all of the segment and try a new complete match, 386as described for <b>pcre2_match()</b> above. Another possibility is to work with 387two buffers. If a partial match at offset <i>n</i> in the first buffer is 388followed by "no match" when PCRE2_DFA_RESTART is used on the second buffer, you 389can then try a new match starting at offset <i>n+1</i> in the first buffer. 390</P> 391<br><a name="SEC7" href="#TOC1">AUTHOR</a><br> 392<P> 393Philip Hazel 394<br> 395University Computing Service 396<br> 397Cambridge, England. 398<br> 399</P> 400<br><a name="SEC8" href="#TOC1">REVISION</a><br> 401<P> 402Last updated: 04 September 2019 403<br> 404Copyright © 1997-2019 University of Cambridge. 405<br> 406<p> 407Return to the <a href="index.html">PCRE2 index page</a>. 408</p> 409