获取搜索模式的索引

时间:2012-03-30 14:45:17

标签: c++ regex boost-regex

我需要在迭代每个循环时获得找到模式的索引。

我尝试了距离,但我收到编译时错误。

最好的方法是什么?

void test2()
{
    std::string text = "I am <single angle bracket> trying to use [single square bracket] [[double square bracket]] Boost::regex library. <single angle bracket>";
    std::string::const_iterator start = text.begin();
    std::string::const_iterator end = text.end();


    boost::regex pattern("((<.*?>)|(\\[.*?\\]))");
    boost::smatch what;
    boost::match_flag_type flags = boost::match_default;

    try
    {
        if(boost::regex_search(text, what, pattern))
        {

            for(unsigned i = 0; i < what.size(); ++i)
            {
                std::cout << what[i] << std::endl;
            }

            // update search position:
            start = what[0].second;
            //std::cout << std::distance(start, end);

            // update flags: 
            flags |= boost::match_prev_avail;
            flags |= boost::match_not_bob;
        }
    }
    catch(std::runtime_error ex)
    {
    }   
}

由于

0 个答案:

没有答案