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