/[pcre]/code/trunk/doc/pcreposix.3
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.49  
changed lines
  Added in v.432

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12