Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4 |
both |
#include <regex.h>
int regexec(const regex_t *preg, const char *string,
size_t nmatch, regmatch_t *pmatch, int eflags);
#define _XOPEN_SOURCE
#include <regex.h>
int regexec(const regex_t *__ preg,
const char *__ string,
size_t nmatch, regmatch_t *__ pmatch, int eflags);
Compares the NULL-terminated string specified by string against the compiled regular expression, preg. (For a description of regular expressions, see Regular expressions.)
preg is a pointer to a compiled regular expression to compare against STRING.
string is a pointer to a string to be matched.
nmatch is the number of sub-expressions to match.
pmatch is an array of offsets into STRING which matched the corresponding sub-expressions in preg.
If nmatch parameter is 0 or REG_NOSUB was set on the call to regcomp(), regexec() ignores the pmatch argument. Otherwise, the pmatch argument points to an array of at least nmatch elements. The regexec() function fills in the elements of the array with offsets of the substrings of STRING that correspond to the parenthesized sub-expressions of the original pattern specified to regcomp(). The 0th element of the array corresponds to the entire pattern. If there are more than nmatch sub-expressions, only the first nmatch-1 are recorded.
If EREG_NOSUB was set when regcomp() was called, the contents of pmatch are unspecified.
If REG_NEWLINE was set when regcomp() was called, newline characters are allowed in STRING.
If a match is found, regexec() returns 0.
If unsuccessful, regexec() returns nonzero indicating either no match or an error.
⁄* CELEBR09
This example compiles an extended regular expression, and
match against a string.
*⁄
#include <regex.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
main() {
regex_t preg;
char *string = "a simple string";
char *pattern = ".*(simple).*";
int rc;
size_t nmatch = 2;
regmatch_t pmatch[2];
if ((rc = regcomp(&preg, pattern, REG_EXTENDED)) != 0) {
printf("regcomp() failed, returning nonzero (%d)\n", rc);
exit(1);
}
if ((rc = regexec(&preg, string, nmatch, pmatch, 0)) != 0) {
printf("failed to ERE match '%s' with '%s',returning %d.\n",
string, pattern, rc);
}
regfree(&preg);
}