fo开发者_如何学Pythonllowing..) and \"|\" (->or) are needed." />
开发者

How to get a basic preg_match_all replacement for std::string in C++?

with "basic" is meant: Only the operators "+" (->fo开发者_如何学Pythonllowing..) and "|" (->or) are needed.

Prototype:

preg_match_all(std::string pattern, std::string subject, std::vector<std::string> &matches)

Usage Example:

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);

The matches should be available afterwardsa via matches[n]. Someone got a hint without using boost and/or PCRE? If not, how I got this realized with boost?


This will return a vector of all matches an the index they were found at.

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


You could rollup something using std::string::find, doing matches via a functor, and pushing the results onto a string vector.

The way it's realized in boost is probably overkill for what you want -- you first would need to decompose the expression into lexems and then compile a state machine for parsing the given regexp.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜