Open System Services Library Calls Reference Manual (G06.28+, H06.05+)

OSS Library Calls (n - r) regcomp(3)
regexec(&preg, start_search, 1, &pmatch,
|
REG_NOTBOL);
|
start_search = start_search + pmatch.rm_eo;
|
count++;
|
};
|
count--;
|
printf("There are %i matches\n", count);
|
regfree(&preg);
|
} |
|
2. ||
The following example nds out which subexpressions in the regular expression have |
matches in the string. This example uses the same main() program as the preceding |
example. This example does not specify REG_EXTENDED in the call to regcomp() |
and, consequently, uses basic regular expressions, not extended regular expressions. |
#define MAX_MATCH 10
|
int match(char *pattern, char *string)
|
{
|
char message[SLENGTH];
|
char *start_search;
|
int error, msize, count, matches_tocheck;
|
regex_t preg;
|
regmatch_t pmatch[MAX_MATCH];
|
error = regcomp(&preg, pattern, REG_ICASE);
|
if (error) {
|
msize = regerror(error, &preg, message, SLENGTH);
|
printf("regcomp: %s\n", message);
|
if (msize > SLENGTH)
|
printf("Additional text lost\n");
|
return;
|
}
|
if (preg.re_nsub > MAX_MATCH) {
|
printf("There are %i subexpressions, checking %i\n",
|
preg.re_nsub, MAX_MATCH);
|
matches_tocheck = MAX_MATCH;
|
} else {
|
printf(
|
"There are %i subexpressions in re\n", preg.re_nsub);
|
matches_tocheck = preg.re_nsub;
|
} |
error = regexec(&preg, string, MAX_MATCH, &pmatch[0], 0);
|
if (error == REG_NOMATCH) {
|
printf("String did not contain match for entire re\n");
|
return;
|
} else if (error != 0) { |
msize = regerror(error, &preg, message, SLENGTH); |
printf("regexe: %s\n", message); |
if (msize > SLENGTH) |
printf("Additional text lost\n"); |
return; |
527187-007 Hewlett-Packard Company 595