HP-UX Reference (11i v2 04/09) - 3 Library Functions N-Z (vol 7)
r
regcomp(3C) regcomp(3C)
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 sub-
strings 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);
}
AUTHOR
regcomp(), regerror(), regexec(), and regfree() were developed by OSF and HP.
SEE ALSO
regexp(5).
STANDARDS CONFORMANCE
regcomp(): XPG4, POSIX.2
regerror(): XPG4, POSIX.2
regexec(): XPG4, POSIX.2
regfree(): XPG4, POSIX.2
HP-UX 11i Version 2: September 2004 − 4 − Hewlett-Packard Company Section 3−−871