在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"
这个正则表达式通过在线测试工具是正确的,但是输出的结果却不是想要的。
各位大神,求助啊。。
黄舟2017-04-17 13:51:39
~~ Well, public review is a crappy feature.
When using regular expressions, it is best to use the c++11
provided by raw string
to remove the interference of escaped characters.
The label of the topic is also very important, don’t post it randomly. .
大家讲道理2017-04-17 13:51:39
1, C
The language does not have a string
type, let alone a .
calling method
2. Please write down your string, whether it is a regular string or a matching string. I want to help you, but I don’t understand what you want to do. "
Flying around
3. If possible, write comments in the code. Knowing regular expressions and knowing regular expressions in a C
language are two different things, although I can also C
迷茫2017-04-17 13:51:39
Thank you for your guidance. I have found the cause of the error. It is because the regular expression I wrote has problems in the regular expression processing function. There are also some problems in this regular expression processing function.