gpt4 book ai didi

C++ 正则表达式 : Conditional replace

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:31 26 4
gpt4 key购买 nike

我想用正则表达式替换文本中所有不在字典中的唯一标识符上的单词。我该怎么做?也许使用回调函数?

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

最佳答案

使用regex_token_iterator

#include <regex>
#include <string>
#include <sstream>
#include <set>
#include <map>

std::string replacer(std::string text) {
std::string output_text;
std::set<std::string> keywords = { "foo", "bar" };
std::map<std::string, int> ids = {};

int counter = 0;
auto callback = [&](std::string const& m){
std::istringstream iss(m);
std::string n;
if (iss >> n)
{
if (keywords.find(m) != keywords.end()) {
output_text += m + " ";
}
else {
if (ids.find(m) != ids.end()) {
output_text += "ID" + std::to_string(ids[m]) + " ";
}
else {
// not found
ids[m] = counter;
output_text += "ID" + std::to_string(counter++) + " ";
}
}
}
else
{
output_text += m;
}
};

std::regex re("\\b\\w*\\b");
std::sregex_token_iterator
begin(text.begin(), text.end(), re, { -1, 0 }),
end;
std::for_each(begin, end, callback);
return output_text;
}

关于C++ 正则表达式 : Conditional replace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34030628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com