By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - General - Quick little C question.

Hey guys, this has been frustruating me for several hours and its bound to have a simple solution so I figured you guys would know the answer.

What is wrong with this statement little bit of code?


#include <regex.h>

int main()
{
regex_t *findDate;
regcomp(findDate,".", 0);
return 0;
}

 

It crashes on regcomp with a seg error.



Around the Network

The answer to all your questions is Yes.



See below.



Okay I figured that one out, it because my regex_t needed to be declared as not a pointer and its address passed to regcomp.

Now I just need to figure out why regexec isn't passing rm_eo and rm_so to time in this;

#include
#include

int main()
{
regex_t findDate;
regmatch_t time[1];
regcomp(&findDate,".", 0);
regexec(&findDate, " . ", 0, time, 0);
return 0;
}

Any ideas on this one?

I've already decided I hate regular expressions and everything they stand for =P



Are you attempting to do something similar to this:

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

main()
{
     regex_t findDate;
     char *pattern = "[0-9][0-9] [A-Z][a-z][a-z] [0-9][0-9][0-9][0-9]";
     char *string = "Today is 25 Apr 2009";
     char buf[256];
     regmatch_t pmatch[100];
     int status, eflag;

     /* Compile the pattern */

     if ((status = regcomp (&findDate, pattern, REG_EXTENDED)) != 0){
          printf("Unable to compile pattern!\n");
          exit(2);
     }

     eflag = 0;

     /* Extract all matches */

     while ( status = regexec(&findDate,string,1,pmatch,eflag) == 0) {
          printf("Date found: %s\n", string + pmatch[0].rm_so);
          string += pmatch[0].rm_eo;
          eflag = REG_NOTBOL;
     }
     regfree (&findDate);
}

Output:

Date found: 25 Apr 2009

 

Slightly modified from example found here:

http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.nls/doc/nlsgdrf/internationalized_reg_expression_subr.htm



Around the Network

Yes, yes that was.

Cheers Legend, now I just have to get around to understanding it =P

Thanks again!

=D