我想使用re2获取给定字符串的子字符串匹配数;
我已经阅读了re2:https://github.com/google/re2/blob/master/re2/re2.h的代码,但没有看到一种简单的方法。
我有以下示例代码:
std::string regexPunc = "[\\p{P}]"; // matches any punctuations;
re2::RE2 re2Punc(regexPunc);
std::string sampleString = "test...test";
if (re2::RE2::PartialMatch(sampleString, re2Punc)) {
std::cout << re2Punc.numOfMatches();
}
我希望它输出3,因为字符串中有三个标点符号;
发布于 2019-06-03 03:41:23
使用FindAndConsume
,并自己计算匹配项。这不会是低效的,因为为了知道匹配的数量,这些匹配无论如何都必须执行和计数。
示例:
std::string regexPunc = "[\\p{P}]"; // matches any punctuations;
re2::RE2 re2Punc(regexPunc);
std::string sampleString = "test...test";
StringPiece input(sampleString);
int numberOfMatches = 0;
while(re2::RE2::FindAndConsume(&input, re2Punc)) {
++numberOfMatches;
}
https://stackoverflow.com/questions/56418122
复制相似问题