...

View Full Version : C - GNU regex library---trouble matching subgroups



why_bird
07-02-2008, 10:59 AM
So I've been munging my way through, trying to get regexes to work in C (it has to be in C, not C++). I've just about got it to match the bits I need it to, but trying to get it to match individual subgroups doesn't seem to be working. I've been using this as a reference. My background is fairly limited, mostly I've coded Perl before (so you can see why I'm finding this aspect of C so frustrating!).
So here is the snippet of code which I'm having problems with:


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

int main(void)
{
char line[256]=" R_D = 1";
char blank[256]={0};
regex_t value_re;
regmatch_t value_ma[2]={0};

regcomp(&value_re,"([_A-z]{1,5})[ \t]*=[ \t]*([0-9]{1,3})",REG_EXTENDED);

if(regexec(&value_re,line,1,value_ma,0)==0){
strncpy(temp,blank,256);
strncpy(temp,&line[value_ma[0].rm_so],(value_ma[0]).rm_eo - (value_ma[0]).rm_so);
printf("matched (value) line: %s matched: %s\n",line,temp);

strncpy(temp,blank,256);
strncpy(temp,&line[value_ma[1].rm_so],(value_ma[1]).rm_eo - (value_ma[1]).rm_so);
printf("matched (value) line: %s matched: %s\n",line,temp);
}

return (0);
}


I get a match only for the first element in the array value_ma, but it matches the whole line rather than the first subgroup, so I get output like this:

matched (value) line: R_D = 1
matched: R_D = 1
matched (value) line: R_D = 1
matched:

So I thought that if you had groups in brackets (subgroups?) and passed a pointer to an array of regmatch_t structs, regexec would fill them up with the bits that matched the subgroups. Am I wrong about this, or is it just my implementation that is faulty?

Thanks for your help!
why_bird



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum