pcre2matching.3 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. .TH PCRE2MATCHING 3 "23 May 2019" "PCRE2 10.34"
  2. .SH NAME
  3. PCRE2 - Perl-compatible regular expressions (revised API)
  4. .SH "PCRE2 MATCHING ALGORITHMS"
  5. .rs
  6. .sp
  7. This document describes the two different algorithms that are available in
  8. PCRE2 for matching a compiled regular expression against a given subject
  9. string. The "standard" algorithm is the one provided by the \fBpcre2_match()\fP
  10. function. This works in the same as as Perl's matching function, and provide a
  11. Perl-compatible matching operation. The just-in-time (JIT) optimization that is
  12. described in the
  13. .\" HREF
  14. \fBpcre2jit\fP
  15. .\"
  16. documentation is compatible with this function.
  17. .P
  18. An alternative algorithm is provided by the \fBpcre2_dfa_match()\fP function;
  19. it operates in a different way, and is not Perl-compatible. This alternative
  20. has advantages and disadvantages compared with the standard algorithm, and
  21. these are described below.
  22. .P
  23. When there is only one possible way in which a given subject string can match a
  24. pattern, the two algorithms give the same answer. A difference arises, however,
  25. when there are multiple possibilities. For example, if the pattern
  26. .sp
  27. ^<.*>
  28. .sp
  29. is matched against the string
  30. .sp
  31. <something> <something else> <something further>
  32. .sp
  33. there are three possible answers. The standard algorithm finds only one of
  34. them, whereas the alternative algorithm finds all three.
  35. .
  36. .
  37. .SH "REGULAR EXPRESSIONS AS TREES"
  38. .rs
  39. .sp
  40. The set of strings that are matched by a regular expression can be represented
  41. as a tree structure. An unlimited repetition in the pattern makes the tree of
  42. infinite size, but it is still a tree. Matching the pattern to a given subject
  43. string (from a given starting point) can be thought of as a search of the tree.
  44. There are two ways to search a tree: depth-first and breadth-first, and these
  45. correspond to the two matching algorithms provided by PCRE2.
  46. .
  47. .
  48. .SH "THE STANDARD MATCHING ALGORITHM"
  49. .rs
  50. .sp
  51. In the terminology of Jeffrey Friedl's book "Mastering Regular Expressions",
  52. the standard algorithm is an "NFA algorithm". It conducts a depth-first search
  53. of the pattern tree. That is, it proceeds along a single path through the tree,
  54. checking that the subject matches what is required. When there is a mismatch,
  55. the algorithm tries any alternatives at the current point, and if they all
  56. fail, it backs up to the previous branch point in the tree, and tries the next
  57. alternative branch at that level. This often involves backing up (moving to the
  58. left) in the subject string as well. The order in which repetition branches are
  59. tried is controlled by the greedy or ungreedy nature of the quantifier.
  60. .P
  61. If a leaf node is reached, a matching string has been found, and at that point
  62. the algorithm stops. Thus, if there is more than one possible match, this
  63. algorithm returns the first one that it finds. Whether this is the shortest,
  64. the longest, or some intermediate length depends on the way the greedy and
  65. ungreedy repetition quantifiers are specified in the pattern.
  66. .P
  67. Because it ends up with a single path through the tree, it is relatively
  68. straightforward for this algorithm to keep track of the substrings that are
  69. matched by portions of the pattern in parentheses. This provides support for
  70. capturing parentheses and backreferences.
  71. .
  72. .
  73. .SH "THE ALTERNATIVE MATCHING ALGORITHM"
  74. .rs
  75. .sp
  76. This algorithm conducts a breadth-first search of the tree. Starting from the
  77. first matching point in the subject, it scans the subject string from left to
  78. right, once, character by character, and as it does this, it remembers all the
  79. paths through the tree that represent valid matches. In Friedl's terminology,
  80. this is a kind of "DFA algorithm", though it is not implemented as a
  81. traditional finite state machine (it keeps multiple states active
  82. simultaneously).
  83. .P
  84. Although the general principle of this matching algorithm is that it scans the
  85. subject string only once, without backtracking, there is one exception: when a
  86. lookaround assertion is encountered, the characters following or preceding the
  87. current point have to be independently inspected.
  88. .P
  89. The scan continues until either the end of the subject is reached, or there are
  90. no more unterminated paths. At this point, terminated paths represent the
  91. different matching possibilities (if there are none, the match has failed).
  92. Thus, if there is more than one possible match, this algorithm finds all of
  93. them, and in particular, it finds the longest. The matches are returned in
  94. decreasing order of length. There is an option to stop the algorithm after the
  95. first match (which is necessarily the shortest) is found.
  96. .P
  97. Note that all the matches that are found start at the same point in the
  98. subject. If the pattern
  99. .sp
  100. cat(er(pillar)?)?
  101. .sp
  102. is matched against the string "the caterpillar catchment", the result is the
  103. three strings "caterpillar", "cater", and "cat" that start at the fifth
  104. character of the subject. The algorithm does not automatically move on to find
  105. matches that start at later positions.
  106. .P
  107. PCRE2's "auto-possessification" optimization usually applies to character
  108. repeats at the end of a pattern (as well as internally). For example, the
  109. pattern "a\ed+" is compiled as if it were "a\ed++" because there is no point
  110. even considering the possibility of backtracking into the repeated digits. For
  111. DFA matching, this means that only one possible match is found. If you really
  112. do want multiple matches in such cases, either use an ungreedy repeat
  113. ("a\ed+?") or set the PCRE2_NO_AUTO_POSSESS option when compiling.
  114. .P
  115. There are a number of features of PCRE2 regular expressions that are not
  116. supported or behave differently in the alternative matching function. Those
  117. that are not supported cause an error if encountered.
  118. .P
  119. 1. Because the algorithm finds all possible matches, the greedy or ungreedy
  120. nature of repetition quantifiers is not relevant (though it may affect
  121. auto-possessification, as just described). During matching, greedy and ungreedy
  122. quantifiers are treated in exactly the same way. However, possessive
  123. quantifiers can make a difference when what follows could also match what is
  124. quantified, for example in a pattern like this:
  125. .sp
  126. ^a++\ew!
  127. .sp
  128. This pattern matches "aaab!" but not "aaa!", which would be matched by a
  129. non-possessive quantifier. Similarly, if an atomic group is present, it is
  130. matched as if it were a standalone pattern at the current point, and the
  131. longest match is then "locked in" for the rest of the overall pattern.
  132. .P
  133. 2. When dealing with multiple paths through the tree simultaneously, it is not
  134. straightforward to keep track of captured substrings for the different matching
  135. possibilities, and PCRE2's implementation of this algorithm does not attempt to
  136. do this. This means that no captured substrings are available.
  137. .P
  138. 3. Because no substrings are captured, backreferences within the pattern are
  139. not supported.
  140. .P
  141. 4. For the same reason, conditional expressions that use a backreference as the
  142. condition or test for a specific group recursion are not supported.
  143. .P
  144. 5. Again for the same reason, script runs are not supported.
  145. .P
  146. 6. Because many paths through the tree may be active, the \eK escape sequence,
  147. which resets the start of the match when encountered (but may be on some paths
  148. and not on others), is not supported.
  149. .P
  150. 7. Callouts are supported, but the value of the \fIcapture_top\fP field is
  151. always 1, and the value of the \fIcapture_last\fP field is always 0.
  152. .P
  153. 8. The \eC escape sequence, which (in the standard algorithm) always matches a
  154. single code unit, even in a UTF mode, is not supported in these modes, because
  155. the alternative algorithm moves through the subject string one character (not
  156. code unit) at a time, for all active paths through the tree.
  157. .P
  158. 9. Except for (*FAIL), the backtracking control verbs such as (*PRUNE) are not
  159. supported. (*FAIL) is supported, and behaves like a failing negative assertion.
  160. .P
  161. 10. The PCRE2_MATCH_INVALID_UTF option for \fBpcre2_compile()\fP is not
  162. supported by \fBpcre2_dfa_match()\fP.
  163. .
  164. .
  165. .SH "ADVANTAGES OF THE ALTERNATIVE ALGORITHM"
  166. .rs
  167. .sp
  168. Using the alternative matching algorithm provides the following advantages:
  169. .P
  170. 1. All possible matches (at a single point in the subject) are automatically
  171. found, and in particular, the longest match is found. To find more than one
  172. match using the standard algorithm, you have to do kludgy things with
  173. callouts.
  174. .P
  175. 2. Because the alternative algorithm scans the subject string just once, and
  176. never needs to backtrack (except for lookbehinds), it is possible to pass very
  177. long subject strings to the matching function in several pieces, checking for
  178. partial matching each time. Although it is also possible to do multi-segment
  179. matching using the standard algorithm, by retaining partially matched
  180. substrings, it is more complicated. The
  181. .\" HREF
  182. \fBpcre2partial\fP
  183. .\"
  184. documentation gives details of partial matching and discusses multi-segment
  185. matching.
  186. .
  187. .
  188. .SH "DISADVANTAGES OF THE ALTERNATIVE ALGORITHM"
  189. .rs
  190. .sp
  191. The alternative algorithm suffers from a number of disadvantages:
  192. .P
  193. 1. It is substantially slower than the standard algorithm. This is partly
  194. because it has to search for all possible matches, but is also because it is
  195. less susceptible to optimization.
  196. .P
  197. 2. Capturing parentheses, backreferences, script runs, and matching within
  198. invalid UTF string are not supported.
  199. .P
  200. 3. Although atomic groups are supported, their use does not provide the
  201. performance advantage that it does for the standard algorithm.
  202. .
  203. .
  204. .SH AUTHOR
  205. .rs
  206. .sp
  207. .nf
  208. Philip Hazel
  209. University Computing Service
  210. Cambridge, England.
  211. .fi
  212. .
  213. .
  214. .SH REVISION
  215. .rs
  216. .sp
  217. .nf
  218. Last updated: 23 May 2019
  219. Copyright (c) 1997-2019 University of Cambridge.
  220. .fi