Search code examples
c++regexre2

Get matched data out of re2 Arg


I'm trying to use re2 but couldn't see how I get the matched data out of the RE2::Arg[] when the number of matches in the regex are determined at run time.

I have something like this:

const RE2::Arg *args[10] = {};
int n = 0;
if (RE2::ConsumeN(_content.get(), rule.first, args, n)) {
  int consumed = _content->data() - start;
  //...stuff
}

If my regex is "(foo)|(bar)" args[0] and args[1] should be foo and bar respectively right?

How do I get the matched string from args[0] and so on...?


Solution

  • IIRC RE2::ConsumeN modifies its first argument, so you can call in the loop to get subsequent matches.

    Try something like this:

    RE2::StringPiece input(_content.get());
    
    while (RE2::ConsumeN(&input, rule.first, args, n)) {
       // do your stuff
    }
    

    See also FindAndConsumeN if you are searching for matches, rather than matching prefixes.