apoorva.groups@gmail.com wrote:
> Hi
>
> I am facing problem while using regexec function.


First point - there is no regexec function in Standard C. Many people
would consider that you should ask your question in a group which
discusses contexts where regexec exists.

As regexec is part of POSIX, comp.unix.programmer seems a reasonable
choice. (If my newsreader allowed me to set followups I would...)

> Ex: String = "abc_def_hig"
> sub string = "def"
>
> regexc if I use regexec the it will find the sub string in string and
> it will return 0.


Second point. Even if you go to a more appropriate group, you will do
better giving decent data. A small self-contained testcase is vastly
better than a few snippets and a vague discussion.

Put the effort in to reduce your program to the smallest version which
illustrates the problem. Often by doing this, you'll solve the problem
yourself. If you still can't fix it, cut and paste the sample into your
posting, so that anyone else can see _exactly_ what the code is and
does.

As I happened to have a suitable system to hand, I rapidly produced a
testcase which seemed to do what I expected from my manual pages. So
you need to produce a much better explanation of your problem.

When you've done so, please pursue this in a different newsgroup, not
comp.lang.c.

Here for reference is what I tried :-

#include
#include
#include
void is_matched(char *string, char *pattern) {
regex_t *reg = malloc(sizeof *reg);
regcomp(reg,pattern,REG_EXTENDED); /* needs error handling!*/
if (regexec(reg,string,(size_t)0,NULL,0) != 0) {
printf("[%s] appears not to contain [%s]\n",string,pattern);
} else {
printf("[%s] appears to contain [%s]\n",string,pattern);
}
regfree(reg);
}

int main(void) {
char *strings[] = {"abcfedghi", "abcdefghi","defghijkl", NULL};
char *patterns[] = {"def", "^def", NULL};
char **nextString;
char **nextPattern = patterns;
for(nextPattern = patterns ; *nextPattern != NULL ; nextPattern++)
for(nextString = strings ; *nextString != NULL ; nextString++)
is_matched(*nextString,*nextPattern);
}