Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022



re_comp, re_exec - compile and execute regular expressions


#include <re_comp.h>

char *re_comp(const char *string);
int re_exec(const char *string);


The re_comp() function converts a regular expression string (RE) into an internal form suitable for pattern matching. The re_exec() function compares the string pointed to by the string argument with the last regular expression passed to re_comp().

If re_comp() is called with a null pointer argument, the current regular expression remains unchanged.

Strings passed to both re_comp() and re_exec() must be terminated by a null byte, and may include NEWLINE characters.

The re_comp() and re_exec() functions support simple regular expressions, which are defined on the regexp(7) manual page. The regular expressions of the form \{m\}, \{ m,\}, or \{m,n\} are not supported.

Return Values

The re_comp() function returns a null pointer when the string pointed to by the string argument is successfully converted. Otherwise, a pointer to one of the following error message strings is returned:

No previous regular expression
Regular expression too long
unmatched \ (
missing ]
too many \ ( \ ) pairs
unmatched \ )

Upon successful completion, re_exec() returns 1 if string matches the last compiled regular expression. Otherwise, re_exec() returns 0 if string fails to match the last compiled regular expression, and −1 if the compiled regular expression is invalid (indicating an internal error).


No errors are defined.


For portability to implementations conforming to X/Open standards prior to SUS, regcomp(3C) and regexec(3C) are preferred to these functions. See standards(7).

See Also

grep(1), regcmp(1), regcmp(3C), regcomp(3C), regexec(3C), regexpr(3GEN), regexp(7), standards(7)