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.

4187 lines
182 KiB

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