github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
18 KiB

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