c++ - C语言 正则表达式结果不对?
大家讲道理
大家讲道理 2017-04-17 13:49:39
0
4
561

在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(&reg,pattern.c_str(),REG_NEWLINE|REG_EXTENDED) < 0){
        regerror(err,&reg,errbuf,sizeof(errbuf));
        cout<<errbuf<<endl;
        }
    err=regexec(&reg,buff,nm,pmatch,0);
    if(err==REG_NOMATCH) {
        cout<<"no match"<<endl;
    }
     else if(err)
    {
        regerror(err,&reg,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(&reg);

     }
    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"
这个正则表达式通过在线测试工具是正确的,但是输出的结果却不是想要的。
各位大神,求助啊。。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
黄舟

~~ 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. .

大家讲道理

1, CThe 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

Ty80

hhghg

opkj;kl;

迷茫

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!