pcre2posix.3 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. .TH PCRE2POSIX 3 "26 April 2021" "PCRE2 10.37"
  2. .SH NAME
  3. PCRE2 - Perl-compatible regular expressions (revised API)
  4. .SH "SYNOPSIS"
  5. .rs
  6. .sp
  7. .B #include <pcre2posix.h>
  8. .PP
  9. .nf
  10. .B int pcre2_regcomp(regex_t *\fIpreg\fP, const char *\fIpattern\fP,
  11. .B " int \fIcflags\fP);"
  12. .sp
  13. .B int pcre2_regexec(const regex_t *\fIpreg\fP, const char *\fIstring\fP,
  14. .B " size_t \fInmatch\fP, regmatch_t \fIpmatch\fP[], int \fIeflags\fP);"
  15. .sp
  16. .B "size_t pcre2_regerror(int \fIerrcode\fP, const regex_t *\fIpreg\fP,"
  17. .B " char *\fIerrbuf\fP, size_t \fIerrbuf_size\fP);"
  18. .sp
  19. .B void pcre2_regfree(regex_t *\fIpreg\fP);
  20. .fi
  21. .
  22. .SH DESCRIPTION
  23. .rs
  24. .sp
  25. This set of functions provides a POSIX-style API for the PCRE2 regular
  26. expression 8-bit library. There are no POSIX-style wrappers for PCRE2's 16-bit
  27. and 32-bit libraries. See the
  28. .\" HREF
  29. \fBpcre2api\fP
  30. .\"
  31. documentation for a description of PCRE2's native API, which contains much
  32. additional functionality.
  33. .P
  34. The functions described here are wrapper functions that ultimately call the
  35. PCRE2 native API. Their prototypes are defined in the \fBpcre2posix.h\fP header
  36. file, and they all have unique names starting with \fBpcre2_\fP. However, the
  37. \fBpcre2posix.h\fP header also contains macro definitions that convert the
  38. standard POSIX names such \fBregcomp()\fP into \fBpcre2_regcomp()\fP etc. This
  39. means that a program can use the usual POSIX names without running the risk of
  40. accidentally linking with POSIX functions from a different library.
  41. .P
  42. On Unix-like systems the PCRE2 POSIX library is called \fBlibpcre2-posix\fP, so
  43. can be accessed by adding \fB-lpcre2-posix\fP to the command for linking an
  44. application. Because the POSIX functions call the native ones, it is also
  45. necessary to add \fB-lpcre2-8\fP.
  46. .P
  47. Although they were not defined as protypes in \fBpcre2posix.h\fP, releases
  48. 10.33 to 10.36 of the library contained functions with the POSIX names
  49. \fBregcomp()\fP etc. These simply passed their arguments to the PCRE2
  50. functions. These functions were provided for backwards compatibility with
  51. earlier versions of PCRE2, which had only POSIX names. However, this has proved
  52. troublesome in situations where a program links with several libraries, some of
  53. which use PCRE2's POSIX interface while others use the real POSIX functions.
  54. For this reason, the POSIX names have been removed since release 10.37.
  55. .P
  56. Calling the header file \fBpcre2posix.h\fP avoids any conflict with other POSIX
  57. libraries. It can, of course, be renamed or aliased as \fBregex.h\fP, which is
  58. the "correct" name, if there is no clash. It provides two structure types,
  59. \fIregex_t\fP for compiled internal forms, and \fIregmatch_t\fP for returning
  60. captured substrings. It also defines some constants whose names start with
  61. "REG_"; these are used for setting options and identifying error codes.
  62. .
  63. .
  64. .SH "USING THE POSIX FUNCTIONS"
  65. .rs
  66. .sp
  67. Those POSIX option bits that can reasonably be mapped to PCRE2 native options
  68. have been implemented. In addition, the option REG_EXTENDED is defined with the
  69. value zero. This has no effect, but since programs that are written to the
  70. POSIX interface often use it, this makes it easier to slot in PCRE2 as a
  71. replacement library. Other POSIX options are not even defined.
  72. .P
  73. There are also some options that are not defined by POSIX. These have been
  74. added at the request of users who want to make use of certain PCRE2-specific
  75. features via the POSIX calling interface or to add BSD or GNU functionality.
  76. .P
  77. When PCRE2 is called via these functions, it is only the API that is POSIX-like
  78. in style. The syntax and semantics of the regular expressions themselves are
  79. still those of Perl, subject to the setting of various PCRE2 options, as
  80. described below. "POSIX-like in style" means that the API approximates to the
  81. POSIX definition; it is not fully POSIX-compatible, and in multi-unit encoding
  82. domains it is probably even less compatible.
  83. .P
  84. The descriptions below use the actual names of the functions, but, as described
  85. above, the standard POSIX names (without the \fBpcre2_\fP prefix) may also be
  86. used.
  87. .
  88. .
  89. .SH "COMPILING A PATTERN"
  90. .rs
  91. .sp
  92. The function \fBpcre2_regcomp()\fP is called to compile a pattern into an
  93. internal form. By default, the pattern is a C string terminated by a binary
  94. zero (but see REG_PEND below). The \fIpreg\fP argument is a pointer to a
  95. \fBregex_t\fP structure that is used as a base for storing information about
  96. the compiled regular expression. (It is also used for input when REG_PEND is
  97. set.)
  98. .P
  99. The argument \fIcflags\fP is either zero, or contains one or more of the bits
  100. defined by the following macros:
  101. .sp
  102. REG_DOTALL
  103. .sp
  104. The PCRE2_DOTALL option is set when the regular expression is passed for
  105. compilation to the native function. Note that REG_DOTALL is not part of the
  106. POSIX standard.
  107. .sp
  108. REG_ICASE
  109. .sp
  110. The PCRE2_CASELESS option is set when the regular expression is passed for
  111. compilation to the native function.
  112. .sp
  113. REG_NEWLINE
  114. .sp
  115. The PCRE2_MULTILINE option is set when the regular expression is passed for
  116. compilation to the native function. Note that this does \fInot\fP mimic the
  117. defined POSIX behaviour for REG_NEWLINE (see the following section).
  118. .sp
  119. REG_NOSPEC
  120. .sp
  121. The PCRE2_LITERAL option is set when the regular expression is passed for
  122. compilation to the native function. This disables all meta characters in the
  123. pattern, causing it to be treated as a literal string. The only other options
  124. that are allowed with REG_NOSPEC are REG_ICASE, REG_NOSUB, REG_PEND, and
  125. REG_UTF. Note that REG_NOSPEC is not part of the POSIX standard.
  126. .sp
  127. REG_NOSUB
  128. .sp
  129. When a pattern that is compiled with this flag is passed to
  130. \fBpcre2_regexec()\fP for matching, the \fInmatch\fP and \fIpmatch\fP arguments
  131. are ignored, and no captured strings are returned. Versions of the PCRE library
  132. prior to 10.22 used to set the PCRE2_NO_AUTO_CAPTURE compile option, but this
  133. no longer happens because it disables the use of backreferences.
  134. .sp
  135. REG_PEND
  136. .sp
  137. If this option is set, the \fBreg_endp\fP field in the \fIpreg\fP structure
  138. (which has the type const char *) must be set to point to the character beyond
  139. the end of the pattern before calling \fBpcre2_regcomp()\fP. The pattern itself
  140. may now contain binary zeros, which are treated as data characters. Without
  141. REG_PEND, a binary zero terminates the pattern and the \fBre_endp\fP field is
  142. ignored. This is a GNU extension to the POSIX standard and should be used with
  143. caution in software intended to be portable to other systems.
  144. .sp
  145. REG_UCP
  146. .sp
  147. The PCRE2_UCP option is set when the regular expression is passed for
  148. compilation to the native function. This causes PCRE2 to use Unicode properties
  149. when matchine \ed, \ew, etc., instead of just recognizing ASCII values. Note
  150. that REG_UCP is not part of the POSIX standard.
  151. .sp
  152. REG_UNGREEDY
  153. .sp
  154. The PCRE2_UNGREEDY option is set when the regular expression is passed for
  155. compilation to the native function. Note that REG_UNGREEDY is not part of the
  156. POSIX standard.
  157. .sp
  158. REG_UTF
  159. .sp
  160. The PCRE2_UTF option is set when the regular expression is passed for
  161. compilation to the native function. This causes the pattern itself and all data
  162. strings used for matching it to be treated as UTF-8 strings. Note that REG_UTF
  163. is not part of the POSIX standard.
  164. .P
  165. In the absence of these flags, no options are passed to the native function.
  166. This means the the regex is compiled with PCRE2 default semantics. In
  167. particular, the way it handles newline characters in the subject string is the
  168. Perl way, not the POSIX way. Note that setting PCRE2_MULTILINE has only
  169. \fIsome\fP of the effects specified for REG_NEWLINE. It does not affect the way
  170. newlines are matched by the dot metacharacter (they are not) or by a negative
  171. class such as [^a] (they are).
  172. .P
  173. The yield of \fBpcre2_regcomp()\fP is zero on success, and non-zero otherwise.
  174. The \fIpreg\fP structure is filled in on success, and one other member of the
  175. structure (as well as \fIre_endp\fP) is public: \fIre_nsub\fP contains the
  176. number of capturing subpatterns in the regular expression. Various error codes
  177. are defined in the header file.
  178. .P
  179. NOTE: If the yield of \fBpcre2_regcomp()\fP is non-zero, you must not attempt
  180. to use the contents of the \fIpreg\fP structure. If, for example, you pass it
  181. to \fBpcre2_regexec()\fP, the result is undefined and your program is likely to
  182. crash.
  183. .
  184. .
  185. .SH "MATCHING NEWLINE CHARACTERS"
  186. .rs
  187. .sp
  188. This area is not simple, because POSIX and Perl take different views of things.
  189. It is not possible to get PCRE2 to obey POSIX semantics, but then PCRE2 was
  190. never intended to be a POSIX engine. The following table lists the different
  191. possibilities for matching newline characters in Perl and PCRE2:
  192. .sp
  193. Default Change with
  194. .sp
  195. . matches newline no PCRE2_DOTALL
  196. newline matches [^a] yes not changeable
  197. $ matches \en at end yes PCRE2_DOLLAR_ENDONLY
  198. $ matches \en in middle no PCRE2_MULTILINE
  199. ^ matches \en in middle no PCRE2_MULTILINE
  200. .sp
  201. This is the equivalent table for a POSIX-compatible pattern matcher:
  202. .sp
  203. Default Change with
  204. .sp
  205. . matches newline yes REG_NEWLINE
  206. newline matches [^a] yes REG_NEWLINE
  207. $ matches \en at end no REG_NEWLINE
  208. $ matches \en in middle no REG_NEWLINE
  209. ^ matches \en in middle no REG_NEWLINE
  210. .sp
  211. This behaviour is not what happens when PCRE2 is called via its POSIX
  212. API. By default, PCRE2's behaviour is the same as Perl's, except that there is
  213. no equivalent for PCRE2_DOLLAR_ENDONLY in Perl. In both PCRE2 and Perl, there
  214. is no way to stop newline from matching [^a].
  215. .P
  216. Default POSIX newline handling can be obtained by setting PCRE2_DOTALL and
  217. PCRE2_DOLLAR_ENDONLY when calling \fBpcre2_compile()\fP directly, but there is
  218. no way to make PCRE2 behave exactly as for the REG_NEWLINE action. When using
  219. the POSIX API, passing REG_NEWLINE to PCRE2's \fBpcre2_regcomp()\fP function
  220. causes PCRE2_MULTILINE to be passed to \fBpcre2_compile()\fP, and REG_DOTALL
  221. passes PCRE2_DOTALL. There is no way to pass PCRE2_DOLLAR_ENDONLY.
  222. .
  223. .
  224. .SH "MATCHING A PATTERN"
  225. .rs
  226. .sp
  227. The function \fBpcre2_regexec()\fP is called to match a compiled pattern
  228. \fIpreg\fP against a given \fIstring\fP, which is by default terminated by a
  229. zero byte (but see REG_STARTEND below), subject to the options in \fIeflags\fP.
  230. These can be:
  231. .sp
  232. REG_NOTBOL
  233. .sp
  234. The PCRE2_NOTBOL option is set when calling the underlying PCRE2 matching
  235. function.
  236. .sp
  237. REG_NOTEMPTY
  238. .sp
  239. The PCRE2_NOTEMPTY option is set when calling the underlying PCRE2 matching
  240. function. Note that REG_NOTEMPTY is not part of the POSIX standard. However,
  241. setting this option can give more POSIX-like behaviour in some situations.
  242. .sp
  243. REG_NOTEOL
  244. .sp
  245. The PCRE2_NOTEOL option is set when calling the underlying PCRE2 matching
  246. function.
  247. .sp
  248. REG_STARTEND
  249. .sp
  250. When this option is set, the subject string starts at \fIstring\fP +
  251. \fIpmatch[0].rm_so\fP and ends at \fIstring\fP + \fIpmatch[0].rm_eo\fP, which
  252. should point to the first character beyond the string. There may be binary
  253. zeros within the subject string, and indeed, using REG_STARTEND is the only
  254. way to pass a subject string that contains a binary zero.
  255. .P
  256. Whatever the value of \fIpmatch[0].rm_so\fP, the offsets of the matched string
  257. and any captured substrings are still given relative to the start of
  258. \fIstring\fP itself. (Before PCRE2 release 10.30 these were given relative to
  259. \fIstring\fP + \fIpmatch[0].rm_so\fP, but this differs from other
  260. implementations.)
  261. .P
  262. This is a BSD extension, compatible with but not specified by IEEE Standard
  263. 1003.2 (POSIX.2), and should be used with caution in software intended to be
  264. portable to other systems. Note that a non-zero \fIrm_so\fP does not imply
  265. REG_NOTBOL; REG_STARTEND affects only the location and length of the string,
  266. not how it is matched. Setting REG_STARTEND and passing \fIpmatch\fP as NULL
  267. are mutually exclusive; the error REG_INVARG is returned.
  268. .P
  269. If the pattern was compiled with the REG_NOSUB flag, no data about any matched
  270. strings is returned. The \fInmatch\fP and \fIpmatch\fP arguments of
  271. \fBpcre2_regexec()\fP are ignored (except possibly as input for REG_STARTEND).
  272. .P
  273. The value of \fInmatch\fP may be zero, and the value \fIpmatch\fP may be NULL
  274. (unless REG_STARTEND is set); in both these cases no data about any matched
  275. strings is returned.
  276. .P
  277. Otherwise, the portion of the string that was matched, and also any captured
  278. substrings, are returned via the \fIpmatch\fP argument, which points to an
  279. array of \fInmatch\fP structures of type \fIregmatch_t\fP, containing the
  280. members \fIrm_so\fP and \fIrm_eo\fP. These contain the byte offset to the first
  281. character of each substring and the offset to the first character after the end
  282. of each substring, respectively. The 0th element of the vector relates to the
  283. entire portion of \fIstring\fP that was matched; subsequent elements relate to
  284. the capturing subpatterns of the regular expression. Unused entries in the
  285. array have both structure members set to -1.
  286. .P
  287. A successful match yields a zero return; various error codes are defined in the
  288. header file, of which REG_NOMATCH is the "expected" failure code.
  289. .
  290. .
  291. .SH "ERROR MESSAGES"
  292. .rs
  293. .sp
  294. The \fBpcre2_regerror()\fP function maps a non-zero errorcode from either
  295. \fBpcre2_regcomp()\fP or \fBpcre2_regexec()\fP to a printable message. If
  296. \fIpreg\fP is not NULL, the error should have arisen from the use of that
  297. structure. A message terminated by a binary zero is placed in \fIerrbuf\fP. If
  298. the buffer is too short, only the first \fIerrbuf_size\fP - 1 characters of the
  299. error message are used. The yield of the function is the size of buffer needed
  300. to hold the whole message, including the terminating zero. This value is
  301. greater than \fIerrbuf_size\fP if the message was truncated.
  302. .
  303. .
  304. .SH MEMORY USAGE
  305. .rs
  306. .sp
  307. Compiling a regular expression causes memory to be allocated and associated
  308. with the \fIpreg\fP structure. The function \fBpcre2_regfree()\fP frees all
  309. such memory, after which \fIpreg\fP may no longer be used as a compiled
  310. expression.
  311. .
  312. .
  313. .SH AUTHOR
  314. .rs
  315. .sp
  316. .nf
  317. Philip Hazel
  318. University Computing Service
  319. Cambridge, England.
  320. .fi
  321. .
  322. .
  323. .SH REVISION
  324. .rs
  325. .sp
  326. .nf
  327. Last updated: 26 April 2021
  328. Copyright (c) 1997-2021 University of Cambridge.
  329. .fi