I am able to match patterns using regex library in C. The matched words can be printed. But, i need to store the matches into a string or char array. The code is below:
void match(regex_t *pexp, char *sz) {
regmatch_t matches[MAX_MATCHES]; //A list of the matches in the string (a list of 1)
if (regexec(pexp, sz, MAX_MATCHES, matches, 0) == 0)
{
printf("%.*s\n", (int)(matches[0].rm_eo - matches[0].rm_so), sz + matches[0].rm_so);
}
else {
printf("\"%s\" does not match\n", sz);
}
}
int main() {
int rv;
regex_t exp;
rv = regcomp(&exp, "-?([A-Z]+)", REG_EXTENDED);
if (rv != 0) {
printf("regcomp failed with %d\n", rv);
}
match(&exp, "456-CCIMI");
regfree(&exp);
return 0;
}
OR may be just i need this. How can i splice char array in C? ie. if a char array has "ABCDEF" 6 characters. I just need the chars in index 2 to index 5 "CDEF".