我正在处理一个异常的json字符串中的一些字符,为了使它正常,所以我用regex匹配它,并在行中替换它,匹配的字符需要转换,那么有什么方法可以使“匹配”和“转换”在一个函数?代码如下:
std::string replace(std::smatch match) {
std::string str = match.str();
return "[" + str + "]";
}
int main(){
std::string str = R"({"key":"v\"a\"lue","key2":"v"a"lue2"})";
std::istringstream iss(str);
stringstream output;
std::string token;
while (std::getline(iss, token, ',')) {
std::cout << token << std::endl;
std::regex pattern("\\:\\s*\"(.*)\"");
std::string tokenAfter = std::regex_replace(token, pattern, replace);
output << tokenAfter;
}
cout << output.str();
return 0;
}
编译信息:
发生错误:
error: no matching function for call to 'regex_replace(std::string&, std::__cxx11::regex&, <unresolved overloaded function type>)'x86-64 gcc 12.2 #1
请指引我走向我的目标,先谢了!
1条答案
按热度按时间mw3dktmi1#
std::regex_replace
没有接受函数或其他可调用函数作为format参数的版本。https://en.cppreference.com/w/cpp/regex/regex_replace在每个版本中,参数类型为
std::string
或const CharT*
,并注明:fmt -正则表达式替换格式字符串,确切的语法取决于标志的值
然后,您可以单击指向flags type的链接,您可以在其中找到指向ECMAScript语法文档(这是默认语法)的链接。