huangyingw
May 23rd, 2009, 04:13 AM
Hello, my code is as bellow. I think my string "fafas.svn" should match my pattern ".(svn|cvs)$". While, subs[i].rm_so keep firmly giving me an incorrect answer of "0"...Rather confusing, could you give me a hand at this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#define SUBSLEN 10
#define EBUFLEN 128 /* error buffer length */
#define BUFLEN 102400 /* matched buffer length */
int main(int argc, char** argv)
{
FILE *fp;
size_t len;/* store error message length */
regex_t re;/* store compilned regular expression */
regmatch_t subs[SUBSLEN];/* store matched string position */
char matched[BUFLEN];/* store matched strings */
char errbuf[EBUFLEN];/* store error message */
int err, i;
size_t nmatch=0;
memset( matched, 0x00,sizeof( matched));
memset( errbuf, 0x00,sizeof( errbuf));
char string[]="fafas.svn";
char pattern[]=".(svn|cvs)$";
//char pattern[]="*afa*";
printf("String:%s\n",string);
printf("Pattern:\"%s\"\n",pattern);
err=regcomp(&re,pattern,REG_EXTENDED);
printf("%d\n",err);
if(err==REG_NOMATCH)
{
fprintf(stderr, "Sorry, no match ...\n");
regfree(&re);
exit(0);
}
else if(err)
{
len=regerror(err,&re,errbuf,sizeof(errbuf));
fprintf(stderr, "error: regexec: %s\n", errbuf);
exit(1);
}
err = regexec(&re, string, nmatch, subs,REG_EXTENDED);
if(REG_NOMATCH==err)
{
printf("no match\n");
}
else
{
printf("%d matched\n",(int)re.re_nsub);
for(i=0;i<=re.re_nsub;i++)
{
if(i==0)
{
//printf("begin: %d, end: %d.\n",subs->rm_so,subs->rm_eo);
printf("begin: %d, end: %d.\n",subs[i].rm_so,subs[i].rm_eo);
}
else
{
//printf("subexpression %d begin: %d, end: %d.\n",i,subs->rm_so,subs->rm_eo);
printf("subexpression %d begin: %d, end: %d.\n",i,subs[i].rm_so,subs[i].rm_eo);
}
len=subs[i].rm_eo-subs[i].rm_so;
memcpy(matched,string+subs[i].rm_so,len);
matched[len]='\0';
printf("match:%s\n",matched);
}
}
regfree(&re);
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#define SUBSLEN 10
#define EBUFLEN 128 /* error buffer length */
#define BUFLEN 102400 /* matched buffer length */
int main(int argc, char** argv)
{
FILE *fp;
size_t len;/* store error message length */
regex_t re;/* store compilned regular expression */
regmatch_t subs[SUBSLEN];/* store matched string position */
char matched[BUFLEN];/* store matched strings */
char errbuf[EBUFLEN];/* store error message */
int err, i;
size_t nmatch=0;
memset( matched, 0x00,sizeof( matched));
memset( errbuf, 0x00,sizeof( errbuf));
char string[]="fafas.svn";
char pattern[]=".(svn|cvs)$";
//char pattern[]="*afa*";
printf("String:%s\n",string);
printf("Pattern:\"%s\"\n",pattern);
err=regcomp(&re,pattern,REG_EXTENDED);
printf("%d\n",err);
if(err==REG_NOMATCH)
{
fprintf(stderr, "Sorry, no match ...\n");
regfree(&re);
exit(0);
}
else if(err)
{
len=regerror(err,&re,errbuf,sizeof(errbuf));
fprintf(stderr, "error: regexec: %s\n", errbuf);
exit(1);
}
err = regexec(&re, string, nmatch, subs,REG_EXTENDED);
if(REG_NOMATCH==err)
{
printf("no match\n");
}
else
{
printf("%d matched\n",(int)re.re_nsub);
for(i=0;i<=re.re_nsub;i++)
{
if(i==0)
{
//printf("begin: %d, end: %d.\n",subs->rm_so,subs->rm_eo);
printf("begin: %d, end: %d.\n",subs[i].rm_so,subs[i].rm_eo);
}
else
{
//printf("subexpression %d begin: %d, end: %d.\n",i,subs->rm_so,subs->rm_eo);
printf("subexpression %d begin: %d, end: %d.\n",i,subs[i].rm_so,subs[i].rm_eo);
}
len=subs[i].rm_eo-subs[i].rm_so;
memcpy(matched,string+subs[i].rm_so,len);
matched[len]='\0';
printf("match:%s\n",matched);
}
}
regfree(&re);
exit(0);
}