Question

with "basic" is meant: Only the operators "+" (->following..) 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?

Was it helpful?

Solution

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.

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top