Guardian Native C Library Calls Reference Manual (G06.29+, H06.08+, J06.03+)
regcomp(3) Guardian Native C Library Calls Reference Manual
The regfree() function frees any memory allocated by the regcomp() function associated with
the preg parameter. An expression defined by the preg parameter is no longer treated as a com-
piled basic or extended regular expression after it is given to the regfree() function.
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");
5−100 Hewlett-Packard Company 527192-018