Cześć,
Zapragnąłem przygotować funkcję, która wspierać będzie moją współpracę z regex'em. Funkcja wygląda tak:
int regex(char * string, char * pattern, regmatch_t match[], exception_event exception_function)
{
regex_t regex;
int resultRegcomp = regcomp(®ex, pattern, REG_NEWLINE|REG_ICASE|REG_EXTENDED);
if (resultRegcomp != 0)
{
(* exception_function)(regex_error(resultRegcomp, ®ex));
regfree(®ex);
return -1;
}
else
{
match = (regmatch_t *)malloc(sizeof(regmatch_t) * regex.re_nsub);
if (match == 0)
{
(* exception_function)("In regex function can't malloc memory");
regfree(®ex);
return -2;
}
else
{
int resultRegexec = regexec(®ex, string, regex.re_nsub, match, 0);
if (resultRegexec == 0)
{
printf("%d %d\n", match[1].rm_so, match[1].rm_eo);
regfree(®ex);
return regex.re_nsub;
}
else
{
(* exception_function)(regex_error(resultRegexec, ®ex));
regfree(®ex);
return -3;
}
}
}
return 0;
}
Dwa pierwsze i ostatni parametr działają bez problemu. Problem mam z parametrem trzecim. Przy takim wywołaniu w funkcji main:
int main(int argc, char * argv[])
{
regmatch_t * match;
int resultRegex = regex("2 TEXT CODE", "^([0-9]{1,})(\\s+)(.+)$", match, exception);
if (resultRegex > 0)
{
printf("Result: %d, Start: %d, End: %d\n", resultRegex, match[1].rm_so, match[1].rm_eo);
}
return 0;
}
Spodziewałem się tego, że match[1] zwróci mi poprawną wartość. Niestety wygląda na to, że marzę po pamięci. Nie bardzo wiem co może być tego powodem, a już dawno nie pracowałem w C :/
Poproszę Was o pomoc.