如何获得一个基本的preg_match_all替代的std :: string在C ++?
题
用 “基本” 是指:仅运算符 “+”( - >以下..)和 “|” ( - >或)。需要
原型:
preg_match_all(std::string pattern, std::string subject, std::vector<std::string> &matches)
用法示例:
std::vector<std::string> matches;
std::string pattern, subject;
subject = "Some text with a lots of foo foo and " + char(255) + " again " + char(255);
pattern = "/" + char(255) + char(255) + "+|foo+/";
preg_match_all(pattern, subject, matches);
在匹配应该是经由matches[n]
可用afterwardsa。有人得到了一个提示的无是推动和/或PCRE?如果不是这样,我怎么得到这个实现了提升?
解决方案
您使用的std :: string ::寻找可以汇总东西经由函子做匹配,结果推到一个串矢量。
它在推动实现的方式可能是矫枉过正你想要的东西 - 你首先需要分解表达式转换lexems再编状态机来解析给定的正则表达式
其他提示
这将返回一个向量的所有匹配的发现它们在索引。
std::vector<std::pair<std::string, unsigned int>> RegexPP::MatchAll(std::string pattern, std::string haystack) {
std::vector<std::pair<std::string, unsigned int>> matches;
std::regex p(pattern);
std::sregex_iterator start(haystack.begin(), haystack.end(), p), end;
for(; start != end; start++) {
auto match = *start; // dereference the iterator to get the match_result
matches.push_back(std::pair<std::string, unsigned int>(match.str(), match.position()));
}
return matches;
}
不隶属于 StackOverflow