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.

348 lines
15 KiB

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