0

我正在尝试使用 re2,但是当在运行时确定正则表达式中的匹配数时,我看不到如何从 RE2::Arg[] 中获取匹配的数据。

我有这样的事情:

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

如果我的正则表达式是 "(foo)|(bar)" args[0] 和 args[1] 应该分别是 foo 和 bar 对吗?

如何从 args[0] 等获取匹配的字符串...?

4

1 回答 1

0

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.

于 2015-05-01T21:11:12.833 回答