REGEX(3) | Linux Programmer's Manual | REGEX(3) |
regcomp, regexec, regerror, regfree - POSIX regex functions
#include <sys/types.h> #include <regex.h>
int regcomp(regex_t *preg, const char *regex, int cflags);
int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
void regfree(regex_t *preg);
regcomp() is used to compile a regular expression into a form that is suitable for subsequent regexec() searches.
regcomp() is supplied with preg, a pointer to a pattern buffer storage area; regex, a pointer to the null-terminated string and cflags, flags used to determine the type of compilation.
All regular expression searching must be done via a compiled pattern buffer, thus regexec() must always be supplied with the address of a regcomp() initialized pattern buffer.
cflags may be the bitwise-or of zero or more of the following:
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.
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.
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 ('\0').
Supplying regfree() with a precompiled pattern buffer, preg will free the memory allocated to the pattern buffer by the compiling process, regcomp().
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.
The following errors can be returned by regcomp():
For an explanation of the terms used in this section, see attributes(7).
Interface | Attribute | Value |
regcomp (), regexec () | Thread safety | MT-Safe locale |
regerror () | Thread safety | MT-Safe env |
regfree () | Thread safety | MT-Safe |
POSIX.1-2001, POSIX.1-2008.
The glibc manual section, Regular Expressions
This page is part of release 4.16 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 https://www.kernel.org/doc/man-pages/.
2017-09-15 | GNU |