C语言正则表达式匹配非负整数,为什么不能匹配成功,用的regexec

#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
/* 主程序 */
int main(int argc, char** argv)
{
int status,i = 100;
int cflags = REG_EXTENDED;
//int cflags = REG_NEWLINE;
regmatch_t pmatch[1];
const size_t nmatch = 1;
regex_t reg;
const char *pattern = "^\\d+$";
printf("%s\n",pattern);
char buf[] = "12345",buff[1024]={0};
i = regcomp(®,pattern,cflags);
printf("i = %d\n",i);
status = regexec(®,"12345",0,NULL,0);
if(status == REG_NOMATCH)
{
printf("NO Match\n");
regerror (status, ®, buff, 1024);
printf("%s\n",buff);
}
else if(status == 0)
{
printf("Match\n");
}
regfree(®);
return 0;
}
代码如下,求解答!急急急

代码的逻辑无错,& reg被百度错转义成®
#include <stdio.h>
#include <regex.h>
#include <sys/types.h>
/* 主程序 */
int main(int argc, char** argv)
{
int status,i = 100;
int cflags = REG_EXTENDED;
regmatch_t pmatch[1];
const size_t nmatch = 1;
regex_t reg;
const char *pattern = "^\\d+$";
printf("%s\n",pattern);
char buf[] = "12345",buff[1024]={0};
i = regcomp(& reg,pattern,cflags);
printf("i = %d\n",i);
status = regexec(& reg,"12345",0,NULL,0);
if(status == REG_NOMATCH) {
printf("NO Match\n");
regerror (status, & reg, buff, 1024);
printf("%s\n",buff);
} else if(status == 0) {
printf("Match\n");
}
regfree(& reg);
return 0;
}

输出
^\d+$
i = 0
Match

主要注意正确链接上pcreposix和pcre两个库,链接的先后也不能错。
温馨提示:答案为网友推荐,仅供参考
相似回答