c++boostboost-regex

Regex_replace from boost library doesn't work as expected


I am trying remove a substring following a pattern.

I am trying to use the boost library as it provides the regex_replace, which as I understood should replace every occurence of the regex with my given new string.

std::string s("m_value[0..3]");
boost::regex rgx("\[.*\]");
return boost::regex_replace(s, rgx, "");

This code returns m_value[03] instead of m_value. Any ideas why?


Solution

  • You forget to escape the escape.

    You need two backslashes to add a backslash in strings:

    boost::regex rgx("\\[.*\\]");
    

    Or use raw string literals:

    boost::regex rgx(R"(\[.*\])");