3 pcreposix - POSIX API for Perl-compatible regular expressions.
\r
5 .B #include <pcreposix.h>
\r
9 .B int regcomp(regex_t *\fIpreg\fR, const char *\fIpattern\fR,
\r
11 .B int \fIcflags\fR);
\r
14 .B int regexec(regex_t *\fIpreg\fR, const char *\fIstring\fR,
\r
16 .B size_t \fInmatch\fR, regmatch_t \fIpmatch\fR[], int \fIeflags\fR);
\r
19 .B size_t regerror(int \fIerrcode\fR, const regex_t *\fIpreg\fR,
\r
21 .B char *\fIerrbuf\fR, size_t \fIerrbuf_size\fR);
\r
24 .B void regfree(regex_t *\fIpreg\fR);
\r
28 This set of functions provides a POSIX-style API to the PCRE regular expression
\r
29 package. See the \fBpcre\fR documentation for a description of the native API,
\r
30 which contains additional functionality.
\r
32 The functions described here are just wrapper functions that ultimately call
\r
33 the native API. Their prototypes are defined in the \fBpcreposix.h\fR header
\r
34 file, and on Unix systems the library itself is called \fBpcreposix.a\fR, so
\r
35 can be accessed by adding \fB-lpcreposix\fR to the command for linking an
\r
36 application which uses them. Because the POSIX functions call the native ones,
\r
37 it is also necessary to add \fR-lpcre\fR.
\r
39 I have implemented only those option bits that can be reasonably mapped to PCRE
\r
40 native options. In addition, the options REG_EXTENDED and REG_NOSUB are defined
\r
41 with the value zero. They have no effect, but since programs that are written
\r
42 to the POSIX interface often use them, this makes it easier to slot in PCRE as
\r
43 a replacement library. Other POSIX options are not even defined.
\r
45 When PCRE is called via these functions, it is only the API that is POSIX-like
\r
46 in style. The syntax and semantics of the regular expressions themselves are
\r
47 still those of Perl, subject to the setting of various PCRE options, as
\r
50 The header for these functions is supplied as \fBpcreposix.h\fR to avoid any
\r
51 potential clash with other POSIX libraries. It can, of course, be renamed or
\r
52 aliased as \fBregex.h\fR, which is the "correct" name. It provides two
\r
53 structure types, \fIregex_t\fR for compiled internal forms, and
\r
54 \fIregmatch_t\fR for returning captured substrings. It also defines some
\r
55 constants whose names start with "REG_"; these are used for setting options and
\r
56 identifying error codes.
\r
59 .SH COMPILING A PATTERN
\r
61 The function \fBregcomp()\fR is called to compile a pattern into an
\r
62 internal form. The pattern is a C string terminated by a binary zero, and
\r
63 is passed in the argument \fIpattern\fR. The \fIpreg\fR argument is a pointer
\r
64 to a regex_t structure which is used as a base for storing information about
\r
65 the compiled expression.
\r
67 The argument \fIcflags\fR is either zero, or contains one or more of the bits
\r
68 defined by the following macros:
\r
72 The PCRE_CASELESS option is set when the expression is passed for compilation
\r
73 to the native function.
\r
77 The PCRE_MULTILINE option is set when the expression is passed for compilation
\r
78 to the native function.
\r
80 In the absence of these flags, no options are passed to the native function.
\r
81 This means the the regex is compiled with PCRE default semantics. In
\r
82 particular, the way it handles newline characters in the subject string is the
\r
83 Perl way, not the POSIX way. Note that setting PCRE_MULTILINE has only
\r
84 \fIsome\fR of the effects specified for REG_NEWLINE. It does not affect the way
\r
85 newlines are matched by . (they aren't) or a negative class such as [^a] (they
\r
88 The yield of \fBregcomp()\fR is zero on success, and non-zero otherwise. The
\r
89 \fIpreg\fR structure is filled in on success, and one member of the structure
\r
90 is publicized: \fIre_nsub\fR contains the number of capturing subpatterns in
\r
91 the regular expression. Various error codes are defined in the header file.
\r
94 .SH MATCHING A PATTERN
\r
95 The function \fBregexec()\fR is called to match a pre-compiled pattern
\r
96 \fIpreg\fR against a given \fIstring\fR, which is terminated by a zero byte,
\r
97 subject to the options in \fIeflags\fR. These can be:
\r
101 The PCRE_NOTBOL option is set when calling the underlying PCRE matching
\r
106 The PCRE_NOTEOL option is set when calling the underlying PCRE matching
\r
109 The portion of the string that was matched, and also any captured substrings,
\r
110 are returned via the \fIpmatch\fR argument, which points to an array of
\r
111 \fInmatch\fR structures of type \fIregmatch_t\fR, containing the members
\r
112 \fIrm_so\fR and \fIrm_eo\fR. These contain the offset to the first character of
\r
113 each substring and the offset to the first character after the end of each
\r
114 substring, respectively. The 0th element of the vector relates to the entire
\r
115 portion of \fIstring\fR that was matched; subsequent elements relate to the
\r
116 capturing subpatterns of the regular expression. Unused entries in the array
\r
117 have both structure members set to -1.
\r
119 A successful match yields a zero return; various error codes are defined in the
\r
120 header file, of which REG_NOMATCH is the "expected" failure code.
\r
124 The \fBregerror()\fR function maps a non-zero errorcode from either
\r
125 \fBregcomp\fR or \fBregexec\fR to a printable message. If \fIpreg\fR is not
\r
126 NULL, the error should have arisen from the use of that structure. A message
\r
127 terminated by a binary zero is placed in \fIerrbuf\fR. The length of the
\r
128 message, including the zero, is limited to \fIerrbuf_size\fR. The yield of the
\r
129 function is the size of buffer needed to hold the whole message.
\r
133 Compiling a regular expression causes memory to be allocated and associated
\r
134 with the \fIpreg\fR structure. The function \fBregfree()\fR frees all such
\r
135 memory, after which \fIpreg\fR may no longer be used as a compiled expression.
\r
139 Philip Hazel <ph10@cam.ac.uk>
\r
141 University Computing Service,
\r
145 Cambridge CB2 3QG, England.
\r
147 Phone: +44 1223 334714
\r
149 Copyright (c) 1997-2000 University of Cambridge.
\r