简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM