wulinbang 2016-02-02 04:05 采纳率: 0%
浏览 1781
已结题

boost reg match的问题,很奇怪,不知道怎么弄

//CListboost::smatch,boost::smatch& Q_Regex::QRegexList(std::string htmlcode,CString RegStr)
//{ //提取子串
// boost::smatch mat;
// boost::regex reg(RegStr);
// bool r=boost::regex_match(htmlcode, mat, reg);
// CListboost::match_results<std::string::const_iterator,boost::match_resultsstd::string::const_iterator&> clist;
// if(r) //如果匹配成功
// {
// //显示所有子串
//
// for (int i = 0; i < mat.size(); i ++)
// clist.AddTail(mat[i]);
// }
//
//

// return clist;
//

//}

clist.AddTail(mat[i]);这里提示类型不对,不知道mat怎么转sub_match,或者用其他什么方法

  • 写回答

1条回答 默认 最新

  • oyljerry 2016-02-02 05:43
    关注

    用sregex_token_iterator

     #include <boost/regex.hpp> 
    #include <string> 
    #include <iostream> 
    
    using namespace std;
    using namespace boost;
    
    int main() 
    { 
        string input = "test1 ,, test2,, test3,, test0,,";
        boost::regex r("(test[0-9])(?:$|[ ,]+)");
        boost::smatch what;
    
        std::string::const_iterator start = input.begin();
        std::string::const_iterator end   = input.end();
    
        while (boost::regex_search(start, end, what, r))
        {
            string stest(what[1].first, what[1].second);
            cout << stest << endl;
            // Update the beginning of the range to the character
            // following the whole match
            start = what[0].second;
        }
    
        // Alternate method using token iterator 
        const int subs[] = {1};  // we just want to see group 1
        boost::sregex_token_iterator i(input.begin(), input.end(), r, subs);
        boost::sregex_token_iterator j;
        while(i != j)
        {
           cout << *i++ << endl;
        }
    
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记