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