Guardian Native C Library Calls Reference Manual (G06.28+, H06.04+)
Guardian Native C Library Calls (n - r) regcomp(3)
EXAMPLES
1. The following example demonstrates how the REG_NOTBOL flag can be used with the
regexec() function to find all substrings in a line that match a pattern supplied by a user.
The main() function in the example accepts two input strings from the user. The
match() function in the example uses regcomp() and regexec() to search for matches.
#include <sys/types.h>
#include <regex.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#define SLENGTH 80
main()
{
char patt[SLENGTH], strng[SLENGTH];
char *eol;
(void)setlocale(LC_ALL, "");
printf("Enter a regular expression:");
fgets(patt, SLENGTH, stdin);
if ((eol = strchr(patt, ’\n’)) != NULL)
*eol = ’\0’; /* Replace newline with null */
else
return; /* Line entered too long */
printf("Enter string to compare\nString: ");
fgets(strng, SLENGTH, stdin);
if ((eol = strchr(strng, ’\n’)) != NULL)
*eol = ’\0’; /* Replace newline with null */
else
return; /* Line entered too long */
match(patt, strng);
}
int match(char *pattern, char *string)
{
char message[SLENGTH];
char *start_search;
int error, msize, count;
regex_t preg;
regmatch_t pmatch;
error = regcomp(&preg, pattern,
REG_ICASE | REG_EXTENDED);
if (error) {
msize = regerror(error, &preg, message, SLENGTH);
printf("%s\n", message);
if (msize > SLENGTH)
printf("Additional text lost\n");
return;
}
error = regexec(&preg, string, 1, &pmatch, 0);
527192-005 Hewlett-Packard Company 5−63