有人可以给出 pcrecpp “DoMatch” 的用法示例吗常规?

发布于 2024-11-10 00:39:39 字数 112 浏览 0 评论 0原文

有人可以给出 pcrecpp DoMatch 例程的用法示例吗?基本上我的要求是将所有匹配捕获到一个向量中。我不想使用 FullMatch 或 PartialMatch,因为它们在传递给它们的参数中记录匹配项。

Can somebody give an example usage of pcrecpp DoMatch routine? Basically my requirement is to capture all the matches into a vector. I don't want to use FullMatch or PartialMatch because they record the matches in the arguments passed to them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2024-11-17 00:39:39

我不久前在网上找到了这个例子。我现在好像找不到链接了

vector<string> regex_tools::regex_matches(const string& str
                                          , const string& regex
                                          , bool ignorecase
                                          , bool submatches)
{
  vector<string> svec;
  pcrecpp::RE_Options opt(PCRE_DEF_FLAGS);
  opt.set_caseless(ignorecase);
  pcrecpp::RE re(regex, opt);

  int n = re.NumberOfCapturingGroups();
  if (n <=0)
    return svec;

  else if(n > 10){
    fprintf(stderr, "Overflow: There are too many capturing groups\n");
    return svec;
  }
  /* sigh */


  string matches[10];
  const pcrecpp::Arg *args[10];

  int z = 0;
  pcrecpp::Arg arg0 = &matches[z];
  args[z++] = &arg0;

  pcrecpp::Arg arg1 = &matches[z];
  args[z++] = &arg1;

  pcrecpp::Arg arg2 = &matches[z];
  args[z++] = &arg2;

  pcrecpp::Arg arg3 = &matches[z];
  args[z++] = &arg3;

  pcrecpp::Arg arg4 = &matches[z];
  args[z++] = &arg4;

  pcrecpp::Arg arg5 = &matches[z];
  args[z++] = &arg5;

  pcrecpp::Arg arg6 = &matches[z];
  args[z++] = &arg6;

  pcrecpp::Arg arg7 = &matches[z];
  args[z++] = &arg7;

  pcrecpp::Arg arg8 = &matches[z];
  args[z++] = &arg8;

  pcrecpp::Arg arg9 = &matches[z];
  args[z++] = &arg9;

  pcrecpp::StringPiece input(str);

  int consumed;

  do {
    if (re.DoMatch(input, RE::UNANCHORED, &consumed, args, n)) {
      input.remove_prefix(consumed);
      for (int t = 0; t < n; t++){
        svec.push_back(matches[t]);
      }
    }

    else
        break;
  } while (submatches);
  return svec;
}

I had found this example on the web a while ago. I can't seem to find the link now.

vector<string> regex_tools::regex_matches(const string& str
                                          , const string& regex
                                          , bool ignorecase
                                          , bool submatches)
{
  vector<string> svec;
  pcrecpp::RE_Options opt(PCRE_DEF_FLAGS);
  opt.set_caseless(ignorecase);
  pcrecpp::RE re(regex, opt);

  int n = re.NumberOfCapturingGroups();
  if (n <=0)
    return svec;

  else if(n > 10){
    fprintf(stderr, "Overflow: There are too many capturing groups\n");
    return svec;
  }
  /* sigh */


  string matches[10];
  const pcrecpp::Arg *args[10];

  int z = 0;
  pcrecpp::Arg arg0 = &matches[z];
  args[z++] = &arg0;

  pcrecpp::Arg arg1 = &matches[z];
  args[z++] = &arg1;

  pcrecpp::Arg arg2 = &matches[z];
  args[z++] = &arg2;

  pcrecpp::Arg arg3 = &matches[z];
  args[z++] = &arg3;

  pcrecpp::Arg arg4 = &matches[z];
  args[z++] = &arg4;

  pcrecpp::Arg arg5 = &matches[z];
  args[z++] = &arg5;

  pcrecpp::Arg arg6 = &matches[z];
  args[z++] = &arg6;

  pcrecpp::Arg arg7 = &matches[z];
  args[z++] = &arg7;

  pcrecpp::Arg arg8 = &matches[z];
  args[z++] = &arg8;

  pcrecpp::Arg arg9 = &matches[z];
  args[z++] = &arg9;

  pcrecpp::StringPiece input(str);

  int consumed;

  do {
    if (re.DoMatch(input, RE::UNANCHORED, &consumed, args, n)) {
      input.remove_prefix(consumed);
      for (int t = 0; t < n; t++){
        svec.push_back(matches[t]);
      }
    }

    else
        break;
  } while (submatches);
  return svec;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文