English 中文(简体)
有人能举例说明pcrecpp“DoMatch”例程的用法吗?
原标题:can somebody give an example usage of pcrecpp "DoMatch" routine?
  • 时间:2011-05-25 06:23:17
  •  标签:
  • pcre

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

最佳回答

我不久前在网上发现了这个例子。我现在似乎找不到链接了。

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
");
    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;
}
问题回答

暂无回答




相关问题
Will [a-z] ever match accented characters in PREG/PCRE?

I m already aware that w in PCRE (particularly PHP s implementation) can sometimes match some non-ASCII characters depending on the locale of the system, but what about [a-z]? I wouldn t think so, ...

[A-Z]{2,4} not limiting to between 2 & 4 characters

PCRE: /A[A-Z0-9_.%+-]+@(?:[A-Z0-9-]+.)+(?:[a-z]{2,4}|museum|travel)z/i POSIX: /^[A-Z0-9_.%+-]+@(?:[A-Z0-9-]+.)+(?:[A-Z]{2,4}|museum|travel)$/i This regex is correct in every way for my ...

Regular expression libraries for Mac OS X 10.6

Is there a library compatible with PCRE that can be used on Mac OS X 10.6, and which is Unicode compatible? I was thinking to use the predicates, but it is a little excessive when the application is ...

Converting an eregi_replace to a preg_replace

I am trying to parse some HTML snippets and want to clean them up for various reasons (XSS et al). I am currently trying to remove all of the attributes on any tag, except for the href on a anchor. ...

Get position of all matches in group

Consider the following example: $target = Xa,a,aX ; $pattern = /X((a),?)*X/ ; $matches = array(); preg_match_all($pattern,$target,$matches,PREG_OFFSET_CAPTURE|PREG_PATTERN_ORDER); var_dump($matches)...

preg match email and name from to

i want to find name and email from following formats (also if you know any other format that been getting use in mail application for sending emails, please tell in comment :)) how can i know name ...

热门标签