ViewVC logotype

Contents of /code/trunk/doc/pcreposix.3

Parent Directory Parent Directory | Revision Log Revision Log

Revision 518 - (hide annotations) (download)
Tue May 18 15:47:01 2010 UTC (4 years, 11 months ago) by ph10
File size: 10591 byte(s)
Added PCRE_UCP and related stuff to make \w etc use Unicode properties.

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


Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

ViewVC Help
Powered by ViewVC 1.1.12