1PCREPOSIX(3) Library Functions Manual PCREPOSIX(3)
2
3
4
6 PCRE - Perl-compatible regular expressions.
7
9
10 #include <pcreposix.h>
11
12 int regcomp(regex_t *preg, const char *pattern,
13 int cflags);
14
15 int regexec(regex_t *preg, const char *string,
16 size_t nmatch, regmatch_t pmatch[], int eflags);
17
18 size_t regerror(int errcode, const regex_t *preg,
19 char *errbuf, size_t errbuf_size);
20
21 void regfree(regex_t *preg);
22
24
25 This set of functions provides a POSIX-style API to the PCRE regular
26 expression package. See the pcreapi documentation for a description of
27 PCRE's native API, which contains much additional functionality.
28
29 The functions described here are just wrapper functions that ultimately
30 call the PCRE native API. Their prototypes are defined in the
31 pcreposix.h header file, and on Unix systems the library itself is
32 called pcreposix.a, so can be accessed by adding -lpcreposix to the
33 command for linking an application that uses them. Because the POSIX
34 functions call the native ones, it is also necessary to add -lpcre.
35
36 I have implemented only those option bits that can be reasonably mapped
37 to PCRE native options. In addition, the option REG_EXTENDED is defined
38 with the value zero. This has no effect, but since programs that are
39 written to the POSIX interface often use it, this makes it easier to
40 slot in PCRE as a replacement library. Other POSIX options are not even
41 defined.
42
43 When PCRE is called via these functions, it is only the API that is
44 POSIX-like in style. The syntax and semantics of the regular expres‐
45 sions themselves are still those of Perl, subject to the setting of
46 various PCRE options, as described below. "POSIX-like in style" means
47 that the API approximates to the POSIX definition; it is not fully
48 POSIX-compatible, and in multi-byte encoding domains it is probably
49 even less compatible.
50
51 The header for these functions is supplied as pcreposix.h to avoid any
52 potential clash with other POSIX libraries. It can, of course, be
53 renamed or aliased as regex.h, which is the "correct" name. It provides
54 two structure types, regex_t for compiled internal forms, and reg‐
55 match_t for returning captured substrings. It also defines some con‐
56 stants whose names start with "REG_"; these are used for setting
57 options and identifying error codes.
58
60
61 The function regcomp() is called to compile a pattern into an internal
62 form. The pattern is a C string terminated by a binary zero, and is
63 passed in the argument pattern. The preg argument is a pointer to a
64 regex_t structure that is used as a base for storing information about
65 the compiled regular expression.
66
67 The argument cflags is either zero, or contains one or more of the bits
68 defined by the following macros:
69
70 REG_DOTALL
71
72 The PCRE_DOTALL option is set when the regular expression is passed for
73 compilation to the native function. Note that REG_DOTALL is not part of
74 the POSIX standard.
75
76 REG_ICASE
77
78 The PCRE_CASELESS option is set when the regular expression is passed
79 for compilation to the native function.
80
81 REG_NEWLINE
82
83 The PCRE_MULTILINE option is set when the regular expression is passed
84 for compilation to the native function. Note that this does not mimic
85 the defined POSIX behaviour for REG_NEWLINE (see the following sec‐
86 tion).
87
88 REG_NOSUB
89
90 The PCRE_NO_AUTO_CAPTURE option is set when the regular expression is
91 passed for compilation to the native function. In addition, when a pat‐
92 tern that is compiled with this flag is passed to regexec() for match‐
93 ing, the nmatch and pmatch arguments are ignored, and no captured
94 strings are returned.
95
96 REG_UTF8
97
98 The PCRE_UTF8 option is set when the regular expression is passed for
99 compilation to the native function. This causes the pattern itself and
100 all data strings used for matching it to be treated as UTF-8 strings.
101 Note that REG_UTF8 is not part of the POSIX standard.
102
103 In the absence of these flags, no options are passed to the native
104 function. This means the the regex is compiled with PCRE default
105 semantics. In particular, the way it handles newline characters in the
106 subject string is the Perl way, not the POSIX way. Note that setting
107 PCRE_MULTILINE has only some of the effects specified for REG_NEWLINE.
108 It does not affect the way newlines are matched by . (they aren't) or
109 by a negative class such as [^a] (they are).
110
111 The yield of regcomp() is zero on success, and non-zero otherwise. The
112 preg structure is filled in on success, and one member of the structure
113 is public: re_nsub contains the number of capturing subpatterns in the
114 regular expression. Various error codes are defined in the header file.
115
117
118 This area is not simple, because POSIX and Perl take different views of
119 things. It is not possible to get PCRE to obey POSIX semantics, but
120 then PCRE was never intended to be a POSIX engine. The following table
121 lists the different possibilities for matching newline characters in
122 PCRE:
123
124 Default Change with
125
126 . matches newline no PCRE_DOTALL
127 newline matches [^a] yes not changeable
128 $ matches \n at end yes PCRE_DOLLARENDONLY
129 $ matches \n in middle no PCRE_MULTILINE
130 ^ matches \n in middle no PCRE_MULTILINE
131
132 This is the equivalent table for POSIX:
133
134 Default Change with
135
136 . matches newline yes REG_NEWLINE
137 newline matches [^a] yes REG_NEWLINE
138 $ matches \n at end no REG_NEWLINE
139 $ matches \n in middle no REG_NEWLINE
140 ^ matches \n in middle no REG_NEWLINE
141
142 PCRE's behaviour is the same as Perl's, except that there is no equiva‐
143 lent for PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is
144 no way to stop newline from matching [^a].
145
146 The default POSIX newline handling can be obtained by setting
147 PCRE_DOTALL and PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE
148 behave exactly as for the REG_NEWLINE action.
149
151
152 The function regexec() is called to match a compiled pattern preg
153 against a given string, which is by default terminated by a zero byte
154 (but see REG_STARTEND below), subject to the options in eflags. These
155 can be:
156
157 REG_NOTBOL
158
159 The PCRE_NOTBOL option is set when calling the underlying PCRE matching
160 function.
161
162 REG_NOTEOL
163
164 The PCRE_NOTEOL option is set when calling the underlying PCRE matching
165 function.
166
167 REG_STARTEND
168
169 The string is considered to start at string + pmatch[0].rm_so and to
170 have a terminating NUL located at string + pmatch[0].rm_eo (there need
171 not actually be a NUL at that location), regardless of the value of
172 nmatch. This is a BSD extension, compatible with but not specified by
173 IEEE Standard 1003.2 (POSIX.2), and should be used with caution in
174 software intended to be portable to other systems. Note that a non-zero
175 rm_so does not imply REG_NOTBOL; REG_STARTEND affects only the location
176 of the string, not how it is matched.
177
178 If the pattern was compiled with the REG_NOSUB flag, no data about any
179 matched strings is returned. The nmatch and pmatch arguments of
180 regexec() are ignored.
181
182 Otherwise,the portion of the string that was matched, and also any cap‐
183 tured substrings, are returned via the pmatch argument, which points to
184 an array of nmatch structures of type regmatch_t, containing the mem‐
185 bers rm_so and rm_eo. These contain the offset to the first character
186 of each substring and the offset to the first character after the end
187 of each substring, respectively. The 0th element of the vector relates
188 to the entire portion of string that was matched; subsequent elements
189 relate to the capturing subpatterns of the regular expression. Unused
190 entries in the array have both structure members set to -1.
191
192 A successful match yields a zero return; various error codes are
193 defined in the header file, of which REG_NOMATCH is the "expected"
194 failure code.
195
197
198 The regerror() function maps a non-zero errorcode from either regcomp()
199 or regexec() to a printable message. If preg is not NULL, the error
200 should have arisen from the use of that structure. A message terminated
201 by a binary zero is placed in errbuf. The length of the message,
202 including the zero, is limited to errbuf_size. The yield of the func‐
203 tion is the size of buffer needed to hold the whole message.
204
206
207 Compiling a regular expression causes memory to be allocated and asso‐
208 ciated with the preg structure. The function regfree() frees all such
209 memory, after which preg may no longer be used as a compiled expres‐
210 sion.
211
213
214 Philip Hazel
215 University Computing Service
216 Cambridge CB2 3QH, England.
217
219
220 Last updated: 05 April 2008
221 Copyright (c) 1997-2008 University of Cambridge.
222
223
224
225 PCREPOSIX(3)