regcomp function is used to compile a regular expression into a form to be used by the subsequent regexec function.
Actually,regexec function only used to match the pattern.
If you want to match the -w[0-9]* string.Then,you use the following program.
Code:
#include <regex.h> //including the functions for matching regular expressions
#include <stdio.h> //including the standard functions
#include <stdlib.h> //including the standard library functions
main() {
regex_t preg; //creating the
char *string = "-w1";
char *pattern = "-w[0-9]*";
int rc;
size_t nmatch = 2; //nmatch and pmatch are used to provide informations about the matches
regmatch_t pmatch[2];
if ((rc = regcomp(&preg, pattern, REG_EXTENDED)) != 0) { //giving the pattern string to regcomp function to make the string pattern in the form to match by regexec function
printf("regcomp() failed, returning nonzero (%d)\n", rc);
//exit(1);
}
if ((rc = regexec(&preg, string, nmatch, pmatch, 0)) != 0) { //matching the regular expression
printf("failed to ERE match '%s' with '%s',returning %d.\n",
string, pattern, rc);
}
// Both the functions on success,returns 0.
else if(rc==0)
printf ( "Pattern Matched\n" );
regfree(&preg);
}