开发者

can somebody give an example usage of pcrecpp "DoMatch" routine?

开发者 https://www.devze.com 2023-03-08 01:05 出处:网络
Can somebody give a开发者_高级运维n 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 becau

Can somebody give a开发者_高级运维n 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消