Using POSIX C (regex.h)

  • Thread starter Thread starter Sharky.
  • 1 comments
  • 796 views

Sharky.

MX-5 gang
Premium
Messages
11,969
New Zealand
Christchurch
I understand this would be better suited to the programming forum, but I need an answer ASAP and the programming forum is as dead as a doornail :dopey:

Short version - part of my programming assignment involves using the POSIX regex package (regex.h) to match a regular expression read from a text file to the name of a TV channel, read from an XML file.

regex.h defines these functions:
Code:
int [B]regcomp[/B](regex_t *preg, const char *regex, int cflags);
int [B]regexec[/B](const  regex_t  *preg,  const char *string, size_t nmatch,
                regmatch_t pmatch[], int eflags);
size_t [B]regerror[/B](int errcode, const regex_t *preg, char *errbuf, 
                size_t errbuf_size);
void [B]regfree[/B](regex_t *preg);
regcomp compiles a regular expression into preg, regexec matches preg with string

regcomp successfully compiles the expression into preg, but regexec keeps segmentation faulting (compiler: gcc, running under Linux [Fedora 10]), even when I pass it the exact arguments it wants.

A Filter struct (defined in my program) has the members regex_t *re_channel, regex_t *re_date and regex_t *re_time.

Taking filt to be of type Filter * and pChannel->name of type char *
If I call regexec(filt->re_channel, pChannel->name, 0, NULL, 0) (makes no difference if the last three parameters are that or a pre-declared size_t and regmatch_t array), I keep getting segmentation faults. I'm passing it a pointer to a compiled expression and a strig to match against, as it needs, so why the segfaults? :confused:

If anyone knows how to get regexec to work and can give an example it would be much appreciated :)
 
Thanks for the help guys :lol:

Nah, seriously, I've figured it out. Needed to malloc space for the struct holding the compiled expressions earlier in the program. :)
 
Back