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