Boost库中的Regex_replace无法正常工作

时间:2019-11-06 12:28:47

标签: c++ boost boost-regex

我正在尝试删除模式后的子字符串。

我正在尝试使用Boost库,因为它提供了regex_replace,据我所知,应该用给定的新字符串替换每次出现的regex。

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

此代码返回m_value [03]而不是m_value。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您忘了逃脱。

您需要两个反斜杠才能在字符串中添加反斜杠:

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

或使用原始字符串文字:

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