POSIX regex matching

regexec()
is used to match a null-terminated string
against the precompiled pattern buffer,
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
regexec()
and the beginning of the string should not be interpreted as the
beginning of the line.

Byte offsets

Unless
REG_NOSUB
was set for the compilation of the pattern buffer, it is possible to
obtain match addressing information.
pmatch
must be dimensioned to have at least
nmatch
elements.
These are filled in by
regexec()
with substring match addresses.
The offsets of the subexpression starting at the
ith
open parenthesis are stored in
pmatch[i].
The entire regular expression's match addresses are stored in
pmatch[0].
(Note that to return the offsets of
N
subexpression matches,
nmatch
must be at least
N+1.)
Any unused structure elements will contain the value -1.

The
regmatch_t
structure which is the type of
pmatch
is defined in
<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,
which is the offset of the first character after the matching text.

POSIX error reporting

regerror()
is used to turn the error codes that can be returned by both
regcomp()
and
regexec()
into error message strings.

regerror()
is passed the error code,
errcode,
the pattern buffer,
preg,
a pointer to a character string buffer,
errbuf,
and the size of the string buffer,
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 nonzero,
errbuf
is filled in with the first
errbuf_size - 1
characters of the error message and a terminating null byte (aq\0aq).

POSIX pattern buffer freeing

Supplying
regfree()
with a precompiled pattern buffer,
preg
will free the memory allocated to the pattern buffer by the compiling
process,
regcomp().

Return Value

regcomp()
returns zero for a successful compilation or an error code for failure.

regexec()
returns zero for a successful match or
REG_NOMATCH
for failure.

Errors

The following errors can be returned by
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 aq*aq
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

Nonspecific 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; for example, 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

Colophon

This page is part of release 3.80 of the Linux
man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
http://www.kernel.org/doc/man-pages/.

License & Copyright

Copyright (C), 1995, Graeme W. Wilford. (Wilf.)
%%%LICENSE_START(VERBATIM)
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.
Since the Linux kernel and libraries are constantly changing, this
manual page may be incorrect or out-of-date. The author(s) assume no
responsibility for errors or omissions, or for damages resulting from
the use of the information contained herein. The author(s) may not
have taken the same level of care in the production of this manual,
which is licensed free of charge, as they might when working
professionally.
Formatted or processed versions of this manual, if unaccompanied by
the source, must acknowledge the copyright and authors of this work.
%%%LICENSE_END
Wed Jun 14 16:10:28 BST 1995 Wilf. (G.Wilford@ee.surrey.ac.uk)
Tiny change in formatting - aeb, 950812
Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
show the synopsis section nicely