| 1 |
nigel |
41 |
.TH PCRE 3 |
| 2 |
|
|
.SH NAME |
| 3 |
nigel |
63 |
PCRE - Perl-compatible regular expressions. |
| 4 |
nigel |
75 |
.SH "SYNOPSIS OF POSIX API" |
| 5 |
|
|
.rs |
| 6 |
|
|
.sp |
| 7 |
nigel |
41 |
.B #include <pcreposix.h> |
| 8 |
|
|
.PP |
| 9 |
|
|
.SM |
| 10 |
|
|
.br |
| 11 |
nigel |
75 |
.B int regcomp(regex_t *\fIpreg\fP, const char *\fIpattern\fP, |
| 12 |
nigel |
41 |
.ti +5n |
| 13 |
nigel |
75 |
.B int \fIcflags\fP); |
| 14 |
nigel |
41 |
.PP |
| 15 |
|
|
.br |
| 16 |
nigel |
75 |
.B int regexec(regex_t *\fIpreg\fP, const char *\fIstring\fP, |
| 17 |
nigel |
41 |
.ti +5n |
| 18 |
nigel |
75 |
.B size_t \fInmatch\fP, regmatch_t \fIpmatch\fP[], int \fIeflags\fP); |
| 19 |
nigel |
41 |
.PP |
| 20 |
|
|
.br |
| 21 |
nigel |
75 |
.B size_t regerror(int \fIerrcode\fP, const regex_t *\fIpreg\fP, |
| 22 |
nigel |
41 |
.ti +5n |
| 23 |
nigel |
75 |
.B char *\fIerrbuf\fP, size_t \fIerrbuf_size\fP); |
| 24 |
nigel |
41 |
.PP |
| 25 |
|
|
.br |
| 26 |
nigel |
75 |
.B void regfree(regex_t *\fIpreg\fP); |
| 27 |
|
|
. |
| 28 |
nigel |
41 |
.SH DESCRIPTION |
| 29 |
nigel |
63 |
.rs |
| 30 |
|
|
.sp |
| 31 |
nigel |
41 |
This set of functions provides a POSIX-style API to the PCRE regular expression |
| 32 |
nigel |
63 |
package. See the |
| 33 |
|
|
.\" HREF |
| 34 |
nigel |
75 |
\fBpcreapi\fP |
| 35 |
nigel |
63 |
.\" |
| 36 |
nigel |
77 |
documentation for a description of PCRE's native API, which contains much |
| 37 |
|
|
additional functionality. |
| 38 |
nigel |
75 |
.P |
| 39 |
nigel |
41 |
The functions described here are just wrapper functions that ultimately call |
| 40 |
nigel |
75 |
the PCRE native API. Their prototypes are defined in the \fBpcreposix.h\fP |
| 41 |
nigel |
63 |
header file, and on Unix systems the library itself is called |
| 42 |
nigel |
75 |
\fBpcreposix.a\fP, so can be accessed by adding \fB-lpcreposix\fP to the |
| 43 |
|
|
command for linking an application that uses them. Because the POSIX functions |
| 44 |
|
|
call the native ones, it is also necessary to add \fB-lpcre\fP. |
| 45 |
|
|
.P |
| 46 |
nigel |
43 |
I have implemented only those option bits that can be reasonably mapped to PCRE |
| 47 |
|
|
native options. In addition, the options REG_EXTENDED and REG_NOSUB are defined |
| 48 |
|
|
with the value zero. They have no effect, but since programs that are written |
| 49 |
|
|
to the POSIX interface often use them, this makes it easier to slot in PCRE as |
| 50 |
|
|
a replacement library. Other POSIX options are not even defined. |
| 51 |
nigel |
75 |
.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 |
nigel |
75 |
.P |
| 67 |
|
|
.SH "COMPILING A PATTERN" |
| 68 |
nigel |
63 |
.rs |
| 69 |
|
|
.sp |
| 70 |
nigel |
75 |
The function \fBregcomp()\fP is called to compile a pattern into an |
| 71 |
nigel |
41 |
internal form. The pattern is a C string terminated by a binary zero, and |
| 72 |
nigel |
75 |
is passed in the argument \fIpattern\fP. The \fIpreg\fP argument is a pointer |
| 73 |
|
|
to a \fBregex_t\fP structure that is used as a base for storing information |
| 74 |
|
|
about the compiled expression. |
| 75 |
|
|
.P |
| 76 |
|
|
The argument \fIcflags\fP is either zero, or contains one or more of the bits |
| 77 |
nigel |
41 |
defined by the following macros: |
| 78 |
nigel |
75 |
.sp |
| 79 |
nigel |
77 |
REG_DOTALL |
| 80 |
|
|
.sp |
| 81 |
|
|
The PCRE_DOTALL option is set when the expression is passed for compilation to |
| 82 |
|
|
the native function. Note that REG_DOTALL is not part of the POSIX standard. |
| 83 |
|
|
.sp |
| 84 |
nigel |
41 |
REG_ICASE |
| 85 |
nigel |
75 |
.sp |
| 86 |
nigel |
41 |
The PCRE_CASELESS option is set when the expression is passed for compilation |
| 87 |
|
|
to the native function. |
| 88 |
nigel |
75 |
.sp |
| 89 |
nigel |
41 |
REG_NEWLINE |
| 90 |
nigel |
75 |
.sp |
| 91 |
nigel |
41 |
The PCRE_MULTILINE option is set when the expression is passed for compilation |
| 92 |
nigel |
75 |
to the native function. Note that this does \fInot\fP mimic the defined POSIX |
| 93 |
nigel |
63 |
behaviour for REG_NEWLINE (see the following section). |
| 94 |
nigel |
75 |
.P |
| 95 |
nigel |
49 |
In the absence of these flags, no options are passed to the native function. |
| 96 |
|
|
This means the the regex is compiled with PCRE default semantics. In |
| 97 |
|
|
particular, the way it handles newline characters in the subject string is the |
| 98 |
|
|
Perl way, not the POSIX way. Note that setting PCRE_MULTILINE has only |
| 99 |
nigel |
75 |
\fIsome\fP of the effects specified for REG_NEWLINE. It does not affect the way |
| 100 |
nigel |
63 |
newlines are matched by . (they aren't) or by a negative class such as [^a] |
| 101 |
|
|
(they are). |
| 102 |
nigel |
75 |
.P |
| 103 |
|
|
The yield of \fBregcomp()\fP is zero on success, and non-zero otherwise. The |
| 104 |
|
|
\fIpreg\fP structure is filled in on success, and one member of the structure |
| 105 |
|
|
is public: \fIre_nsub\fP contains the number of capturing subpatterns in |
| 106 |
nigel |
41 |
the regular expression. Various error codes are defined in the header file. |
| 107 |
nigel |
75 |
. |
| 108 |
|
|
. |
| 109 |
|
|
.SH "MATCHING NEWLINE CHARACTERS" |
| 110 |
nigel |
63 |
.rs |
| 111 |
|
|
.sp |
| 112 |
|
|
This area is not simple, because POSIX and Perl take different views of things. |
| 113 |
|
|
It is not possible to get PCRE to obey POSIX semantics, but then PCRE was never |
| 114 |
|
|
intended to be a POSIX engine. The following table lists the different |
| 115 |
|
|
possibilities for matching newline characters in PCRE: |
| 116 |
nigel |
75 |
.sp |
| 117 |
nigel |
63 |
Default Change with |
| 118 |
nigel |
75 |
.sp |
| 119 |
nigel |
63 |
. matches newline no PCRE_DOTALL |
| 120 |
|
|
newline matches [^a] yes not changeable |
| 121 |
nigel |
75 |
$ matches \en at end yes PCRE_DOLLARENDONLY |
| 122 |
|
|
$ matches \en in middle no PCRE_MULTILINE |
| 123 |
|
|
^ matches \en in middle no PCRE_MULTILINE |
| 124 |
|
|
.sp |
| 125 |
nigel |
63 |
This is the equivalent table for POSIX: |
| 126 |
nigel |
75 |
.sp |
| 127 |
nigel |
63 |
Default Change with |
| 128 |
nigel |
75 |
.sp |
| 129 |
|
|
. matches newline yes REG_NEWLINE |
| 130 |
|
|
newline matches [^a] yes REG_NEWLINE |
| 131 |
|
|
$ matches \en at end no REG_NEWLINE |
| 132 |
|
|
$ matches \en in middle no REG_NEWLINE |
| 133 |
|
|
^ matches \en in middle no REG_NEWLINE |
| 134 |
|
|
.sp |
| 135 |
nigel |
63 |
PCRE's behaviour is the same as Perl's, except that there is no equivalent for |
| 136 |
nigel |
75 |
PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is no way to stop |
| 137 |
nigel |
63 |
newline from matching [^a]. |
| 138 |
nigel |
75 |
.P |
| 139 |
nigel |
63 |
The default POSIX newline handling can be obtained by setting PCRE_DOTALL and |
| 140 |
nigel |
75 |
PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE behave exactly as for the |
| 141 |
nigel |
63 |
REG_NEWLINE action. |
| 142 |
nigel |
75 |
. |
| 143 |
|
|
. |
| 144 |
|
|
.SH "MATCHING A PATTERN" |
| 145 |
nigel |
63 |
.rs |
| 146 |
|
|
.sp |
| 147 |
nigel |
75 |
The function \fBregexec()\fP is called to match a compiled pattern \fIpreg\fP |
| 148 |
|
|
against a given \fIstring\fP, which is terminated by a zero byte, subject to |
| 149 |
|
|
the options in \fIeflags\fP. These can be: |
| 150 |
|
|
.sp |
| 151 |
nigel |
41 |
REG_NOTBOL |
| 152 |
nigel |
75 |
.sp |
| 153 |
nigel |
41 |
The PCRE_NOTBOL option is set when calling the underlying PCRE matching |
| 154 |
|
|
function. |
| 155 |
nigel |
75 |
.sp |
| 156 |
nigel |
41 |
REG_NOTEOL |
| 157 |
nigel |
75 |
.sp |
| 158 |
nigel |
41 |
The PCRE_NOTEOL option is set when calling the underlying PCRE matching |
| 159 |
|
|
function. |
| 160 |
nigel |
75 |
.P |
| 161 |
nigel |
41 |
The portion of the string that was matched, and also any captured substrings, |
| 162 |
nigel |
75 |
are returned via the \fIpmatch\fP argument, which points to an array of |
| 163 |
|
|
\fInmatch\fP structures of type \fIregmatch_t\fP, containing the members |
| 164 |
|
|
\fIrm_so\fP and \fIrm_eo\fP. These contain the offset to the first character of |
| 165 |
nigel |
41 |
each substring and the offset to the first character after the end of each |
| 166 |
|
|
substring, respectively. The 0th element of the vector relates to the entire |
| 167 |
nigel |
75 |
portion of \fIstring\fP that was matched; subsequent elements relate to the |
| 168 |
nigel |
41 |
capturing subpatterns of the regular expression. Unused entries in the array |
| 169 |
|
|
have both structure members set to -1. |
| 170 |
nigel |
75 |
.P |
| 171 |
nigel |
41 |
A successful match yields a zero return; various error codes are defined in the |
| 172 |
|
|
header file, of which REG_NOMATCH is the "expected" failure code. |
| 173 |
nigel |
75 |
. |
| 174 |
|
|
. |
| 175 |
|
|
.SH "ERROR MESSAGES" |
| 176 |
nigel |
63 |
.rs |
| 177 |
|
|
.sp |
| 178 |
nigel |
75 |
The \fBregerror()\fP function maps a non-zero errorcode from either |
| 179 |
|
|
\fBregcomp()\fP or \fBregexec()\fP to a printable message. If \fIpreg\fP is not |
| 180 |
nigel |
41 |
NULL, the error should have arisen from the use of that structure. A message |
| 181 |
nigel |
75 |
terminated by a binary zero is placed in \fIerrbuf\fP. The length of the |
| 182 |
|
|
message, including the zero, is limited to \fIerrbuf_size\fP. The yield of the |
| 183 |
nigel |
41 |
function is the size of buffer needed to hold the whole message. |
| 184 |
nigel |
75 |
. |
| 185 |
|
|
. |
| 186 |
|
|
.SH MEMORY USAGE |
| 187 |
nigel |
63 |
.rs |
| 188 |
|
|
.sp |
| 189 |
nigel |
41 |
Compiling a regular expression causes memory to be allocated and associated |
| 190 |
nigel |
75 |
with the \fIpreg\fP structure. The function \fBregfree()\fP frees all such |
| 191 |
|
|
memory, after which \fIpreg\fP may no longer be used as a compiled expression. |
| 192 |
|
|
. |
| 193 |
|
|
. |
| 194 |
nigel |
41 |
.SH AUTHOR |
| 195 |
nigel |
63 |
.rs |
| 196 |
|
|
.sp |
| 197 |
nigel |
77 |
Philip Hazel |
| 198 |
nigel |
41 |
.br |
| 199 |
|
|
University Computing Service, |
| 200 |
|
|
.br |
| 201 |
|
|
Cambridge CB2 3QG, England. |
| 202 |
nigel |
75 |
.P |
| 203 |
nigel |
63 |
.in 0 |
| 204 |
nigel |
77 |
Last updated: 28 February 2005 |
| 205 |
nigel |
41 |
.br |
| 206 |
nigel |
77 |
Copyright (c) 1997-2005 University of Cambridge. |