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

问题描述

嘿,假设我有这个正则表达式:(test [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";

现在, what [1] test0 (最后一次出现)。让我们说,我需要得到 test1 ,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?

注意:真正的正则表达式非常复杂,必须保持一个整体匹配,所以将示例regex更改为(test [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.

推荐答案

我认为Dot Net能够创建单个捕获组集合,以便(grp)+将在group1上创建一个集合对象。 boost引擎的regex_search()将像任何普通的匹配函数一样。你坐在一个while()循环匹配最后一个匹配留下的模式。您使用的表单不​​使用bid-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表单:

编辑 - 您也可以使用令牌迭代器,定义要迭代的组)。

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

输出:

test1

test2

test3

test0

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

09-05 18:43