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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘