Reputation: 21
I am working on this code where i have to compile some regular expression and use these compiled versions multiple times on different strings. So i decided to make a function where i could pass these compiled version for matching the string. My problem is that when i pass the compiled version in the function its showing a match but setting the regmatch_t structure fields to 0. However if i use them within the same function i am getting correct results.
void match_a(regex_t *a,char *str)
{
regmatch_t match_ptr;
size_t nmatch;
regexec(a,str,nmatch,&match_ptr,0);
}
int main()
{
regex_t a;
regmatch_t match_ptr;
size_t nmatch;
char *str="acbdsfs";
regcomp(&a,str,RE_EXTENDED);
match_a(&a,str);
}
This is the general structure of the code.Please suggests any ways to debug this program
Upvotes: 1
Views: 2056
Reputation: 1
Why matches is not filled in position 1?
regex_t a;
regcomp(&a,"brasil",REG_ICASE);
regmatch_t matches[2];
size_t nmatch = 2;
regexec(&a,"brasil brasil",nmatch,matches,0);
int x;
for(x=0;x<2;x++)
printf("%i\n",matches[x].rm_so);
Upvotes: -1
Reputation: 95335
I'm not sure you understand how to use regexec
. The nmatch
argument tells regexec
the number of regmatch_t
objects you have provided. You haven't initialised the nmatch
variable so it could be any indeterminate value, which will likely lead to a crash at some stage, or it may be 0
in which case the regexec
function is defined to ignore the pmatch
argument.
If you want only one regmatch_t
result, try this:
void match_a(regex_t *a,char *str)
{
regmatch_t match;
size_t nmatch = 1;
regexec(a, str, nmatch, &match, 0);
}
If you want up to 10 regmatch_t
(for regular expressions with groups etc), try this:
void match_a(regex_t *a,char *str)
{
regmatch_t matches[10];
size_t nmatch = 10;
regexec(a, str, nmatch, matches, 0);
}
For more information, read this documentation.
Upvotes: 2