linux下regex库使用问题,跪求解答

xuzhitong2009 2010-03-16 10:50:18
//下面为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"这样也能匹配?小弟百思不得其解,望各位大侠帮忙解决.
...全文
423 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
罗耗子 2010-03-16
  • 打赏
  • 举报
回复
赶紧来蹭分,貌似楼主对正则不熟哦
xuzhitong2009 2010-03-16
  • 打赏
  • 举报
回复
多谢两位,小弟太菜了,以后不敢提这种弱智问题了,赶紧好好看下正则表达式
barbara2008 2010-03-16
  • 打赏
  • 举报
回复
楼上正解
taodm 2010-03-16
  • 打赏
  • 举报
回复
先找regextest工具,验证自己的正则。
另外,google “正则表达式30分钟入门教程”多练。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧