问题描述
在linux平台下,使用c++11正则表达式错误。。
bool isPattern(string str,string pattern){ bool tag=false; if(str.size() > 0 && pattern.size() >0) {string match;regex_t reg;char errbuf[1024];char *buff=new char[str.size()];int err=0;int nm=1024;memcpy(buff,str.c_str(),str.size());regmatch_t pmatch[nm];if(regcomp(®,pattern.c_str(),REG_NEWLINE|REG_EXTENDED) < 0){regerror(err,®,errbuf,sizeof(errbuf));cout<<errbuf<<endl;} err=regexec(®,buff,nm,pmatch,0); if(err==REG_NOMATCH) {cout<<'no match'<<endl; } else if(err) {regerror(err,®,errbuf,sizeof(errbuf));cout<<errbuf<<endl; } else {int len=0;for(int i=0;i<nm && pmatch[i].rm_so!=-1;++i){ len=pmatch[i].rm_eo-pmatch[i].rm_so; if(len > 0) {match.clear();match.assign(buff+pmatch[i].rm_so,len);cout<<'匹配结果:'<<match<<endl;tag=true;// match.clear(); }}regfree(®); } delete [] buff; } return tag;}
原始字符串string str='huangyong'urlname:' '三明治'h good way 'urlname:' '蔬菜'h hello world 'urlname:'h 'apple', wiofjofiewajoi';正则表达式string pattern=''urlname:'.*?'';程序运行结束后,输出的结果是:匹配结果:'urlname:' '三明治'h good way 'urlname:' '蔬菜'h hello world 'urlname:'h 'apple'这个正则表达式通过在线测试工具是正确的,但是输出的结果却不是想要的。各位大神,求助啊。。
问题解答
回答1:~~ 好吧,众审真是个扯淡的功能。使用正则表达式最好使用c++11提供的raw string去除转义字符的干扰。还有题目的标签是非常重要的,不要乱贴。。
回答2:1, C语言没有string类型, 更不会有.调用方法
2, 你那字符串, 无论是正则字串还是匹配字串请写好呀, 想帮你, 也不明白你想做什么, '乱飞
3, 可以的话, 代码中要写注释, 会正则跟会C语言的正则是两回事, 虽然我也会C
回答3:hhghg
opkj;kl;
回答4:感谢各位大牛们的指导,已找到错误的原因,是因为写的正则表达式,在这个正则表达式处理函数中存在问题,这个正则处理函数也存在一些问题。