我有这个方法:
std::string pluralize(std::string const& word) const {
std::regex_replace(word, m_pattern, m_replacement);
return word;
}
但它没有按预期工作。字符串未被给定规则替换。是否可以对引用执行 regex_replace
并返回此变量引用?
最佳答案
regex_replace
没有原地改变,而是返回新的 string
:
std::string pluralize(std::string const& word) const {
return std::regex_replace(word, m_pattern, m_replacement);;
}
如果你想编辑原始的string
:
void pluralize(std::string &word) const {
word = std::regex_replace(word, m_pattern, m_replacement);
}
如果你想同时修改和返回:
std::string pluralize(std::string &word) const {
return word = std::regex_replace(word, m_pattern, m_replacement);
}
关于c++ - 如何根据引用进行 regex_replace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58378097/