mercredi 8 mars 2023

How to make function as thrid parameter of regex_replace() in C++11 above?

I was attending to replace some characters in an abnormal json string in order to make it normal, so i used regex to match it and replace it in line, the matched character need to be transformed, so is there any method to make "matching" and "transforming" in one function? code below:

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 occurred:

error: no matching function for call to 'regex_replace(std::string&, std::__cxx11::regex&, <unresolved overloaded function type>)'x86-64 gcc 12.2 #1

please guide me to my goal, thanks in advance!

Aucun commentaire:

Enregistrer un commentaire