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.

4186 lines
191 KiB

  1. <html>
  2. <head>
  3. <title>pcre2api specification</title>
  4. </head>
  5. <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
  6. <h1>pcre2api 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">PCRE2 NATIVE API BASIC FUNCTIONS</a>
  17. <li><a name="TOC2" href="#SEC2">PCRE2 NATIVE API AUXILIARY MATCH FUNCTIONS</a>
  18. <li><a name="TOC3" href="#SEC3">PCRE2 NATIVE API GENERAL CONTEXT FUNCTIONS</a>
  19. <li><a name="TOC4" href="#SEC4">PCRE2 NATIVE API COMPILE CONTEXT FUNCTIONS</a>
  20. <li><a name="TOC5" href="#SEC5">PCRE2 NATIVE API MATCH CONTEXT FUNCTIONS</a>
  21. <li><a name="TOC6" href="#SEC6">PCRE2 NATIVE API STRING EXTRACTION FUNCTIONS</a>
  22. <li><a name="TOC7" href="#SEC7">PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION</a>
  23. <li><a name="TOC8" href="#SEC8">PCRE2 NATIVE API JIT FUNCTIONS</a>
  24. <li><a name="TOC9" href="#SEC9">PCRE2 NATIVE API SERIALIZATION FUNCTIONS</a>
  25. <li><a name="TOC10" href="#SEC10">PCRE2 NATIVE API AUXILIARY FUNCTIONS</a>
  26. <li><a name="TOC11" href="#SEC11">PCRE2 NATIVE API OBSOLETE FUNCTIONS</a>
  27. <li><a name="TOC12" href="#SEC12">PCRE2 EXPERIMENTAL PATTERN CONVERSION FUNCTIONS</a>
  28. <li><a name="TOC13" href="#SEC13">PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES</a>
  29. <li><a name="TOC14" href="#SEC14">PCRE2 API OVERVIEW</a>
  30. <li><a name="TOC15" href="#SEC15">STRING LENGTHS AND OFFSETS</a>
  31. <li><a name="TOC16" href="#SEC16">NEWLINES</a>
  32. <li><a name="TOC17" href="#SEC17">MULTITHREADING</a>
  33. <li><a name="TOC18" href="#SEC18">PCRE2 CONTEXTS</a>
  34. <li><a name="TOC19" href="#SEC19">CHECKING BUILD-TIME OPTIONS</a>
  35. <li><a name="TOC20" href="#SEC20">COMPILING A PATTERN</a>
  36. <li><a name="TOC21" href="#SEC21">JUST-IN-TIME (JIT) COMPILATION</a>
  37. <li><a name="TOC22" href="#SEC22">LOCALE SUPPORT</a>
  38. <li><a name="TOC23" href="#SEC23">INFORMATION ABOUT A COMPILED PATTERN</a>
  39. <li><a name="TOC24" href="#SEC24">INFORMATION ABOUT A PATTERN'S CALLOUTS</a>
  40. <li><a name="TOC25" href="#SEC25">SERIALIZATION AND PRECOMPILING</a>
  41. <li><a name="TOC26" href="#SEC26">THE MATCH DATA BLOCK</a>
  42. <li><a name="TOC27" href="#SEC27">MEMORY USE FOR MATCH DATA BLOCKS</a>
  43. <li><a name="TOC28" href="#SEC28">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a>
  44. <li><a name="TOC29" href="#SEC29">NEWLINE HANDLING WHEN MATCHING</a>
  45. <li><a name="TOC30" href="#SEC30">HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS</a>
  46. <li><a name="TOC31" href="#SEC31">OTHER INFORMATION ABOUT A MATCH</a>
  47. <li><a name="TOC32" href="#SEC32">ERROR RETURNS FROM <b>pcre2_match()</b></a>
  48. <li><a name="TOC33" href="#SEC33">OBTAINING A TEXTUAL ERROR MESSAGE</a>
  49. <li><a name="TOC34" href="#SEC34">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a>
  50. <li><a name="TOC35" href="#SEC35">EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS</a>
  51. <li><a name="TOC36" href="#SEC36">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a>
  52. <li><a name="TOC37" href="#SEC37">CREATING A NEW STRING WITH SUBSTITUTIONS</a>
  53. <li><a name="TOC38" href="#SEC38">DUPLICATE CAPTURE GROUP NAMES</a>
  54. <li><a name="TOC39" href="#SEC39">FINDING ALL POSSIBLE MATCHES AT ONE POSITION</a>
  55. <li><a name="TOC40" href="#SEC40">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a>
  56. <li><a name="TOC41" href="#SEC41">SEE ALSO</a>
  57. <li><a name="TOC42" href="#SEC42">AUTHOR</a>
  58. <li><a name="TOC43" href="#SEC43">REVISION</a>
  59. </ul>
  60. <P>
  61. <b>#include &#60;pcre2.h&#62;</b>
  62. <br>
  63. <br>
  64. PCRE2 is a new API for PCRE, starting at release 10.0. This document contains a
  65. description of all its native functions. See the
  66. <a href="pcre2.html"><b>pcre2</b></a>
  67. document for an overview of all the PCRE2 documentation.
  68. </P>
  69. <br><a name="SEC1" href="#TOC1">PCRE2 NATIVE API BASIC FUNCTIONS</a><br>
  70. <P>
  71. <b>pcre2_code *pcre2_compile(PCRE2_SPTR <i>pattern</i>, PCRE2_SIZE <i>length</i>,</b>
  72. <b> uint32_t <i>options</i>, int *<i>errorcode</i>, PCRE2_SIZE *<i>erroroffset,</i></b>
  73. <b> pcre2_compile_context *<i>ccontext</i>);</b>
  74. <br>
  75. <br>
  76. <b>void pcre2_code_free(pcre2_code *<i>code</i>);</b>
  77. <br>
  78. <br>
  79. <b>pcre2_match_data *pcre2_match_data_create(uint32_t <i>ovecsize</i>,</b>
  80. <b> pcre2_general_context *<i>gcontext</i>);</b>
  81. <br>
  82. <br>
  83. <b>pcre2_match_data *pcre2_match_data_create_from_pattern(</b>
  84. <b> const pcre2_code *<i>code</i>, pcre2_general_context *<i>gcontext</i>);</b>
  85. <br>
  86. <br>
  87. <b>int pcre2_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  88. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  89. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  90. <b> pcre2_match_context *<i>mcontext</i>);</b>
  91. <br>
  92. <br>
  93. <b>int pcre2_dfa_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  94. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  95. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  96. <b> pcre2_match_context *<i>mcontext</i>,</b>
  97. <b> int *<i>workspace</i>, PCRE2_SIZE <i>wscount</i>);</b>
  98. <br>
  99. <br>
  100. <b>void pcre2_match_data_free(pcre2_match_data *<i>match_data</i>);</b>
  101. </P>
  102. <br><a name="SEC2" href="#TOC1">PCRE2 NATIVE API AUXILIARY MATCH FUNCTIONS</a><br>
  103. <P>
  104. <b>PCRE2_SPTR pcre2_get_mark(pcre2_match_data *<i>match_data</i>);</b>
  105. <br>
  106. <br>
  107. <b>PCRE2_SIZE pcre2_get_match_data_size(pcre2_match_data *<i>match_data</i>);</b>
  108. <br>
  109. <br>
  110. <b>PCRE2_SIZE pcre2_get_match_data_heapframes_size(</b>
  111. <b> pcre2_match_data *<i>match_data</i>);</b>
  112. <br>
  113. <br>
  114. <b>uint32_t pcre2_get_ovector_count(pcre2_match_data *<i>match_data</i>);</b>
  115. <br>
  116. <br>
  117. <b>PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *<i>match_data</i>);</b>
  118. <br>
  119. <br>
  120. <b>PCRE2_SIZE pcre2_get_startchar(pcre2_match_data *<i>match_data</i>);</b>
  121. </P>
  122. <br><a name="SEC3" href="#TOC1">PCRE2 NATIVE API GENERAL CONTEXT FUNCTIONS</a><br>
  123. <P>
  124. <b>pcre2_general_context *pcre2_general_context_create(</b>
  125. <b> void *(*<i>private_malloc</i>)(PCRE2_SIZE, void *),</b>
  126. <b> void (*<i>private_free</i>)(void *, void *), void *<i>memory_data</i>);</b>
  127. <br>
  128. <br>
  129. <b>pcre2_general_context *pcre2_general_context_copy(</b>
  130. <b> pcre2_general_context *<i>gcontext</i>);</b>
  131. <br>
  132. <br>
  133. <b>void pcre2_general_context_free(pcre2_general_context *<i>gcontext</i>);</b>
  134. </P>
  135. <br><a name="SEC4" href="#TOC1">PCRE2 NATIVE API COMPILE CONTEXT FUNCTIONS</a><br>
  136. <P>
  137. <b>pcre2_compile_context *pcre2_compile_context_create(</b>
  138. <b> pcre2_general_context *<i>gcontext</i>);</b>
  139. <br>
  140. <br>
  141. <b>pcre2_compile_context *pcre2_compile_context_copy(</b>
  142. <b> pcre2_compile_context *<i>ccontext</i>);</b>
  143. <br>
  144. <br>
  145. <b>void pcre2_compile_context_free(pcre2_compile_context *<i>ccontext</i>);</b>
  146. <br>
  147. <br>
  148. <b>int pcre2_set_bsr(pcre2_compile_context *<i>ccontext</i>,</b>
  149. <b> uint32_t <i>value</i>);</b>
  150. <br>
  151. <br>
  152. <b>int pcre2_set_character_tables(pcre2_compile_context *<i>ccontext</i>,</b>
  153. <b> const uint8_t *<i>tables</i>);</b>
  154. <br>
  155. <br>
  156. <b>int pcre2_set_compile_extra_options(pcre2_compile_context *<i>ccontext</i>,</b>
  157. <b> uint32_t <i>extra_options</i>);</b>
  158. <br>
  159. <br>
  160. <b>int pcre2_set_max_pattern_length(pcre2_compile_context *<i>ccontext</i>,</b>
  161. <b> PCRE2_SIZE <i>value</i>);</b>
  162. <br>
  163. <br>
  164. <b>int pcre2_set_max_pattern_compiled_length(</b>
  165. <b> pcre2_compile_context *<i>ccontext</i>, PCRE2_SIZE <i>value</i>);</b>
  166. <br>
  167. <br>
  168. <b>int pcre2_set_max_varlookbehind(pcre2_compile_contest *<i>ccontext</i>,</b>
  169. <b>" uint32_t <i>value</i>);</b>
  170. <br>
  171. <br>
  172. <b>int pcre2_set_newline(pcre2_compile_context *<i>ccontext</i>,</b>
  173. <b> uint32_t <i>value</i>);</b>
  174. <br>
  175. <br>
  176. <b>int pcre2_set_parens_nest_limit(pcre2_compile_context *<i>ccontext</i>,</b>
  177. <b> uint32_t <i>value</i>);</b>
  178. <br>
  179. <br>
  180. <b>int pcre2_set_compile_recursion_guard(pcre2_compile_context *<i>ccontext</i>,</b>
  181. <b> int (*<i>guard_function</i>)(uint32_t, void *), void *<i>user_data</i>);</b>
  182. </P>
  183. <br><a name="SEC5" href="#TOC1">PCRE2 NATIVE API MATCH CONTEXT FUNCTIONS</a><br>
  184. <P>
  185. <b>pcre2_match_context *pcre2_match_context_create(</b>
  186. <b> pcre2_general_context *<i>gcontext</i>);</b>
  187. <br>
  188. <br>
  189. <b>pcre2_match_context *pcre2_match_context_copy(</b>
  190. <b> pcre2_match_context *<i>mcontext</i>);</b>
  191. <br>
  192. <br>
  193. <b>void pcre2_match_context_free(pcre2_match_context *<i>mcontext</i>);</b>
  194. <br>
  195. <br>
  196. <b>int pcre2_set_callout(pcre2_match_context *<i>mcontext</i>,</b>
  197. <b> int (*<i>callout_function</i>)(pcre2_callout_block *, void *),</b>
  198. <b> void *<i>callout_data</i>);</b>
  199. <br>
  200. <br>
  201. <b>int pcre2_set_substitute_callout(pcre2_match_context *<i>mcontext</i>,</b>
  202. <b> int (*<i>callout_function</i>)(pcre2_substitute_callout_block *, void *),</b>
  203. <b> void *<i>callout_data</i>);</b>
  204. <br>
  205. <br>
  206. <b>int pcre2_set_offset_limit(pcre2_match_context *<i>mcontext</i>,</b>
  207. <b> PCRE2_SIZE <i>value</i>);</b>
  208. <br>
  209. <br>
  210. <b>int pcre2_set_heap_limit(pcre2_match_context *<i>mcontext</i>,</b>
  211. <b> uint32_t <i>value</i>);</b>
  212. <br>
  213. <br>
  214. <b>int pcre2_set_match_limit(pcre2_match_context *<i>mcontext</i>,</b>
  215. <b> uint32_t <i>value</i>);</b>
  216. <br>
  217. <br>
  218. <b>int pcre2_set_depth_limit(pcre2_match_context *<i>mcontext</i>,</b>
  219. <b> uint32_t <i>value</i>);</b>
  220. </P>
  221. <br><a name="SEC6" href="#TOC1">PCRE2 NATIVE API STRING EXTRACTION FUNCTIONS</a><br>
  222. <P>
  223. <b>int pcre2_substring_copy_byname(pcre2_match_data *<i>match_data</i>,</b>
  224. <b> PCRE2_SPTR <i>name</i>, PCRE2_UCHAR *<i>buffer</i>, PCRE2_SIZE *<i>bufflen</i>);</b>
  225. <br>
  226. <br>
  227. <b>int pcre2_substring_copy_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  228. <b> uint32_t <i>number</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
  229. <b> PCRE2_SIZE *<i>bufflen</i>);</b>
  230. <br>
  231. <br>
  232. <b>void pcre2_substring_free(PCRE2_UCHAR *<i>buffer</i>);</b>
  233. <br>
  234. <br>
  235. <b>int pcre2_substring_get_byname(pcre2_match_data *<i>match_data</i>,</b>
  236. <b> PCRE2_SPTR <i>name</i>, PCRE2_UCHAR **<i>bufferptr</i>, PCRE2_SIZE *<i>bufflen</i>);</b>
  237. <br>
  238. <br>
  239. <b>int pcre2_substring_get_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  240. <b> uint32_t <i>number</i>, PCRE2_UCHAR **<i>bufferptr</i>,</b>
  241. <b> PCRE2_SIZE *<i>bufflen</i>);</b>
  242. <br>
  243. <br>
  244. <b>int pcre2_substring_length_byname(pcre2_match_data *<i>match_data</i>,</b>
  245. <b> PCRE2_SPTR <i>name</i>, PCRE2_SIZE *<i>length</i>);</b>
  246. <br>
  247. <br>
  248. <b>int pcre2_substring_length_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  249. <b> uint32_t <i>number</i>, PCRE2_SIZE *<i>length</i>);</b>
  250. <br>
  251. <br>
  252. <b>int pcre2_substring_nametable_scan(const pcre2_code *<i>code</i>,</b>
  253. <b> PCRE2_SPTR <i>name</i>, PCRE2_SPTR *<i>first</i>, PCRE2_SPTR *<i>last</i>);</b>
  254. <br>
  255. <br>
  256. <b>int pcre2_substring_number_from_name(const pcre2_code *<i>code</i>,</b>
  257. <b> PCRE2_SPTR <i>name</i>);</b>
  258. <br>
  259. <br>
  260. <b>void pcre2_substring_list_free(PCRE2_UCHAR **<i>list</i>);</b>
  261. <br>
  262. <br>
  263. <b>int pcre2_substring_list_get(pcre2_match_data *<i>match_data</i>,</b>
  264. <b>" PCRE2_UCHAR ***<i>listptr</i>, PCRE2_SIZE **<i>lengthsptr</i>);</b>
  265. </P>
  266. <br><a name="SEC7" href="#TOC1">PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION</a><br>
  267. <P>
  268. <b>int pcre2_substitute(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  269. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  270. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  271. <b> pcre2_match_context *<i>mcontext</i>, PCRE2_SPTR <i>replacementz</i>,</b>
  272. <b> PCRE2_SIZE <i>rlength</i>, PCRE2_UCHAR *<i>outputbuffer</i>,</b>
  273. <b> PCRE2_SIZE *<i>outlengthptr</i>);</b>
  274. </P>
  275. <br><a name="SEC8" href="#TOC1">PCRE2 NATIVE API JIT FUNCTIONS</a><br>
  276. <P>
  277. <b>int pcre2_jit_compile(pcre2_code *<i>code</i>, uint32_t <i>options</i>);</b>
  278. <br>
  279. <br>
  280. <b>int pcre2_jit_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  281. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  282. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  283. <b> pcre2_match_context *<i>mcontext</i>);</b>
  284. <br>
  285. <br>
  286. <b>void pcre2_jit_free_unused_memory(pcre2_general_context *<i>gcontext</i>);</b>
  287. <br>
  288. <br>
  289. <b>pcre2_jit_stack *pcre2_jit_stack_create(size_t <i>startsize</i>,</b>
  290. <b> size_t <i>maxsize</i>, pcre2_general_context *<i>gcontext</i>);</b>
  291. <br>
  292. <br>
  293. <b>void pcre2_jit_stack_assign(pcre2_match_context *<i>mcontext</i>,</b>
  294. <b> pcre2_jit_callback <i>callback_function</i>, void *<i>callback_data</i>);</b>
  295. <br>
  296. <br>
  297. <b>void pcre2_jit_stack_free(pcre2_jit_stack *<i>jit_stack</i>);</b>
  298. </P>
  299. <br><a name="SEC9" href="#TOC1">PCRE2 NATIVE API SERIALIZATION FUNCTIONS</a><br>
  300. <P>
  301. <b>int32_t pcre2_serialize_decode(pcre2_code **<i>codes</i>,</b>
  302. <b> int32_t <i>number_of_codes</i>, const uint8_t *<i>bytes</i>,</b>
  303. <b> pcre2_general_context *<i>gcontext</i>);</b>
  304. <br>
  305. <br>
  306. <b>int32_t pcre2_serialize_encode(const pcre2_code **<i>codes</i>,</b>
  307. <b> int32_t <i>number_of_codes</i>, uint8_t **<i>serialized_bytes</i>,</b>
  308. <b> PCRE2_SIZE *<i>serialized_size</i>, pcre2_general_context *<i>gcontext</i>);</b>
  309. <br>
  310. <br>
  311. <b>void pcre2_serialize_free(uint8_t *<i>bytes</i>);</b>
  312. <br>
  313. <br>
  314. <b>int32_t pcre2_serialize_get_number_of_codes(const uint8_t *<i>bytes</i>);</b>
  315. </P>
  316. <br><a name="SEC10" href="#TOC1">PCRE2 NATIVE API AUXILIARY FUNCTIONS</a><br>
  317. <P>
  318. <b>pcre2_code *pcre2_code_copy(const pcre2_code *<i>code</i>);</b>
  319. <br>
  320. <br>
  321. <b>pcre2_code *pcre2_code_copy_with_tables(const pcre2_code *<i>code</i>);</b>
  322. <br>
  323. <br>
  324. <b>int pcre2_get_error_message(int <i>errorcode</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
  325. <b> PCRE2_SIZE <i>bufflen</i>);</b>
  326. <br>
  327. <br>
  328. <b>const uint8_t *pcre2_maketables(pcre2_general_context *<i>gcontext</i>);</b>
  329. <br>
  330. <br>
  331. <b>void pcre2_maketables_free(pcre2_general_context *<i>gcontext</i>,</b>
  332. <b> const uint8_t *<i>tables</i>);</b>
  333. <br>
  334. <br>
  335. <b>int pcre2_pattern_info(const pcre2_code *<i>code</i>, uint32_t <i>what</i>,</b>
  336. <b> void *<i>where</i>);</b>
  337. <br>
  338. <br>
  339. <b>int pcre2_callout_enumerate(const pcre2_code *<i>code</i>,</b>
  340. <b> int (*<i>callback</i>)(pcre2_callout_enumerate_block *, void *),</b>
  341. <b> void *<i>user_data</i>);</b>
  342. <br>
  343. <br>
  344. <b>int pcre2_config(uint32_t <i>what</i>, void *<i>where</i>);</b>
  345. </P>
  346. <br><a name="SEC11" href="#TOC1">PCRE2 NATIVE API OBSOLETE FUNCTIONS</a><br>
  347. <P>
  348. <b>int pcre2_set_recursion_limit(pcre2_match_context *<i>mcontext</i>,</b>
  349. <b> uint32_t <i>value</i>);</b>
  350. <br>
  351. <br>
  352. <b>int pcre2_set_recursion_memory_management(</b>
  353. <b> pcre2_match_context *<i>mcontext</i>,</b>
  354. <b> void *(*<i>private_malloc</i>)(size_t, void *),</b>
  355. <b> void (*<i>private_free</i>)(void *, void *), void *<i>memory_data</i>);</b>
  356. <br>
  357. <br>
  358. These functions became obsolete at release 10.30 and are retained only for
  359. backward compatibility. They should not be used in new code. The first is
  360. replaced by <b>pcre2_set_depth_limit()</b>; the second is no longer needed and
  361. has no effect (it always returns zero).
  362. </P>
  363. <br><a name="SEC12" href="#TOC1">PCRE2 EXPERIMENTAL PATTERN CONVERSION FUNCTIONS</a><br>
  364. <P>
  365. <b>pcre2_convert_context *pcre2_convert_context_create(</b>
  366. <b> pcre2_general_context *<i>gcontext</i>);</b>
  367. <br>
  368. <br>
  369. <b>pcre2_convert_context *pcre2_convert_context_copy(</b>
  370. <b> pcre2_convert_context *<i>cvcontext</i>);</b>
  371. <br>
  372. <br>
  373. <b>void pcre2_convert_context_free(pcre2_convert_context *<i>cvcontext</i>);</b>
  374. <br>
  375. <br>
  376. <b>int pcre2_set_glob_escape(pcre2_convert_context *<i>cvcontext</i>,</b>
  377. <b> uint32_t <i>escape_char</i>);</b>
  378. <br>
  379. <br>
  380. <b>int pcre2_set_glob_separator(pcre2_convert_context *<i>cvcontext</i>,</b>
  381. <b> uint32_t <i>separator_char</i>);</b>
  382. <br>
  383. <br>
  384. <b>int pcre2_pattern_convert(PCRE2_SPTR <i>pattern</i>, PCRE2_SIZE <i>length</i>,</b>
  385. <b> uint32_t <i>options</i>, PCRE2_UCHAR **<i>buffer</i>,</b>
  386. <b> PCRE2_SIZE *<i>blength</i>, pcre2_convert_context *<i>cvcontext</i>);</b>
  387. <br>
  388. <br>
  389. <b>void pcre2_converted_pattern_free(PCRE2_UCHAR *<i>converted_pattern</i>);</b>
  390. <br>
  391. <br>
  392. These functions provide a way of converting non-PCRE2 patterns into
  393. patterns that can be processed by <b>pcre2_compile()</b>. This facility is
  394. experimental and may be changed in future releases. At present, "globs" and
  395. POSIX basic and extended patterns can be converted. Details are given in the
  396. <a href="pcre2convert.html"><b>pcre2convert</b></a>
  397. documentation.
  398. </P>
  399. <br><a name="SEC13" href="#TOC1">PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES</a><br>
  400. <P>
  401. There are three PCRE2 libraries, supporting 8-bit, 16-bit, and 32-bit code
  402. units, respectively. However, there is just one header file, <b>pcre2.h</b>.
  403. This contains the function prototypes and other definitions for all three
  404. libraries. One, two, or all three can be installed simultaneously. On Unix-like
  405. systems the libraries are called <b>libpcre2-8</b>, <b>libpcre2-16</b>, and
  406. <b>libpcre2-32</b>, and they can also co-exist with the original PCRE libraries.
  407. Every PCRE2 function comes in three different forms, one for each library, for
  408. example:
  409. <pre>
  410. <b>pcre2_compile_8()</b>
  411. <b>pcre2_compile_16()</b>
  412. <b>pcre2_compile_32()</b>
  413. </pre>
  414. There are also three different sets of data types:
  415. <pre>
  416. <b>PCRE2_UCHAR8, PCRE2_UCHAR16, PCRE2_UCHAR32</b>
  417. <b>PCRE2_SPTR8, PCRE2_SPTR16, PCRE2_SPTR32</b>
  418. </pre>
  419. The UCHAR types define unsigned code units of the appropriate widths.
  420. For example, PCRE2_UCHAR16 is usually defined as `uint16_t'.
  421. The SPTR types are pointers to constants of the equivalent UCHAR types,
  422. that is, they are pointers to vectors of unsigned code units.
  423. </P>
  424. <P>
  425. Character strings are passed to a PCRE2 library as sequences of unsigned
  426. integers in code units of the appropriate width. The length of a string may
  427. be given as a number of code units, or the string may be specified as
  428. zero-terminated.
  429. </P>
  430. <P>
  431. Many applications use only one code unit width. For their convenience, macros
  432. are defined whose names are the generic forms such as <b>pcre2_compile()</b> and
  433. PCRE2_SPTR. These macros use the value of the macro PCRE2_CODE_UNIT_WIDTH to
  434. generate the appropriate width-specific function and macro names.
  435. PCRE2_CODE_UNIT_WIDTH is not defined by default. An application must define it
  436. to be 8, 16, or 32 before including <b>pcre2.h</b> in order to make use of the
  437. generic names.
  438. </P>
  439. <P>
  440. Applications that use more than one code unit width can be linked with more
  441. than one PCRE2 library, but must define PCRE2_CODE_UNIT_WIDTH to be 0 before
  442. including <b>pcre2.h</b>, and then use the real function names. Any code that is
  443. to be included in an environment where the value of PCRE2_CODE_UNIT_WIDTH is
  444. unknown should also use the real function names. (Unfortunately, it is not
  445. possible in C code to save and restore the value of a macro.)
  446. </P>
  447. <P>
  448. If PCRE2_CODE_UNIT_WIDTH is not defined before including <b>pcre2.h</b>, a
  449. compiler error occurs.
  450. </P>
  451. <P>
  452. When using multiple libraries in an application, you must take care when
  453. processing any particular pattern to use only functions from a single library.
  454. For example, if you want to run a match using a pattern that was compiled with
  455. <b>pcre2_compile_16()</b>, you must do so with <b>pcre2_match_16()</b>, not
  456. <b>pcre2_match_8()</b> or <b>pcre2_match_32()</b>.
  457. </P>
  458. <P>
  459. In the function summaries above, and in the rest of this document and other
  460. PCRE2 documents, functions and data types are described using their generic
  461. names, without the _8, _16, or _32 suffix.
  462. </P>
  463. <br><a name="SEC14" href="#TOC1">PCRE2 API OVERVIEW</a><br>
  464. <P>
  465. PCRE2 has its own native API, which is described in this document. There are
  466. also some wrapper functions for the 8-bit library that correspond to the
  467. POSIX regular expression API, but they do not give access to all the
  468. functionality of PCRE2 and they are not thread-safe. They are described in the
  469. <a href="pcre2posix.html"><b>pcre2posix</b></a>
  470. documentation. Both these APIs define a set of C function calls.
  471. </P>
  472. <P>
  473. The native API C data types, function prototypes, option values, and error
  474. codes are defined in the header file <b>pcre2.h</b>, which also contains
  475. definitions of PCRE2_MAJOR and PCRE2_MINOR, the major and minor release numbers
  476. for the library. Applications can use these to include support for different
  477. releases of PCRE2.
  478. </P>
  479. <P>
  480. In a Windows environment, if you want to statically link an application program
  481. against a non-dll PCRE2 library, you must define PCRE2_STATIC before including
  482. <b>pcre2.h</b>.
  483. </P>
  484. <P>
  485. The functions <b>pcre2_compile()</b> and <b>pcre2_match()</b> are used for
  486. compiling and matching regular expressions in a Perl-compatible manner. A
  487. sample program that demonstrates the simplest way of using them is provided in
  488. the file called <i>pcre2demo.c</i> in the PCRE2 source distribution. A listing
  489. of this program is given in the
  490. <a href="pcre2demo.html"><b>pcre2demo</b></a>
  491. documentation, and the
  492. <a href="pcre2sample.html"><b>pcre2sample</b></a>
  493. documentation describes how to compile and run it.
  494. </P>
  495. <P>
  496. The compiling and matching functions recognize various options that are passed
  497. as bits in an options argument. There are also some more complicated parameters
  498. such as custom memory management functions and resource limits that are passed
  499. in "contexts" (which are just memory blocks, described below). Simple
  500. applications do not need to make use of contexts.
  501. </P>
  502. <P>
  503. Just-in-time (JIT) compiler support is an optional feature of PCRE2 that can be
  504. built in appropriate hardware environments. It greatly speeds up the matching
  505. performance of many patterns. Programs can request that it be used if
  506. available by calling <b>pcre2_jit_compile()</b> after a pattern has been
  507. successfully compiled by <b>pcre2_compile()</b>. This does nothing if JIT
  508. support is not available.
  509. </P>
  510. <P>
  511. More complicated programs might need to make use of the specialist functions
  512. <b>pcre2_jit_stack_create()</b>, <b>pcre2_jit_stack_free()</b>, and
  513. <b>pcre2_jit_stack_assign()</b> in order to control the JIT code's memory usage.
  514. </P>
  515. <P>
  516. JIT matching is automatically used by <b>pcre2_match()</b> if it is available,
  517. unless the PCRE2_NO_JIT option is set. There is also a direct interface for JIT
  518. matching, which gives improved performance at the expense of less sanity
  519. checking. The JIT-specific functions are discussed in the
  520. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  521. documentation.
  522. </P>
  523. <P>
  524. A second matching function, <b>pcre2_dfa_match()</b>, which is not
  525. Perl-compatible, is also provided. This uses a different algorithm for the
  526. matching. The alternative algorithm finds all possible matches (at a given
  527. point in the subject), and scans the subject just once (unless there are
  528. lookaround assertions). However, this algorithm does not return captured
  529. substrings. A description of the two matching algorithms and their advantages
  530. and disadvantages is given in the
  531. <a href="pcre2matching.html"><b>pcre2matching</b></a>
  532. documentation. There is no JIT support for <b>pcre2_dfa_match()</b>.
  533. </P>
  534. <P>
  535. In addition to the main compiling and matching functions, there are convenience
  536. functions for extracting captured substrings from a subject string that has
  537. been matched by <b>pcre2_match()</b>. They are:
  538. <pre>
  539. <b>pcre2_substring_copy_byname()</b>
  540. <b>pcre2_substring_copy_bynumber()</b>
  541. <b>pcre2_substring_get_byname()</b>
  542. <b>pcre2_substring_get_bynumber()</b>
  543. <b>pcre2_substring_list_get()</b>
  544. <b>pcre2_substring_length_byname()</b>
  545. <b>pcre2_substring_length_bynumber()</b>
  546. <b>pcre2_substring_nametable_scan()</b>
  547. <b>pcre2_substring_number_from_name()</b>
  548. </pre>
  549. <b>pcre2_substring_free()</b> and <b>pcre2_substring_list_free()</b> are also
  550. provided, to free memory used for extracted strings. If either of these
  551. functions is called with a NULL argument, the function returns immediately
  552. without doing anything.
  553. </P>
  554. <P>
  555. The function <b>pcre2_substitute()</b> can be called to match a pattern and
  556. return a copy of the subject string with substitutions for parts that were
  557. matched.
  558. </P>
  559. <P>
  560. Functions whose names begin with <b>pcre2_serialize_</b> are used for saving
  561. compiled patterns on disc or elsewhere, and reloading them later.
  562. </P>
  563. <P>
  564. Finally, there are functions for finding out information about a compiled
  565. pattern (<b>pcre2_pattern_info()</b>) and about the configuration with which
  566. PCRE2 was built (<b>pcre2_config()</b>).
  567. </P>
  568. <P>
  569. Functions with names ending with <b>_free()</b> are used for freeing memory
  570. blocks of various sorts. In all cases, if one of these functions is called with
  571. a NULL argument, it does nothing.
  572. </P>
  573. <br><a name="SEC15" href="#TOC1">STRING LENGTHS AND OFFSETS</a><br>
  574. <P>
  575. The PCRE2 API uses string lengths and offsets into strings of code units in
  576. several places. These values are always of type PCRE2_SIZE, which is an
  577. unsigned integer type, currently always defined as <i>size_t</i>. The largest
  578. value that can be stored in such a type (that is ~(PCRE2_SIZE)0) is reserved
  579. as a special indicator for zero-terminated strings and unset offsets.
  580. Therefore, the longest string that can be handled is one less than this
  581. maximum. Note that string lengths are always given in code units. Only in the
  582. 8-bit library is such a length the same as the number of bytes in the string.
  583. <a name="newlines"></a></P>
  584. <br><a name="SEC16" href="#TOC1">NEWLINES</a><br>
  585. <P>
  586. PCRE2 supports five different conventions for indicating line breaks in
  587. strings: a single CR (carriage return) character, a single LF (linefeed)
  588. character, the two-character sequence CRLF, any of the three preceding, or any
  589. Unicode newline sequence. The Unicode newline sequences are the three just
  590. mentioned, plus the single characters VT (vertical tab, U+000B), FF (form feed,
  591. U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and PS
  592. (paragraph separator, U+2029).
  593. </P>
  594. <P>
  595. Each of the first three conventions is used by at least one operating system as
  596. its standard newline sequence. When PCRE2 is built, a default can be specified.
  597. If it is not, the default is set to LF, which is the Unix standard. However,
  598. the newline convention can be changed by an application when calling
  599. <b>pcre2_compile()</b>, or it can be specified by special text at the start of
  600. the pattern itself; this overrides any other settings. See the
  601. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  602. page for details of the special character sequences.
  603. </P>
  604. <P>
  605. In the PCRE2 documentation the word "newline" is used to mean "the character or
  606. pair of characters that indicate a line break". The choice of newline
  607. convention affects the handling of the dot, circumflex, and dollar
  608. metacharacters, the handling of #-comments in /x mode, and, when CRLF is a
  609. recognized line ending sequence, the match position advancement for a
  610. non-anchored pattern. There is more detail about this in the
  611. <a href="#matchoptions">section on <b>pcre2_match()</b> options</a>
  612. below.
  613. </P>
  614. <P>
  615. The choice of newline convention does not affect the interpretation of
  616. the \n or \r escape sequences, nor does it affect what \R matches; this has
  617. its own separate convention.
  618. </P>
  619. <br><a name="SEC17" href="#TOC1">MULTITHREADING</a><br>
  620. <P>
  621. In a multithreaded application it is important to keep thread-specific data
  622. separate from data that can be shared between threads. The PCRE2 library code
  623. itself is thread-safe: it contains no static or global variables. The API is
  624. designed to be fairly simple for non-threaded applications while at the same
  625. time ensuring that multithreaded applications can use it.
  626. </P>
  627. <P>
  628. There are several different blocks of data that are used to pass information
  629. between the application and the PCRE2 libraries.
  630. </P>
  631. <br><b>
  632. The compiled pattern
  633. </b><br>
  634. <P>
  635. A pointer to the compiled form of a pattern is returned to the user when
  636. <b>pcre2_compile()</b> is successful. The data in the compiled pattern is fixed,
  637. and does not change when the pattern is matched. Therefore, it is thread-safe,
  638. that is, the same compiled pattern can be used by more than one thread
  639. simultaneously. For example, an application can compile all its patterns at the
  640. start, before forking off multiple threads that use them. However, if the
  641. just-in-time (JIT) optimization feature is being used, it needs separate memory
  642. stack areas for each thread. See the
  643. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  644. documentation for more details.
  645. </P>
  646. <P>
  647. In a more complicated situation, where patterns are compiled only when they are
  648. first needed, but are still shared between threads, pointers to compiled
  649. patterns must be protected from simultaneous writing by multiple threads. This
  650. is somewhat tricky to do correctly. If you know that writing to a pointer is
  651. atomic in your environment, you can use logic like this:
  652. <pre>
  653. Get a read-only (shared) lock (mutex) for pointer
  654. if (pointer == NULL)
  655. {
  656. Get a write (unique) lock for pointer
  657. if (pointer == NULL) pointer = pcre2_compile(...
  658. }
  659. Release the lock
  660. Use pointer in pcre2_match()
  661. </pre>
  662. Of course, testing for compilation errors should also be included in the code.
  663. </P>
  664. <P>
  665. The reason for checking the pointer a second time is as follows: Several
  666. threads may have acquired the shared lock and tested the pointer for being
  667. NULL, but only one of them will be given the write lock, with the rest kept
  668. waiting. The winning thread will compile the pattern and store the result.
  669. After this thread releases the write lock, another thread will get it, and if
  670. it does not retest pointer for being NULL, will recompile the pattern and
  671. overwrite the pointer, creating a memory leak and possibly causing other
  672. issues.
  673. </P>
  674. <P>
  675. In an environment where writing to a pointer may not be atomic, the above logic
  676. is not sufficient. The thread that is doing the compiling may be descheduled
  677. after writing only part of the pointer, which could cause other threads to use
  678. an invalid value. Instead of checking the pointer itself, a separate "pointer
  679. is valid" flag (that can be updated atomically) must be used:
  680. <pre>
  681. Get a read-only (shared) lock (mutex) for pointer
  682. if (!pointer_is_valid)
  683. {
  684. Get a write (unique) lock for pointer
  685. if (!pointer_is_valid)
  686. {
  687. pointer = pcre2_compile(...
  688. pointer_is_valid = TRUE
  689. }
  690. }
  691. Release the lock
  692. Use pointer in pcre2_match()
  693. </pre>
  694. If JIT is being used, but the JIT compilation is not being done immediately
  695. (perhaps waiting to see if the pattern is used often enough), similar logic is
  696. required. JIT compilation updates a value within the compiled code block, so a
  697. thread must gain unique write access to the pointer before calling
  698. <b>pcre2_jit_compile()</b>. Alternatively, <b>pcre2_code_copy()</b> or
  699. <b>pcre2_code_copy_with_tables()</b> can be used to obtain a private copy of the
  700. compiled code before calling the JIT compiler.
  701. </P>
  702. <br><b>
  703. Context blocks
  704. </b><br>
  705. <P>
  706. The next main section below introduces the idea of "contexts" in which PCRE2
  707. functions are called. A context is nothing more than a collection of parameters
  708. that control the way PCRE2 operates. Grouping a number of parameters together
  709. in a context is a convenient way of passing them to a PCRE2 function without
  710. using lots of arguments. The parameters that are stored in contexts are in some
  711. sense "advanced features" of the API. Many straightforward applications will
  712. not need to use contexts.
  713. </P>
  714. <P>
  715. In a multithreaded application, if the parameters in a context are values that
  716. are never changed, the same context can be used by all the threads. However, if
  717. any thread needs to change any value in a context, it must make its own
  718. thread-specific copy.
  719. </P>
  720. <br><b>
  721. Match blocks
  722. </b><br>
  723. <P>
  724. The matching functions need a block of memory for storing the results of a
  725. match. This includes details of what was matched, as well as additional
  726. information such as the name of a (*MARK) setting. Each thread must provide its
  727. own copy of this memory.
  728. </P>
  729. <br><a name="SEC18" href="#TOC1">PCRE2 CONTEXTS</a><br>
  730. <P>
  731. Some PCRE2 functions have a lot of parameters, many of which are used only by
  732. specialist applications, for example, those that use custom memory management
  733. or non-standard character tables. To keep function argument lists at a
  734. reasonable size, and at the same time to keep the API extensible, "uncommon"
  735. parameters are passed to certain functions in a <b>context</b> instead of
  736. directly. A context is just a block of memory that holds the parameter values.
  737. Applications that do not need to adjust any of the context parameters can pass
  738. NULL when a context pointer is required.
  739. </P>
  740. <P>
  741. There are three different types of context: a general context that is relevant
  742. for several PCRE2 operations, a compile-time context, and a match-time context.
  743. </P>
  744. <br><b>
  745. The general context
  746. </b><br>
  747. <P>
  748. At present, this context just contains pointers to (and data for) external
  749. memory management functions that are called from several places in the PCRE2
  750. library. The context is named `general' rather than specifically `memory'
  751. because in future other fields may be added. If you do not want to supply your
  752. own custom memory management functions, you do not need to bother with a
  753. general context. A general context is created by:
  754. <br>
  755. <br>
  756. <b>pcre2_general_context *pcre2_general_context_create(</b>
  757. <b> void *(*<i>private_malloc</i>)(PCRE2_SIZE, void *),</b>
  758. <b> void (*<i>private_free</i>)(void *, void *), void *<i>memory_data</i>);</b>
  759. <br>
  760. <br>
  761. The two function pointers specify custom memory management functions, whose
  762. prototypes are:
  763. <pre>
  764. <b>void *private_malloc(PCRE2_SIZE, void *);</b>
  765. <b>void private_free(void *, void *);</b>
  766. </pre>
  767. Whenever code in PCRE2 calls these functions, the final argument is the value
  768. of <i>memory_data</i>. Either of the first two arguments of the creation
  769. function may be NULL, in which case the system memory management functions
  770. <i>malloc()</i> and <i>free()</i> are used. (This is not currently useful, as
  771. there are no other fields in a general context, but in future there might be.)
  772. The <i>private_malloc()</i> function is used (if supplied) to obtain memory for
  773. storing the context, and all three values are saved as part of the context.
  774. </P>
  775. <P>
  776. Whenever PCRE2 creates a data block of any kind, the block contains a pointer
  777. to the <i>free()</i> function that matches the <i>malloc()</i> function that was
  778. used. When the time comes to free the block, this function is called.
  779. </P>
  780. <P>
  781. A general context can be copied by calling:
  782. <br>
  783. <br>
  784. <b>pcre2_general_context *pcre2_general_context_copy(</b>
  785. <b> pcre2_general_context *<i>gcontext</i>);</b>
  786. <br>
  787. <br>
  788. The memory used for a general context should be freed by calling:
  789. <br>
  790. <br>
  791. <b>void pcre2_general_context_free(pcre2_general_context *<i>gcontext</i>);</b>
  792. <br>
  793. <br>
  794. If this function is passed a NULL argument, it returns immediately without
  795. doing anything.
  796. <a name="compilecontext"></a></P>
  797. <br><b>
  798. The compile context
  799. </b><br>
  800. <P>
  801. A compile context is required if you want to provide an external function for
  802. stack checking during compilation or to change the default values of any of the
  803. following compile-time parameters:
  804. <pre>
  805. What \R matches (Unicode newlines or CR, LF, CRLF only)
  806. PCRE2's character tables
  807. The newline character sequence
  808. The compile time nested parentheses limit
  809. The maximum length of the pattern string
  810. The extra options bits (none set by default)
  811. </pre>
  812. A compile context is also required if you are using custom memory management.
  813. If none of these apply, just pass NULL as the context argument of
  814. <i>pcre2_compile()</i>.
  815. </P>
  816. <P>
  817. A compile context is created, copied, and freed by the following functions:
  818. <br>
  819. <br>
  820. <b>pcre2_compile_context *pcre2_compile_context_create(</b>
  821. <b> pcre2_general_context *<i>gcontext</i>);</b>
  822. <br>
  823. <br>
  824. <b>pcre2_compile_context *pcre2_compile_context_copy(</b>
  825. <b> pcre2_compile_context *<i>ccontext</i>);</b>
  826. <br>
  827. <br>
  828. <b>void pcre2_compile_context_free(pcre2_compile_context *<i>ccontext</i>);</b>
  829. <br>
  830. <br>
  831. A compile context is created with default values for its parameters. These can
  832. be changed by calling the following functions, which return 0 on success, or
  833. PCRE2_ERROR_BADDATA if invalid data is detected.
  834. <br>
  835. <br>
  836. <b>int pcre2_set_bsr(pcre2_compile_context *<i>ccontext</i>,</b>
  837. <b> uint32_t <i>value</i>);</b>
  838. <br>
  839. <br>
  840. The value must be PCRE2_BSR_ANYCRLF, to specify that \R matches only CR, LF,
  841. or CRLF, or PCRE2_BSR_UNICODE, to specify that \R matches any Unicode line
  842. ending sequence. The value is used by the JIT compiler and by the two
  843. interpreted matching functions, <i>pcre2_match()</i> and
  844. <i>pcre2_dfa_match()</i>.
  845. <br>
  846. <br>
  847. <b>int pcre2_set_character_tables(pcre2_compile_context *<i>ccontext</i>,</b>
  848. <b> const uint8_t *<i>tables</i>);</b>
  849. <br>
  850. <br>
  851. The value must be the result of a call to <b>pcre2_maketables()</b>, whose only
  852. argument is a general context. This function builds a set of character tables
  853. in the current locale.
  854. <br>
  855. <br>
  856. <b>int pcre2_set_compile_extra_options(pcre2_compile_context *<i>ccontext</i>,</b>
  857. <b> uint32_t <i>extra_options</i>);</b>
  858. <br>
  859. <br>
  860. As PCRE2 has developed, almost all the 32 option bits that are available in
  861. the <i>options</i> argument of <b>pcre2_compile()</b> have been used up. To avoid
  862. running out, the compile context contains a set of extra option bits which are
  863. used for some newer, assumed rarer, options. This function sets those bits. It
  864. always sets all the bits (either on or off). It does not modify any existing
  865. setting. The available options are defined in the section entitled "Extra
  866. compile options"
  867. <a href="#extracompileoptions">below.</a>
  868. <br>
  869. <br>
  870. <b>int pcre2_set_max_pattern_length(pcre2_compile_context *<i>ccontext</i>,</b>
  871. <b> PCRE2_SIZE <i>value</i>);</b>
  872. <br>
  873. <br>
  874. This sets a maximum length, in code units, for any pattern string that is
  875. compiled with this context. If the pattern is longer, an error is generated.
  876. This facility is provided so that applications that accept patterns from
  877. external sources can limit their size. The default is the largest number that a
  878. PCRE2_SIZE variable can hold, which is effectively unlimited.
  879. <br>
  880. <br>
  881. <b>int pcre2_set_max_pattern_compiled_length(</b>
  882. <b> pcre2_compile_context *<i>ccontext</i>, PCRE2_SIZE <i>value</i>);</b>
  883. <br>
  884. <br>
  885. This sets a maximum size, in bytes, for the memory needed to hold the compiled
  886. version of a pattern that is compiled with this context. If the pattern needs
  887. more memory, an error is generated. This facility is provided so that
  888. applications that accept patterns from external sources can limit the amount of
  889. memory they use. The default is the largest number that a PCRE2_SIZE variable
  890. can hold, which is effectively unlimited.
  891. <br>
  892. <br>
  893. <b>int pcre2_set_max_varlookbehind(pcre2_compile_contest *<i>ccontext</i>,</b>
  894. <b>" uint32_t <i>value</i>);</b>
  895. <br>
  896. <br>
  897. This sets a maximum length for the number of characters matched by a
  898. variable-length lookbehind assertion. The default is set when PCRE2 is built,
  899. with the ultimate default being 255, the same as Perl. Lookbehind assertions
  900. without a bounding length are not supported.
  901. <br>
  902. <br>
  903. <b>int pcre2_set_newline(pcre2_compile_context *<i>ccontext</i>,</b>
  904. <b> uint32_t <i>value</i>);</b>
  905. <br>
  906. <br>
  907. This specifies which characters or character sequences are to be recognized as
  908. newlines. The value must be one of PCRE2_NEWLINE_CR (carriage return only),
  909. PCRE2_NEWLINE_LF (linefeed only), PCRE2_NEWLINE_CRLF (the two-character
  910. sequence CR followed by LF), PCRE2_NEWLINE_ANYCRLF (any of the above),
  911. PCRE2_NEWLINE_ANY (any Unicode newline sequence), or PCRE2_NEWLINE_NUL (the
  912. NUL character, that is a binary zero).
  913. </P>
  914. <P>
  915. A pattern can override the value set in the compile context by starting with a
  916. sequence such as (*CRLF). See the
  917. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  918. page for details.
  919. </P>
  920. <P>
  921. When a pattern is compiled with the PCRE2_EXTENDED or PCRE2_EXTENDED_MORE
  922. option, the newline convention affects the recognition of the end of internal
  923. comments starting with #. The value is saved with the compiled pattern for
  924. subsequent use by the JIT compiler and by the two interpreted matching
  925. functions, <i>pcre2_match()</i> and <i>pcre2_dfa_match()</i>.
  926. <br>
  927. <br>
  928. <b>int pcre2_set_parens_nest_limit(pcre2_compile_context *<i>ccontext</i>,</b>
  929. <b> uint32_t <i>value</i>);</b>
  930. <br>
  931. <br>
  932. This parameter adjusts the limit, set when PCRE2 is built (default 250), on the
  933. depth of parenthesis nesting in a pattern. This limit stops rogue patterns
  934. using up too much system stack when being compiled. The limit applies to
  935. parentheses of all kinds, not just capturing parentheses.
  936. <br>
  937. <br>
  938. <b>int pcre2_set_compile_recursion_guard(pcre2_compile_context *<i>ccontext</i>,</b>
  939. <b> int (*<i>guard_function</i>)(uint32_t, void *), void *<i>user_data</i>);</b>
  940. <br>
  941. <br>
  942. There is at least one application that runs PCRE2 in threads with very limited
  943. system stack, where running out of stack is to be avoided at all costs. The
  944. parenthesis limit above cannot take account of how much stack is actually
  945. available during compilation. For a finer control, you can supply a function
  946. that is called whenever <b>pcre2_compile()</b> starts to compile a parenthesized
  947. part of a pattern. This function can check the actual stack size (or anything
  948. else that it wants to, of course).
  949. </P>
  950. <P>
  951. The first argument to the callout function gives the current depth of
  952. nesting, and the second is user data that is set up by the last argument of
  953. <b>pcre2_set_compile_recursion_guard()</b>. The callout function should return
  954. zero if all is well, or non-zero to force an error.
  955. <a name="matchcontext"></a></P>
  956. <br><b>
  957. The match context
  958. </b><br>
  959. <P>
  960. A match context is required if you want to:
  961. <pre>
  962. Set up a callout function
  963. Set an offset limit for matching an unanchored pattern
  964. Change the limit on the amount of heap used when matching
  965. Change the backtracking match limit
  966. Change the backtracking depth limit
  967. Set custom memory management specifically for the match
  968. </pre>
  969. If none of these apply, just pass NULL as the context argument of
  970. <b>pcre2_match()</b>, <b>pcre2_dfa_match()</b>, or <b>pcre2_jit_match()</b>.
  971. </P>
  972. <P>
  973. A match context is created, copied, and freed by the following functions:
  974. <br>
  975. <br>
  976. <b>pcre2_match_context *pcre2_match_context_create(</b>
  977. <b> pcre2_general_context *<i>gcontext</i>);</b>
  978. <br>
  979. <br>
  980. <b>pcre2_match_context *pcre2_match_context_copy(</b>
  981. <b> pcre2_match_context *<i>mcontext</i>);</b>
  982. <br>
  983. <br>
  984. <b>void pcre2_match_context_free(pcre2_match_context *<i>mcontext</i>);</b>
  985. <br>
  986. <br>
  987. A match context is created with default values for its parameters. These can
  988. be changed by calling the following functions, which return 0 on success, or
  989. PCRE2_ERROR_BADDATA if invalid data is detected.
  990. <br>
  991. <br>
  992. <b>int pcre2_set_callout(pcre2_match_context *<i>mcontext</i>,</b>
  993. <b> int (*<i>callout_function</i>)(pcre2_callout_block *, void *),</b>
  994. <b> void *<i>callout_data</i>);</b>
  995. <br>
  996. <br>
  997. This sets up a callout function for PCRE2 to call at specified points
  998. during a matching operation. Details are given in the
  999. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  1000. documentation.
  1001. <br>
  1002. <br>
  1003. <b>int pcre2_set_substitute_callout(pcre2_match_context *<i>mcontext</i>,</b>
  1004. <b> int (*<i>callout_function</i>)(pcre2_substitute_callout_block *, void *),</b>
  1005. <b> void *<i>callout_data</i>);</b>
  1006. <br>
  1007. <br>
  1008. This sets up a callout function for PCRE2 to call after each substitution
  1009. made by <b>pcre2_substitute()</b>. Details are given in the section entitled
  1010. "Creating a new string with substitutions"
  1011. <a href="#substitutions">below.</a>
  1012. <br>
  1013. <br>
  1014. <b>int pcre2_set_offset_limit(pcre2_match_context *<i>mcontext</i>,</b>
  1015. <b> PCRE2_SIZE <i>value</i>);</b>
  1016. <br>
  1017. <br>
  1018. The <i>offset_limit</i> parameter limits how far an unanchored search can
  1019. advance in the subject string. The default value is PCRE2_UNSET. The
  1020. <b>pcre2_match()</b> and <b>pcre2_dfa_match()</b> functions return
  1021. PCRE2_ERROR_NOMATCH if a match with a starting point before or at the given
  1022. offset is not found. The <b>pcre2_substitute()</b> function makes no more
  1023. substitutions.
  1024. </P>
  1025. <P>
  1026. For example, if the pattern /abc/ is matched against "123abc" with an offset
  1027. limit less than 3, the result is PCRE2_ERROR_NOMATCH. A match can never be
  1028. found if the <i>startoffset</i> argument of <b>pcre2_match()</b>,
  1029. <b>pcre2_dfa_match()</b>, or <b>pcre2_substitute()</b> is greater than the offset
  1030. limit set in the match context.
  1031. </P>
  1032. <P>
  1033. When using this facility, you must set the PCRE2_USE_OFFSET_LIMIT option when
  1034. calling <b>pcre2_compile()</b> so that when JIT is in use, different code can be
  1035. compiled. If a match is started with a non-default match limit when
  1036. PCRE2_USE_OFFSET_LIMIT is not set, an error is generated.
  1037. </P>
  1038. <P>
  1039. The offset limit facility can be used to track progress when searching large
  1040. subject strings or to limit the extent of global substitutions. See also the
  1041. PCRE2_FIRSTLINE option, which requires a match to start before or at the first
  1042. newline that follows the start of matching in the subject. If this is set with
  1043. an offset limit, a match must occur in the first line and also within the
  1044. offset limit. In other words, whichever limit comes first is used.
  1045. <br>
  1046. <br>
  1047. <b>int pcre2_set_heap_limit(pcre2_match_context *<i>mcontext</i>,</b>
  1048. <b> uint32_t <i>value</i>);</b>
  1049. <br>
  1050. <br>
  1051. The <i>heap_limit</i> parameter specifies, in units of kibibytes (1024 bytes),
  1052. the maximum amount of heap memory that <b>pcre2_match()</b> may use to hold
  1053. backtracking information when running an interpretive match. This limit also
  1054. applies to <b>pcre2_dfa_match()</b>, which may use the heap when processing
  1055. patterns with a lot of nested pattern recursion or lookarounds or atomic
  1056. groups. This limit does not apply to matching with the JIT optimization, which
  1057. has its own memory control arrangements (see the
  1058. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  1059. documentation for more details). If the limit is reached, the negative error
  1060. code PCRE2_ERROR_HEAPLIMIT is returned. The default limit can be set when PCRE2
  1061. is built; if it is not, the default is set very large and is essentially
  1062. unlimited.
  1063. </P>
  1064. <P>
  1065. A value for the heap limit may also be supplied by an item at the start of a
  1066. pattern of the form
  1067. <pre>
  1068. (*LIMIT_HEAP=ddd)
  1069. </pre>
  1070. where ddd is a decimal number. However, such a setting is ignored unless ddd is
  1071. less than the limit set by the caller of <b>pcre2_match()</b> or, if no such
  1072. limit is set, less than the default.
  1073. </P>
  1074. <P>
  1075. The <b>pcre2_match()</b> function always needs some heap memory, so setting a
  1076. value of zero guarantees a "heap limit exceeded" error. Details of how
  1077. <b>pcre2_match()</b> uses the heap are given in the
  1078. <a href="pcre2perform.html"><b>pcre2perform</b></a>
  1079. documentation.
  1080. </P>
  1081. <P>
  1082. For <b>pcre2_dfa_match()</b>, a vector on the system stack is used when
  1083. processing pattern recursions, lookarounds, or atomic groups, and only if this
  1084. is not big enough is heap memory used. In this case, setting a value of zero
  1085. disables the use of the heap.
  1086. <br>
  1087. <br>
  1088. <b>int pcre2_set_match_limit(pcre2_match_context *<i>mcontext</i>,</b>
  1089. <b> uint32_t <i>value</i>);</b>
  1090. <br>
  1091. <br>
  1092. The <i>match_limit</i> parameter provides a means of preventing PCRE2 from using
  1093. up too many computing resources when processing patterns that are not going to
  1094. match, but which have a very large number of possibilities in their search
  1095. trees. The classic example is a pattern that uses nested unlimited repeats.
  1096. </P>
  1097. <P>
  1098. There is an internal counter in <b>pcre2_match()</b> that is incremented each
  1099. time round its main matching loop. If this value reaches the match limit,
  1100. <b>pcre2_match()</b> returns the negative value PCRE2_ERROR_MATCHLIMIT. This has
  1101. the effect of limiting the amount of backtracking that can take place. For
  1102. patterns that are not anchored, the count restarts from zero for each position
  1103. in the subject string. This limit also applies to <b>pcre2_dfa_match()</b>,
  1104. though the counting is done in a different way.
  1105. </P>
  1106. <P>
  1107. When <b>pcre2_match()</b> is called with a pattern that was successfully
  1108. processed by <b>pcre2_jit_compile()</b>, the way in which matching is executed
  1109. is entirely different. However, there is still the possibility of runaway
  1110. matching that goes on for a very long time, and so the <i>match_limit</i> value
  1111. is also used in this case (but in a different way) to limit how long the
  1112. matching can continue.
  1113. </P>
  1114. <P>
  1115. The default value for the limit can be set when PCRE2 is built; the default is
  1116. 10 million, which handles all but the most extreme cases. A value for the match
  1117. limit may also be supplied by an item at the start of a pattern of the form
  1118. <pre>
  1119. (*LIMIT_MATCH=ddd)
  1120. </pre>
  1121. where ddd is a decimal number. However, such a setting is ignored unless ddd is
  1122. less than the limit set by the caller of <b>pcre2_match()</b> or
  1123. <b>pcre2_dfa_match()</b> or, if no such limit is set, less than the default.
  1124. <br>
  1125. <br>
  1126. <b>int pcre2_set_depth_limit(pcre2_match_context *<i>mcontext</i>,</b>
  1127. <b> uint32_t <i>value</i>);</b>
  1128. <br>
  1129. <br>
  1130. This parameter limits the depth of nested backtracking in <b>pcre2_match()</b>.
  1131. Each time a nested backtracking point is passed, a new memory frame is used
  1132. to remember the state of matching at that point. Thus, this parameter
  1133. indirectly limits the amount of memory that is used in a match. However,
  1134. because the size of each memory frame depends on the number of capturing
  1135. parentheses, the actual memory limit varies from pattern to pattern. This limit
  1136. was more useful in versions before 10.30, where function recursion was used for
  1137. backtracking.
  1138. </P>
  1139. <P>
  1140. The depth limit is not relevant, and is ignored, when matching is done using
  1141. JIT compiled code. However, it is supported by <b>pcre2_dfa_match()</b>, which
  1142. uses it to limit the depth of nested internal recursive function calls that
  1143. implement atomic groups, lookaround assertions, and pattern recursions. This
  1144. limits, indirectly, the amount of system stack that is used. It was more useful
  1145. in versions before 10.32, when stack memory was used for local workspace
  1146. vectors for recursive function calls. From version 10.32, only local variables
  1147. are allocated on the stack and as each call uses only a few hundred bytes, even
  1148. a small stack can support quite a lot of recursion.
  1149. </P>
  1150. <P>
  1151. If the depth of internal recursive function calls is great enough, local
  1152. workspace vectors are allocated on the heap from version 10.32 onwards, so the
  1153. depth limit also indirectly limits the amount of heap memory that is used. A
  1154. recursive pattern such as /(.(?2))((?1)|)/, when matched to a very long string
  1155. using <b>pcre2_dfa_match()</b>, can use a great deal of memory. However, it is
  1156. probably better to limit heap usage directly by calling
  1157. <b>pcre2_set_heap_limit()</b>.
  1158. </P>
  1159. <P>
  1160. The default value for the depth limit can be set when PCRE2 is built; if it is
  1161. not, the default is set to the same value as the default for the match limit.
  1162. If the limit is exceeded, <b>pcre2_match()</b> or <b>pcre2_dfa_match()</b>
  1163. returns PCRE2_ERROR_DEPTHLIMIT. A value for the depth limit may also be
  1164. supplied by an item at the start of a pattern of the form
  1165. <pre>
  1166. (*LIMIT_DEPTH=ddd)
  1167. </pre>
  1168. where ddd is a decimal number. However, such a setting is ignored unless ddd is
  1169. less than the limit set by the caller of <b>pcre2_match()</b> or
  1170. <b>pcre2_dfa_match()</b> or, if no such limit is set, less than the default.
  1171. </P>
  1172. <br><a name="SEC19" href="#TOC1">CHECKING BUILD-TIME OPTIONS</a><br>
  1173. <P>
  1174. <b>int pcre2_config(uint32_t <i>what</i>, void *<i>where</i>);</b>
  1175. </P>
  1176. <P>
  1177. The function <b>pcre2_config()</b> makes it possible for a PCRE2 client to find
  1178. the value of certain configuration parameters and to discover which optional
  1179. features have been compiled into the PCRE2 library. The
  1180. <a href="pcre2build.html"><b>pcre2build</b></a>
  1181. documentation has more details about these features.
  1182. </P>
  1183. <P>
  1184. The first argument for <b>pcre2_config()</b> specifies which information is
  1185. required. The second argument is a pointer to memory into which the information
  1186. is placed. If NULL is passed, the function returns the amount of memory that is
  1187. needed for the requested information. For calls that return numerical values,
  1188. the value is in bytes; when requesting these values, <i>where</i> should point
  1189. to appropriately aligned memory. For calls that return strings, the required
  1190. length is given in code units, not counting the terminating zero.
  1191. </P>
  1192. <P>
  1193. When requesting information, the returned value from <b>pcre2_config()</b> is
  1194. non-negative on success, or the negative error code PCRE2_ERROR_BADOPTION if
  1195. the value in the first argument is not recognized. The following information is
  1196. available:
  1197. <pre>
  1198. PCRE2_CONFIG_BSR
  1199. </pre>
  1200. The output is a uint32_t integer whose value indicates what character
  1201. sequences the \R escape sequence matches by default. A value of
  1202. PCRE2_BSR_UNICODE means that \R matches any Unicode line ending sequence; a
  1203. value of PCRE2_BSR_ANYCRLF means that \R matches only CR, LF, or CRLF. The
  1204. default can be overridden when a pattern is compiled.
  1205. <pre>
  1206. PCRE2_CONFIG_COMPILED_WIDTHS
  1207. </pre>
  1208. The output is a uint32_t integer whose lower bits indicate which code unit
  1209. widths were selected when PCRE2 was built. The 1-bit indicates 8-bit support,
  1210. and the 2-bit and 4-bit indicate 16-bit and 32-bit support, respectively.
  1211. <pre>
  1212. PCRE2_CONFIG_DEPTHLIMIT
  1213. </pre>
  1214. The output is a uint32_t integer that gives the default limit for the depth of
  1215. nested backtracking in <b>pcre2_match()</b> or the depth of nested recursions,
  1216. lookarounds, and atomic groups in <b>pcre2_dfa_match()</b>. Further details are
  1217. given with <b>pcre2_set_depth_limit()</b> above.
  1218. <pre>
  1219. PCRE2_CONFIG_HEAPLIMIT
  1220. </pre>
  1221. The output is a uint32_t integer that gives, in kibibytes, the default limit
  1222. for the amount of heap memory used by <b>pcre2_match()</b> or
  1223. <b>pcre2_dfa_match()</b>. Further details are given with
  1224. <b>pcre2_set_heap_limit()</b> above.
  1225. <pre>
  1226. PCRE2_CONFIG_JIT
  1227. </pre>
  1228. The output is a uint32_t integer that is set to one if support for just-in-time
  1229. compiling is included in the library; otherwise it is set to zero. Note that
  1230. having the support in the library does not guarantee that JIT will be used for
  1231. any given match. See the
  1232. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  1233. documentation for more details.
  1234. <pre>
  1235. PCRE2_CONFIG_JITTARGET
  1236. </pre>
  1237. The <i>where</i> argument should point to a buffer that is at least 48 code
  1238. units long. (The exact length required can be found by calling
  1239. <b>pcre2_config()</b> with <b>where</b> set to NULL.) The buffer is filled with a
  1240. string that contains the name of the architecture for which the JIT compiler is
  1241. configured, for example "x86 32bit (little endian + unaligned)". If JIT support
  1242. is not available, PCRE2_ERROR_BADOPTION is returned, otherwise the number of
  1243. code units used is returned. This is the length of the string, plus one unit
  1244. for the terminating zero.
  1245. <pre>
  1246. PCRE2_CONFIG_LINKSIZE
  1247. </pre>
  1248. The output is a uint32_t integer that contains the number of bytes used for
  1249. internal linkage in compiled regular expressions. When PCRE2 is configured, the
  1250. value can be set to 2, 3, or 4, with the default being 2. This is the value
  1251. that is returned by <b>pcre2_config()</b>. However, when the 16-bit library is
  1252. compiled, a value of 3 is rounded up to 4, and when the 32-bit library is
  1253. compiled, internal linkages always use 4 bytes, so the configured value is not
  1254. relevant.
  1255. </P>
  1256. <P>
  1257. The default value of 2 for the 8-bit and 16-bit libraries is sufficient for all
  1258. but the most massive patterns, since it allows the size of the compiled pattern
  1259. to be up to 65535 code units. Larger values allow larger regular expressions to
  1260. be compiled by those two libraries, but at the expense of slower matching.
  1261. <pre>
  1262. PCRE2_CONFIG_MATCHLIMIT
  1263. </pre>
  1264. The output is a uint32_t integer that gives the default match limit for
  1265. <b>pcre2_match()</b>. Further details are given with
  1266. <b>pcre2_set_match_limit()</b> above.
  1267. <pre>
  1268. PCRE2_CONFIG_NEWLINE
  1269. </pre>
  1270. The output is a uint32_t integer whose value specifies the default character
  1271. sequence that is recognized as meaning "newline". The values are:
  1272. <pre>
  1273. PCRE2_NEWLINE_CR Carriage return (CR)
  1274. PCRE2_NEWLINE_LF Linefeed (LF)
  1275. PCRE2_NEWLINE_CRLF Carriage return, linefeed (CRLF)
  1276. PCRE2_NEWLINE_ANY Any Unicode line ending
  1277. PCRE2_NEWLINE_ANYCRLF Any of CR, LF, or CRLF
  1278. PCRE2_NEWLINE_NUL The NUL character (binary zero)
  1279. </pre>
  1280. The default should normally correspond to the standard sequence for your
  1281. operating system.
  1282. <pre>
  1283. PCRE2_CONFIG_NEVER_BACKSLASH_C
  1284. </pre>
  1285. The output is a uint32_t integer that is set to one if the use of \C was
  1286. permanently disabled when PCRE2 was built; otherwise it is set to zero.
  1287. <pre>
  1288. PCRE2_CONFIG_PARENSLIMIT
  1289. </pre>
  1290. The output is a uint32_t integer that gives the maximum depth of nesting
  1291. of parentheses (of any kind) in a pattern. This limit is imposed to cap the
  1292. amount of system stack used when a pattern is compiled. It is specified when
  1293. PCRE2 is built; the default is 250. This limit does not take into account the
  1294. stack that may already be used by the calling application. For finer control
  1295. over compilation stack usage, see <b>pcre2_set_compile_recursion_guard()</b>.
  1296. <pre>
  1297. PCRE2_CONFIG_STACKRECURSE
  1298. </pre>
  1299. This parameter is obsolete and should not be used in new code. The output is a
  1300. uint32_t integer that is always set to zero.
  1301. <pre>
  1302. PCRE2_CONFIG_TABLES_LENGTH
  1303. </pre>
  1304. The output is a uint32_t integer that gives the length of PCRE2's character
  1305. processing tables in bytes. For details of these tables see the
  1306. <a href="#localesupport">section on locale support</a>
  1307. below.
  1308. <pre>
  1309. PCRE2_CONFIG_UNICODE_VERSION
  1310. </pre>
  1311. The <i>where</i> argument should point to a buffer that is at least 24 code
  1312. units long. (The exact length required can be found by calling
  1313. <b>pcre2_config()</b> with <b>where</b> set to NULL.) If PCRE2 has been compiled
  1314. without Unicode support, the buffer is filled with the text "Unicode not
  1315. supported". Otherwise, the Unicode version string (for example, "8.0.0") is
  1316. inserted. The number of code units used is returned. This is the length of the
  1317. string plus one unit for the terminating zero.
  1318. <pre>
  1319. PCRE2_CONFIG_UNICODE
  1320. </pre>
  1321. The output is a uint32_t integer that is set to one if Unicode support is
  1322. available; otherwise it is set to zero. Unicode support implies UTF support.
  1323. <pre>
  1324. PCRE2_CONFIG_VERSION
  1325. </pre>
  1326. The <i>where</i> argument should point to a buffer that is at least 24 code
  1327. units long. (The exact length required can be found by calling
  1328. <b>pcre2_config()</b> with <b>where</b> set to NULL.) The buffer is filled with
  1329. the PCRE2 version string, zero-terminated. The number of code units used is
  1330. returned. This is the length of the string plus one unit for the terminating
  1331. zero.
  1332. <a name="compiling"></a></P>
  1333. <br><a name="SEC20" href="#TOC1">COMPILING A PATTERN</a><br>
  1334. <P>
  1335. <b>pcre2_code *pcre2_compile(PCRE2_SPTR <i>pattern</i>, PCRE2_SIZE <i>length</i>,</b>
  1336. <b> uint32_t <i>options</i>, int *<i>errorcode</i>, PCRE2_SIZE *<i>erroroffset,</i></b>
  1337. <b> pcre2_compile_context *<i>ccontext</i>);</b>
  1338. <br>
  1339. <br>
  1340. <b>void pcre2_code_free(pcre2_code *<i>code</i>);</b>
  1341. <br>
  1342. <br>
  1343. <b>pcre2_code *pcre2_code_copy(const pcre2_code *<i>code</i>);</b>
  1344. <br>
  1345. <br>
  1346. <b>pcre2_code *pcre2_code_copy_with_tables(const pcre2_code *<i>code</i>);</b>
  1347. </P>
  1348. <P>
  1349. The <b>pcre2_compile()</b> function compiles a pattern into an internal form.
  1350. The pattern is defined by a pointer to a string of code units and a length in
  1351. code units. If the pattern is zero-terminated, the length can be specified as
  1352. PCRE2_ZERO_TERMINATED. A NULL pattern pointer with a length of zero is treated
  1353. as an empty string (NULL with a non-zero length causes an error return). The
  1354. function returns a pointer to a block of memory that contains the compiled
  1355. pattern and related data, or NULL if an error occurred.
  1356. </P>
  1357. <P>
  1358. If the compile context argument <i>ccontext</i> is NULL, memory for the compiled
  1359. pattern is obtained by calling <b>malloc()</b>. Otherwise, it is obtained from
  1360. the same memory function that was used for the compile context. The caller must
  1361. free the memory by calling <b>pcre2_code_free()</b> when it is no longer needed.
  1362. If <b>pcre2_code_free()</b> is called with a NULL argument, it returns
  1363. immediately, without doing anything.
  1364. </P>
  1365. <P>
  1366. The function <b>pcre2_code_copy()</b> makes a copy of the compiled code in new
  1367. memory, using the same memory allocator as was used for the original. However,
  1368. if the code has been processed by the JIT compiler (see
  1369. <a href="#jitcompiling">below),</a>
  1370. the JIT information cannot be copied (because it is position-dependent).
  1371. The new copy can initially be used only for non-JIT matching, though it can be
  1372. passed to <b>pcre2_jit_compile()</b> if required. If <b>pcre2_code_copy()</b> is
  1373. called with a NULL argument, it returns NULL.
  1374. </P>
  1375. <P>
  1376. The <b>pcre2_code_copy()</b> function provides a way for individual threads in a
  1377. multithreaded application to acquire a private copy of shared compiled code.
  1378. However, it does not make a copy of the character tables used by the compiled
  1379. pattern; the new pattern code points to the same tables as the original code.
  1380. (See
  1381. <a href="#jitcompiling">"Locale Support"</a>
  1382. below for details of these character tables.) In many applications the same
  1383. tables are used throughout, so this behaviour is appropriate. Nevertheless,
  1384. there are occasions when a copy of a compiled pattern and the relevant tables
  1385. are needed. The <b>pcre2_code_copy_with_tables()</b> provides this facility.
  1386. Copies of both the code and the tables are made, with the new code pointing to
  1387. the new tables. The memory for the new tables is automatically freed when
  1388. <b>pcre2_code_free()</b> is called for the new copy of the compiled code. If
  1389. <b>pcre2_code_copy_with_tables()</b> is called with a NULL argument, it returns
  1390. NULL.
  1391. </P>
  1392. <P>
  1393. NOTE: When one of the matching functions is called, pointers to the compiled
  1394. pattern and the subject string are set in the match data block so that they can
  1395. be referenced by the substring extraction functions after a successful match.
  1396. After running a match, you must not free a compiled pattern or a subject string
  1397. until after all operations on the
  1398. <a href="#matchdatablock">match data block</a>
  1399. have taken place, unless, in the case of the subject string, you have used the
  1400. PCRE2_COPY_MATCHED_SUBJECT option, which is described in the section entitled
  1401. "Option bits for <b>pcre2_match()</b>"
  1402. <a href="#matchoptions>">below.</a>
  1403. </P>
  1404. <P>
  1405. The <i>options</i> argument for <b>pcre2_compile()</b> contains various bit
  1406. settings that affect the compilation. It should be zero if none of them are
  1407. required. The available options are described below. Some of them (in
  1408. particular, those that are compatible with Perl, but some others as well) can
  1409. also be set and unset from within the pattern (see the detailed description in
  1410. the
  1411. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  1412. documentation).
  1413. </P>
  1414. <P>
  1415. For those options that can be different in different parts of the pattern, the
  1416. contents of the <i>options</i> argument specifies their settings at the start of
  1417. compilation. The PCRE2_ANCHORED, PCRE2_ENDANCHORED, and PCRE2_NO_UTF_CHECK
  1418. options can be set at the time of matching as well as at compile time.
  1419. </P>
  1420. <P>
  1421. Some additional options and less frequently required compile-time parameters
  1422. (for example, the newline setting) can be provided in a compile context (as
  1423. described
  1424. <a href="#compilecontext">above).</a>
  1425. </P>
  1426. <P>
  1427. If <i>errorcode</i> or <i>erroroffset</i> is NULL, <b>pcre2_compile()</b> returns
  1428. NULL immediately. Otherwise, the variables to which these point are set to an
  1429. error code and an offset (number of code units) within the pattern,
  1430. respectively, when <b>pcre2_compile()</b> returns NULL because a compilation
  1431. error has occurred.
  1432. </P>
  1433. <P>
  1434. There are nearly 100 positive error codes that <b>pcre2_compile()</b> may return
  1435. if it finds an error in the pattern. There are also some negative error codes
  1436. that are used for invalid UTF strings when validity checking is in force. These
  1437. are the same as given by <b>pcre2_match()</b> and <b>pcre2_dfa_match()</b>, and
  1438. are described in the
  1439. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  1440. documentation. There is no separate documentation for the positive error codes,
  1441. because the textual error messages that are obtained by calling the
  1442. <b>pcre2_get_error_message()</b> function (see "Obtaining a textual error
  1443. message"
  1444. <a href="#geterrormessage">below)</a>
  1445. should be self-explanatory. Macro names starting with PCRE2_ERROR_ are defined
  1446. for both positive and negative error codes in <b>pcre2.h</b>. When compilation
  1447. is successful <i>errorcode</i> is set to a value that returns the message "no
  1448. error" if passed to <b>pcre2_get_error_message()</b>.
  1449. </P>
  1450. <P>
  1451. The value returned in <i>erroroffset</i> is an indication of where in the
  1452. pattern an error occurred. When there is no error, zero is returned. A non-zero
  1453. value is not necessarily the furthest point in the pattern that was read. For
  1454. example, after the error "lookbehind assertion is not fixed length", the error
  1455. offset points to the start of the failing assertion. For an invalid UTF-8 or
  1456. UTF-16 string, the offset is that of the first code unit of the failing
  1457. character.
  1458. </P>
  1459. <P>
  1460. Some errors are not detected until the whole pattern has been scanned; in these
  1461. cases, the offset passed back is the length of the pattern. Note that the
  1462. offset is in code units, not characters, even in a UTF mode. It may sometimes
  1463. point into the middle of a UTF-8 or UTF-16 character.
  1464. </P>
  1465. <P>
  1466. This code fragment shows a typical straightforward call to
  1467. <b>pcre2_compile()</b>:
  1468. <pre>
  1469. pcre2_code *re;
  1470. PCRE2_SIZE erroffset;
  1471. int errorcode;
  1472. re = pcre2_compile(
  1473. "^A.*Z", /* the pattern */
  1474. PCRE2_ZERO_TERMINATED, /* the pattern is zero-terminated */
  1475. 0, /* default options */
  1476. &errorcode, /* for error code */
  1477. &erroffset, /* for error offset */
  1478. NULL); /* no compile context */
  1479. </PRE>
  1480. </P>
  1481. <br><b>
  1482. Main compile options
  1483. </b><br>
  1484. <P>
  1485. The following names for option bits are defined in the <b>pcre2.h</b> header
  1486. file:
  1487. <pre>
  1488. PCRE2_ANCHORED
  1489. </pre>
  1490. If this bit is set, the pattern is forced to be "anchored", that is, it is
  1491. constrained to match only at the first matching point in the string that is
  1492. being searched (the "subject string"). This effect can also be achieved by
  1493. appropriate constructs in the pattern itself, which is the only way to do it in
  1494. Perl.
  1495. <pre>
  1496. PCRE2_ALLOW_EMPTY_CLASS
  1497. </pre>
  1498. By default, for compatibility with Perl, a closing square bracket that
  1499. immediately follows an opening one is treated as a data character for the
  1500. class. When PCRE2_ALLOW_EMPTY_CLASS is set, it terminates the class, which
  1501. therefore contains no characters and so can never match.
  1502. <pre>
  1503. PCRE2_ALT_BSUX
  1504. </pre>
  1505. This option request alternative handling of three escape sequences, which
  1506. makes PCRE2's behaviour more like ECMAscript (aka JavaScript). When it is set:
  1507. </P>
  1508. <P>
  1509. (1) \U matches an upper case "U" character; by default \U causes a compile
  1510. time error (Perl uses \U to upper case subsequent characters).
  1511. </P>
  1512. <P>
  1513. (2) \u matches a lower case "u" character unless it is followed by four
  1514. hexadecimal digits, in which case the hexadecimal number defines the code point
  1515. to match. By default, \u causes a compile time error (Perl uses it to upper
  1516. case the following character).
  1517. </P>
  1518. <P>
  1519. (3) \x matches a lower case "x" character unless it is followed by two
  1520. hexadecimal digits, in which case the hexadecimal number defines the code point
  1521. to match. By default, as in Perl, a hexadecimal number is always expected after
  1522. \x, but it may have zero, one, or two digits (so, for example, \xz matches a
  1523. binary zero character followed by z).
  1524. </P>
  1525. <P>
  1526. ECMAscript 6 added additional functionality to \u. This can be accessed using
  1527. the PCRE2_EXTRA_ALT_BSUX extra option (see "Extra compile options"
  1528. <a href="#extracompileoptions">below).</a>
  1529. Note that this alternative escape handling applies only to patterns. Neither of
  1530. these options affects the processing of replacement strings passed to
  1531. <b>pcre2_substitute()</b>.
  1532. <pre>
  1533. PCRE2_ALT_CIRCUMFLEX
  1534. </pre>
  1535. In multiline mode (when PCRE2_MULTILINE is set), the circumflex metacharacter
  1536. matches at the start of the subject (unless PCRE2_NOTBOL is set), and also
  1537. after any internal newline. However, it does not match after a newline at the
  1538. end of the subject, for compatibility with Perl. If you want a multiline
  1539. circumflex also to match after a terminating newline, you must set
  1540. PCRE2_ALT_CIRCUMFLEX.
  1541. <pre>
  1542. PCRE2_ALT_VERBNAMES
  1543. </pre>
  1544. By default, for compatibility with Perl, the name in any verb sequence such as
  1545. (*MARK:NAME) is any sequence of characters that does not include a closing
  1546. parenthesis. The name is not processed in any way, and it is not possible to
  1547. include a closing parenthesis in the name. However, if the PCRE2_ALT_VERBNAMES
  1548. option is set, normal backslash processing is applied to verb names and only an
  1549. unescaped closing parenthesis terminates the name. A closing parenthesis can be
  1550. included in a name either as \) or between \Q and \E. If the PCRE2_EXTENDED
  1551. or PCRE2_EXTENDED_MORE option is set with PCRE2_ALT_VERBNAMES, unescaped
  1552. whitespace in verb names is skipped and #-comments are recognized, exactly as
  1553. in the rest of the pattern.
  1554. <pre>
  1555. PCRE2_AUTO_CALLOUT
  1556. </pre>
  1557. If this bit is set, <b>pcre2_compile()</b> automatically inserts callout items,
  1558. all with number 255, before each pattern item, except immediately before or
  1559. after an explicit callout in the pattern. For discussion of the callout
  1560. facility, see the
  1561. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  1562. documentation.
  1563. <pre>
  1564. PCRE2_CASELESS
  1565. </pre>
  1566. If this bit is set, letters in the pattern match both upper and lower case
  1567. letters in the subject. It is equivalent to Perl's /i option, and it can be
  1568. changed within a pattern by a (?i) option setting. If either PCRE2_UTF or
  1569. PCRE2_UCP is set, Unicode properties are used for all characters with more than
  1570. one other case, and for all characters whose code points are greater than
  1571. U+007F. Note that there are two ASCII characters, K and S, that, in addition to
  1572. their lower case ASCII equivalents, are case-equivalent with U+212A (Kelvin
  1573. sign) and U+017F (long S) respectively. If you do not want this case
  1574. equivalence, you can suppress it by setting PCRE2_EXTRA_CASELESS_RESTRICT.
  1575. </P>
  1576. <P>
  1577. For lower valued characters with only one other case, a lookup table is used
  1578. for speed. When neither PCRE2_UTF nor PCRE2_UCP is set, a lookup table is used
  1579. for all code points less than 256, and higher code points (available only in
  1580. 16-bit or 32-bit mode) are treated as not having another case.
  1581. <pre>
  1582. PCRE2_DOLLAR_ENDONLY
  1583. </pre>
  1584. If this bit is set, a dollar metacharacter in the pattern matches only at the
  1585. end of the subject string. Without this option, a dollar also matches
  1586. immediately before a newline at the end of the string (but not before any other
  1587. newlines). The PCRE2_DOLLAR_ENDONLY option is ignored if PCRE2_MULTILINE is
  1588. set. There is no equivalent to this option in Perl, and no way to set it within
  1589. a pattern.
  1590. <pre>
  1591. PCRE2_DOTALL
  1592. </pre>
  1593. If this bit is set, a dot metacharacter in the pattern matches any character,
  1594. including one that indicates a newline. However, it only ever matches one
  1595. character, even if newlines are coded as CRLF. Without this option, a dot does
  1596. not match when the current position in the subject is at a newline. This option
  1597. is equivalent to Perl's /s option, and it can be changed within a pattern by a
  1598. (?s) option setting. A negative class such as [^a] always matches newline
  1599. characters, and the \N escape sequence always matches a non-newline character,
  1600. independent of the setting of PCRE2_DOTALL.
  1601. <pre>
  1602. PCRE2_DUPNAMES
  1603. </pre>
  1604. If this bit is set, names used to identify capture groups need not be unique.
  1605. This can be helpful for certain types of pattern when it is known that only one
  1606. instance of the named group can ever be matched. There are more details of
  1607. named capture groups below; see also the
  1608. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  1609. documentation.
  1610. <pre>
  1611. PCRE2_ENDANCHORED
  1612. </pre>
  1613. If this bit is set, the end of any pattern match must be right at the end of
  1614. the string being searched (the "subject string"). If the pattern match
  1615. succeeds by reaching (*ACCEPT), but does not reach the end of the subject, the
  1616. match fails at the current starting point. For unanchored patterns, a new match
  1617. is then tried at the next starting point. However, if the match succeeds by
  1618. reaching the end of the pattern, but not the end of the subject, backtracking
  1619. occurs and an alternative match may be found. Consider these two patterns:
  1620. <pre>
  1621. .(*ACCEPT)|..
  1622. .|..
  1623. </pre>
  1624. If matched against "abc" with PCRE2_ENDANCHORED set, the first matches "c"
  1625. whereas the second matches "bc". The effect of PCRE2_ENDANCHORED can also be
  1626. achieved by appropriate constructs in the pattern itself, which is the only way
  1627. to do it in Perl.
  1628. </P>
  1629. <P>
  1630. For DFA matching with <b>pcre2_dfa_match()</b>, PCRE2_ENDANCHORED applies only
  1631. to the first (that is, the longest) matched string. Other parallel matches,
  1632. which are necessarily substrings of the first one, must obviously end before
  1633. the end of the subject.
  1634. <pre>
  1635. PCRE2_EXTENDED
  1636. </pre>
  1637. If this bit is set, most white space characters in the pattern are totally
  1638. ignored except when escaped, inside a character class, or inside a \Q...\E
  1639. sequence. However, white space is not allowed within sequences such as (?&#62; that
  1640. introduce various parenthesized groups, nor within numerical quantifiers such
  1641. as {1,3}. Ignorable white space is permitted between an item and a following
  1642. quantifier and between a quantifier and a following + that indicates
  1643. possessiveness. PCRE2_EXTENDED is equivalent to Perl's /x option, and it can be
  1644. changed within a pattern by a (?x) option setting.
  1645. </P>
  1646. <P>
  1647. When PCRE2 is compiled without Unicode support, PCRE2_EXTENDED recognizes as
  1648. white space only those characters with code points less than 256 that are
  1649. flagged as white space in its low-character table. The table is normally
  1650. created by
  1651. <a href="pcre2_maketables.html"><b>pcre2_maketables()</b>,</a>
  1652. which uses the <b>isspace()</b> function to identify space characters. In most
  1653. ASCII environments, the relevant characters are those with code points 0x0009
  1654. (tab), 0x000A (linefeed), 0x000B (vertical tab), 0x000C (formfeed), 0x000D
  1655. (carriage return), and 0x0020 (space).
  1656. </P>
  1657. <P>
  1658. When PCRE2 is compiled with Unicode support, in addition to these characters,
  1659. five more Unicode "Pattern White Space" characters are recognized by
  1660. PCRE2_EXTENDED. These are U+0085 (next line), U+200E (left-to-right mark),
  1661. U+200F (right-to-left mark), U+2028 (line separator), and U+2029 (paragraph
  1662. separator). This set of characters is the same as recognized by Perl's /x
  1663. option. Note that the horizontal and vertical space characters that are matched
  1664. by the \h and \v escapes in patterns are a much bigger set.
  1665. </P>
  1666. <P>
  1667. As well as ignoring most white space, PCRE2_EXTENDED also causes characters
  1668. between an unescaped # outside a character class and the next newline,
  1669. inclusive, to be ignored, which makes it possible to include comments inside
  1670. complicated patterns. Note that the end of this type of comment is a literal
  1671. newline sequence in the pattern; escape sequences that happen to represent a
  1672. newline do not count.
  1673. </P>
  1674. <P>
  1675. Which characters are interpreted as newlines can be specified by a setting in
  1676. the compile context that is passed to <b>pcre2_compile()</b> or by a special
  1677. sequence at the start of the pattern, as described in the section entitled
  1678. <a href="pcre2pattern.html#newlines">"Newline conventions"</a>
  1679. in the <b>pcre2pattern</b> documentation. A default is defined when PCRE2 is
  1680. built.
  1681. <pre>
  1682. PCRE2_EXTENDED_MORE
  1683. </pre>
  1684. This option has the effect of PCRE2_EXTENDED, but, in addition, unescaped space
  1685. and horizontal tab characters are ignored inside a character class. Note: only
  1686. these two characters are ignored, not the full set of pattern white space
  1687. characters that are ignored outside a character class. PCRE2_EXTENDED_MORE is
  1688. equivalent to Perl's /xx option, and it can be changed within a pattern by a
  1689. (?xx) option setting.
  1690. <pre>
  1691. PCRE2_FIRSTLINE
  1692. </pre>
  1693. If this option is set, the start of an unanchored pattern match must be before
  1694. or at the first newline in the subject string following the start of matching,
  1695. though the matched text may continue over the newline. If <i>startoffset</i> is
  1696. non-zero, the limiting newline is not necessarily the first newline in the
  1697. subject. For example, if the subject string is "abc\nxyz" (where \n
  1698. represents a single-character newline) a pattern match for "yz" succeeds with
  1699. PCRE2_FIRSTLINE if <i>startoffset</i> is greater than 3. See also
  1700. PCRE2_USE_OFFSET_LIMIT, which provides a more general limiting facility. If
  1701. PCRE2_FIRSTLINE is set with an offset limit, a match must occur in the first
  1702. line and also within the offset limit. In other words, whichever limit comes
  1703. first is used. This option has no effect for anchored patterns.
  1704. <pre>
  1705. PCRE2_LITERAL
  1706. </pre>
  1707. If this option is set, all meta-characters in the pattern are disabled, and it
  1708. is treated as a literal string. Matching literal strings with a regular
  1709. expression engine is not the most efficient way of doing it. If you are doing a
  1710. lot of literal matching and are worried about efficiency, you should consider
  1711. using other approaches. The only other main options that are allowed with
  1712. PCRE2_LITERAL are: PCRE2_ANCHORED, PCRE2_ENDANCHORED, PCRE2_AUTO_CALLOUT,
  1713. PCRE2_CASELESS, PCRE2_FIRSTLINE, PCRE2_MATCH_INVALID_UTF,
  1714. PCRE2_NO_START_OPTIMIZE, PCRE2_NO_UTF_CHECK, PCRE2_UTF, and
  1715. PCRE2_USE_OFFSET_LIMIT. The extra options PCRE2_EXTRA_MATCH_LINE and
  1716. PCRE2_EXTRA_MATCH_WORD are also supported. Any other options cause an error.
  1717. <pre>
  1718. PCRE2_MATCH_INVALID_UTF
  1719. </pre>
  1720. This option forces PCRE2_UTF (see below) and also enables support for matching
  1721. by <b>pcre2_match()</b> in subject strings that contain invalid UTF sequences.
  1722. Note, however, that the 16-bit and 32-bit PCRE2 libraries process strings as
  1723. sequences of uint16_t or uint32_t code points. They cannot find valid UTF
  1724. sequences within an arbitrary string of bytes unless such sequences are
  1725. suitably aligned. This facility is not supported for DFA matching. For details,
  1726. see the
  1727. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  1728. documentation.
  1729. <pre>
  1730. PCRE2_MATCH_UNSET_BACKREF
  1731. </pre>
  1732. If this option is set, a backreference to an unset capture group matches an
  1733. empty string (by default this causes the current matching alternative to fail).
  1734. A pattern such as (\1)(a) succeeds when this option is set (assuming it can
  1735. find an "a" in the subject), whereas it fails by default, for Perl
  1736. compatibility. Setting this option makes PCRE2 behave more like ECMAscript (aka
  1737. JavaScript).
  1738. <pre>
  1739. PCRE2_MULTILINE
  1740. </pre>
  1741. By default, for the purposes of matching "start of line" and "end of line",
  1742. PCRE2 treats the subject string as consisting of a single line of characters,
  1743. even if it actually contains newlines. The "start of line" metacharacter (^)
  1744. matches only at the start of the string, and the "end of line" metacharacter
  1745. ($) matches only at the end of the string, or before a terminating newline
  1746. (except when PCRE2_DOLLAR_ENDONLY is set). Note, however, that unless
  1747. PCRE2_DOTALL is set, the "any character" metacharacter (.) does not match at a
  1748. newline. This behaviour (for ^, $, and dot) is the same as Perl.
  1749. </P>
  1750. <P>
  1751. When PCRE2_MULTILINE it is set, the "start of line" and "end of line"
  1752. constructs match immediately following or immediately before internal newlines
  1753. in the subject string, respectively, as well as at the very start and end. This
  1754. is equivalent to Perl's /m option, and it can be changed within a pattern by a
  1755. (?m) option setting. Note that the "start of line" metacharacter does not match
  1756. after a newline at the end of the subject, for compatibility with Perl.
  1757. However, you can change this by setting the PCRE2_ALT_CIRCUMFLEX option. If
  1758. there are no newlines in a subject string, or no occurrences of ^ or $ in a
  1759. pattern, setting PCRE2_MULTILINE has no effect.
  1760. <pre>
  1761. PCRE2_NEVER_BACKSLASH_C
  1762. </pre>
  1763. This option locks out the use of \C in the pattern that is being compiled.
  1764. This escape can cause unpredictable behaviour in UTF-8 or UTF-16 modes, because
  1765. it may leave the current matching point in the middle of a multi-code-unit
  1766. character. This option may be useful in applications that process patterns from
  1767. external sources. Note that there is also a build-time option that permanently
  1768. locks out the use of \C.
  1769. <pre>
  1770. PCRE2_NEVER_UCP
  1771. </pre>
  1772. This option locks out the use of Unicode properties for handling \B, \b, \D,
  1773. \d, \S, \s, \W, \w, and some of the POSIX character classes, as described
  1774. for the PCRE2_UCP option below. In particular, it prevents the creator of the
  1775. pattern from enabling this facility by starting the pattern with (*UCP). This
  1776. option may be useful in applications that process patterns from external
  1777. sources. The option combination PCRE_UCP and PCRE_NEVER_UCP causes an error.
  1778. <pre>
  1779. PCRE2_NEVER_UTF
  1780. </pre>
  1781. This option locks out interpretation of the pattern as UTF-8, UTF-16, or
  1782. UTF-32, depending on which library is in use. In particular, it prevents the
  1783. creator of the pattern from switching to UTF interpretation by starting the
  1784. pattern with (*UTF). This option may be useful in applications that process
  1785. patterns from external sources. The combination of PCRE2_UTF and
  1786. PCRE2_NEVER_UTF causes an error.
  1787. <pre>
  1788. PCRE2_NO_AUTO_CAPTURE
  1789. </pre>
  1790. If this option is set, it disables the use of numbered capturing parentheses in
  1791. the pattern. Any opening parenthesis that is not followed by ? behaves as if it
  1792. were followed by ?: but named parentheses can still be used for capturing (and
  1793. they acquire numbers in the usual way). This is the same as Perl's /n option.
  1794. Note that, when this option is set, references to capture groups
  1795. (backreferences or recursion/subroutine calls) may only refer to named groups,
  1796. though the reference can be by name or by number.
  1797. <pre>
  1798. PCRE2_NO_AUTO_POSSESS
  1799. </pre>
  1800. If this option is set, it disables "auto-possessification", which is an
  1801. optimization that, for example, turns a+b into a++b in order to avoid
  1802. backtracks into a+ that can never be successful. However, if callouts are in
  1803. use, auto-possessification means that some callouts are never taken. You can
  1804. set this option if you want the matching functions to do a full unoptimized
  1805. search and run all the callouts, but it is mainly provided for testing
  1806. purposes.
  1807. <pre>
  1808. PCRE2_NO_DOTSTAR_ANCHOR
  1809. </pre>
  1810. If this option is set, it disables an optimization that is applied when .* is
  1811. the first significant item in a top-level branch of a pattern, and all the
  1812. other branches also start with .* or with \A or \G or ^. The optimization is
  1813. automatically disabled for .* if it is inside an atomic group or a capture
  1814. group that is the subject of a backreference, or if the pattern contains
  1815. (*PRUNE) or (*SKIP). When the optimization is not disabled, such a pattern is
  1816. automatically anchored if PCRE2_DOTALL is set for all the .* items and
  1817. PCRE2_MULTILINE is not set for any ^ items. Otherwise, the fact that any match
  1818. must start either at the start of the subject or following a newline is
  1819. remembered. Like other optimizations, this can cause callouts to be skipped.
  1820. <pre>
  1821. PCRE2_NO_START_OPTIMIZE
  1822. </pre>
  1823. This is an option whose main effect is at matching time. It does not change
  1824. what <b>pcre2_compile()</b> generates, but it does affect the output of the JIT
  1825. compiler.
  1826. </P>
  1827. <P>
  1828. There are a number of optimizations that may occur at the start of a match, in
  1829. order to speed up the process. For example, if it is known that an unanchored
  1830. match must start with a specific code unit value, the matching code searches
  1831. the subject for that value, and fails immediately if it cannot find it, without
  1832. actually running the main matching function. This means that a special item
  1833. such as (*COMMIT) at the start of a pattern is not considered until after a
  1834. suitable starting point for the match has been found. Also, when callouts or
  1835. (*MARK) items are in use, these "start-up" optimizations can cause them to be
  1836. skipped if the pattern is never actually used. The start-up optimizations are
  1837. in effect a pre-scan of the subject that takes place before the pattern is run.
  1838. </P>
  1839. <P>
  1840. The PCRE2_NO_START_OPTIMIZE option disables the start-up optimizations,
  1841. possibly causing performance to suffer, but ensuring that in cases where the
  1842. result is "no match", the callouts do occur, and that items such as (*COMMIT)
  1843. and (*MARK) are considered at every possible starting position in the subject
  1844. string.
  1845. </P>
  1846. <P>
  1847. Setting PCRE2_NO_START_OPTIMIZE may change the outcome of a matching operation.
  1848. Consider the pattern
  1849. <pre>
  1850. (*COMMIT)ABC
  1851. </pre>
  1852. When this is compiled, PCRE2 records the fact that a match must start with the
  1853. character "A". Suppose the subject string is "DEFABC". The start-up
  1854. optimization scans along the subject, finds "A" and runs the first match
  1855. attempt from there. The (*COMMIT) item means that the pattern must match the
  1856. current starting position, which in this case, it does. However, if the same
  1857. match is run with PCRE2_NO_START_OPTIMIZE set, the initial scan along the
  1858. subject string does not happen. The first match attempt is run starting from
  1859. "D" and when this fails, (*COMMIT) prevents any further matches being tried, so
  1860. the overall result is "no match".
  1861. </P>
  1862. <P>
  1863. As another start-up optimization makes use of a minimum length for a matching
  1864. subject, which is recorded when possible. Consider the pattern
  1865. <pre>
  1866. (*MARK:1)B(*MARK:2)(X|Y)
  1867. </pre>
  1868. The minimum length for a match is two characters. If the subject is "XXBB", the
  1869. "starting character" optimization skips "XX", then tries to match "BB", which
  1870. is long enough. In the process, (*MARK:2) is encountered and remembered. When
  1871. the match attempt fails, the next "B" is found, but there is only one character
  1872. left, so there are no more attempts, and "no match" is returned with the "last
  1873. mark seen" set to "2". If NO_START_OPTIMIZE is set, however, matches are tried
  1874. at every possible starting position, including at the end of the subject, where
  1875. (*MARK:1) is encountered, but there is no "B", so the "last mark seen" that is
  1876. returned is "1". In this case, the optimizations do not affect the overall
  1877. match result, which is still "no match", but they do affect the auxiliary
  1878. information that is returned.
  1879. <pre>
  1880. PCRE2_NO_UTF_CHECK
  1881. </pre>
  1882. When PCRE2_UTF is set, the validity of the pattern as a UTF string is
  1883. automatically checked. There are discussions about the validity of
  1884. <a href="pcre2unicode.html#utf8strings">UTF-8 strings,</a>
  1885. <a href="pcre2unicode.html#utf16strings">UTF-16 strings,</a>
  1886. and
  1887. <a href="pcre2unicode.html#utf32strings">UTF-32 strings</a>
  1888. in the
  1889. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  1890. document. If an invalid UTF sequence is found, <b>pcre2_compile()</b> returns a
  1891. negative error code.
  1892. </P>
  1893. <P>
  1894. If you know that your pattern is a valid UTF string, and you want to skip this
  1895. check for performance reasons, you can set the PCRE2_NO_UTF_CHECK option. When
  1896. it is set, the effect of passing an invalid UTF string as a pattern is
  1897. undefined. It may cause your program to crash or loop.
  1898. </P>
  1899. <P>
  1900. Note that this option can also be passed to <b>pcre2_match()</b> and
  1901. <b>pcre2_dfa_match()</b>, to suppress UTF validity checking of the subject
  1902. string.
  1903. </P>
  1904. <P>
  1905. Note also that setting PCRE2_NO_UTF_CHECK at compile time does not disable the
  1906. error that is given if an escape sequence for an invalid Unicode code point is
  1907. encountered in the pattern. In particular, the so-called "surrogate" code
  1908. points (0xd800 to 0xdfff) are invalid. If you want to allow escape sequences
  1909. such as \x{d800} you can set the PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES extra
  1910. option, as described in the section entitled "Extra compile options"
  1911. <a href="#extracompileoptions">below.</a>
  1912. However, this is possible only in UTF-8 and UTF-32 modes, because these values
  1913. are not representable in UTF-16.
  1914. <pre>
  1915. PCRE2_UCP
  1916. </pre>
  1917. This option has two effects. Firstly, it change the way PCRE2 processes \B,
  1918. \b, \D, \d, \S, \s, \W, \w, and some of the POSIX character classes. By
  1919. default, only ASCII characters are recognized, but if PCRE2_UCP is set, Unicode
  1920. properties are used to classify characters. There are some PCRE2_EXTRA
  1921. options (see below) that add finer control to this behaviour. More details are
  1922. given in the section on
  1923. <a href="pcre2pattern.html#genericchartypes">generic character types</a>
  1924. in the
  1925. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  1926. page.
  1927. </P>
  1928. <P>
  1929. The second effect of PCRE2_UCP is to force the use of Unicode properties for
  1930. upper/lower casing operations, even when PCRE2_UTF is not set. This makes it
  1931. possible to process strings in the 16-bit UCS-2 code. This option is available
  1932. only if PCRE2 has been compiled with Unicode support (which is the default).
  1933. The PCRE2_EXTRA_CASELESS_RESTRICT option (see below) restricts caseless
  1934. matching such that ASCII characters match only ASCII characters and non-ASCII
  1935. characters match only non-ASCII characters.
  1936. <pre>
  1937. PCRE2_UNGREEDY
  1938. </pre>
  1939. This option inverts the "greediness" of the quantifiers so that they are not
  1940. greedy by default, but become greedy if followed by "?". It is not compatible
  1941. with Perl. It can also be set by a (?U) option setting within the pattern.
  1942. <pre>
  1943. PCRE2_USE_OFFSET_LIMIT
  1944. </pre>
  1945. This option must be set for <b>pcre2_compile()</b> if
  1946. <b>pcre2_set_offset_limit()</b> is going to be used to set a non-default offset
  1947. limit in a match context for matches that use this pattern. An error is
  1948. generated if an offset limit is set without this option. For more details, see
  1949. the description of <b>pcre2_set_offset_limit()</b> in the
  1950. <a href="#matchcontext">section</a>
  1951. that describes match contexts. See also the PCRE2_FIRSTLINE
  1952. option above.
  1953. <pre>
  1954. PCRE2_UTF
  1955. </pre>
  1956. This option causes PCRE2 to regard both the pattern and the subject strings
  1957. that are subsequently processed as strings of UTF characters instead of
  1958. single-code-unit strings. It is available when PCRE2 is built to include
  1959. Unicode support (which is the default). If Unicode support is not available,
  1960. the use of this option provokes an error. Details of how PCRE2_UTF changes the
  1961. behaviour of PCRE2 are given in the
  1962. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  1963. page. In particular, note that it changes the way PCRE2_CASELESS works.
  1964. <a name="extracompileoptions"></a></P>
  1965. <br><b>
  1966. Extra compile options
  1967. </b><br>
  1968. <P>
  1969. The option bits that can be set in a compile context by calling the
  1970. <b>pcre2_set_compile_extra_options()</b> function are as follows:
  1971. <pre>
  1972. PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
  1973. </pre>
  1974. Since release 10.38 PCRE2 has forbidden the use of \K within lookaround
  1975. assertions, following Perl's lead. This option is provided to re-enable the
  1976. previous behaviour (act in positive lookarounds, ignore in negative ones) in
  1977. case anybody is relying on it.
  1978. <pre>
  1979. PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES
  1980. </pre>
  1981. This option applies when compiling a pattern in UTF-8 or UTF-32 mode. It is
  1982. forbidden in UTF-16 mode, and ignored in non-UTF modes. Unicode "surrogate"
  1983. code points in the range 0xd800 to 0xdfff are used in pairs in UTF-16 to encode
  1984. code points with values in the range 0x10000 to 0x10ffff. The surrogates cannot
  1985. therefore be represented in UTF-16. They can be represented in UTF-8 and
  1986. UTF-32, but are defined as invalid code points, and cause errors if encountered
  1987. in a UTF-8 or UTF-32 string that is being checked for validity by PCRE2.
  1988. </P>
  1989. <P>
  1990. These values also cause errors if encountered in escape sequences such as
  1991. \x{d912} within a pattern. However, it seems that some applications, when
  1992. using PCRE2 to check for unwanted characters in UTF-8 strings, explicitly test
  1993. for the surrogates using escape sequences. The PCRE2_NO_UTF_CHECK option does
  1994. not disable the error that occurs, because it applies only to the testing of
  1995. input strings for UTF validity.
  1996. </P>
  1997. <P>
  1998. If the extra option PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES is set, surrogate code
  1999. point values in UTF-8 and UTF-32 patterns no longer provoke errors and are
  2000. incorporated in the compiled pattern. However, they can only match subject
  2001. characters if the matching function is called with PCRE2_NO_UTF_CHECK set.
  2002. <pre>
  2003. PCRE2_EXTRA_ALT_BSUX
  2004. </pre>
  2005. The original option PCRE2_ALT_BSUX causes PCRE2 to process \U, \u, and \x in
  2006. the way that ECMAscript (aka JavaScript) does. Additional functionality was
  2007. defined by ECMAscript 6; setting PCRE2_EXTRA_ALT_BSUX has the effect of
  2008. PCRE2_ALT_BSUX, but in addition it recognizes \u{hhh..} as a hexadecimal
  2009. character code, where hhh.. is any number of hexadecimal digits.
  2010. <pre>
  2011. PCRE2_EXTRA_ASCII_BSD
  2012. </pre>
  2013. This option forces \d to match only ASCII digits, even when PCRE2_UCP is set.
  2014. It can be changed within a pattern by means of the (?aD) option setting.
  2015. <pre>
  2016. PCRE2_EXTRA_ASCII_BSS
  2017. </pre>
  2018. This option forces \s to match only ASCII space characters, even when
  2019. PCRE2_UCP is set. It can be changed within a pattern by means of the (?aS)
  2020. option setting.
  2021. <pre>
  2022. PCRE2_EXTRA_ASCII_BSW
  2023. </pre>
  2024. This option forces \w to match only ASCII word characters, even when PCRE2_UCP
  2025. is set. It can be changed within a pattern by means of the (?aW) option
  2026. setting.
  2027. <pre>
  2028. PCRE2_EXTRA_ASCII_DIGIT
  2029. </pre>
  2030. This option forces the POSIX character classes [:digit:] and [:xdigit:] to
  2031. match only ASCII digits, even when PCRE2_UCP is set. It can be changed within
  2032. a pattern by means of the (?aT) option setting.
  2033. <pre>
  2034. PCRE2_EXTRA_ASCII_POSIX
  2035. </pre>
  2036. This option forces all the POSIX character classes, including [:digit:] and
  2037. [:xdigit:], to match only ASCII characters, even when PCRE2_UCP is set. It can
  2038. be changed within a pattern by means of the (?aP) option setting, but note that
  2039. this also sets PCRE2_EXTRA_ASCII_DIGIT in order to ensure that (?-aP) unsets
  2040. all ASCII restrictions for POSIX classes.
  2041. <pre>
  2042. PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL
  2043. </pre>
  2044. This is a dangerous option. Use with care. By default, an unrecognized escape
  2045. such as \j or a malformed one such as \x{2z} causes a compile-time error when
  2046. detected by <b>pcre2_compile()</b>. Perl is somewhat inconsistent in handling
  2047. such items: for example, \j is treated as a literal "j", and non-hexadecimal
  2048. digits in \x{} are just ignored, though warnings are given in both cases if
  2049. Perl's warning switch is enabled. However, a malformed octal number after \o{
  2050. always causes an error in Perl.
  2051. </P>
  2052. <P>
  2053. If the PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL extra option is passed to
  2054. <b>pcre2_compile()</b>, all unrecognized or malformed escape sequences are
  2055. treated as single-character escapes. For example, \j is a literal "j" and
  2056. \x{2z} is treated as the literal string "x{2z}". Setting this option means
  2057. that typos in patterns may go undetected and have unexpected results. Also note
  2058. that a sequence such as [\N{] is interpreted as a malformed attempt at
  2059. [\N{...}] and so is treated as [N{] whereas [\N] gives an error because an
  2060. unqualified \N is a valid escape sequence but is not supported in a character
  2061. class. To reiterate: this is a dangerous option. Use with great care.
  2062. <pre>
  2063. PCRE2_EXTRA_CASELESS_RESTRICT
  2064. </pre>
  2065. When either PCRE2_UCP or PCRE2_UTF is set, caseless matching follows Unicode
  2066. rules, which allow for more than two cases per character. There are two
  2067. case-equivalent character sets that contain both ASCII and non-ASCII
  2068. characters. The ASCII letter S is case-equivalent to U+017f (long S) and the
  2069. ASCII letter K is case-equivalent to U+212a (Kelvin sign). This option disables
  2070. recognition of case-equivalences that cross the ASCII/non-ASCII boundary. In a
  2071. caseless match, both characters must either be ASCII or non-ASCII. The option
  2072. can be changed with a pattern by the (?r) option setting.
  2073. <pre>
  2074. PCRE2_EXTRA_ESCAPED_CR_IS_LF
  2075. </pre>
  2076. There are some legacy applications where the escape sequence \r in a pattern
  2077. is expected to match a newline. If this option is set, \r in a pattern is
  2078. converted to \n so that it matches a LF (linefeed) instead of a CR (carriage
  2079. return) character. The option does not affect a literal CR in the pattern, nor
  2080. does it affect CR specified as an explicit code point such as \x{0D}.
  2081. <pre>
  2082. PCRE2_EXTRA_MATCH_LINE
  2083. </pre>
  2084. This option is provided for use by the <b>-x</b> option of <b>pcre2grep</b>. It
  2085. causes the pattern only to match complete lines. This is achieved by
  2086. automatically inserting the code for "^(?:" at the start of the compiled
  2087. pattern and ")$" at the end. Thus, when PCRE2_MULTILINE is set, the matched
  2088. line may be in the middle of the subject string. This option can be used with
  2089. PCRE2_LITERAL.
  2090. <pre>
  2091. PCRE2_EXTRA_MATCH_WORD
  2092. </pre>
  2093. This option is provided for use by the <b>-w</b> option of <b>pcre2grep</b>. It
  2094. causes the pattern only to match strings that have a word boundary at the start
  2095. and the end. This is achieved by automatically inserting the code for "\b(?:"
  2096. at the start of the compiled pattern and ")\b" at the end. The option may be
  2097. used with PCRE2_LITERAL. However, it is ignored if PCRE2_EXTRA_MATCH_LINE is
  2098. also set.
  2099. <a name="jitcompiling"></a></P>
  2100. <br><a name="SEC21" href="#TOC1">JUST-IN-TIME (JIT) COMPILATION</a><br>
  2101. <P>
  2102. <b>int pcre2_jit_compile(pcre2_code *<i>code</i>, uint32_t <i>options</i>);</b>
  2103. <br>
  2104. <br>
  2105. <b>int pcre2_jit_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  2106. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  2107. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  2108. <b> pcre2_match_context *<i>mcontext</i>);</b>
  2109. <br>
  2110. <br>
  2111. <b>void pcre2_jit_free_unused_memory(pcre2_general_context *<i>gcontext</i>);</b>
  2112. <br>
  2113. <br>
  2114. <b>pcre2_jit_stack *pcre2_jit_stack_create(size_t <i>startsize</i>,</b>
  2115. <b> size_t <i>maxsize</i>, pcre2_general_context *<i>gcontext</i>);</b>
  2116. <br>
  2117. <br>
  2118. <b>void pcre2_jit_stack_assign(pcre2_match_context *<i>mcontext</i>,</b>
  2119. <b> pcre2_jit_callback <i>callback_function</i>, void *<i>callback_data</i>);</b>
  2120. <br>
  2121. <br>
  2122. <b>void pcre2_jit_stack_free(pcre2_jit_stack *<i>jit_stack</i>);</b>
  2123. </P>
  2124. <P>
  2125. These functions provide support for JIT compilation, which, if the just-in-time
  2126. compiler is available, further processes a compiled pattern into machine code
  2127. that executes much faster than the <b>pcre2_match()</b> interpretive matching
  2128. function. Full details are given in the
  2129. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  2130. documentation.
  2131. </P>
  2132. <P>
  2133. JIT compilation is a heavyweight optimization. It can take some time for
  2134. patterns to be analyzed, and for one-off matches and simple patterns the
  2135. benefit of faster execution might be offset by a much slower compilation time.
  2136. Most (but not all) patterns can be optimized by the JIT compiler.
  2137. <a name="localesupport"></a></P>
  2138. <br><a name="SEC22" href="#TOC1">LOCALE SUPPORT</a><br>
  2139. <P>
  2140. <b>const uint8_t *pcre2_maketables(pcre2_general_context *<i>gcontext</i>);</b>
  2141. <br>
  2142. <br>
  2143. <b>void pcre2_maketables_free(pcre2_general_context *<i>gcontext</i>,</b>
  2144. <b> const uint8_t *<i>tables</i>);</b>
  2145. </P>
  2146. <P>
  2147. PCRE2 handles caseless matching, and determines whether characters are letters,
  2148. digits, or whatever, by reference to a set of tables, indexed by character code
  2149. point. However, this applies only to characters whose code points are less than
  2150. 256. By default, higher-valued code points never match escapes such as \w or
  2151. \d.
  2152. </P>
  2153. <P>
  2154. When PCRE2 is built with Unicode support (the default), certain Unicode
  2155. character properties can be tested with \p and \P, or, alternatively, the
  2156. PCRE2_UCP option can be set when a pattern is compiled; this causes \w and
  2157. friends to use Unicode property support instead of the built-in tables.
  2158. PCRE2_UCP also causes upper/lower casing operations on characters with code
  2159. points greater than 127 to use Unicode properties. These effects apply even
  2160. when PCRE2_UTF is not set. There are, however, some PCRE2_EXTRA options (see
  2161. above) that can be used to modify or suppress them.
  2162. </P>
  2163. <P>
  2164. The use of locales with Unicode is discouraged. If you are handling characters
  2165. with code points greater than 127, you should either use Unicode support, or
  2166. use locales, but not try to mix the two.
  2167. </P>
  2168. <P>
  2169. PCRE2 contains a built-in set of character tables that are used by default.
  2170. These are sufficient for many applications. Normally, the internal tables
  2171. recognize only ASCII characters. However, when PCRE2 is built, it is possible
  2172. to cause the internal tables to be rebuilt in the default "C" locale of the
  2173. local system, which may cause them to be different.
  2174. </P>
  2175. <P>
  2176. The built-in tables can be overridden by tables supplied by the application
  2177. that calls PCRE2. These may be created in a different locale from the default.
  2178. As more and more applications change to using Unicode, the need for this locale
  2179. support is expected to die away.
  2180. </P>
  2181. <P>
  2182. External tables are built by calling the <b>pcre2_maketables()</b> function, in
  2183. the relevant locale. The only argument to this function is a general context,
  2184. which can be used to pass a custom memory allocator. If the argument is NULL,
  2185. the system <b>malloc()</b> is used. The result can be passed to
  2186. <b>pcre2_compile()</b> as often as necessary, by creating a compile context and
  2187. calling <b>pcre2_set_character_tables()</b> to set the tables pointer therein.
  2188. </P>
  2189. <P>
  2190. For example, to build and use tables that are appropriate for the French locale
  2191. (where accented characters with values greater than 127 are treated as
  2192. letters), the following code could be used:
  2193. <pre>
  2194. setlocale(LC_CTYPE, "fr_FR");
  2195. tables = pcre2_maketables(NULL);
  2196. ccontext = pcre2_compile_context_create(NULL);
  2197. pcre2_set_character_tables(ccontext, tables);
  2198. re = pcre2_compile(..., ccontext);
  2199. </pre>
  2200. The locale name "fr_FR" is used on Linux and other Unix-like systems; if you
  2201. are using Windows, the name for the French locale is "french".
  2202. </P>
  2203. <P>
  2204. The pointer that is passed (via the compile context) to <b>pcre2_compile()</b>
  2205. is saved with the compiled pattern, and the same tables are used by the
  2206. matching functions. Thus, for any single pattern, compilation and matching both
  2207. happen in the same locale, but different patterns can be processed in different
  2208. locales.
  2209. </P>
  2210. <P>
  2211. It is the caller's responsibility to ensure that the memory containing the
  2212. tables remains available while they are still in use. When they are no longer
  2213. needed, you can discard them using <b>pcre2_maketables_free()</b>, which should
  2214. pass as its first parameter the same global context that was used to create the
  2215. tables.
  2216. </P>
  2217. <br><b>
  2218. Saving locale tables
  2219. </b><br>
  2220. <P>
  2221. The tables described above are just a sequence of binary bytes, which makes
  2222. them independent of hardware characteristics such as endianness or whether the
  2223. processor is 32-bit or 64-bit. A copy of the result of <b>pcre2_maketables()</b>
  2224. can therefore be saved in a file or elsewhere and re-used later, even in a
  2225. different program or on another computer. The size of the tables (number of
  2226. bytes) must be obtained by calling <b>pcre2_config()</b> with the
  2227. PCRE2_CONFIG_TABLES_LENGTH option because <b>pcre2_maketables()</b> does not
  2228. return this value. Note that the <b>pcre2_dftables</b> program, which is part of
  2229. the PCRE2 build system, can be used stand-alone to create a file that contains
  2230. a set of binary tables. See the
  2231. <a href="pcre2build.html#createtables"><b>pcre2build</b></a>
  2232. documentation for details.
  2233. <a name="infoaboutpattern"></a></P>
  2234. <br><a name="SEC23" href="#TOC1">INFORMATION ABOUT A COMPILED PATTERN</a><br>
  2235. <P>
  2236. <b>int pcre2_pattern_info(const pcre2 *<i>code</i>, uint32_t <i>what</i>, void *<i>where</i>);</b>
  2237. </P>
  2238. <P>
  2239. The <b>pcre2_pattern_info()</b> function returns general information about a
  2240. compiled pattern. For information about callouts, see the
  2241. <a href="#infoaboutcallouts">next section.</a>
  2242. The first argument for <b>pcre2_pattern_info()</b> is a pointer to the compiled
  2243. pattern. The second argument specifies which piece of information is required,
  2244. and the third argument is a pointer to a variable to receive the data. If the
  2245. third argument is NULL, the first argument is ignored, and the function returns
  2246. the size in bytes of the variable that is required for the information
  2247. requested. Otherwise, the yield of the function is zero for success, or one of
  2248. the following negative numbers:
  2249. <pre>
  2250. PCRE2_ERROR_NULL the argument <i>code</i> was NULL
  2251. PCRE2_ERROR_BADMAGIC the "magic number" was not found
  2252. PCRE2_ERROR_BADOPTION the value of <i>what</i> was invalid
  2253. PCRE2_ERROR_UNSET the requested field is not set
  2254. </pre>
  2255. The "magic number" is placed at the start of each compiled pattern as a simple
  2256. check against passing an arbitrary memory pointer. Here is a typical call of
  2257. <b>pcre2_pattern_info()</b>, to obtain the length of the compiled pattern:
  2258. <pre>
  2259. int rc;
  2260. size_t length;
  2261. rc = pcre2_pattern_info(
  2262. re, /* result of pcre2_compile() */
  2263. PCRE2_INFO_SIZE, /* what is required */
  2264. &length); /* where to put the data */
  2265. </pre>
  2266. The possible values for the second argument are defined in <b>pcre2.h</b>, and
  2267. are as follows:
  2268. <pre>
  2269. PCRE2_INFO_ALLOPTIONS
  2270. PCRE2_INFO_ARGOPTIONS
  2271. PCRE2_INFO_EXTRAOPTIONS
  2272. </pre>
  2273. Return copies of the pattern's options. The third argument should point to a
  2274. <b>uint32_t</b> variable. PCRE2_INFO_ARGOPTIONS returns exactly the options that
  2275. were passed to <b>pcre2_compile()</b>, whereas PCRE2_INFO_ALLOPTIONS returns
  2276. the compile options as modified by any top-level (*XXX) option settings such as
  2277. (*UTF) at the start of the pattern itself. PCRE2_INFO_EXTRAOPTIONS returns the
  2278. extra options that were set in the compile context by calling the
  2279. pcre2_set_compile_extra_options() function.
  2280. </P>
  2281. <P>
  2282. For example, if the pattern /(*UTF)abc/ is compiled with the PCRE2_EXTENDED
  2283. option, the result for PCRE2_INFO_ALLOPTIONS is PCRE2_EXTENDED and PCRE2_UTF.
  2284. Option settings such as (?i) that can change within a pattern do not affect the
  2285. result of PCRE2_INFO_ALLOPTIONS, even if they appear right at the start of the
  2286. pattern. (This was different in some earlier releases.)
  2287. </P>
  2288. <P>
  2289. A pattern compiled without PCRE2_ANCHORED is automatically anchored by PCRE2 if
  2290. the first significant item in every top-level branch is one of the following:
  2291. <pre>
  2292. ^ unless PCRE2_MULTILINE is set
  2293. \A always
  2294. \G always
  2295. .* sometimes - see below
  2296. </pre>
  2297. When .* is the first significant item, anchoring is possible only when all the
  2298. following are true:
  2299. <pre>
  2300. .* is not in an atomic group
  2301. .* is not in a capture group that is the subject of a backreference
  2302. PCRE2_DOTALL is in force for .*
  2303. Neither (*PRUNE) nor (*SKIP) appears in the pattern
  2304. PCRE2_NO_DOTSTAR_ANCHOR is not set
  2305. </pre>
  2306. For patterns that are auto-anchored, the PCRE2_ANCHORED bit is set in the
  2307. options returned for PCRE2_INFO_ALLOPTIONS.
  2308. <pre>
  2309. PCRE2_INFO_BACKREFMAX
  2310. </pre>
  2311. Return the number of the highest backreference in the pattern. The third
  2312. argument should point to a <b>uint32_t</b> variable. Named capture groups
  2313. acquire numbers as well as names, and these count towards the highest
  2314. backreference. Backreferences such as \4 or \g{12} match the captured
  2315. characters of the given group, but in addition, the check that a capture
  2316. group is set in a conditional group such as (?(3)a|b) is also a backreference.
  2317. Zero is returned if there are no backreferences.
  2318. <pre>
  2319. PCRE2_INFO_BSR
  2320. </pre>
  2321. The output is a uint32_t integer whose value indicates what character sequences
  2322. the \R escape sequence matches. A value of PCRE2_BSR_UNICODE means that \R
  2323. matches any Unicode line ending sequence; a value of PCRE2_BSR_ANYCRLF means
  2324. that \R matches only CR, LF, or CRLF.
  2325. <pre>
  2326. PCRE2_INFO_CAPTURECOUNT
  2327. </pre>
  2328. Return the highest capture group number in the pattern. In patterns where (?|
  2329. is not used, this is also the total number of capture groups. The third
  2330. argument should point to a <b>uint32_t</b> variable.
  2331. <pre>
  2332. PCRE2_INFO_DEPTHLIMIT
  2333. </pre>
  2334. If the pattern set a backtracking depth limit by including an item of the form
  2335. (*LIMIT_DEPTH=nnnn) at the start, the value is returned. The third argument
  2336. should point to a uint32_t integer. If no such value has been set, the call to
  2337. <b>pcre2_pattern_info()</b> returns the error PCRE2_ERROR_UNSET. Note that this
  2338. limit will only be used during matching if it is less than the limit set or
  2339. defaulted by the caller of the match function.
  2340. <pre>
  2341. PCRE2_INFO_FIRSTBITMAP
  2342. </pre>
  2343. In the absence of a single first code unit for a non-anchored pattern,
  2344. <b>pcre2_compile()</b> may construct a 256-bit table that defines a fixed set of
  2345. values for the first code unit in any match. For example, a pattern that starts
  2346. with [abc] results in a table with three bits set. When code unit values
  2347. greater than 255 are supported, the flag bit for 255 means "any code unit of
  2348. value 255 or above". If such a table was constructed, a pointer to it is
  2349. returned. Otherwise NULL is returned. The third argument should point to a
  2350. <b>const uint8_t *</b> variable.
  2351. <pre>
  2352. PCRE2_INFO_FIRSTCODETYPE
  2353. </pre>
  2354. Return information about the first code unit of any matched string, for a
  2355. non-anchored pattern. The third argument should point to a <b>uint32_t</b>
  2356. variable. If there is a fixed first value, for example, the letter "c" from a
  2357. pattern such as (cat|cow|coyote), 1 is returned, and the value can be retrieved
  2358. using PCRE2_INFO_FIRSTCODEUNIT. If there is no fixed first value, but it is
  2359. known that a match can occur only at the start of the subject or following a
  2360. newline in the subject, 2 is returned. Otherwise, and for anchored patterns, 0
  2361. is returned.
  2362. <pre>
  2363. PCRE2_INFO_FIRSTCODEUNIT
  2364. </pre>
  2365. Return the value of the first code unit of any matched string for a pattern
  2366. where PCRE2_INFO_FIRSTCODETYPE returns 1; otherwise return 0. The third
  2367. argument should point to a <b>uint32_t</b> variable. In the 8-bit library, the
  2368. value is always less than 256. In the 16-bit library the value can be up to
  2369. 0xffff. In the 32-bit library in UTF-32 mode the value can be up to 0x10ffff,
  2370. and up to 0xffffffff when not using UTF-32 mode.
  2371. <pre>
  2372. PCRE2_INFO_FRAMESIZE
  2373. </pre>
  2374. Return the size (in bytes) of the data frames that are used to remember
  2375. backtracking positions when the pattern is processed by <b>pcre2_match()</b>
  2376. without the use of JIT. The third argument should point to a <b>size_t</b>
  2377. variable. The frame size depends on the number of capturing parentheses in the
  2378. pattern. Each additional capture group adds two PCRE2_SIZE variables.
  2379. <pre>
  2380. PCRE2_INFO_HASBACKSLASHC
  2381. </pre>
  2382. Return 1 if the pattern contains any instances of \C, otherwise 0. The third
  2383. argument should point to a <b>uint32_t</b> variable.
  2384. <pre>
  2385. PCRE2_INFO_HASCRORLF
  2386. </pre>
  2387. Return 1 if the pattern contains any explicit matches for CR or LF characters,
  2388. otherwise 0. The third argument should point to a <b>uint32_t</b> variable. An
  2389. explicit match is either a literal CR or LF character, or \r or \n or one of
  2390. the equivalent hexadecimal or octal escape sequences.
  2391. <pre>
  2392. PCRE2_INFO_HEAPLIMIT
  2393. </pre>
  2394. If the pattern set a heap memory limit by including an item of the form
  2395. (*LIMIT_HEAP=nnnn) at the start, the value is returned. The third argument
  2396. should point to a uint32_t integer. If no such value has been set, the call to
  2397. <b>pcre2_pattern_info()</b> returns the error PCRE2_ERROR_UNSET. Note that this
  2398. limit will only be used during matching if it is less than the limit set or
  2399. defaulted by the caller of the match function.
  2400. <pre>
  2401. PCRE2_INFO_JCHANGED
  2402. </pre>
  2403. Return 1 if the (?J) or (?-J) option setting is used in the pattern, otherwise
  2404. 0. The third argument should point to a <b>uint32_t</b> variable. (?J) and
  2405. (?-J) set and unset the local PCRE2_DUPNAMES option, respectively.
  2406. <pre>
  2407. PCRE2_INFO_JITSIZE
  2408. </pre>
  2409. If the compiled pattern was successfully processed by
  2410. <b>pcre2_jit_compile()</b>, return the size of the JIT compiled code, otherwise
  2411. return zero. The third argument should point to a <b>size_t</b> variable.
  2412. <pre>
  2413. PCRE2_INFO_LASTCODETYPE
  2414. </pre>
  2415. Returns 1 if there is a rightmost literal code unit that must exist in any
  2416. matched string, other than at its start. The third argument should point to a
  2417. <b>uint32_t</b> variable. If there is no such value, 0 is returned. When 1 is
  2418. returned, the code unit value itself can be retrieved using
  2419. PCRE2_INFO_LASTCODEUNIT. For anchored patterns, a last literal value is
  2420. recorded only if it follows something of variable length. For example, for the
  2421. pattern /^a\d+z\d+/ the returned value is 1 (with "z" returned from
  2422. PCRE2_INFO_LASTCODEUNIT), but for /^a\dz\d/ the returned value is 0.
  2423. <pre>
  2424. PCRE2_INFO_LASTCODEUNIT
  2425. </pre>
  2426. Return the value of the rightmost literal code unit that must exist in any
  2427. matched string, other than at its start, for a pattern where
  2428. PCRE2_INFO_LASTCODETYPE returns 1. Otherwise, return 0. The third argument
  2429. should point to a <b>uint32_t</b> variable.
  2430. <pre>
  2431. PCRE2_INFO_MATCHEMPTY
  2432. </pre>
  2433. Return 1 if the pattern might match an empty string, otherwise 0. The third
  2434. argument should point to a <b>uint32_t</b> variable. When a pattern contains
  2435. recursive subroutine calls it is not always possible to determine whether or
  2436. not it can match an empty string. PCRE2 takes a cautious approach and returns 1
  2437. in such cases.
  2438. <pre>
  2439. PCRE2_INFO_MATCHLIMIT
  2440. </pre>
  2441. If the pattern set a match limit by including an item of the form
  2442. (*LIMIT_MATCH=nnnn) at the start, the value is returned. The third argument
  2443. should point to a uint32_t integer. If no such value has been set, the call to
  2444. <b>pcre2_pattern_info()</b> returns the error PCRE2_ERROR_UNSET. Note that this
  2445. limit will only be used during matching if it is less than the limit set or
  2446. defaulted by the caller of the match function.
  2447. <pre>
  2448. PCRE2_INFO_MAXLOOKBEHIND
  2449. </pre>
  2450. A lookbehind assertion moves back a certain number of characters (not code
  2451. units) when it starts to process each of its branches. This request returns the
  2452. largest of these backward moves. The third argument should point to a uint32_t
  2453. integer. The simple assertions \b and \B require a one-character lookbehind
  2454. and cause PCRE2_INFO_MAXLOOKBEHIND to return 1 in the absence of anything
  2455. longer. \A also registers a one-character lookbehind, though it does not
  2456. actually inspect the previous character.
  2457. </P>
  2458. <P>
  2459. Note that this information is useful for multi-segment matching only
  2460. if the pattern contains no nested lookbehinds. For example, the pattern
  2461. (?&#60;=a(?&#60;=ba)c) returns a maximum lookbehind of 2, but when it is processed, the
  2462. first lookbehind moves back by two characters, matches one character, then the
  2463. nested lookbehind also moves back by two characters. This puts the matching
  2464. point three characters earlier than it was at the start.
  2465. PCRE2_INFO_MAXLOOKBEHIND is really only useful as a debugging tool. See the
  2466. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  2467. documentation for a discussion of multi-segment matching.
  2468. <pre>
  2469. PCRE2_INFO_MINLENGTH
  2470. </pre>
  2471. If a minimum length for matching subject strings was computed, its value is
  2472. returned. Otherwise the returned value is 0. This value is not computed when
  2473. PCRE2_NO_START_OPTIMIZE is set. The value is a number of characters, which in
  2474. UTF mode may be different from the number of code units. The third argument
  2475. should point to a <b>uint32_t</b> variable. The value is a lower bound to the
  2476. length of any matching string. There may not be any strings of that length that
  2477. do actually match, but every string that does match is at least that long.
  2478. <pre>
  2479. PCRE2_INFO_NAMECOUNT
  2480. PCRE2_INFO_NAMEENTRYSIZE
  2481. PCRE2_INFO_NAMETABLE
  2482. </pre>
  2483. PCRE2 supports the use of named as well as numbered capturing parentheses. The
  2484. names are just an additional way of identifying the parentheses, which still
  2485. acquire numbers. Several convenience functions such as
  2486. <b>pcre2_substring_get_byname()</b> are provided for extracting captured
  2487. substrings by name. It is also possible to extract the data directly, by first
  2488. converting the name to a number in order to access the correct pointers in the
  2489. output vector (described with <b>pcre2_match()</b> below). To do the conversion,
  2490. you need to use the name-to-number map, which is described by these three
  2491. values.
  2492. </P>
  2493. <P>
  2494. The map consists of a number of fixed-size entries. PCRE2_INFO_NAMECOUNT gives
  2495. the number of entries, and PCRE2_INFO_NAMEENTRYSIZE gives the size of each
  2496. entry in code units; both of these return a <b>uint32_t</b> value. The entry
  2497. size depends on the length of the longest name.
  2498. </P>
  2499. <P>
  2500. PCRE2_INFO_NAMETABLE returns a pointer to the first entry of the table. This is
  2501. a PCRE2_SPTR pointer to a block of code units. In the 8-bit library, the first
  2502. two bytes of each entry are the number of the capturing parenthesis, most
  2503. significant byte first. In the 16-bit library, the pointer points to 16-bit
  2504. code units, the first of which contains the parenthesis number. In the 32-bit
  2505. library, the pointer points to 32-bit code units, the first of which contains
  2506. the parenthesis number. The rest of the entry is the corresponding name, zero
  2507. terminated.
  2508. </P>
  2509. <P>
  2510. The names are in alphabetical order. If (?| is used to create multiple capture
  2511. groups with the same number, as described in the
  2512. <a href="pcre2pattern.html#dupgroupnumber">section on duplicate group numbers</a>
  2513. in the
  2514. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  2515. page, the groups may be given the same name, but there is only one entry in the
  2516. table. Different names for groups of the same number are not permitted.
  2517. </P>
  2518. <P>
  2519. Duplicate names for capture groups with different numbers are permitted, but
  2520. only if PCRE2_DUPNAMES is set. They appear in the table in the order in which
  2521. they were found in the pattern. In the absence of (?| this is the order of
  2522. increasing number; when (?| is used this is not necessarily the case because
  2523. later capture groups may have lower numbers.
  2524. </P>
  2525. <P>
  2526. As a simple example of the name/number table, consider the following pattern
  2527. after compilation by the 8-bit library (assume PCRE2_EXTENDED is set, so white
  2528. space - including newlines - is ignored):
  2529. <pre>
  2530. (?&#60;date&#62; (?&#60;year&#62;(\d\d)?\d\d) - (?&#60;month&#62;\d\d) - (?&#60;day&#62;\d\d) )
  2531. </pre>
  2532. There are four named capture groups, so the table has four entries, and each
  2533. entry in the table is eight bytes long. The table is as follows, with
  2534. non-printing bytes shows in hexadecimal, and undefined bytes shown as ??:
  2535. <pre>
  2536. 00 01 d a t e 00 ??
  2537. 00 05 d a y 00 ?? ??
  2538. 00 04 m o n t h 00
  2539. 00 02 y e a r 00 ??
  2540. </pre>
  2541. When writing code to extract data from named capture groups using the
  2542. name-to-number map, remember that the length of the entries is likely to be
  2543. different for each compiled pattern.
  2544. <pre>
  2545. PCRE2_INFO_NEWLINE
  2546. </pre>
  2547. The output is one of the following <b>uint32_t</b> values:
  2548. <pre>
  2549. PCRE2_NEWLINE_CR Carriage return (CR)
  2550. PCRE2_NEWLINE_LF Linefeed (LF)
  2551. PCRE2_NEWLINE_CRLF Carriage return, linefeed (CRLF)
  2552. PCRE2_NEWLINE_ANY Any Unicode line ending
  2553. PCRE2_NEWLINE_ANYCRLF Any of CR, LF, or CRLF
  2554. PCRE2_NEWLINE_NUL The NUL character (binary zero)
  2555. </pre>
  2556. This identifies the character sequence that will be recognized as meaning
  2557. "newline" while matching.
  2558. <pre>
  2559. PCRE2_INFO_SIZE
  2560. </pre>
  2561. Return the size of the compiled pattern in bytes (for all three libraries). The
  2562. third argument should point to a <b>size_t</b> variable. This value includes the
  2563. size of the general data block that precedes the code units of the compiled
  2564. pattern itself. The value that is used when <b>pcre2_compile()</b> is getting
  2565. memory in which to place the compiled pattern may be slightly larger than the
  2566. value returned by this option, because there are cases where the code that
  2567. calculates the size has to over-estimate. Processing a pattern with the JIT
  2568. compiler does not alter the value returned by this option.
  2569. <a name="infoaboutcallouts"></a></P>
  2570. <br><a name="SEC24" href="#TOC1">INFORMATION ABOUT A PATTERN'S CALLOUTS</a><br>
  2571. <P>
  2572. <b>int pcre2_callout_enumerate(const pcre2_code *<i>code</i>,</b>
  2573. <b> int (*<i>callback</i>)(pcre2_callout_enumerate_block *, void *),</b>
  2574. <b> void *<i>user_data</i>);</b>
  2575. <br>
  2576. <br>
  2577. A script language that supports the use of string arguments in callouts might
  2578. like to scan all the callouts in a pattern before running the match. This can
  2579. be done by calling <b>pcre2_callout_enumerate()</b>. The first argument is a
  2580. pointer to a compiled pattern, the second points to a callback function, and
  2581. the third is arbitrary user data. The callback function is called for every
  2582. callout in the pattern in the order in which they appear. Its first argument is
  2583. a pointer to a callout enumeration block, and its second argument is the
  2584. <i>user_data</i> value that was passed to <b>pcre2_callout_enumerate()</b>. The
  2585. contents of the callout enumeration block are described in the
  2586. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  2587. documentation, which also gives further details about callouts.
  2588. </P>
  2589. <br><a name="SEC25" href="#TOC1">SERIALIZATION AND PRECOMPILING</a><br>
  2590. <P>
  2591. It is possible to save compiled patterns on disc or elsewhere, and reload them
  2592. later, subject to a number of restrictions. The host on which the patterns are
  2593. reloaded must be running the same version of PCRE2, with the same code unit
  2594. width, and must also have the same endianness, pointer width, and PCRE2_SIZE
  2595. type. Before compiled patterns can be saved, they must be converted to a
  2596. "serialized" form, which in the case of PCRE2 is really just a bytecode dump.
  2597. The functions whose names begin with <b>pcre2_serialize_</b> are used for
  2598. converting to and from the serialized form. They are described in the
  2599. <a href="pcre2serialize.html"><b>pcre2serialize</b></a>
  2600. documentation. Note that PCRE2 serialization does not convert compiled patterns
  2601. to an abstract format like Java or .NET serialization.
  2602. <a name="matchdatablock"></a></P>
  2603. <br><a name="SEC26" href="#TOC1">THE MATCH DATA BLOCK</a><br>
  2604. <P>
  2605. <b>pcre2_match_data *pcre2_match_data_create(uint32_t <i>ovecsize</i>,</b>
  2606. <b> pcre2_general_context *<i>gcontext</i>);</b>
  2607. <br>
  2608. <br>
  2609. <b>pcre2_match_data *pcre2_match_data_create_from_pattern(</b>
  2610. <b> const pcre2_code *<i>code</i>, pcre2_general_context *<i>gcontext</i>);</b>
  2611. <br>
  2612. <br>
  2613. <b>void pcre2_match_data_free(pcre2_match_data *<i>match_data</i>);</b>
  2614. </P>
  2615. <P>
  2616. Information about a successful or unsuccessful match is placed in a match
  2617. data block, which is an opaque structure that is accessed by function calls. In
  2618. particular, the match data block contains a vector of offsets into the subject
  2619. string that define the matched parts of the subject. This is known as the
  2620. <i>ovector</i>.
  2621. </P>
  2622. <P>
  2623. Before calling <b>pcre2_match()</b>, <b>pcre2_dfa_match()</b>, or
  2624. <b>pcre2_jit_match()</b> you must create a match data block by calling one of
  2625. the creation functions above. For <b>pcre2_match_data_create()</b>, the first
  2626. argument is the number of pairs of offsets in the <i>ovector</i>.
  2627. </P>
  2628. <P>
  2629. When using <b>pcre2_match()</b>, one pair of offsets is required to identify the
  2630. string that matched the whole pattern, with an additional pair for each
  2631. captured substring. For example, a value of 4 creates enough space to record
  2632. the matched portion of the subject plus three captured substrings.
  2633. </P>
  2634. <P>
  2635. When using <b>pcre2_dfa_match()</b> there may be multiple matched substrings of
  2636. different lengths at the same point in the subject. The ovector should be made
  2637. large enough to hold as many as are expected.
  2638. </P>
  2639. <P>
  2640. A minimum of at least 1 pair is imposed by <b>pcre2_match_data_create()</b>, so
  2641. it is always possible to return the overall matched string in the case of
  2642. <b>pcre2_match()</b> or the longest match in the case of
  2643. <b>pcre2_dfa_match()</b>. The maximum number of pairs is 65535; if the first
  2644. argument of <b>pcre2_match_data_create()</b> is greater than this, 65535 is
  2645. used.
  2646. </P>
  2647. <P>
  2648. The second argument of <b>pcre2_match_data_create()</b> is a pointer to a
  2649. general context, which can specify custom memory management for obtaining the
  2650. memory for the match data block. If you are not using custom memory management,
  2651. pass NULL, which causes <b>malloc()</b> to be used.
  2652. </P>
  2653. <P>
  2654. For <b>pcre2_match_data_create_from_pattern()</b>, the first argument is a
  2655. pointer to a compiled pattern. The ovector is created to be exactly the right
  2656. size to hold all the substrings a pattern might capture when matched using
  2657. <b>pcre2_match()</b>. You should not use this call when matching with
  2658. <b>pcre2_dfa_match()</b>. The second argument is again a pointer to a general
  2659. context, but in this case if NULL is passed, the memory is obtained using the
  2660. same allocator that was used for the compiled pattern (custom or default).
  2661. </P>
  2662. <P>
  2663. A match data block can be used many times, with the same or different compiled
  2664. patterns. You can extract information from a match data block after a match
  2665. operation has finished, using functions that are described in the sections on
  2666. <a href="#matchedstrings">matched strings</a>
  2667. and
  2668. <a href="#matchotherdata">other match data</a>
  2669. below.
  2670. </P>
  2671. <P>
  2672. When a call of <b>pcre2_match()</b> fails, valid data is available in the match
  2673. block only when the error is PCRE2_ERROR_NOMATCH, PCRE2_ERROR_PARTIAL, or one
  2674. of the error codes for an invalid UTF string. Exactly what is available depends
  2675. on the error, and is detailed below.
  2676. </P>
  2677. <P>
  2678. When one of the matching functions is called, pointers to the compiled pattern
  2679. and the subject string are set in the match data block so that they can be
  2680. referenced by the extraction functions after a successful match. After running
  2681. a match, you must not free a compiled pattern or a subject string until after
  2682. all operations on the match data block (for that match) have taken place,
  2683. unless, in the case of the subject string, you have used the
  2684. PCRE2_COPY_MATCHED_SUBJECT option, which is described in the section entitled
  2685. "Option bits for <b>pcre2_match()</b>"
  2686. <a href="#matchoptions>">below.</a>
  2687. </P>
  2688. <P>
  2689. When a match data block itself is no longer needed, it should be freed by
  2690. calling <b>pcre2_match_data_free()</b>. If this function is called with a NULL
  2691. argument, it returns immediately, without doing anything.
  2692. </P>
  2693. <br><a name="SEC27" href="#TOC1">MEMORY USE FOR MATCH DATA BLOCKS</a><br>
  2694. <P>
  2695. <b>PCRE2_SIZE pcre2_get_match_data_size(pcre2_match_data *<i>match_data</i>);</b>
  2696. <br>
  2697. <br>
  2698. <b>PCRE2_SIZE pcre2_get_match_data_heapframes_size(</b>
  2699. <b> pcre2_match_data *<i>match_data</i>);</b>
  2700. </P>
  2701. <P>
  2702. The size of a match data block depends on the size of the ovector that it
  2703. contains. The function <b>pcre2_get_match_data_size()</b> returns the size, in
  2704. bytes, of the block that is its argument.
  2705. </P>
  2706. <P>
  2707. When <b>pcre2_match()</b> runs interpretively (that is, without using JIT), it
  2708. makes use of a vector of data frames for remembering backtracking positions.
  2709. The size of each individual frame depends on the number of capturing
  2710. parentheses in the pattern and can be obtained by calling
  2711. <b>pcre2_pattern_info()</b> with the PCRE2_INFO_FRAMESIZE option (see the
  2712. section entitled "Information about a compiled pattern"
  2713. <a href="#infoaboutpattern>">above).</a>
  2714. </P>
  2715. <P>
  2716. Heap memory is used for the frames vector; if the initial memory block turns
  2717. out to be too small during matching, it is automatically expanded. When
  2718. <b>pcre2_match()</b> returns, the memory is not freed, but remains attached to
  2719. the match data block, for use by any subsequent matches that use the same
  2720. block. It is automatically freed when the match data block itself is freed.
  2721. </P>
  2722. <P>
  2723. You can find the current size of the frames vector that a match data block owns
  2724. by calling <b>pcre2_get_match_data_heapframes_size()</b>. For a newly created
  2725. match data block the size will be zero. Some types of match may require a lot
  2726. of frames and thus a large vector; applications that run in environments where
  2727. memory is constrained can check this and free the match data block if the heap
  2728. frames vector has become too big.
  2729. </P>
  2730. <br><a name="SEC28" href="#TOC1">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a><br>
  2731. <P>
  2732. <b>int pcre2_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  2733. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  2734. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  2735. <b> pcre2_match_context *<i>mcontext</i>);</b>
  2736. </P>
  2737. <P>
  2738. The function <b>pcre2_match()</b> is called to match a subject string against a
  2739. compiled pattern, which is passed in the <i>code</i> argument. You can call
  2740. <b>pcre2_match()</b> with the same <i>code</i> argument as many times as you
  2741. like, in order to find multiple matches in the subject string or to match
  2742. different subject strings with the same pattern.
  2743. </P>
  2744. <P>
  2745. This function is the main matching facility of the library, and it operates in
  2746. a Perl-like manner. For specialist use there is also an alternative matching
  2747. function, which is described
  2748. <a href="#dfamatch">below</a>
  2749. in the section about the <b>pcre2_dfa_match()</b> function.
  2750. </P>
  2751. <P>
  2752. Here is an example of a simple call to <b>pcre2_match()</b>:
  2753. <pre>
  2754. pcre2_match_data *md = pcre2_match_data_create(4, NULL);
  2755. int rc = pcre2_match(
  2756. re, /* result of pcre2_compile() */
  2757. "some string", /* the subject string */
  2758. 11, /* the length of the subject string */
  2759. 0, /* start at offset 0 in the subject */
  2760. 0, /* default options */
  2761. md, /* the match data block */
  2762. NULL); /* a match context; NULL means use defaults */
  2763. </pre>
  2764. If the subject string is zero-terminated, the length can be given as
  2765. PCRE2_ZERO_TERMINATED. A match context must be provided if certain less common
  2766. matching parameters are to be changed. For details, see the section on
  2767. <a href="#matchcontext">the match context</a>
  2768. above.
  2769. </P>
  2770. <br><b>
  2771. The string to be matched by <b>pcre2_match()</b>
  2772. </b><br>
  2773. <P>
  2774. The subject string is passed to <b>pcre2_match()</b> as a pointer in
  2775. <i>subject</i>, a length in <i>length</i>, and a starting offset in
  2776. <i>startoffset</i>. The length and offset are in code units, not characters.
  2777. That is, they are in bytes for the 8-bit library, 16-bit code units for the
  2778. 16-bit library, and 32-bit code units for the 32-bit library, whether or not
  2779. UTF processing is enabled. As a special case, if <i>subject</i> is NULL and
  2780. <i>length</i> is zero, the subject is assumed to be an empty string. If
  2781. <i>length</i> is non-zero, an error occurs if <i>subject</i> is NULL.
  2782. </P>
  2783. <P>
  2784. If <i>startoffset</i> is greater than the length of the subject,
  2785. <b>pcre2_match()</b> returns PCRE2_ERROR_BADOFFSET. When the starting offset is
  2786. zero, the search for a match starts at the beginning of the subject, and this
  2787. is by far the most common case. In UTF-8 or UTF-16 mode, the starting offset
  2788. must point to the start of a character, or to the end of the subject (in UTF-32
  2789. mode, one code unit equals one character, so all offsets are valid). Like the
  2790. pattern string, the subject may contain binary zeros.
  2791. </P>
  2792. <P>
  2793. A non-zero starting offset is useful when searching for another match in the
  2794. same subject by calling <b>pcre2_match()</b> again after a previous success.
  2795. Setting <i>startoffset</i> differs from passing over a shortened string and
  2796. setting PCRE2_NOTBOL in the case of a pattern that begins with any kind of
  2797. lookbehind. For example, consider the pattern
  2798. <pre>
  2799. \Biss\B
  2800. </pre>
  2801. which finds occurrences of "iss" in the middle of words. (\B matches only if
  2802. the current position in the subject is not a word boundary.) When applied to
  2803. the string "Mississippi" the first call to <b>pcre2_match()</b> finds the first
  2804. occurrence. If <b>pcre2_match()</b> is called again with just the remainder of
  2805. the subject, namely "issippi", it does not match, because \B is always false
  2806. at the start of the subject, which is deemed to be a word boundary. However, if
  2807. <b>pcre2_match()</b> is passed the entire string again, but with
  2808. <i>startoffset</i> set to 4, it finds the second occurrence of "iss" because it
  2809. is able to look behind the starting point to discover that it is preceded by a
  2810. letter.
  2811. </P>
  2812. <P>
  2813. Finding all the matches in a subject is tricky when the pattern can match an
  2814. empty string. It is possible to emulate Perl's /g behaviour by first trying the
  2815. match again at the same offset, with the PCRE2_NOTEMPTY_ATSTART and
  2816. PCRE2_ANCHORED options, and then if that fails, advancing the starting offset
  2817. and trying an ordinary match again. There is some code that demonstrates how to
  2818. do this in the
  2819. <a href="pcre2demo.html"><b>pcre2demo</b></a>
  2820. sample program. In the most general case, you have to check to see if the
  2821. newline convention recognizes CRLF as a newline, and if so, and the current
  2822. character is CR followed by LF, advance the starting offset by two characters
  2823. instead of one.
  2824. </P>
  2825. <P>
  2826. If a non-zero starting offset is passed when the pattern is anchored, a single
  2827. attempt to match at the given offset is made. This can only succeed if the
  2828. pattern does not require the match to be at the start of the subject. In other
  2829. words, the anchoring must be the result of setting the PCRE2_ANCHORED option or
  2830. the use of .* with PCRE2_DOTALL, not by starting the pattern with ^ or \A.
  2831. <a name="matchoptions"></a></P>
  2832. <br><b>
  2833. Option bits for <b>pcre2_match()</b>
  2834. </b><br>
  2835. <P>
  2836. The unused bits of the <i>options</i> argument for <b>pcre2_match()</b> must be
  2837. zero. The only bits that may be set are PCRE2_ANCHORED,
  2838. PCRE2_COPY_MATCHED_SUBJECT, PCRE2_DISABLE_RECURSELOOP_CHECK, PCRE2_ENDANCHORED,
  2839. PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY, PCRE2_NOTEMPTY_ATSTART,
  2840. PCRE2_NO_JIT, PCRE2_NO_UTF_CHECK, PCRE2_PARTIAL_HARD, and PCRE2_PARTIAL_SOFT.
  2841. Their action is described below.
  2842. </P>
  2843. <P>
  2844. Setting PCRE2_ANCHORED or PCRE2_ENDANCHORED at match time is not supported by
  2845. the just-in-time (JIT) compiler. If it is set, JIT matching is disabled and the
  2846. interpretive code in <b>pcre2_match()</b> is run.
  2847. PCRE2_DISABLE_RECURSELOOP_CHECK is ignored by JIT, but apart from PCRE2_NO_JIT
  2848. (obviously), the remaining options are supported for JIT matching.
  2849. <pre>
  2850. PCRE2_ANCHORED
  2851. </pre>
  2852. The PCRE2_ANCHORED option limits <b>pcre2_match()</b> to matching at the first
  2853. matching position. If a pattern was compiled with PCRE2_ANCHORED, or turned out
  2854. to be anchored by virtue of its contents, it cannot be made unachored at
  2855. matching time. Note that setting the option at match time disables JIT
  2856. matching.
  2857. <pre>
  2858. PCRE2_COPY_MATCHED_SUBJECT
  2859. </pre>
  2860. By default, a pointer to the subject is remembered in the match data block so
  2861. that, after a successful match, it can be referenced by the substring
  2862. extraction functions. This means that the subject's memory must not be freed
  2863. until all such operations are complete. For some applications where the
  2864. lifetime of the subject string is not guaranteed, it may be necessary to make a
  2865. copy of the subject string, but it is wasteful to do this unless the match is
  2866. successful. After a successful match, if PCRE2_COPY_MATCHED_SUBJECT is set, the
  2867. subject is copied and the new pointer is remembered in the match data block
  2868. instead of the original subject pointer. The memory allocator that was used for
  2869. the match block itself is used. The copy is automatically freed when
  2870. <b>pcre2_match_data_free()</b> is called to free the match data block. It is also
  2871. automatically freed if the match data block is re-used for another match
  2872. operation.
  2873. <pre>
  2874. PCRE2_DISABLE_RECURSELOOP_CHECK
  2875. </pre>
  2876. This option is relevant only to <b>pcre2_match()</b> for interpretive matching.
  2877. It is ignored when JIT is used, and is forbidden for <b>pcre2_dfa_match()</b>.
  2878. </P>
  2879. <P>
  2880. The use of recursion in patterns can lead to infinite loops. In the
  2881. interpretive matcher these would be eventually caught by the match or heap
  2882. limits, but this could take a long time and/or use a lot of memory if the
  2883. limits are large. There is therefore a check at the start of each recursion.
  2884. If the same group is still active from a previous call, and the current subject
  2885. pointer is the same as it was at the start of that group, and the furthest
  2886. inspected character of the subject has not changed, an error is generated.
  2887. </P>
  2888. <P>
  2889. There are rare cases of matches that would complete, but nevertheless trigger
  2890. this error. This option disables the check. It is provided mainly for testing
  2891. when comparing JIT and interpretive behaviour.
  2892. <pre>
  2893. PCRE2_ENDANCHORED
  2894. </pre>
  2895. If the PCRE2_ENDANCHORED option is set, any string that <b>pcre2_match()</b>
  2896. matches must be right at the end of the subject string. Note that setting the
  2897. option at match time disables JIT matching.
  2898. <pre>
  2899. PCRE2_NOTBOL
  2900. </pre>
  2901. This option specifies that first character of the subject string is not the
  2902. beginning of a line, so the circumflex metacharacter should not match before
  2903. it. Setting this without having set PCRE2_MULTILINE at compile time causes
  2904. circumflex never to match. This option affects only the behaviour of the
  2905. circumflex metacharacter. It does not affect \A.
  2906. <pre>
  2907. PCRE2_NOTEOL
  2908. </pre>
  2909. This option specifies that the end of the subject string is not the end of a
  2910. line, so the dollar metacharacter should not match it nor (except in multiline
  2911. mode) a newline immediately before it. Setting this without having set
  2912. PCRE2_MULTILINE at compile time causes dollar never to match. This option
  2913. affects only the behaviour of the dollar metacharacter. It does not affect \Z
  2914. or \z.
  2915. <pre>
  2916. PCRE2_NOTEMPTY
  2917. </pre>
  2918. An empty string is not considered to be a valid match if this option is set. If
  2919. there are alternatives in the pattern, they are tried. If all the alternatives
  2920. match the empty string, the entire match fails. For example, if the pattern
  2921. <pre>
  2922. a?b?
  2923. </pre>
  2924. is applied to a string not beginning with "a" or "b", it matches an empty
  2925. string at the start of the subject. With PCRE2_NOTEMPTY set, this match is not
  2926. valid, so <b>pcre2_match()</b> searches further into the string for occurrences
  2927. of "a" or "b".
  2928. <pre>
  2929. PCRE2_NOTEMPTY_ATSTART
  2930. </pre>
  2931. This is like PCRE2_NOTEMPTY, except that it locks out an empty string match
  2932. only at the first matching position, that is, at the start of the subject plus
  2933. the starting offset. An empty string match later in the subject is permitted.
  2934. If the pattern is anchored, such a match can occur only if the pattern contains
  2935. \K.
  2936. <pre>
  2937. PCRE2_NO_JIT
  2938. </pre>
  2939. By default, if a pattern has been successfully processed by
  2940. <b>pcre2_jit_compile()</b>, JIT is automatically used when <b>pcre2_match()</b>
  2941. is called with options that JIT supports. Setting PCRE2_NO_JIT disables the use
  2942. of JIT; it forces matching to be done by the interpreter.
  2943. <pre>
  2944. PCRE2_NO_UTF_CHECK
  2945. </pre>
  2946. When PCRE2_UTF is set at compile time, the validity of the subject as a UTF
  2947. string is checked unless PCRE2_NO_UTF_CHECK is passed to <b>pcre2_match()</b> or
  2948. PCRE2_MATCH_INVALID_UTF was passed to <b>pcre2_compile()</b>. The latter special
  2949. case is discussed in detail in the
  2950. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  2951. documentation.
  2952. </P>
  2953. <P>
  2954. In the default case, if a non-zero starting offset is given, the check is
  2955. applied only to that part of the subject that could be inspected during
  2956. matching, and there is a check that the starting offset points to the first
  2957. code unit of a character or to the end of the subject. If there are no
  2958. lookbehind assertions in the pattern, the check starts at the starting offset.
  2959. Otherwise, it starts at the length of the longest lookbehind before the
  2960. starting offset, or at the start of the subject if there are not that many
  2961. characters before the starting offset. Note that the sequences \b and \B are
  2962. one-character lookbehinds.
  2963. </P>
  2964. <P>
  2965. The check is carried out before any other processing takes place, and a
  2966. negative error code is returned if the check fails. There are several UTF error
  2967. codes for each code unit width, corresponding to different problems with the
  2968. code unit sequence. There are discussions about the validity of
  2969. <a href="pcre2unicode.html#utf8strings">UTF-8 strings,</a>
  2970. <a href="pcre2unicode.html#utf16strings">UTF-16 strings,</a>
  2971. and
  2972. <a href="pcre2unicode.html#utf32strings">UTF-32 strings</a>
  2973. in the
  2974. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  2975. documentation.
  2976. </P>
  2977. <P>
  2978. If you know that your subject is valid, and you want to skip this check for
  2979. performance reasons, you can set the PCRE2_NO_UTF_CHECK option when calling
  2980. <b>pcre2_match()</b>. You might want to do this for the second and subsequent
  2981. calls to <b>pcre2_match()</b> if you are making repeated calls to find multiple
  2982. matches in the same subject string.
  2983. </P>
  2984. <P>
  2985. <b>Warning:</b> Unless PCRE2_MATCH_INVALID_UTF was set at compile time, when
  2986. PCRE2_NO_UTF_CHECK is set at match time the effect of passing an invalid
  2987. string as a subject, or an invalid value of <i>startoffset</i>, is undefined.
  2988. Your program may crash or loop indefinitely or give wrong results.
  2989. <pre>
  2990. PCRE2_PARTIAL_HARD
  2991. PCRE2_PARTIAL_SOFT
  2992. </pre>
  2993. These options turn on the partial matching feature. A partial match occurs if
  2994. the end of the subject string is reached successfully, but there are not enough
  2995. subject characters to complete the match. In addition, either at least one
  2996. character must have been inspected or the pattern must contain a lookbehind, or
  2997. the pattern must be one that could match an empty string.
  2998. </P>
  2999. <P>
  3000. If this situation arises when PCRE2_PARTIAL_SOFT (but not PCRE2_PARTIAL_HARD)
  3001. is set, matching continues by testing any remaining alternatives. Only if no
  3002. complete match can be found is PCRE2_ERROR_PARTIAL returned instead of
  3003. PCRE2_ERROR_NOMATCH. In other words, PCRE2_PARTIAL_SOFT specifies that the
  3004. caller is prepared to handle a partial match, but only if no complete match can
  3005. be found.
  3006. </P>
  3007. <P>
  3008. If PCRE2_PARTIAL_HARD is set, it overrides PCRE2_PARTIAL_SOFT. In this case, if
  3009. a partial match is found, <b>pcre2_match()</b> immediately returns
  3010. PCRE2_ERROR_PARTIAL, without considering any other alternatives. In other
  3011. words, when PCRE2_PARTIAL_HARD is set, a partial match is considered to be more
  3012. important that an alternative complete match.
  3013. </P>
  3014. <P>
  3015. There is a more detailed discussion of partial and multi-segment matching, with
  3016. examples, in the
  3017. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  3018. documentation.
  3019. </P>
  3020. <br><a name="SEC29" href="#TOC1">NEWLINE HANDLING WHEN MATCHING</a><br>
  3021. <P>
  3022. When PCRE2 is built, a default newline convention is set; this is usually the
  3023. standard convention for the operating system. The default can be overridden in
  3024. a
  3025. <a href="#compilecontext">compile context</a>
  3026. by calling <b>pcre2_set_newline()</b>. It can also be overridden by starting a
  3027. pattern string with, for example, (*CRLF), as described in the
  3028. <a href="pcre2pattern.html#newlines">section on newline conventions</a>
  3029. in the
  3030. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  3031. page. During matching, the newline choice affects the behaviour of the dot,
  3032. circumflex, and dollar metacharacters. It may also alter the way the match
  3033. starting position is advanced after a match failure for an unanchored pattern.
  3034. </P>
  3035. <P>
  3036. When PCRE2_NEWLINE_CRLF, PCRE2_NEWLINE_ANYCRLF, or PCRE2_NEWLINE_ANY is set as
  3037. the newline convention, and a match attempt for an unanchored pattern fails
  3038. when the current starting position is at a CRLF sequence, and the pattern
  3039. contains no explicit matches for CR or LF characters, the match position is
  3040. advanced by two characters instead of one, in other words, to after the CRLF.
  3041. </P>
  3042. <P>
  3043. The above rule is a compromise that makes the most common cases work as
  3044. expected. For example, if the pattern is .+A (and the PCRE2_DOTALL option is
  3045. not set), it does not match the string "\r\nA" because, after failing at the
  3046. start, it skips both the CR and the LF before retrying. However, the pattern
  3047. [\r\n]A does match that string, because it contains an explicit CR or LF
  3048. reference, and so advances only by one character after the first failure.
  3049. </P>
  3050. <P>
  3051. An explicit match for CR of LF is either a literal appearance of one of those
  3052. characters in the pattern, or one of the \r or \n or equivalent octal or
  3053. hexadecimal escape sequences. Implicit matches such as [^X] do not count, nor
  3054. does \s, even though it includes CR and LF in the characters that it matches.
  3055. </P>
  3056. <P>
  3057. Notwithstanding the above, anomalous effects may still occur when CRLF is a
  3058. valid newline sequence and explicit \r or \n escapes appear in the pattern.
  3059. <a name="matchedstrings"></a></P>
  3060. <br><a name="SEC30" href="#TOC1">HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS</a><br>
  3061. <P>
  3062. <b>uint32_t pcre2_get_ovector_count(pcre2_match_data *<i>match_data</i>);</b>
  3063. <br>
  3064. <br>
  3065. <b>PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *<i>match_data</i>);</b>
  3066. </P>
  3067. <P>
  3068. In general, a pattern matches a certain portion of the subject, and in
  3069. addition, further substrings from the subject may be picked out by
  3070. parenthesized parts of the pattern. Following the usage in Jeffrey Friedl's
  3071. book, this is called "capturing" in what follows, and the phrase "capture
  3072. group" (Perl terminology) is used for a fragment of a pattern that picks out a
  3073. substring. PCRE2 supports several other kinds of parenthesized group that do
  3074. not cause substrings to be captured. The <b>pcre2_pattern_info()</b> function
  3075. can be used to find out how many capture groups there are in a compiled
  3076. pattern.
  3077. </P>
  3078. <P>
  3079. You can use auxiliary functions for accessing captured substrings
  3080. <a href="#extractbynumber">by number</a>
  3081. or
  3082. <a href="#extractbyname">by name,</a>
  3083. as described in sections below.
  3084. </P>
  3085. <P>
  3086. Alternatively, you can make direct use of the vector of PCRE2_SIZE values,
  3087. called the <b>ovector</b>, which contains the offsets of captured strings. It is
  3088. part of the
  3089. <a href="#matchdatablock">match data block.</a>
  3090. The function <b>pcre2_get_ovector_pointer()</b> returns the address of the
  3091. ovector, and <b>pcre2_get_ovector_count()</b> returns the number of pairs of
  3092. values it contains.
  3093. </P>
  3094. <P>
  3095. Within the ovector, the first in each pair of values is set to the offset of
  3096. the first code unit of a substring, and the second is set to the offset of the
  3097. first code unit after the end of a substring. These values are always code unit
  3098. offsets, not character offsets. That is, they are byte offsets in the 8-bit
  3099. library, 16-bit offsets in the 16-bit library, and 32-bit offsets in the 32-bit
  3100. library.
  3101. </P>
  3102. <P>
  3103. After a partial match (error return PCRE2_ERROR_PARTIAL), only the first pair
  3104. of offsets (that is, <i>ovector[0]</i> and <i>ovector[1]</i>) are set. They
  3105. identify the part of the subject that was partially matched. See the
  3106. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  3107. documentation for details of partial matching.
  3108. </P>
  3109. <P>
  3110. After a fully successful match, the first pair of offsets identifies the
  3111. portion of the subject string that was matched by the entire pattern. The next
  3112. pair is used for the first captured substring, and so on. The value returned by
  3113. <b>pcre2_match()</b> is one more than the highest numbered pair that has been
  3114. set. For example, if two substrings have been captured, the returned value is
  3115. 3. If there are no captured substrings, the return value from a successful
  3116. match is 1, indicating that just the first pair of offsets has been set.
  3117. </P>
  3118. <P>
  3119. If a pattern uses the \K escape sequence within a positive assertion, the
  3120. reported start of a successful match can be greater than the end of the match.
  3121. For example, if the pattern (?=ab\K) is matched against "ab", the start and
  3122. end offset values for the match are 2 and 0.
  3123. </P>
  3124. <P>
  3125. If a capture group is matched repeatedly within a single match operation, it is
  3126. the last portion of the subject that it matched that is returned.
  3127. </P>
  3128. <P>
  3129. If the ovector is too small to hold all the captured substring offsets, as much
  3130. as possible is filled in, and the function returns a value of zero. If captured
  3131. substrings are not of interest, <b>pcre2_match()</b> may be called with a match
  3132. data block whose ovector is of minimum length (that is, one pair).
  3133. </P>
  3134. <P>
  3135. It is possible for capture group number <i>n+1</i> to match some part of the
  3136. subject when group <i>n</i> has not been used at all. For example, if the string
  3137. "abc" is matched against the pattern (a|(z))(bc) the return from the function
  3138. is 4, and groups 1 and 3 are matched, but 2 is not. When this happens, both
  3139. values in the offset pairs corresponding to unused groups are set to
  3140. PCRE2_UNSET.
  3141. </P>
  3142. <P>
  3143. Offset values that correspond to unused groups at the end of the expression are
  3144. also set to PCRE2_UNSET. For example, if the string "abc" is matched against
  3145. the pattern (abc)(x(yz)?)? groups 2 and 3 are not matched. The return from the
  3146. function is 2, because the highest used capture group number is 1. The offsets
  3147. for the second and third capture groups (assuming the vector is large enough,
  3148. of course) are set to PCRE2_UNSET.
  3149. </P>
  3150. <P>
  3151. Elements in the ovector that do not correspond to capturing parentheses in the
  3152. pattern are never changed. That is, if a pattern contains <i>n</i> capturing
  3153. parentheses, no more than <i>ovector[0]</i> to <i>ovector[2n+1]</i> are set by
  3154. <b>pcre2_match()</b>. The other elements retain whatever values they previously
  3155. had. After a failed match attempt, the contents of the ovector are unchanged.
  3156. <a name="matchotherdata"></a></P>
  3157. <br><a name="SEC31" href="#TOC1">OTHER INFORMATION ABOUT A MATCH</a><br>
  3158. <P>
  3159. <b>PCRE2_SPTR pcre2_get_mark(pcre2_match_data *<i>match_data</i>);</b>
  3160. <br>
  3161. <br>
  3162. <b>PCRE2_SIZE pcre2_get_startchar(pcre2_match_data *<i>match_data</i>);</b>
  3163. </P>
  3164. <P>
  3165. As well as the offsets in the ovector, other information about a match is
  3166. retained in the match data block and can be retrieved by the above functions in
  3167. appropriate circumstances. If they are called at other times, the result is
  3168. undefined.
  3169. </P>
  3170. <P>
  3171. After a successful match, a partial match (PCRE2_ERROR_PARTIAL), or a failure
  3172. to match (PCRE2_ERROR_NOMATCH), a mark name may be available. The function
  3173. <b>pcre2_get_mark()</b> can be called to access this name, which can be
  3174. specified in the pattern by any of the backtracking control verbs, not just
  3175. (*MARK). The same function applies to all the verbs. It returns a pointer to
  3176. the zero-terminated name, which is within the compiled pattern. If no name is
  3177. available, NULL is returned. The length of the name (excluding the terminating
  3178. zero) is stored in the code unit that precedes the name. You should use this
  3179. length instead of relying on the terminating zero if the name might contain a
  3180. binary zero.
  3181. </P>
  3182. <P>
  3183. After a successful match, the name that is returned is the last mark name
  3184. encountered on the matching path through the pattern. Instances of backtracking
  3185. verbs without names do not count. Thus, for example, if the matching path
  3186. contains (*MARK:A)(*PRUNE), the name "A" is returned. After a "no match" or a
  3187. partial match, the last encountered name is returned. For example, consider
  3188. this pattern:
  3189. <pre>
  3190. ^(*MARK:A)((*MARK:B)a|b)c
  3191. </pre>
  3192. When it matches "bc", the returned name is A. The B mark is "seen" in the first
  3193. branch of the group, but it is not on the matching path. On the other hand,
  3194. when this pattern fails to match "bx", the returned name is B.
  3195. </P>
  3196. <P>
  3197. <b>Warning:</b> By default, certain start-of-match optimizations are used to
  3198. give a fast "no match" result in some situations. For example, if the anchoring
  3199. is removed from the pattern above, there is an initial check for the presence
  3200. of "c" in the subject before running the matching engine. This check fails for
  3201. "bx", causing a match failure without seeing any marks. You can disable the
  3202. start-of-match optimizations by setting the PCRE2_NO_START_OPTIMIZE option for
  3203. <b>pcre2_compile()</b> or by starting the pattern with (*NO_START_OPT).
  3204. </P>
  3205. <P>
  3206. After a successful match, a partial match, or one of the invalid UTF errors
  3207. (for example, PCRE2_ERROR_UTF8_ERR5), <b>pcre2_get_startchar()</b> can be
  3208. called. After a successful or partial match it returns the code unit offset of
  3209. the character at which the match started. For a non-partial match, this can be
  3210. different to the value of <i>ovector[0]</i> if the pattern contains the \K
  3211. escape sequence. After a partial match, however, this value is always the same
  3212. as <i>ovector[0]</i> because \K does not affect the result of a partial match.
  3213. </P>
  3214. <P>
  3215. After a UTF check failure, <b>pcre2_get_startchar()</b> can be used to obtain
  3216. the code unit offset of the invalid UTF character. Details are given in the
  3217. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  3218. page.
  3219. <a name="errorlist"></a></P>
  3220. <br><a name="SEC32" href="#TOC1">ERROR RETURNS FROM <b>pcre2_match()</b></a><br>
  3221. <P>
  3222. If <b>pcre2_match()</b> fails, it returns a negative number. This can be
  3223. converted to a text string by calling the <b>pcre2_get_error_message()</b>
  3224. function (see "Obtaining a textual error message"
  3225. <a href="#geterrormessage">below).</a>
  3226. Negative error codes are also returned by other functions, and are documented
  3227. with them. The codes are given names in the header file. If UTF checking is in
  3228. force and an invalid UTF subject string is detected, one of a number of
  3229. UTF-specific negative error codes is returned. Details are given in the
  3230. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  3231. page. The following are the other errors that may be returned by
  3232. <b>pcre2_match()</b>:
  3233. <pre>
  3234. PCRE2_ERROR_NOMATCH
  3235. </pre>
  3236. The subject string did not match the pattern.
  3237. <pre>
  3238. PCRE2_ERROR_PARTIAL
  3239. </pre>
  3240. The subject string did not match, but it did match partially. See the
  3241. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  3242. documentation for details of partial matching.
  3243. <pre>
  3244. PCRE2_ERROR_BADMAGIC
  3245. </pre>
  3246. PCRE2 stores a 4-byte "magic number" at the start of the compiled code, to
  3247. catch the case when it is passed a junk pointer. This is the error that is
  3248. returned when the magic number is not present.
  3249. <pre>
  3250. PCRE2_ERROR_BADMODE
  3251. </pre>
  3252. This error is given when a compiled pattern is passed to a function in a
  3253. library of a different code unit width, for example, a pattern compiled by
  3254. the 8-bit library is passed to a 16-bit or 32-bit library function.
  3255. <pre>
  3256. PCRE2_ERROR_BADOFFSET
  3257. </pre>
  3258. The value of <i>startoffset</i> was greater than the length of the subject.
  3259. <pre>
  3260. PCRE2_ERROR_BADOPTION
  3261. </pre>
  3262. An unrecognized bit was set in the <i>options</i> argument.
  3263. <pre>
  3264. PCRE2_ERROR_BADUTFOFFSET
  3265. </pre>
  3266. The UTF code unit sequence that was passed as a subject was checked and found
  3267. to be valid (the PCRE2_NO_UTF_CHECK option was not set), but the value of
  3268. <i>startoffset</i> did not point to the beginning of a UTF character or the end
  3269. of the subject.
  3270. <pre>
  3271. PCRE2_ERROR_CALLOUT
  3272. </pre>
  3273. This error is never generated by <b>pcre2_match()</b> itself. It is provided for
  3274. use by callout functions that want to cause <b>pcre2_match()</b> or
  3275. <b>pcre2_callout_enumerate()</b> to return a distinctive error code. See the
  3276. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  3277. documentation for details.
  3278. <pre>
  3279. PCRE2_ERROR_DEPTHLIMIT
  3280. </pre>
  3281. The nested backtracking depth limit was reached.
  3282. <pre>
  3283. PCRE2_ERROR_HEAPLIMIT
  3284. </pre>
  3285. The heap limit was reached.
  3286. <pre>
  3287. PCRE2_ERROR_INTERNAL
  3288. </pre>
  3289. An unexpected internal error has occurred. This error could be caused by a bug
  3290. in PCRE2 or by overwriting of the compiled pattern.
  3291. <pre>
  3292. PCRE2_ERROR_JIT_STACKLIMIT
  3293. </pre>
  3294. This error is returned when a pattern that was successfully studied using JIT
  3295. is being matched, but the memory available for the just-in-time processing
  3296. stack is not large enough. See the
  3297. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  3298. documentation for more details.
  3299. <pre>
  3300. PCRE2_ERROR_MATCHLIMIT
  3301. </pre>
  3302. The backtracking match limit was reached.
  3303. <pre>
  3304. PCRE2_ERROR_NOMEMORY
  3305. </pre>
  3306. Heap memory is used to remember backtracking points. This error is given when
  3307. the memory allocation function (default or custom) fails. Note that a different
  3308. error, PCRE2_ERROR_HEAPLIMIT, is given if the amount of memory needed exceeds
  3309. the heap limit. PCRE2_ERROR_NOMEMORY is also returned if
  3310. PCRE2_COPY_MATCHED_SUBJECT is set and memory allocation fails.
  3311. <pre>
  3312. PCRE2_ERROR_NULL
  3313. </pre>
  3314. Either the <i>code</i>, <i>subject</i>, or <i>match_data</i> argument was passed
  3315. as NULL.
  3316. <pre>
  3317. PCRE2_ERROR_RECURSELOOP
  3318. </pre>
  3319. This error is returned when <b>pcre2_match()</b> detects a recursion loop within
  3320. the pattern. Specifically, it means that either the whole pattern or a
  3321. capture group has been called recursively for the second time at the same
  3322. position in the subject string. Some simple patterns that might do this are
  3323. detected and faulted at compile time, but more complicated cases, in particular
  3324. mutual recursions between two different groups, cannot be detected until
  3325. matching is attempted.
  3326. <a name="geterrormessage"></a></P>
  3327. <br><a name="SEC33" href="#TOC1">OBTAINING A TEXTUAL ERROR MESSAGE</a><br>
  3328. <P>
  3329. <b>int pcre2_get_error_message(int <i>errorcode</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
  3330. <b> PCRE2_SIZE <i>bufflen</i>);</b>
  3331. </P>
  3332. <P>
  3333. A text message for an error code from any PCRE2 function (compile, match, or
  3334. auxiliary) can be obtained by calling <b>pcre2_get_error_message()</b>. The code
  3335. is passed as the first argument, with the remaining two arguments specifying a
  3336. code unit buffer and its length in code units, into which the text message is
  3337. placed. The message is returned in code units of the appropriate width for the
  3338. library that is being used.
  3339. </P>
  3340. <P>
  3341. The returned message is terminated with a trailing zero, and the function
  3342. returns the number of code units used, excluding the trailing zero. If the
  3343. error number is unknown, the negative error code PCRE2_ERROR_BADDATA is
  3344. returned. If the buffer is too small, the message is truncated (but still with
  3345. a trailing zero), and the negative error code PCRE2_ERROR_NOMEMORY is returned.
  3346. None of the messages are very long; a buffer size of 120 code units is ample.
  3347. <a name="extractbynumber"></a></P>
  3348. <br><a name="SEC34" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a><br>
  3349. <P>
  3350. <b>int pcre2_substring_length_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  3351. <b> uint32_t <i>number</i>, PCRE2_SIZE *<i>length</i>);</b>
  3352. <br>
  3353. <br>
  3354. <b>int pcre2_substring_copy_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  3355. <b> uint32_t <i>number</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
  3356. <b> PCRE2_SIZE *<i>bufflen</i>);</b>
  3357. <br>
  3358. <br>
  3359. <b>int pcre2_substring_get_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  3360. <b> uint32_t <i>number</i>, PCRE2_UCHAR **<i>bufferptr</i>,</b>
  3361. <b> PCRE2_SIZE *<i>bufflen</i>);</b>
  3362. <br>
  3363. <br>
  3364. <b>void pcre2_substring_free(PCRE2_UCHAR *<i>buffer</i>);</b>
  3365. </P>
  3366. <P>
  3367. Captured substrings can be accessed directly by using the ovector as described
  3368. <a href="#matchedstrings">above.</a>
  3369. For convenience, auxiliary functions are provided for extracting captured
  3370. substrings as new, separate, zero-terminated strings. A substring that contains
  3371. a binary zero is correctly extracted and has a further zero added on the end,
  3372. but the result is not, of course, a C string.
  3373. </P>
  3374. <P>
  3375. The functions in this section identify substrings by number. The number zero
  3376. refers to the entire matched substring, with higher numbers referring to
  3377. substrings captured by parenthesized groups. After a partial match, only
  3378. substring zero is available. An attempt to extract any other substring gives
  3379. the error PCRE2_ERROR_PARTIAL. The next section describes similar functions for
  3380. extracting captured substrings by name.
  3381. </P>
  3382. <P>
  3383. If a pattern uses the \K escape sequence within a positive assertion, the
  3384. reported start of a successful match can be greater than the end of the match.
  3385. For example, if the pattern (?=ab\K) is matched against "ab", the start and
  3386. end offset values for the match are 2 and 0. In this situation, calling these
  3387. functions with a zero substring number extracts a zero-length empty string.
  3388. </P>
  3389. <P>
  3390. You can find the length in code units of a captured substring without
  3391. extracting it by calling <b>pcre2_substring_length_bynumber()</b>. The first
  3392. argument is a pointer to the match data block, the second is the group number,
  3393. and the third is a pointer to a variable into which the length is placed. If
  3394. you just want to know whether or not the substring has been captured, you can
  3395. pass the third argument as NULL.
  3396. </P>
  3397. <P>
  3398. The <b>pcre2_substring_copy_bynumber()</b> function copies a captured substring
  3399. into a supplied buffer, whereas <b>pcre2_substring_get_bynumber()</b> copies it
  3400. into new memory, obtained using the same memory allocation function that was
  3401. used for the match data block. The first two arguments of these functions are a
  3402. pointer to the match data block and a capture group number.
  3403. </P>
  3404. <P>
  3405. The final arguments of <b>pcre2_substring_copy_bynumber()</b> are a pointer to
  3406. the buffer and a pointer to a variable that contains its length in code units.
  3407. This is updated to contain the actual number of code units used for the
  3408. extracted substring, excluding the terminating zero.
  3409. </P>
  3410. <P>
  3411. For <b>pcre2_substring_get_bynumber()</b> the third and fourth arguments point
  3412. to variables that are updated with a pointer to the new memory and the number
  3413. of code units that comprise the substring, again excluding the terminating
  3414. zero. When the substring is no longer needed, the memory should be freed by
  3415. calling <b>pcre2_substring_free()</b>.
  3416. </P>
  3417. <P>
  3418. The return value from all these functions is zero for success, or a negative
  3419. error code. If the pattern match failed, the match failure code is returned.
  3420. If a substring number greater than zero is used after a partial match,
  3421. PCRE2_ERROR_PARTIAL is returned. Other possible error codes are:
  3422. <pre>
  3423. PCRE2_ERROR_NOMEMORY
  3424. </pre>
  3425. The buffer was too small for <b>pcre2_substring_copy_bynumber()</b>, or the
  3426. attempt to get memory failed for <b>pcre2_substring_get_bynumber()</b>.
  3427. <pre>
  3428. PCRE2_ERROR_NOSUBSTRING
  3429. </pre>
  3430. There is no substring with that number in the pattern, that is, the number is
  3431. greater than the number of capturing parentheses.
  3432. <pre>
  3433. PCRE2_ERROR_UNAVAILABLE
  3434. </pre>
  3435. The substring number, though not greater than the number of captures in the
  3436. pattern, is greater than the number of slots in the ovector, so the substring
  3437. could not be captured.
  3438. <pre>
  3439. PCRE2_ERROR_UNSET
  3440. </pre>
  3441. The substring did not participate in the match. For example, if the pattern is
  3442. (abc)|(def) and the subject is "def", and the ovector contains at least two
  3443. capturing slots, substring number 1 is unset.
  3444. </P>
  3445. <br><a name="SEC35" href="#TOC1">EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS</a><br>
  3446. <P>
  3447. <b>int pcre2_substring_list_get(pcre2_match_data *<i>match_data</i>,</b>
  3448. <b>" PCRE2_UCHAR ***<i>listptr</i>, PCRE2_SIZE **<i>lengthsptr</i>);</b>
  3449. <br>
  3450. <br>
  3451. <b>void pcre2_substring_list_free(PCRE2_UCHAR **<i>list</i>);</b>
  3452. </P>
  3453. <P>
  3454. The <b>pcre2_substring_list_get()</b> function extracts all available substrings
  3455. and builds a list of pointers to them. It also (optionally) builds a second
  3456. list that contains their lengths (in code units), excluding a terminating zero
  3457. that is added to each of them. All this is done in a single block of memory
  3458. that is obtained using the same memory allocation function that was used to get
  3459. the match data block.
  3460. </P>
  3461. <P>
  3462. This function must be called only after a successful match. If called after a
  3463. partial match, the error code PCRE2_ERROR_PARTIAL is returned.
  3464. </P>
  3465. <P>
  3466. The address of the memory block is returned via <i>listptr</i>, which is also
  3467. the start of the list of string pointers. The end of the list is marked by a
  3468. NULL pointer. The address of the list of lengths is returned via
  3469. <i>lengthsptr</i>. If your strings do not contain binary zeros and you do not
  3470. therefore need the lengths, you may supply NULL as the <b>lengthsptr</b>
  3471. argument to disable the creation of a list of lengths. The yield of the
  3472. function is zero if all went well, or PCRE2_ERROR_NOMEMORY if the memory block
  3473. could not be obtained. When the list is no longer needed, it should be freed by
  3474. calling <b>pcre2_substring_list_free()</b>.
  3475. </P>
  3476. <P>
  3477. If this function encounters a substring that is unset, which can happen when
  3478. capture group number <i>n+1</i> matches some part of the subject, but group
  3479. <i>n</i> has not been used at all, it returns an empty string. This can be
  3480. distinguished from a genuine zero-length substring by inspecting the
  3481. appropriate offset in the ovector, which contain PCRE2_UNSET for unset
  3482. substrings, or by calling <b>pcre2_substring_length_bynumber()</b>.
  3483. <a name="extractbyname"></a></P>
  3484. <br><a name="SEC36" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a><br>
  3485. <P>
  3486. <b>int pcre2_substring_number_from_name(const pcre2_code *<i>code</i>,</b>
  3487. <b> PCRE2_SPTR <i>name</i>);</b>
  3488. <br>
  3489. <br>
  3490. <b>int pcre2_substring_length_byname(pcre2_match_data *<i>match_data</i>,</b>
  3491. <b> PCRE2_SPTR <i>name</i>, PCRE2_SIZE *<i>length</i>);</b>
  3492. <br>
  3493. <br>
  3494. <b>int pcre2_substring_copy_byname(pcre2_match_data *<i>match_data</i>,</b>
  3495. <b> PCRE2_SPTR <i>name</i>, PCRE2_UCHAR *<i>buffer</i>, PCRE2_SIZE *<i>bufflen</i>);</b>
  3496. <br>
  3497. <br>
  3498. <b>int pcre2_substring_get_byname(pcre2_match_data *<i>match_data</i>,</b>
  3499. <b> PCRE2_SPTR <i>name</i>, PCRE2_UCHAR **<i>bufferptr</i>, PCRE2_SIZE *<i>bufflen</i>);</b>
  3500. <br>
  3501. <br>
  3502. <b>void pcre2_substring_free(PCRE2_UCHAR *<i>buffer</i>);</b>
  3503. </P>
  3504. <P>
  3505. To extract a substring by name, you first have to find associated number.
  3506. For example, for this pattern:
  3507. <pre>
  3508. (a+)b(?&#60;xxx&#62;\d+)...
  3509. </pre>
  3510. the number of the capture group called "xxx" is 2. If the name is known to be
  3511. unique (PCRE2_DUPNAMES was not set), you can find the number from the name by
  3512. calling <b>pcre2_substring_number_from_name()</b>. The first argument is the
  3513. compiled pattern, and the second is the name. The yield of the function is the
  3514. group number, PCRE2_ERROR_NOSUBSTRING if there is no group with that name, or
  3515. PCRE2_ERROR_NOUNIQUESUBSTRING if there is more than one group with that name.
  3516. Given the number, you can extract the substring directly from the ovector, or
  3517. use one of the "bynumber" functions described above.
  3518. </P>
  3519. <P>
  3520. For convenience, there are also "byname" functions that correspond to the
  3521. "bynumber" functions, the only difference being that the second argument is a
  3522. name instead of a number. If PCRE2_DUPNAMES is set and there are duplicate
  3523. names, these functions scan all the groups with the given name, and return the
  3524. captured substring from the first named group that is set.
  3525. </P>
  3526. <P>
  3527. If there are no groups with the given name, PCRE2_ERROR_NOSUBSTRING is
  3528. returned. If all groups with the name have numbers that are greater than the
  3529. number of slots in the ovector, PCRE2_ERROR_UNAVAILABLE is returned. If there
  3530. is at least one group with a slot in the ovector, but no group is found to be
  3531. set, PCRE2_ERROR_UNSET is returned.
  3532. </P>
  3533. <P>
  3534. <b>Warning:</b> If the pattern uses the (?| feature to set up multiple
  3535. capture groups with the same number, as described in the
  3536. <a href="pcre2pattern.html#dupgroupnumber">section on duplicate group numbers</a>
  3537. in the
  3538. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  3539. page, you cannot use names to distinguish the different capture groups, because
  3540. names are not included in the compiled code. The matching process uses only
  3541. numbers. For this reason, the use of different names for groups with the
  3542. same number causes an error at compile time.
  3543. <a name="substitutions"></a></P>
  3544. <br><a name="SEC37" href="#TOC1">CREATING A NEW STRING WITH SUBSTITUTIONS</a><br>
  3545. <P>
  3546. <b>int pcre2_substitute(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  3547. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  3548. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  3549. <b> pcre2_match_context *<i>mcontext</i>, PCRE2_SPTR <i>replacement</i>,</b>
  3550. <b> PCRE2_SIZE <i>rlength</i>, PCRE2_UCHAR *<i>outputbuffer</i>,</b>
  3551. <b> PCRE2_SIZE *<i>outlengthptr</i>);</b>
  3552. </P>
  3553. <P>
  3554. This function optionally calls <b>pcre2_match()</b> and then makes a copy of the
  3555. subject string in <i>outputbuffer</i>, replacing parts that were matched with
  3556. the <i>replacement</i> string, whose length is supplied in <b>rlength</b>, which
  3557. can be given as PCRE2_ZERO_TERMINATED for a zero-terminated string. As a
  3558. special case, if <i>replacement</i> is NULL and <i>rlength</i> is zero, the
  3559. replacement is assumed to be an empty string. If <i>rlength</i> is non-zero, an
  3560. error occurs if <i>replacement</i> is NULL.
  3561. </P>
  3562. <P>
  3563. There is an option (see PCRE2_SUBSTITUTE_REPLACEMENT_ONLY below) to return just
  3564. the replacement string(s). The default action is to perform just one
  3565. replacement if the pattern matches, but there is an option that requests
  3566. multiple replacements (see PCRE2_SUBSTITUTE_GLOBAL below).
  3567. </P>
  3568. <P>
  3569. If successful, <b>pcre2_substitute()</b> returns the number of substitutions
  3570. that were carried out. This may be zero if no match was found, and is never
  3571. greater than one unless PCRE2_SUBSTITUTE_GLOBAL is set. A negative value is
  3572. returned if an error is detected.
  3573. </P>
  3574. <P>
  3575. Matches in which a \K item in a lookahead in the pattern causes the match to
  3576. end before it starts are not supported, and give rise to an error return. For
  3577. global replacements, matches in which \K in a lookbehind causes the match to
  3578. start earlier than the point that was reached in the previous iteration are
  3579. also not supported.
  3580. </P>
  3581. <P>
  3582. The first seven arguments of <b>pcre2_substitute()</b> are the same as for
  3583. <b>pcre2_match()</b>, except that the partial matching options are not
  3584. permitted, and <i>match_data</i> may be passed as NULL, in which case a match
  3585. data block is obtained and freed within this function, using memory management
  3586. functions from the match context, if provided, or else those that were used to
  3587. allocate memory for the compiled code.
  3588. </P>
  3589. <P>
  3590. If <i>match_data</i> is not NULL and PCRE2_SUBSTITUTE_MATCHED is not set, the
  3591. provided block is used for all calls to <b>pcre2_match()</b>, and its contents
  3592. afterwards are the result of the final call. For global changes, this will
  3593. always be a no-match error. The contents of the ovector within the match data
  3594. block may or may not have been changed.
  3595. </P>
  3596. <P>
  3597. As well as the usual options for <b>pcre2_match()</b>, a number of additional
  3598. options can be set in the <i>options</i> argument of <b>pcre2_substitute()</b>.
  3599. One such option is PCRE2_SUBSTITUTE_MATCHED. When this is set, an external
  3600. <i>match_data</i> block must be provided, and it must have already been used for
  3601. an external call to <b>pcre2_match()</b> with the same pattern and subject
  3602. arguments. The data in the <i>match_data</i> block (return code, offset vector)
  3603. is then used for the first substitution instead of calling <b>pcre2_match()</b>
  3604. from within <b>pcre2_substitute()</b>. This allows an application to check for a
  3605. match before choosing to substitute, without having to repeat the match.
  3606. </P>
  3607. <P>
  3608. The contents of the externally supplied match data block are not changed when
  3609. PCRE2_SUBSTITUTE_MATCHED is set. If PCRE2_SUBSTITUTE_GLOBAL is also set,
  3610. <b>pcre2_match()</b> is called after the first substitution to check for further
  3611. matches, but this is done using an internally obtained match data block, thus
  3612. always leaving the external block unchanged.
  3613. </P>
  3614. <P>
  3615. The <i>code</i> argument is not used for matching before the first substitution
  3616. when PCRE2_SUBSTITUTE_MATCHED is set, but it must be provided, even when
  3617. PCRE2_SUBSTITUTE_GLOBAL is not set, because it contains information such as the
  3618. UTF setting and the number of capturing parentheses in the pattern.
  3619. </P>
  3620. <P>
  3621. The default action of <b>pcre2_substitute()</b> is to return a copy of the
  3622. subject string with matched substrings replaced. However, if
  3623. PCRE2_SUBSTITUTE_REPLACEMENT_ONLY is set, only the replacement substrings are
  3624. returned. In the global case, multiple replacements are concatenated in the
  3625. output buffer. Substitution callouts (see
  3626. <a href="#subcallouts">below)</a>
  3627. can be used to separate them if necessary.
  3628. </P>
  3629. <P>
  3630. The <i>outlengthptr</i> argument of <b>pcre2_substitute()</b> must point to a
  3631. variable that contains the length, in code units, of the output buffer. If the
  3632. function is successful, the value is updated to contain the length in code
  3633. units of the new string, excluding the trailing zero that is automatically
  3634. added.
  3635. </P>
  3636. <P>
  3637. If the function is not successful, the value set via <i>outlengthptr</i> depends
  3638. on the type of error. For syntax errors in the replacement string, the value is
  3639. the offset in the replacement string where the error was detected. For other
  3640. errors, the value is PCRE2_UNSET by default. This includes the case of the
  3641. output buffer being too small, unless PCRE2_SUBSTITUTE_OVERFLOW_LENGTH is set.
  3642. </P>
  3643. <P>
  3644. PCRE2_SUBSTITUTE_OVERFLOW_LENGTH changes what happens when the output buffer is
  3645. too small. The default action is to return PCRE2_ERROR_NOMEMORY immediately. If
  3646. this option is set, however, <b>pcre2_substitute()</b> continues to go through
  3647. the motions of matching and substituting (without, of course, writing anything)
  3648. in order to compute the size of buffer that is needed. This value is passed
  3649. back via the <i>outlengthptr</i> variable, with the result of the function still
  3650. being PCRE2_ERROR_NOMEMORY.
  3651. </P>
  3652. <P>
  3653. Passing a buffer size of zero is a permitted way of finding out how much memory
  3654. is needed for given substitution. However, this does mean that the entire
  3655. operation is carried out twice. Depending on the application, it may be more
  3656. efficient to allocate a large buffer and free the excess afterwards, instead of
  3657. using PCRE2_SUBSTITUTE_OVERFLOW_LENGTH.
  3658. </P>
  3659. <P>
  3660. The replacement string, which is interpreted as a UTF string in UTF mode, is
  3661. checked for UTF validity unless PCRE2_NO_UTF_CHECK is set. An invalid UTF
  3662. replacement string causes an immediate return with the relevant UTF error code.
  3663. </P>
  3664. <P>
  3665. If PCRE2_SUBSTITUTE_LITERAL is set, the replacement string is not interpreted
  3666. in any way. By default, however, a dollar character is an escape character that
  3667. can specify the insertion of characters from capture groups and names from
  3668. (*MARK) or other control verbs in the pattern. Dollar is the only escape
  3669. character (backslash is treated as literal). The following forms are always
  3670. recognized:
  3671. <pre>
  3672. $$ insert a dollar character
  3673. $&#60;n&#62; or ${&#60;n&#62;} insert the contents of group &#60;n&#62;
  3674. $*MARK or ${*MARK} insert a control verb name
  3675. </pre>
  3676. Either a group number or a group name can be given for &#60;n&#62;. Curly brackets are
  3677. required only if the following character would be interpreted as part of the
  3678. number or name. The number may be zero to include the entire matched string.
  3679. For example, if the pattern a(b)c is matched with "=abc=" and the replacement
  3680. string "+$1$0$1+", the result is "=+babcb+=".
  3681. </P>
  3682. <P>
  3683. $*MARK inserts the name from the last encountered backtracking control verb on
  3684. the matching path that has a name. (*MARK) must always include a name, but the
  3685. other verbs need not. For example, in the case of (*MARK:A)(*PRUNE) the name
  3686. inserted is "A", but for (*MARK:A)(*PRUNE:B) the relevant name is "B". This
  3687. facility can be used to perform simple simultaneous substitutions, as this
  3688. <b>pcre2test</b> example shows:
  3689. <pre>
  3690. /(*MARK:pear)apple|(*MARK:orange)lemon/g,replace=${*MARK}
  3691. apple lemon
  3692. 2: pear orange
  3693. </pre>
  3694. PCRE2_SUBSTITUTE_GLOBAL causes the function to iterate over the subject string,
  3695. replacing every matching substring. If this option is not set, only the first
  3696. matching substring is replaced. The search for matches takes place in the
  3697. original subject string (that is, previous replacements do not affect it).
  3698. Iteration is implemented by advancing the <i>startoffset</i> value for each
  3699. search, which is always passed the entire subject string. If an offset limit is
  3700. set in the match context, searching stops when that limit is reached.
  3701. </P>
  3702. <P>
  3703. You can restrict the effect of a global substitution to a portion of the
  3704. subject string by setting either or both of <i>startoffset</i> and an offset
  3705. limit. Here is a <b>pcre2test</b> example:
  3706. <pre>
  3707. /B/g,replace=!,use_offset_limit
  3708. ABC ABC ABC ABC\=offset=3,offset_limit=12
  3709. 2: ABC A!C A!C ABC
  3710. </pre>
  3711. When continuing with global substitutions after matching a substring with zero
  3712. length, an attempt to find a non-empty match at the same offset is performed.
  3713. If this is not successful, the offset is advanced by one character except when
  3714. CRLF is a valid newline sequence and the next two characters are CR, LF. In
  3715. this case, the offset is advanced by two characters.
  3716. </P>
  3717. <P>
  3718. PCRE2_SUBSTITUTE_UNKNOWN_UNSET causes references to capture groups that do
  3719. not appear in the pattern to be treated as unset groups. This option should be
  3720. used with care, because it means that a typo in a group name or number no
  3721. longer causes the PCRE2_ERROR_NOSUBSTRING error.
  3722. </P>
  3723. <P>
  3724. PCRE2_SUBSTITUTE_UNSET_EMPTY causes unset capture groups (including unknown
  3725. groups when PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set) to be treated as empty
  3726. strings when inserted as described above. If this option is not set, an attempt
  3727. to insert an unset group causes the PCRE2_ERROR_UNSET error. This option does
  3728. not influence the extended substitution syntax described below.
  3729. </P>
  3730. <P>
  3731. PCRE2_SUBSTITUTE_EXTENDED causes extra processing to be applied to the
  3732. replacement string. Without this option, only the dollar character is special,
  3733. and only the group insertion forms listed above are valid. When
  3734. PCRE2_SUBSTITUTE_EXTENDED is set, two things change:
  3735. </P>
  3736. <P>
  3737. Firstly, backslash in a replacement string is interpreted as an escape
  3738. character. The usual forms such as \n or \x{ddd} can be used to specify
  3739. particular character codes, and backslash followed by any non-alphanumeric
  3740. character quotes that character. Extended quoting can be coded using \Q...\E,
  3741. exactly as in pattern strings.
  3742. </P>
  3743. <P>
  3744. There are also four escape sequences for forcing the case of inserted letters.
  3745. The insertion mechanism has three states: no case forcing, force upper case,
  3746. and force lower case. The escape sequences change the current state: \U and
  3747. \L change to upper or lower case forcing, respectively, and \E (when not
  3748. terminating a \Q quoted sequence) reverts to no case forcing. The sequences
  3749. \u and \l force the next character (if it is a letter) to upper or lower
  3750. case, respectively, and then the state automatically reverts to no case
  3751. forcing. Case forcing applies to all inserted characters, including those from
  3752. capture groups and letters within \Q...\E quoted sequences. If either
  3753. PCRE2_UTF or PCRE2_UCP was set when the pattern was compiled, Unicode
  3754. properties are used for case forcing characters whose code points are greater
  3755. than 127.
  3756. </P>
  3757. <P>
  3758. Note that case forcing sequences such as \U...\E do not nest. For example,
  3759. the result of processing "\Uaa\LBB\Ecc\E" is "AAbbcc"; the final \E has no
  3760. effect. Note also that the PCRE2_ALT_BSUX and PCRE2_EXTRA_ALT_BSUX options do
  3761. not apply to replacement strings.
  3762. </P>
  3763. <P>
  3764. The second effect of setting PCRE2_SUBSTITUTE_EXTENDED is to add more
  3765. flexibility to capture group substitution. The syntax is similar to that used
  3766. by Bash:
  3767. <pre>
  3768. ${&#60;n&#62;:-&#60;string&#62;}
  3769. ${&#60;n&#62;:+&#60;string1&#62;:&#60;string2&#62;}
  3770. </pre>
  3771. As before, &#60;n&#62; may be a group number or a name. The first form specifies a
  3772. default value. If group &#60;n&#62; is set, its value is inserted; if not, &#60;string&#62; is
  3773. expanded and the result inserted. The second form specifies strings that are
  3774. expanded and inserted when group &#60;n&#62; is set or unset, respectively. The first
  3775. form is just a convenient shorthand for
  3776. <pre>
  3777. ${&#60;n&#62;:+${&#60;n&#62;}:&#60;string&#62;}
  3778. </pre>
  3779. Backslash can be used to escape colons and closing curly brackets in the
  3780. replacement strings. A change of the case forcing state within a replacement
  3781. string remains in force afterwards, as shown in this <b>pcre2test</b> example:
  3782. <pre>
  3783. /(some)?(body)/substitute_extended,replace=${1:+\U:\L}HeLLo
  3784. body
  3785. 1: hello
  3786. somebody
  3787. 1: HELLO
  3788. </pre>
  3789. The PCRE2_SUBSTITUTE_UNSET_EMPTY option does not affect these extended
  3790. substitutions. However, PCRE2_SUBSTITUTE_UNKNOWN_UNSET does cause unknown
  3791. groups in the extended syntax forms to be treated as unset.
  3792. </P>
  3793. <P>
  3794. If PCRE2_SUBSTITUTE_LITERAL is set, PCRE2_SUBSTITUTE_UNKNOWN_UNSET,
  3795. PCRE2_SUBSTITUTE_UNSET_EMPTY, and PCRE2_SUBSTITUTE_EXTENDED are irrelevant and
  3796. are ignored.
  3797. </P>
  3798. <br><b>
  3799. Substitution errors
  3800. </b><br>
  3801. <P>
  3802. In the event of an error, <b>pcre2_substitute()</b> returns a negative error
  3803. code. Except for PCRE2_ERROR_NOMATCH (which is never returned), errors from
  3804. <b>pcre2_match()</b> are passed straight back.
  3805. </P>
  3806. <P>
  3807. PCRE2_ERROR_NOSUBSTRING is returned for a non-existent substring insertion,
  3808. unless PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set.
  3809. </P>
  3810. <P>
  3811. PCRE2_ERROR_UNSET is returned for an unset substring insertion (including an
  3812. unknown substring when PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set) when the simple
  3813. (non-extended) syntax is used and PCRE2_SUBSTITUTE_UNSET_EMPTY is not set.
  3814. </P>
  3815. <P>
  3816. PCRE2_ERROR_NOMEMORY is returned if the output buffer is not big enough. If the
  3817. PCRE2_SUBSTITUTE_OVERFLOW_LENGTH option is set, the size of buffer that is
  3818. needed is returned via <i>outlengthptr</i>. Note that this does not happen by
  3819. default.
  3820. </P>
  3821. <P>
  3822. PCRE2_ERROR_NULL is returned if PCRE2_SUBSTITUTE_MATCHED is set but the
  3823. <i>match_data</i> argument is NULL or if the <i>subject</i> or <i>replacement</i>
  3824. arguments are NULL. For backward compatibility reasons an exception is made for
  3825. the <i>replacement</i> argument if the <i>rlength</i> argument is also 0.
  3826. </P>
  3827. <P>
  3828. PCRE2_ERROR_BADREPLACEMENT is used for miscellaneous syntax errors in the
  3829. replacement string, with more particular errors being PCRE2_ERROR_BADREPESCAPE
  3830. (invalid escape sequence), PCRE2_ERROR_REPMISSINGBRACE (closing curly bracket
  3831. not found), PCRE2_ERROR_BADSUBSTITUTION (syntax error in extended group
  3832. substitution), and PCRE2_ERROR_BADSUBSPATTERN (the pattern match ended before
  3833. it started or the match started earlier than the current position in the
  3834. subject, which can happen if \K is used in an assertion).
  3835. </P>
  3836. <P>
  3837. As for all PCRE2 errors, a text message that describes the error can be
  3838. obtained by calling the <b>pcre2_get_error_message()</b> function (see
  3839. "Obtaining a textual error message"
  3840. <a href="#geterrormessage">above).</a>
  3841. <a name="subcallouts"></a></P>
  3842. <br><b>
  3843. Substitution callouts
  3844. </b><br>
  3845. <P>
  3846. <b>int pcre2_set_substitute_callout(pcre2_match_context *<i>mcontext</i>,</b>
  3847. <b> int (*<i>callout_function</i>)(pcre2_substitute_callout_block *, void *),</b>
  3848. <b> void *<i>callout_data</i>);</b>
  3849. <br>
  3850. <br>
  3851. The <b>pcre2_set_substitution_callout()</b> function can be used to specify a
  3852. callout function for <b>pcre2_substitute()</b>. This information is passed in
  3853. a match context. The callout function is called after each substitution has
  3854. been processed, but it can cause the replacement not to happen. The callout
  3855. function is not called for simulated substitutions that happen as a result of
  3856. the PCRE2_SUBSTITUTE_OVERFLOW_LENGTH option.
  3857. </P>
  3858. <P>
  3859. The first argument of the callout function is a pointer to a substitute callout
  3860. block structure, which contains the following fields, not necessarily in this
  3861. order:
  3862. <pre>
  3863. uint32_t <i>version</i>;
  3864. uint32_t <i>subscount</i>;
  3865. PCRE2_SPTR <i>input</i>;
  3866. PCRE2_SPTR <i>output</i>;
  3867. PCRE2_SIZE <i>*ovector</i>;
  3868. uint32_t <i>oveccount</i>;
  3869. PCRE2_SIZE <i>output_offsets[2]</i>;
  3870. </pre>
  3871. The <i>version</i> field contains the version number of the block format. The
  3872. current version is 0. The version number will increase in future if more fields
  3873. are added, but the intention is never to remove any of the existing fields.
  3874. </P>
  3875. <P>
  3876. The <i>subscount</i> field is the number of the current match. It is 1 for the
  3877. first callout, 2 for the second, and so on. The <i>input</i> and <i>output</i>
  3878. pointers are copies of the values passed to <b>pcre2_substitute()</b>.
  3879. </P>
  3880. <P>
  3881. The <i>ovector</i> field points to the ovector, which contains the result of the
  3882. most recent match. The <i>oveccount</i> field contains the number of pairs that
  3883. are set in the ovector, and is always greater than zero.
  3884. </P>
  3885. <P>
  3886. The <i>output_offsets</i> vector contains the offsets of the replacement in the
  3887. output string. This has already been processed for dollar and (if requested)
  3888. backslash substitutions as described above.
  3889. </P>
  3890. <P>
  3891. The second argument of the callout function is the value passed as
  3892. <i>callout_data</i> when the function was registered. The value returned by the
  3893. callout function is interpreted as follows:
  3894. </P>
  3895. <P>
  3896. If the value is zero, the replacement is accepted, and, if
  3897. PCRE2_SUBSTITUTE_GLOBAL is set, processing continues with a search for the next
  3898. match. If the value is not zero, the current replacement is not accepted. If
  3899. the value is greater than zero, processing continues when
  3900. PCRE2_SUBSTITUTE_GLOBAL is set. Otherwise (the value is less than zero or
  3901. PCRE2_SUBSTITUTE_GLOBAL is not set), the rest of the input is copied to the
  3902. output and the call to <b>pcre2_substitute()</b> exits, returning the number of
  3903. matches so far.
  3904. </P>
  3905. <br><a name="SEC38" href="#TOC1">DUPLICATE CAPTURE GROUP NAMES</a><br>
  3906. <P>
  3907. <b>int pcre2_substring_nametable_scan(const pcre2_code *<i>code</i>,</b>
  3908. <b> PCRE2_SPTR <i>name</i>, PCRE2_SPTR *<i>first</i>, PCRE2_SPTR *<i>last</i>);</b>
  3909. </P>
  3910. <P>
  3911. When a pattern is compiled with the PCRE2_DUPNAMES option, names for capture
  3912. groups are not required to be unique. Duplicate names are always allowed for
  3913. groups with the same number, created by using the (?| feature. Indeed, if such
  3914. groups are named, they are required to use the same names.
  3915. </P>
  3916. <P>
  3917. Normally, patterns that use duplicate names are such that in any one match,
  3918. only one of each set of identically-named groups participates. An example is
  3919. shown in the
  3920. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  3921. documentation.
  3922. </P>
  3923. <P>
  3924. When duplicates are present, <b>pcre2_substring_copy_byname()</b> and
  3925. <b>pcre2_substring_get_byname()</b> return the first substring corresponding to
  3926. the given name that is set. Only if none are set is PCRE2_ERROR_UNSET is
  3927. returned. The <b>pcre2_substring_number_from_name()</b> function returns the
  3928. error PCRE2_ERROR_NOUNIQUESUBSTRING when there are duplicate names.
  3929. </P>
  3930. <P>
  3931. If you want to get full details of all captured substrings for a given name,
  3932. you must use the <b>pcre2_substring_nametable_scan()</b> function. The first
  3933. argument is the compiled pattern, and the second is the name. If the third and
  3934. fourth arguments are NULL, the function returns a group number for a unique
  3935. name, or PCRE2_ERROR_NOUNIQUESUBSTRING otherwise.
  3936. </P>
  3937. <P>
  3938. When the third and fourth arguments are not NULL, they must be pointers to
  3939. variables that are updated by the function. After it has run, they point to the
  3940. first and last entries in the name-to-number table for the given name, and the
  3941. function returns the length of each entry in code units. In both cases,
  3942. PCRE2_ERROR_NOSUBSTRING is returned if there are no entries for the given name.
  3943. </P>
  3944. <P>
  3945. The format of the name table is described
  3946. <a href="#infoaboutpattern">above</a>
  3947. in the section entitled <i>Information about a pattern</i>. Given all the
  3948. relevant entries for the name, you can extract each of their numbers, and hence
  3949. the captured data.
  3950. </P>
  3951. <br><a name="SEC39" href="#TOC1">FINDING ALL POSSIBLE MATCHES AT ONE POSITION</a><br>
  3952. <P>
  3953. The traditional matching function uses a similar algorithm to Perl, which stops
  3954. when it finds the first match at a given point in the subject. If you want to
  3955. find all possible matches, or the longest possible match at a given position,
  3956. consider using the alternative matching function (see below) instead. If you
  3957. cannot use the alternative function, you can kludge it up by making use of the
  3958. callout facility, which is described in the
  3959. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  3960. documentation.
  3961. </P>
  3962. <P>
  3963. What you have to do is to insert a callout right at the end of the pattern.
  3964. When your callout function is called, extract and save the current matched
  3965. substring. Then return 1, which forces <b>pcre2_match()</b> to backtrack and try
  3966. other alternatives. Ultimately, when it runs out of matches,
  3967. <b>pcre2_match()</b> will yield PCRE2_ERROR_NOMATCH.
  3968. <a name="dfamatch"></a></P>
  3969. <br><a name="SEC40" href="#TOC1">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a><br>
  3970. <P>
  3971. <b>int pcre2_dfa_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  3972. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  3973. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  3974. <b> pcre2_match_context *<i>mcontext</i>,</b>
  3975. <b> int *<i>workspace</i>, PCRE2_SIZE <i>wscount</i>);</b>
  3976. </P>
  3977. <P>
  3978. The function <b>pcre2_dfa_match()</b> is called to match a subject string
  3979. against a compiled pattern, using a matching algorithm that scans the subject
  3980. string just once (not counting lookaround assertions), and does not backtrack
  3981. (except when processing lookaround assertions). This has different
  3982. characteristics to the normal algorithm, and is not compatible with Perl. Some
  3983. of the features of PCRE2 patterns are not supported. Nevertheless, there are
  3984. times when this kind of matching can be useful. For a discussion of the two
  3985. matching algorithms, and a list of features that <b>pcre2_dfa_match()</b> does
  3986. not support, see the
  3987. <a href="pcre2matching.html"><b>pcre2matching</b></a>
  3988. documentation.
  3989. </P>
  3990. <P>
  3991. The arguments for the <b>pcre2_dfa_match()</b> function are the same as for
  3992. <b>pcre2_match()</b>, plus two extras. The ovector within the match data block
  3993. is used in a different way, and this is described below. The other common
  3994. arguments are used in the same way as for <b>pcre2_match()</b>, so their
  3995. description is not repeated here.
  3996. </P>
  3997. <P>
  3998. The two additional arguments provide workspace for the function. The workspace
  3999. vector should contain at least 20 elements. It is used for keeping track of
  4000. multiple paths through the pattern tree. More workspace is needed for patterns
  4001. and subjects where there are a lot of potential matches.
  4002. </P>
  4003. <P>
  4004. Here is an example of a simple call to <b>pcre2_dfa_match()</b>:
  4005. <pre>
  4006. int wspace[20];
  4007. pcre2_match_data *md = pcre2_match_data_create(4, NULL);
  4008. int rc = pcre2_dfa_match(
  4009. re, /* result of pcre2_compile() */
  4010. "some string", /* the subject string */
  4011. 11, /* the length of the subject string */
  4012. 0, /* start at offset 0 in the subject */
  4013. 0, /* default options */
  4014. md, /* the match data block */
  4015. NULL, /* a match context; NULL means use defaults */
  4016. wspace, /* working space vector */
  4017. 20); /* number of elements (NOT size in bytes) */
  4018. </PRE>
  4019. </P>
  4020. <br><b>
  4021. Option bits for <b>pcre2_dfa_match()</b>
  4022. </b><br>
  4023. <P>
  4024. The unused bits of the <i>options</i> argument for <b>pcre2_dfa_match()</b> must
  4025. be zero. The only bits that may be set are PCRE2_ANCHORED,
  4026. PCRE2_COPY_MATCHED_SUBJECT, PCRE2_ENDANCHORED, PCRE2_NOTBOL, PCRE2_NOTEOL,
  4027. PCRE2_NOTEMPTY, PCRE2_NOTEMPTY_ATSTART, PCRE2_NO_UTF_CHECK, PCRE2_PARTIAL_HARD,
  4028. PCRE2_PARTIAL_SOFT, PCRE2_DFA_SHORTEST, and PCRE2_DFA_RESTART. All but the last
  4029. four of these are exactly the same as for <b>pcre2_match()</b>, so their
  4030. description is not repeated here.
  4031. <pre>
  4032. PCRE2_PARTIAL_HARD
  4033. PCRE2_PARTIAL_SOFT
  4034. </pre>
  4035. These have the same general effect as they do for <b>pcre2_match()</b>, but the
  4036. details are slightly different. When PCRE2_PARTIAL_HARD is set for
  4037. <b>pcre2_dfa_match()</b>, it returns PCRE2_ERROR_PARTIAL if the end of the
  4038. subject is reached and there is still at least one matching possibility that
  4039. requires additional characters. This happens even if some complete matches have
  4040. already been found. When PCRE2_PARTIAL_SOFT is set, the return code
  4041. PCRE2_ERROR_NOMATCH is converted into PCRE2_ERROR_PARTIAL if the end of the
  4042. subject is reached, there have been no complete matches, but there is still at
  4043. least one matching possibility. The portion of the string that was inspected
  4044. when the longest partial match was found is set as the first matching string in
  4045. both cases. There is a more detailed discussion of partial and multi-segment
  4046. matching, with examples, in the
  4047. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  4048. documentation.
  4049. <pre>
  4050. PCRE2_DFA_SHORTEST
  4051. </pre>
  4052. Setting the PCRE2_DFA_SHORTEST option causes the matching algorithm to stop as
  4053. soon as it has found one match. Because of the way the alternative algorithm
  4054. works, this is necessarily the shortest possible match at the first possible
  4055. matching point in the subject string.
  4056. <pre>
  4057. PCRE2_DFA_RESTART
  4058. </pre>
  4059. When <b>pcre2_dfa_match()</b> returns a partial match, it is possible to call it
  4060. again, with additional subject characters, and have it continue with the same
  4061. match. The PCRE2_DFA_RESTART option requests this action; when it is set, the
  4062. <i>workspace</i> and <i>wscount</i> options must reference the same vector as
  4063. before because data about the match so far is left in them after a partial
  4064. match. There is more discussion of this facility in the
  4065. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  4066. documentation.
  4067. </P>
  4068. <br><b>
  4069. Successful returns from <b>pcre2_dfa_match()</b>
  4070. </b><br>
  4071. <P>
  4072. When <b>pcre2_dfa_match()</b> succeeds, it may have matched more than one
  4073. substring in the subject. Note, however, that all the matches from one run of
  4074. the function start at the same point in the subject. The shorter matches are
  4075. all initial substrings of the longer matches. For example, if the pattern
  4076. <pre>
  4077. &#60;.*&#62;
  4078. </pre>
  4079. is matched against the string
  4080. <pre>
  4081. This is &#60;something&#62; &#60;something else&#62; &#60;something further&#62; no more
  4082. </pre>
  4083. the three matched strings are
  4084. <pre>
  4085. &#60;something&#62; &#60;something else&#62; &#60;something further&#62;
  4086. &#60;something&#62; &#60;something else&#62;
  4087. &#60;something&#62;
  4088. </pre>
  4089. On success, the yield of the function is a number greater than zero, which is
  4090. the number of matched substrings. The offsets of the substrings are returned in
  4091. the ovector, and can be extracted by number in the same way as for
  4092. <b>pcre2_match()</b>, but the numbers bear no relation to any capture groups
  4093. that may exist in the pattern, because DFA matching does not support capturing.
  4094. </P>
  4095. <P>
  4096. Calls to the convenience functions that extract substrings by name
  4097. return the error PCRE2_ERROR_DFA_UFUNC (unsupported function) if used after a
  4098. DFA match. The convenience functions that extract substrings by number never
  4099. return PCRE2_ERROR_NOSUBSTRING.
  4100. </P>
  4101. <P>
  4102. The matched strings are stored in the ovector in reverse order of length; that
  4103. is, the longest matching string is first. If there were too many matches to fit
  4104. into the ovector, the yield of the function is zero, and the vector is filled
  4105. with the longest matches.
  4106. </P>
  4107. <P>
  4108. NOTE: PCRE2's "auto-possessification" optimization usually applies to character
  4109. repeats at the end of a pattern (as well as internally). For example, the
  4110. pattern "a\d+" is compiled as if it were "a\d++". For DFA matching, this
  4111. means that only one possible match is found. If you really do want multiple
  4112. matches in such cases, either use an ungreedy repeat such as "a\d+?" or set
  4113. the PCRE2_NO_AUTO_POSSESS option when compiling.
  4114. </P>
  4115. <br><b>
  4116. Error returns from <b>pcre2_dfa_match()</b>
  4117. </b><br>
  4118. <P>
  4119. The <b>pcre2_dfa_match()</b> function returns a negative number when it fails.
  4120. Many of the errors are the same as for <b>pcre2_match()</b>, as described
  4121. <a href="#errorlist">above.</a>
  4122. There are in addition the following errors that are specific to
  4123. <b>pcre2_dfa_match()</b>:
  4124. <pre>
  4125. PCRE2_ERROR_DFA_UITEM
  4126. </pre>
  4127. This return is given if <b>pcre2_dfa_match()</b> encounters an item in the
  4128. pattern that it does not support, for instance, the use of \C in a UTF mode or
  4129. a backreference.
  4130. <pre>
  4131. PCRE2_ERROR_DFA_UCOND
  4132. </pre>
  4133. This return is given if <b>pcre2_dfa_match()</b> encounters a condition item
  4134. that uses a backreference for the condition, or a test for recursion in a
  4135. specific capture group. These are not supported.
  4136. <pre>
  4137. PCRE2_ERROR_DFA_UINVALID_UTF
  4138. </pre>
  4139. This return is given if <b>pcre2_dfa_match()</b> is called for a pattern that
  4140. was compiled with PCRE2_MATCH_INVALID_UTF. This is not supported for DFA
  4141. matching.
  4142. <pre>
  4143. PCRE2_ERROR_DFA_WSSIZE
  4144. </pre>
  4145. This return is given if <b>pcre2_dfa_match()</b> runs out of space in the
  4146. <i>workspace</i> vector.
  4147. <pre>
  4148. PCRE2_ERROR_DFA_RECURSE
  4149. </pre>
  4150. When a recursion or subroutine call is processed, the matching function calls
  4151. itself recursively, using private memory for the ovector and <i>workspace</i>.
  4152. This error is given if the internal ovector is not large enough. This should be
  4153. extremely rare, as a vector of size 1000 is used.
  4154. <pre>
  4155. PCRE2_ERROR_DFA_BADRESTART
  4156. </pre>
  4157. When <b>pcre2_dfa_match()</b> is called with the <b>PCRE2_DFA_RESTART</b> option,
  4158. some plausibility checks are made on the contents of the workspace, which
  4159. should contain data about the previous partial match. If any of these checks
  4160. fail, this error is given.
  4161. </P>
  4162. <br><a name="SEC41" href="#TOC1">SEE ALSO</a><br>
  4163. <P>
  4164. <b>pcre2build</b>(3), <b>pcre2callout</b>(3), <b>pcre2demo(3)</b>,
  4165. <b>pcre2matching</b>(3), <b>pcre2partial</b>(3), <b>pcre2posix</b>(3),
  4166. <b>pcre2sample</b>(3), <b>pcre2unicode</b>(3).
  4167. </P>
  4168. <br><a name="SEC42" href="#TOC1">AUTHOR</a><br>
  4169. <P>
  4170. Philip Hazel
  4171. <br>
  4172. Retired from University Computing Service
  4173. <br>
  4174. Cambridge, England.
  4175. <br>
  4176. </P>
  4177. <br><a name="SEC43" href="#TOC1">REVISION</a><br>
  4178. <P>
  4179. Last updated: 24 April 2024
  4180. <br>
  4181. Copyright &copy; 1997-2024 University of Cambridge.
  4182. <br>
  4183. <p>
  4184. Return to the <a href="index.html">PCRE2 index page</a>.
  4185. </p>