pcreposix.3 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. .TH PCREPOSIX 3 "09 January 2012" "PCRE 8.30"
  2. .SH NAME
  3. PCRE - Perl-compatible regular expressions.
  4. .SH "SYNOPSIS"
  5. .rs
  6. .sp
  7. .B #include <pcreposix.h>
  8. .PP
  9. .nf
  10. .B int regcomp(regex_t *\fIpreg\fP, const char *\fIpattern\fP,
  11. .B " int \fIcflags\fP);"
  12. .sp
  13. .B int regexec(regex_t *\fIpreg\fP, const char *\fIstring\fP,
  14. .B " size_t \fInmatch\fP, regmatch_t \fIpmatch\fP[], int \fIeflags\fP);"
  15. .B " size_t regerror(int \fIerrcode\fP, const regex_t *\fIpreg\fP,"
  16. .B " char *\fIerrbuf\fP, size_t \fIerrbuf_size\fP);"
  17. .sp
  18. .B void regfree(regex_t *\fIpreg\fP);
  19. .fi
  20. .
  21. .SH DESCRIPTION
  22. .rs
  23. .sp
  24. This set of functions provides a POSIX-style API for the PCRE regular
  25. expression 8-bit library. See the
  26. .\" HREF
  27. \fBpcreapi\fP
  28. .\"
  29. documentation for a description of PCRE's native API, which contains much
  30. additional functionality. There is no POSIX-style wrapper for PCRE's 16-bit
  31. and 32-bit library.
  32. .P
  33. The functions described here are just wrapper functions that ultimately call
  34. the PCRE native API. Their prototypes are defined in the \fBpcreposix.h\fP
  35. header file, and on Unix systems the library itself is called
  36. \fBpcreposix.a\fP, so can be accessed by adding \fB-lpcreposix\fP to the
  37. command for linking an application that uses them. Because the POSIX functions
  38. call the native ones, it is also necessary to add \fB-lpcre\fP.
  39. .P
  40. I have implemented only those POSIX option bits that can be reasonably mapped
  41. to PCRE native options. In addition, the option REG_EXTENDED is defined with
  42. the value zero. This has no effect, but since programs that are written to the
  43. POSIX interface often use it, this makes it easier to slot in PCRE as a
  44. replacement library. Other POSIX options are not even defined.
  45. .P
  46. There are also some other options that are not defined by POSIX. These have
  47. been added at the request of users who want to make use of certain
  48. PCRE-specific features via the POSIX calling interface.
  49. .P
  50. When PCRE is called via these functions, it is only the API that is POSIX-like
  51. in style. The syntax and semantics of the regular expressions themselves are
  52. still those of Perl, subject to the setting of various PCRE options, as
  53. described below. "POSIX-like in style" means that the API approximates to the
  54. POSIX definition; it is not fully POSIX-compatible, and in multi-byte encoding
  55. domains it is probably even less compatible.
  56. .P
  57. The header for these functions is supplied as \fBpcreposix.h\fP to avoid any
  58. potential clash with other POSIX libraries. It can, of course, be renamed or
  59. aliased as \fBregex.h\fP, which is the "correct" name. It provides two
  60. structure types, \fIregex_t\fP for compiled internal forms, and
  61. \fIregmatch_t\fP for returning captured substrings. It also defines some
  62. constants whose names start with "REG_"; these are used for setting options and
  63. identifying error codes.
  64. .
  65. .
  66. .SH "COMPILING A PATTERN"
  67. .rs
  68. .sp
  69. The function \fBregcomp()\fP is called to compile a pattern into an
  70. internal form. The pattern is a C string terminated by a binary zero, and
  71. is passed in the argument \fIpattern\fP. The \fIpreg\fP argument is a pointer
  72. to a \fBregex_t\fP structure that is used as a base for storing information
  73. about the compiled regular expression.
  74. .P
  75. The argument \fIcflags\fP is either zero, or contains one or more of the bits
  76. defined by the following macros:
  77. .sp
  78. REG_DOTALL
  79. .sp
  80. The PCRE_DOTALL option is set when the regular expression is passed for
  81. compilation to the native function. Note that REG_DOTALL is not part of the
  82. POSIX standard.
  83. .sp
  84. REG_ICASE
  85. .sp
  86. The PCRE_CASELESS option is set when the regular expression is passed for
  87. compilation to the native function.
  88. .sp
  89. REG_NEWLINE
  90. .sp
  91. The PCRE_MULTILINE option is set when the regular expression is passed for
  92. compilation to the native function. Note that this does \fInot\fP mimic the
  93. defined POSIX behaviour for REG_NEWLINE (see the following section).
  94. .sp
  95. REG_NOSUB
  96. .sp
  97. The PCRE_NO_AUTO_CAPTURE option is set when the regular expression is passed
  98. for compilation to the native function. In addition, when a pattern that is
  99. compiled with this flag is passed to \fBregexec()\fP for matching, the
  100. \fInmatch\fP and \fIpmatch\fP arguments are ignored, and no captured strings
  101. are returned.
  102. .sp
  103. REG_UCP
  104. .sp
  105. The PCRE_UCP option is set when the regular expression is passed for
  106. compilation to the native function. This causes PCRE to use Unicode properties
  107. when matchine \ed, \ew, etc., instead of just recognizing ASCII values. Note
  108. that REG_UTF8 is not part of the POSIX standard.
  109. .sp
  110. REG_UNGREEDY
  111. .sp
  112. The PCRE_UNGREEDY option is set when the regular expression is passed for
  113. compilation to the native function. Note that REG_UNGREEDY is not part of the
  114. POSIX standard.
  115. .sp
  116. REG_UTF8
  117. .sp
  118. The PCRE_UTF8 option is set when the regular expression is passed for
  119. compilation to the native function. This causes the pattern itself and all data
  120. strings used for matching it to be treated as UTF-8 strings. Note that REG_UTF8
  121. is not part of the POSIX standard.
  122. .P
  123. In the absence of these flags, no options are passed to the native function.
  124. This means the the regex is compiled with PCRE default semantics. In
  125. particular, the way it handles newline characters in the subject string is the
  126. Perl way, not the POSIX way. Note that setting PCRE_MULTILINE has only
  127. \fIsome\fP of the effects specified for REG_NEWLINE. It does not affect the way
  128. newlines are matched by . (they are not) or by a negative class such as [^a]
  129. (they are).
  130. .P
  131. The yield of \fBregcomp()\fP is zero on success, and non-zero otherwise. The
  132. \fIpreg\fP structure is filled in on success, and one member of the structure
  133. is public: \fIre_nsub\fP contains the number of capturing subpatterns in
  134. the regular expression. Various error codes are defined in the header file.
  135. .P
  136. NOTE: If the yield of \fBregcomp()\fP is non-zero, you must not attempt to
  137. use the contents of the \fIpreg\fP structure. If, for example, you pass it to
  138. \fBregexec()\fP, the result is undefined and your program is likely to crash.
  139. .
  140. .
  141. .SH "MATCHING NEWLINE CHARACTERS"
  142. .rs
  143. .sp
  144. This area is not simple, because POSIX and Perl take different views of things.
  145. It is not possible to get PCRE to obey POSIX semantics, but then PCRE was never
  146. intended to be a POSIX engine. The following table lists the different
  147. possibilities for matching newline characters in PCRE:
  148. .sp
  149. Default Change with
  150. .sp
  151. . matches newline no PCRE_DOTALL
  152. newline matches [^a] yes not changeable
  153. $ matches \en at end yes PCRE_DOLLARENDONLY
  154. $ matches \en in middle no PCRE_MULTILINE
  155. ^ matches \en in middle no PCRE_MULTILINE
  156. .sp
  157. This is the equivalent table for POSIX:
  158. .sp
  159. Default Change with
  160. .sp
  161. . matches newline yes REG_NEWLINE
  162. newline matches [^a] yes REG_NEWLINE
  163. $ matches \en at end no REG_NEWLINE
  164. $ matches \en in middle no REG_NEWLINE
  165. ^ matches \en in middle no REG_NEWLINE
  166. .sp
  167. PCRE's behaviour is the same as Perl's, except that there is no equivalent for
  168. PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is no way to stop
  169. newline from matching [^a].
  170. .P
  171. The default POSIX newline handling can be obtained by setting PCRE_DOTALL and
  172. PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE behave exactly as for the
  173. REG_NEWLINE action.
  174. .
  175. .
  176. .SH "MATCHING A PATTERN"
  177. .rs
  178. .sp
  179. The function \fBregexec()\fP is called to match a compiled pattern \fIpreg\fP
  180. against a given \fIstring\fP, which is by default terminated by a zero byte
  181. (but see REG_STARTEND below), subject to the options in \fIeflags\fP. These can
  182. be:
  183. .sp
  184. REG_NOTBOL
  185. .sp
  186. The PCRE_NOTBOL option is set when calling the underlying PCRE matching
  187. function.
  188. .sp
  189. REG_NOTEMPTY
  190. .sp
  191. The PCRE_NOTEMPTY option is set when calling the underlying PCRE matching
  192. function. Note that REG_NOTEMPTY is not part of the POSIX standard. However,
  193. setting this option can give more POSIX-like behaviour in some situations.
  194. .sp
  195. REG_NOTEOL
  196. .sp
  197. The PCRE_NOTEOL option is set when calling the underlying PCRE matching
  198. function.
  199. .sp
  200. REG_STARTEND
  201. .sp
  202. The string is considered to start at \fIstring\fP + \fIpmatch[0].rm_so\fP and
  203. to have a terminating NUL located at \fIstring\fP + \fIpmatch[0].rm_eo\fP
  204. (there need not actually be a NUL at that location), regardless of the value of
  205. \fInmatch\fP. This is a BSD extension, compatible with but not specified by
  206. IEEE Standard 1003.2 (POSIX.2), and should be used with caution in software
  207. intended to be portable to other systems. Note that a non-zero \fIrm_so\fP does
  208. not imply REG_NOTBOL; REG_STARTEND affects only the location of the string, not
  209. how it is matched.
  210. .P
  211. If the pattern was compiled with the REG_NOSUB flag, no data about any matched
  212. strings is returned. The \fInmatch\fP and \fIpmatch\fP arguments of
  213. \fBregexec()\fP are ignored.
  214. .P
  215. If the value of \fInmatch\fP is zero, or if the value \fIpmatch\fP is NULL,
  216. no data about any matched strings is returned.
  217. .P
  218. Otherwise,the portion of the string that was matched, and also any captured
  219. substrings, are returned via the \fIpmatch\fP argument, which points to an
  220. array of \fInmatch\fP structures of type \fIregmatch_t\fP, containing the
  221. members \fIrm_so\fP and \fIrm_eo\fP. These contain the offset to the first
  222. character of each substring and the offset to the first character after the end
  223. of each substring, respectively. The 0th element of the vector relates to the
  224. entire portion of \fIstring\fP that was matched; subsequent elements relate to
  225. the capturing subpatterns of the regular expression. Unused entries in the
  226. array have both structure members set to -1.
  227. .P
  228. A successful match yields a zero return; various error codes are defined in the
  229. header file, of which REG_NOMATCH is the "expected" failure code.
  230. .
  231. .
  232. .SH "ERROR MESSAGES"
  233. .rs
  234. .sp
  235. The \fBregerror()\fP function maps a non-zero errorcode from either
  236. \fBregcomp()\fP or \fBregexec()\fP to a printable message. If \fIpreg\fP is not
  237. NULL, the error should have arisen from the use of that structure. A message
  238. terminated by a binary zero is placed in \fIerrbuf\fP. The length of the
  239. message, including the zero, is limited to \fIerrbuf_size\fP. The yield of the
  240. function is the size of buffer needed to hold the whole message.
  241. .
  242. .
  243. .SH MEMORY USAGE
  244. .rs
  245. .sp
  246. Compiling a regular expression causes memory to be allocated and associated
  247. with the \fIpreg\fP structure. The function \fBregfree()\fP frees all such
  248. memory, after which \fIpreg\fP may no longer be used as a compiled expression.
  249. .
  250. .
  251. .SH AUTHOR
  252. .rs
  253. .sp
  254. .nf
  255. Philip Hazel
  256. University Computing Service
  257. Cambridge CB2 3QH, England.
  258. .fi
  259. .
  260. .
  261. .SH REVISION
  262. .rs
  263. .sp
  264. .nf
  265. Last updated: 09 January 2012
  266. Copyright (c) 1997-2012 University of Cambridge.
  267. .fi