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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?