[Previous] [Contents] [Next]

regexec()

Compare a string with a compiled regular expression

Synopsis:

#include <regex.h>

int regexec( const regex_t * preg,
             const char * string,
             size_t nmatch,
             regmatch_t * pmatch,
             int eflags );

Library:

libc

Description:

The regexec() function compares string against the compiled regular expression preg. If regexec() finds a match it returns zero; otherwise, it returns nonzero.

The preg argument must have been successfully initialized by regcomp(). It represents a compiled form of either a Basic Regular Expression or Extended Regular Expression. These classes are rigorously defined in IEEE P1003.2, Regular Expression Notation, and are summarized in the documentation for regcomp().

Matches are recorded in the pmatch array, with nmatch specifying the maximum number of matches to record. The regmatch_t structure contains the pointer members rm_sp and rm_ep, which are updated to identify the start and end of each matched substring. The pointers in pmatch[0] identify the substring corresponding to the entire expression, while those in pmatch[1...nmatch] identify up to the first nmatch subexpressions. Unused elements of the pmatch array are set to NULL.


Note:

You can disable the recording of substrings by either specifying REG_NOSUB in regcomp(), or by setting nmatch to zero.


The eflags argument specifies execution parameters to regexec(). For example, you may need to call regexec() multiple times if the line you're processing is too large to fit into string. The eflags argument is the bitwise inclusive OR of zero or more of the following flags:

REG_NOTBOL
The string argument doesn't point to the beginning of a line.
REG_NOTEOL
The end of string isn't the end of a line.

Returns:

0
The string argument matches preg.
<>0
A match wasn't found, or an error occurred (use regerror() to get an explanation).

Examples:

See regcomp().

Classification:

POSIX 1003.1a

Safety:
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes

Author:

Henry Spencer. For copyright information, see Third-Party Copyright Notices in this reference.

See also:

regcomp(), regerror(), regfree()


[Previous] [Contents] [Next]