mardi 1 décembre 2015

C++ regex: Conditional replace

I want to replace with a regular expression all the words in the text that are not in the dictionary on the unique identifier. How I can do it? Maybe using callback function?

std::string getToken(const std::smatch &m) {
    static int x = 0;
    std::string keyword = m[0].str();
    std::set<std::string> keywords = {"foo", "bar"};

    if (keywords.find(keyword) != keywords.end()) {
        return keyword;
    } else {
        return "i" + x++;
    }
}

std::string replacer(std::string text) {
    std::string ret = text;

    ret = std::regex_replace(ret , std::regex("\\b.*\\b"), getToken); // It's don't works

    return ret;
}

Aucun commentaire:

Enregistrer un commentaire