Re: c regex example



gert <gert.cuykens@xxxxxxxxx> wrote:
Can anybody tell whats wrong ?

You forgot to check the return value of the regcomp() call:

"regcomp() returns zero for a successful compilation or an error code
for failure."

#include <stdio.h>
#include <string.h>
#include <regex.h>

void
csv(char *buffer)
{
char *line;
char *errbuf;
size_t errbuf_size;
int errcode;
regex_t re;
regmatch_t rm;
regcomp (&re, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))",

Save the return value of regcomp() and use regerror() to convert the
error code into a readable string when not zero

REG_EXTENDED);
errcode=regexec(&re, &buffer[0], 1, &rm, 0);
while(errcode==0)
{
strncpy (line, buffer+rm.rm_so, rm.rm_eo-rm.rm_so);
printf("%s\n",line);
errcode=regexec (&re, buffer+rm.rm_eo, 1, &rm, 0);
}
regerror(errcode,&re,errbuf,errbuf_size);
printf("%s\n",errbuf);
regfree(&re);
}

int
main(void)
{
csv("test,test");
}


--
:wq
^X^Cy^K^X^C^C^C^C
.