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