获取子的match_results用的boost ::正则表达式 [英] Getting sub-match_results with boost::regex

查看:722
本文介绍了获取子的match_results用的boost ::正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,让我们说我有这样的正则表达式:(测试[0-9])+

Hey, let's say I have this regex: (test[0-9])+

和我匹配它: test1test2test3test0

const bool ret = boost::regex_search(input, what, r);

for (size_t i = 0; i < what.size(); ++i)
    cout << i << ':' << string(what[i]) << "\n";

现在,什么[1] TEST0 (最后出现的位置)。比方说,我需要获得测试1 ,2和3,以及:?我该怎么办

Now, what[1] will be test0 (the last occurrence). Let's say that I need to get test1, 2 and 3 as well: what should I do?

请注意:真正的正则表达式是极其更为复杂,必须保持一个总的比赛​​,所以改变的例子正则表达式(测试[0-9])不会的工作。

Note: the real regex is extremely more complex and has to remain one overall match, so changing the example regex to (test[0-9]) won't work.

推荐答案

我想点网已经进行单个捕获组集合使(GRP)+将创建一个组别集合对象的能力。升压引擎的regex_search()将是就像任何普通的匹配功能。你坐在那里相匹配的最后一场比赛离开的格局while()循环。你使用不使用投标itterator,所以那里的最后一场比赛不放过功能将不会开始下一次比赛的形式。

I think Dot Net has the ability to make single capture group Collections so that (grp)+ will create a collection object on group1. The boost engine's regex_search() is going to be just like any ordinary match function. You sit in a while() loop matching the pattern where the last match left off. The form you used does not use a bid-itterator, so the function won't start the next match where the last match left off.

您可以使用itterator形式:结果
修改 - 你也可以使用该令牌迭代器,定义哪些组迭代添加下面的code)。

You can use the itterator form:
(Edit - you can also use the token iterator, defining what groups to iterate over. Added in the code below).

#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;
}

输出:

测试1 结果
测试2 结果
TEST3 结果
TEST0

这篇关于获取子的match_results用的boost ::正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆