1 |
.TH PCRE 3
|
2 |
.SH NAME
|
3 |
pcreposix - POSIX API for Perl-compatible regular expressions.
|
4 |
.SH SYNOPSIS
|
5 |
.B #include <pcreposix.h>
|
6 |
.PP
|
7 |
.SM
|
8 |
.br
|
9 |
.B int regcomp(regex_t *\fIpreg\fR, const char *\fIpattern\fR,
|
10 |
.ti +5n
|
11 |
.B int \fIcflags\fR);
|
12 |
.PP
|
13 |
.br
|
14 |
.B int regexec(regex_t *\fIpreg\fR, const char *\fIstring\fR,
|
15 |
.ti +5n
|
16 |
.B size_t \fInmatch\fR, regmatch_t \fIpmatch\fR[], int \fIeflags\fR);
|
17 |
.PP
|
18 |
.br
|
19 |
.B size_t regerror(int \fIerrcode\fR, const regex_t *\fIpreg\fR,
|
20 |
.ti +5n
|
21 |
.B char *\fIerrbuf\fR, size_t \fIerrbuf_size\fR);
|
22 |
.PP
|
23 |
.br
|
24 |
.B void regfree(regex_t *\fIpreg\fR);
|
25 |
|
26 |
|
27 |
.SH DESCRIPTION
|
28 |
This set of functions provides a POSIX-style API to the PCRE regular expression
|
29 |
package. See \fBpcre (3)\fR for a description of the native API, which contains
|
30 |
additional functionality. The functions described here are just wrapper
|
31 |
functions that ultimately call the native API.
|
32 |
|
33 |
As I am pretty ignorant about POSIX, these functions must be considered as
|
34 |
experimental. I have implemented only those option bits that can be reasonably
|
35 |
mapped to PCRE native options. Other POSIX options are not even defined. It may
|
36 |
be that it is useful to define, but ignore, other options. Feedback from more
|
37 |
knowledgeable folk may cause this kind of detail to change.
|
38 |
|
39 |
When PCRE is called via these functions, it is only the API that is POSIX-like
|
40 |
in style. The syntax and semantics of the regular expressions themselves are
|
41 |
still those of Perl, subject to the setting of various PCRE options, as
|
42 |
described below.
|
43 |
|
44 |
The header for these functions is supplied as \fBpcreposix.h\fR to avoid any
|
45 |
potential clash with other POSIX libraries. It can, of course, be renamed or
|
46 |
aliased as \fBregex.h\fR, which is the "correct" name. It provides two
|
47 |
structure types, \fIregex_t\fR for compiled internal forms, and
|
48 |
\fIregmatch_t\fR for returning captured substrings. It also defines some
|
49 |
constants whose names start with "REG_"; these are used for setting options and
|
50 |
identifying error codes.
|
51 |
|
52 |
|
53 |
.SH COMPILING A PATTERN
|
54 |
|
55 |
The function \fBregcomp()\fR is called to compile a pattern into an
|
56 |
internal form. The pattern is a C string terminated by a binary zero, and
|
57 |
is passed in the argument \fIpattern\fR. The \fIpreg\fR argument is a pointer
|
58 |
to a regex_t structure which is used as a base for storing information about
|
59 |
the compiled expression.
|
60 |
|
61 |
The argument \fIcflags\fR is either zero, or contains one or more of the bits
|
62 |
defined by the following macros:
|
63 |
|
64 |
REG_ICASE
|
65 |
|
66 |
The PCRE_CASELESS option is set when the expression is passed for compilation
|
67 |
to the native function.
|
68 |
|
69 |
REG_NEWLINE
|
70 |
|
71 |
The PCRE_MULTILINE option is set when the expression is passed for compilation
|
72 |
to the native function.
|
73 |
|
74 |
The yield of \fBregcomp()\fR is zero on success, and non-zero otherwise. The
|
75 |
\fIpreg\fR structure is filled in on success, and one member of the structure
|
76 |
is publicized: \fIre_nsub\fR contains the number of capturing subpatterns in
|
77 |
the regular expression. Various error codes are defined in the header file.
|
78 |
|
79 |
|
80 |
.SH MATCHING A PATTERN
|
81 |
The function \fBregexec()\fR is called to match a pre-compiled pattern
|
82 |
\fIpreg\fR against a given \fIstring\fR, which is terminated by a zero byte,
|
83 |
subject to the options in \fIeflags\fR. These can be:
|
84 |
|
85 |
REG_NOTBOL
|
86 |
|
87 |
The PCRE_NOTBOL option is set when calling the underlying PCRE matching
|
88 |
function.
|
89 |
|
90 |
REG_NOTEOL
|
91 |
|
92 |
The PCRE_NOTEOL option is set when calling the underlying PCRE matching
|
93 |
function.
|
94 |
|
95 |
The portion of the string that was matched, and also any captured substrings,
|
96 |
are returned via the \fIpmatch\fR argument, which points to an array of
|
97 |
\fInmatch\fR structures of type \fIregmatch_t\fR, containing the members
|
98 |
\fIrm_so\fR and \fIrm_eo\fR. These contain the offset to the first character of
|
99 |
each substring and the offset to the first character after the end of each
|
100 |
substring, respectively. The 0th element of the vector relates to the entire
|
101 |
portion of \fIstring\fR that was matched; subsequent elements relate to the
|
102 |
capturing subpatterns of the regular expression. Unused entries in the array
|
103 |
have both structure members set to -1.
|
104 |
|
105 |
A successful match yields a zero return; various error codes are defined in the
|
106 |
header file, of which REG_NOMATCH is the "expected" failure code.
|
107 |
|
108 |
|
109 |
.SH ERROR MESSAGES
|
110 |
The \fBregerror()\fR function maps a non-zero errorcode from either
|
111 |
\fBregcomp\fR or \fBregexec\fR to a printable message. If \fIpreg\fR is not
|
112 |
NULL, the error should have arisen from the use of that structure. A message
|
113 |
terminated by a binary zero is placed in \fIerrbuf\fR. The length of the
|
114 |
message, including the zero, is limited to \fIerrbuf_size\fR. The yield of the
|
115 |
function is the size of buffer needed to hold the whole message.
|
116 |
|
117 |
|
118 |
.SH STORAGE
|
119 |
Compiling a regular expression causes memory to be allocated and associated
|
120 |
with the \fIpreg\fR structure. The function \fBregfree()\fR frees all such
|
121 |
memory, after which \fIpreg\fR may no longer be used as a compiled expression.
|
122 |
|
123 |
|
124 |
.SH AUTHOR
|
125 |
Philip Hazel <ph10@cam.ac.uk>
|
126 |
.br
|
127 |
University Computing Service,
|
128 |
.br
|
129 |
New Museums Site,
|
130 |
.br
|
131 |
Cambridge CB2 3QG, England.
|
132 |
.br
|
133 |
Phone: +44 1223 334714
|
134 |
|
135 |
Copyright (c) 1997-1999 University of Cambridge.
|