NAME
regcomp, regexec, regerror, regfree - POSIX regex functions
SYNOPSIS
#include <sys/types.h>
#include <regex.h>
I int regcomp(regex_t * preg , const char * regex ,
I int cflags );
I int regexec(const regex_t * preg , const char * string ,
I size_t nmatch , regmatch_t pmatch[] ,
I int eflags );
I size_t regerror(int errcode ,
I const regex_t * preg , char * errbuf ,
I size_t errbuf_size );
I void regfree(regex_t * preg );
DESCRIPTION
POSIX Regex Compiling
R regcomp ()
is used to compile a regular expression into a form that is suitable
for subsequent
R regexec ()
searches.
R regcomp ()
is supplied with
R preg ,
a pointer to a pattern buffer storage area;
R regex ,
a pointer to the null-terminated string and
R cflags ,
flags used to determine the type of compilation.
All regular expression searching must be done via a compiled pattern
buffer, thus
R regexec ()
must always be supplied with the address of a
R regcomp ()
initialized pattern buffer.
cflags
may be the
bitwise-or
of one or more of the following:
REG_EXTENDED
Use
POSIX
Extended Regular Expression syntax when interpreting
R regex .
If not set,
POSIX
Basic Regular Expression syntax is used.
REG_ICASE
Do not differentiate case.
Subsequent
R regexec ()
searches using this pattern buffer will be case insensitive.
REG_NOSUB
Support for substring addressing of matches is not required.
The
nmatch
and
pmatch
parameters to
R regexec ()
are ignored if the pattern buffer supplied was compiled with this flag set.
REG_NEWLINE
Match-any-character operators don't match a newline.
A non-matching list
([^...])
not containing a newline does not match a newline.
Match-beginning-of-line operator
(^)
matches the empty string immediately after a newline, regardless of
whether
R eflags ,
the execution flags of
R regexec (),
contains
R REG_NOTBOL .
Match-end-of-line operator
($)
matches the empty string immediately before a newline, regardless of
whether
R eflags
contains
R REG_NOTEOL .
POSIX Regex Matching
R regexec ()
is used to match a null-terminated string
against the precompiled pattern buffer,
R preg .
nmatch
and
pmatch
are used to provide information regarding the location of any matches.
eflags
may be the
bitwise-or
of one or both of
REG_NOTBOL
and
REG_NOTEOL
which cause changes in matching behavior described below.
REG_NOTBOL
The match-beginning-of-line operator always fails to match (but see the
compilation flag
REG_NEWLINE
above)
This flag may be used when different portions of a string are passed to
R regexec ()
and the beginning of the string should not be interpreted as the
beginning of the line.
REG_NOTEOL
The match-end-of-line operator always fails to match (but see the
compilation flag
REG_NEWLINE
above)
BYTE OFFSETS
Unless
REG_NOSUB
was set for the compilation of the pattern buffer, it is possible to
obtain substring match addressing information.
pmatch
must be dimensioned to have at least
nmatch
elements.
These are filled in by
R regexec ()
with substring match addresses.
Any unused structure elements will contain the value -1.
The
regmatch_t
structure which is the type of
pmatch
is defined in
R regex.h .
typedef struct {
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;
Each
rm_so
element that is not -1 indicates the start offset of the next largest
substring match within the string.
The relative
rm_eo
element indicates the end offset of the match.
Posix Error Reporting
R regerror ()
is used to turn the error codes that can be returned by both
R regcomp ()
and
R regexec ()
into error message strings.
R regerror ()
is passed the error code,
R errcode ,
the pattern buffer,
R preg ,
a pointer to a character string buffer,
R errbuf ,
and the size of the string buffer,
R errbuf_size .
It returns the size of the
errbuf
required to contain the null-terminated error message string.
If both
errbuf
and
errbuf_size
are non-zero,
errbuf
is filled in with the first
errbuf_size - 1
characters of the error message and a terminating null.
POSIX Pattern Buffer Freeing
Supplying
R regfree ()
with a precompiled pattern buffer,
preg
will free the memory allocated to the pattern buffer by the compiling
process,
R regcomp ().
RETURN VALUE
R regcomp ()
returns zero for a successful compilation or an error code for failure.
R regexec ()
returns zero for a successful match or
REG_NOMATCH
for failure.
ERRORS
The following errors can be returned by
R regcomp ():
REG_BADBR
Invalid use of back reference operator.
REG_BADPAT
Invalid use of pattern operators such as group or list.
REG_BADRPT
Invalid use of repetition operators such as using
`*'
as the first character.
REG_EBRACE
Un-matched brace interval operators.
REG_EBRACK
Un-matched bracket list operators.
REG_ECOLLATE
Invalid collating element.
REG_ECTYPE
Unknown character class name.
REG_EEND
Non specific error.
This is not defined by POSIX.2.
REG_EESCAPE
Trailing backslash.
REG_EPAREN
Un-matched parenthesis group operators.
REG_ERANGE
Invalid use of the range operator, eg. the ending point of the range
occurs prior to the starting point.
REG_ESIZE
Compiled regular expression requires a pattern buffer larger than 64Kb.
This is not defined by POSIX.2.
REG_ESPACE
The regex routines ran out of memory.
REG_ESUBREG
Invalid back reference to a subexpression.
CONFORMING TO
POSIX.1-2001.
SEE ALSO