linux下regex库使用问题,跪求解答
//下面为linux下c代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
int regexMatch(char * regStr,char * str)
{
char * pattern;
int z = 1;
int cflags = 0;
char ebuf[128],lbuf[256];
regex_t reg;
regmatch_t pm[10];
const size_t nmatch = 10;
int result = 0;
if (regStr == NULL ||str == NULL)
{
return -1;
}
if (strlen(regStr) == 0)
{
return -1;
}
printf("regexMatch(%s,%s)\n",regStr,str);
pattern = regStr;
z = regcomp(®,pattern,REG_EXTENDED);
if (z != 0)
{
printf("regcomp failed\n");
regerror(z,®,ebuf,sizeof(ebuf));
return 1;
}
memset(lbuf,0,sizeof(lbuf));
strcpy(lbuf,str);
printf("lbuf=%s,z=%d\n",lbuf,strlen(lbuf));
/*if ((z = strlen(lbuf))> 0 && lbuf[z-1] == '\n')
regfree(®);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
int regexMatch(char * regStr,char * str)
{
char * pattern;
int z = 1;
int cflags = 0;
char ebuf[128],lbuf[256];
regex_t reg;
regmatch_t pm[10];
const size_t nmatch = 10;
int result = 0;
if (regStr == NULL ||str == NULL)
{
return -1;
}
if (strlen(regStr) == 0)
{
return -1;
}
printf("regexMatch(%s,%s)\n",regStr,str);
pattern = regStr;
z = regcomp(®,pattern,REG_EXTENDED);
if (z != 0)
{
printf("regcomp failed\n");
regerror(z,®,ebuf,sizeof(ebuf));
return 1;
}
memset(lbuf,0,sizeof(lbuf));
strcpy(lbuf,str);
printf("lbuf=%s,z=%d\n",lbuf,strlen(lbuf));
/*if ((z = strlen(lbuf))> 0 && lbuf[z-1] == '\n')
lbuf[z - 1] = 0;*/
z = regexec(®,lbuf,nmatch,pm,0);
printf("REG_NOMATCH=%d\n",REG_NOMATCH);
if (z == REG_NOMATCH)
{
regerror(z,®,ebuf,sizeof(ebuf));
fprintf(stderr,"%s: regcom('%s')\n",ebuf,lbuf);
printf("no match!\n");
result = 1;
}
else if (z != 0)
{
regerror(z,®,ebuf,sizeof(ebuf));
fprintf(stderr,"%s: regcom('%s')\n",ebuf,lbuf);
result = 2;
}
else
{
printf("match\n");
}
regfree(®);
return result;
}
int main(int argc,char ** argv)
{
if (argc != 3)
{
printf("uage:test regex string\n");
return 1;
}
if (regexMatch(argv[1],argv[2]) != 0)
{
printf("not match!\n");
return 1;
}
else
{
printf("is matched\n");
return 0;
}
}
在linux下运行根本不是期望值啊,比如test "123*" "12",这样竟然能匹配成功?还有test "14*" "12"这样也能匹配?小弟百思不得其解,望各位大侠帮忙解决.