United States-English |
|
|
HP-UX Reference > Rregcomp(3C)HP-UX 11i Version 3: February 2007 |
|
NAMEregcomp(), regerror(), regexec(), regfree() — regular expression matching routines SYNOPSIS#include <regex.h> int regcomp(regex_t *__restrict preg, const char *__restrict pattern, int cflags); int regexec( const regex_t *__restrict preg, const char *__restrict string, size_t nmatch, regmatch_t pmatch[__restrict], int eflags ); void regfree(regex_t *preg); size_t regerror( int errcode, const regex_t *__restrict preg, char *__restrict errbuf, size_t errbuf_size ); DESCRIPTIONThese functions interpret regular expressions as described in regexp(5). They support both basic and extended regular expressions. The structures regex_t and regmatch_t are defined in the header <regex.h>. The regex_t structure contains at least the following member (use of other members results in non-portable code):
The regmatch_t structure contains at least the following members:
regcomp() compiles the regular expression specified by the pattern argument and places the results in the structure pointed to by preg. The cflags argument is the bit-wise logical OR of zero or more of the following flags (defined in <regex.h>):
The default regular expression type for pattern is Basic Regular Expression. The application can specify Extended Regular Expressions by using the REG_EXTENDED cflags value. If the function regcomp() succeeds, it returns zero; otherwise it returns a non-zero value indicating the error. If regcomp() succeeds, and if the REG_NOSUB flag was not set in cflags, regcomp() sets re_nsub to the number of parenthesized subexpressions (delimited by \( and \) in basic regular expressions or ( and ) in extended regular expressions) found in pattern. regexec() matches the null-terminated string specified by string against the compiled regular expression preg initialized by a previous call to regcomp(). If it finds a match, regexec() returns zero; otherwise it returns non-zero indicating either no match or an error. The eflags argument is the bit-wise logical OR of the following flags:
If nmatch is not zero, and REG_NOSUB was not set in the cflags argument to regcomp(), then regexec() fills in the pmatch array with byte offsets to the substrings of string that correspond to the parenthesized subexpressions of pattern: pmatch[i].rm_so is the byte offset of the beginning and pmatch[i].rm_eo is the byte offset one byte past the end of the substring i. (Subexpression i begins at the ith matched left parenthesis, counting from 1). Offsets in pmatch[0] identify the substring that corresponds to the entire regular expression. Unused elements of pmatch are set to -1. If there are more than nmatch subexpressions in pattern (pattern itself counts as a subexpression), regexec() still does the match, but only records the first nmatch substrings. When matching a regular expression, any given parenthesized subexpression of pattern might participate in the match of several different substrings of string, or it might not match any substring, even though the pattern as a whole did match. The following explains which substrings are reported in pmatch when matching regular expressions:
If REG_NOSUB was set in cflags in the call to regcomp(), and nmatch is not zero in the call to regexec(), the content of the pmatch array is unspecified. regfree() frees any memory allocated by regcomp() associated with preg. If the preg argument to regexec() or regfree() is not a compiled regular expression returned by regcomp(), the result is undefined. A preg can no longer be treated as a compiled regular expression after it is given to regfree(). regerror() provides a mapping from error codes returned by regcomp() and regexec() to printable strings. regerror() generates a string corresponding to the value of the errcode parameter, which was the last non-zero value returned by regcomp() or regexec() with the given value of preg. The errcode parameter can take on any of the error values defined in <regex.h>. If errbuf_size is not zero, regerror() copies an appropriate error message into the buffer specified by errbuf. If the error message (including the terminating null) cannot fit in the buffer, it is truncated to errbuf_size - 1 bytes and null terminated. If errbuf_size is zero, the errbuf parameter is ignored, but the return value is as defined below. regerror() returns the size of the buffer (including terminating null) that is required to hold the entire error message. EXTERNAL INFLUENCESLocaleThe LC_COLLATE category determines the collating sequence used in compiling and executing regular expressions. The LC_CTYPE category determines the interpretation of text as single and/or multi-byte characters, the characters matched by character-class expressions in regular expressions, and the opposite-case counterpart for each character. RETURN VALUEregcomp() returns zero for success and non-zero for an invalid expression or other failure. regexec() returns zero if it finds a match and non-zero for no match or other failure. ERRORSIf regcomp() or regexec() detects one of the error conditions listed below, it returns the corresponding non-zero error code. The error codes are defined in the header <regex.h>.
EXAMPLES/* match string against the extended regular expression in pattern, treating errors as no match. Return 1 for match, 0 for no match. Print an error message if an error occurs. */ int match(string, pattern) char *string; char *pattern; { int i; regex_t re; char buf[256]; i=regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB); if (i != 0) { (void)regerror(i,&re,buf,sizeof buf); printf("%s\n",buf); return(0); /* report error */ } i = regexec(&re, string, (size_t) 0, NULL, 0); regfree(&re); if (i != 0) { (void)regerror(i,&re,buf,sizeof buf); printf("%s\n",buf); return(0); /* report error */ } return(1); } The following demonstrates how the REG_NOTBOL flag could be used with regexec() to find all substrings in a line that match a pattern supplied by a user. (void) regcomp(&re, pattern, 0); /* look for first match at start of line */ error = regexec(&re, &buffer[0], 1, &pm, 0); while (error == 0) { /* while matches found */ /* find next match on line */ error = regexec(&re, &buffer[pm.rm_eo], 1, &pm, REG_NOTBOL); } |
Printable version | ||
|