1

带有“基本”的意思是:只有运算符“+”(->following..)和“|” (-> 或) 是必需的。

原型:

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]. 有人在使用boost和/或 PCRE 的情况下得到了提示?如果没有,我是如何通过 boost 实现这一点的?

4

3 回答 3

2

这将返回一个包含所有匹配项的向量以及它们所在的索引。

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;
}
于 2011-12-31T23:56:28.900 回答
0

您可以使用std::string::find汇总某些内容,通过仿函数进行匹配,并将结果推送到字符串向量上。

在 boost 中实现它的方式可能对你想要的有点过分——你首先需要将表达式分解为词法,然后编译一个状态机来解析给定的正则表达式。

于 2010-02-01T15:30:27.163 回答
0

查看 Boost.Regex,http://www.boost.org/doc/libs/1_41_0/libs/regex/doc/html/index.html

于 2010-02-01T15:30:57.243 回答