Tituuss10
Tituuss10

Reputation: 92

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?

Upvotes: 0

Views: 70

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409196

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"(\[.*\])");

Upvotes: 2

Related Questions