regexec() – Searching for Matches

#include <tre/tre.h>
 
int tre_regexec(const regex_t *preg, const char *string, size_t nmatch,
                regmatch_t pmatch[], int eflags);
int tre_regnexec(const regex_t *preg, const char *string, size_t len,
                 size_t nmatch, regmatch_t pmatch[], int eflags);
int tre_regwexec(const regex_t *preg, const wchar_t *string, size_t nmatch,
                 regmatch_t pmatch[], int eflags);
int tre_regwnexec(const regex_t *preg, const wchar_t *string, size_t len,
                  size_t nmatch, regmatch_t pmatch[], int eflags);

The tre_regexec() function matches the null-terminated string against the compiled regexp preg, initialized by a previous call to any one of the regcomp functions. The tre_regnexec() function is like tre_regexec(), but string is not terminated with a null byte. Instead, the len argument is used to give the length of the string, and the string may contain null bytes. The tre_regwexec() and tre_regwnexec() functions work like tre_regexec() and tre_regnexec(), respectively, but take a wide character (wchar_t) string instead of a byte string. The eflags argument is a bitwise OR of zero or more of the following flags:

REG_NOTBOL
When this flag is used, the match-beginning-of-line operator ^ does not match the empty string at the beginning of string. If REG_NEWLINE was used when compiling preg the empty string immediately after a newline character will still be matched.
REG_NOTEOL
When this flag is used, the match-end-of-line operator $ does not match the empty string at the end of string. If REG_NEWLINE was used when compiling preg the empty string immediately before a newline character will still be matched.

These flags are useful when different portions of a string are passed to regexec and the beginning or end of the partial string should not be interpreted as the beginning or end of a line.

If REG_NOSUB was used when compiling preg, nmatch is zero, or pmatch is NULL, then the pmatch argument is ignored. Otherwise, the submatches corresponding to the parenthesized subexpressions are filled in the elements of pmatch, which must be dimensioned to have at least nmatch elements.

The regmatch_t structure contains at least the following fields:

regoff_t rm_so
Offset from start of string to start of substring.
regoff_t rm_eo
Offset from start of string to the first character after the substring.

The length of a submatch can be computed by subtracting rm_eo and rm_so. If a parenthesized subexpression did not participate in a match, the rm_so and rm_eo fields for the corresponding pmatch element are set to -1. Note that when a multibyte character set such as UTF-8 is in effect, the submatch offsets are given as byte offsets, not character offsets. When matching wide character strings, the submatch offsets are always character offsets (and not byte offsets, for example).