Compiles a specified basic or extended regular expression into an executable string.
#include <regex.h>
int regcomp (Preg, Pattern, CFlags) const char *Preg; const char *Pattern; int CFlags;
The regcomp subroutine compiles the basic or extended regular expression specified by the Pattern parameter and places the output in the structure pointed to by the Preg parameter.
If successful, the regcomp subroutine returns a value of 0. Otherwise, it returns another value indicating the type of failure, and the content of the Preg parameter is undefined.
The following macro names for error codes may be written to the errno global variable under error conditions:
REG_BADPAT | Indicates a basic or extended regular expression that is not valid. |
REG_ECOLLATE | Indicates a collating element referenced that is not valid. |
REG_ECTYPE | Indicates a character class-type reference that is not valid. |
REG_EESCAPE | Indicates a trailing \ in pattern. |
REG_ESUBREG | Indicates a number in \digit is not valid or in error. |
REG_EBRACK | Indicates a [] imbalance. |
REG_EPAREN | Indicates a \(\) or () imbalance. |
REG_EBRACE | Indicates a \{\} imbalance. |
REG_BADBR | Indicates the content of \{\} is unusable: not a number, number too large, more than two numbers, or first number larger than second. |
REG_ERANGE | Indicates an unusable end point in range expression. |
REG_ESPACE | Indicates out of memory. |
REG_BADRPT | Indicates a ? (question mark), * (asterisk), or + (plus sign) not preceded by valid basic or extended regular expression. |
If the regcomp subroutine detects an illegal basic or extended regular expression, it can return either the REG_BADPAT error code or another that more precisely describes the error.
The following example illustrates how to match a string (specified in the string parameter) against an extended regular expression (specified in the Pattern parameter):
#include <sys/types.h> #include <regex.h> int match(char *string, char *pattern) { int status; regex_t re;
if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) { return(0) ; /* report error */ } status = regexec(&re, string, (size_t) 0, NULL, 0); regfree(&re); if (status != 0) { return(0) ; /* report error */ } return(1); }
In the preceding example, errors are treated as no match. When there is no match or error, the calling process can get details by calling the regerror subroutine.
This subroutine is part of Base Operating System (BOS) Runtime.
The regerror subroutine, regexec subroutine, regfree subroutine.
Subroutines Overview and Understanding Internationalized Regular Expression Subroutines in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.