2
我想用正則表達式替換文本中所有不在字典中唯一標識符的單詞。我如何做到這一點?也許使用回調函數?C++正則表達式:有條件替換
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;
}
請提供MCVE:http://stackoverflow.com/help/mcve –