Open System Services Library Calls Reference Manual (G06.29+, H06.08+, J06.03+)

OSS Library Calls (n - r) regcomp(3)
return;
}
error = regexec(&preg, string, 1, &pmatch, 0);
if (error == REG_NOMATCH) {
printf("No matches in string\n");
return;
} else if (error != 0) {
msize = regerror(error, &preg, message, SLENGTH);
printf("%s\n", message);
if (msize > SLENGTH)
printf("Additional text lost\n");
return;
};
count = 1;
start_search = string + pmatch.rm_eo;
while (error == 0) {
error =
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 finds 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;
527187-017 Hewlett-Packard Company 5167