我试图在 C++ 的字符串中找到所有"#"字符并用 "hash" 替换它们,但正则表达式无法识别字符"#"
I'm trying to find all the "#" characters in a string in C++ and replace them with "hash" but the regex does not recognise the character "#"
我试图在C++中找到字符串中的所有"#"字符,并用"hash"替换它们,但正则表达式无法识别字符"#"
、"#"
或"\#"
。你知道我必须用什么才能找到#吗?
std::string local = std::regex_replace(test, "#", "hash");
正如Robert Harvey所建议的那样,使用std::sting::replace效果良好。它希望用一个索引来替换through,而不是字符。因此,您必须将它与诸如"std::string::find like So:"之类的东西结合使用
int index;
while( (index = str.find("#') != std::string::npos) {
str.replace(index, 1, "hash");
}
您可以对更复杂的字符串使用相同的想法来匹配,所以如果您不需要模式替换,这将起作用。
此处运行示例:http://ideone.com/WqDZAl
相关文章:
- 生成随机.txt文件,10行,10列。充满随机:"*"、"#"、"@"。然后将随机字符替换为'P'
- 我想仅使用递归将字符"a"替换为字符"z"
- 将一个字符替换为字符串中更多数量的字符,而不删除C++中的其他字母
- 如何将像'A'这样的单个字符替换为类似"10"?
- Qt:文件名中的非 ASCII 字符替换为"?"
- 我无法用字符替换重复的数字"a"
- QtLineEdit 占位符:如何让用户在插入模式下将字符 1 替换为 1
- 将一个特定字符替换为正则表达式匹配中的另一个字符
- 如何将所有出现的字符替换为给定的字符串
- 将一个数组到另一个数组的字符替换为字符串
- C++ 字符串,用于删除字符'b'并将字符'a'替换为两个'd'的构建函数
- 将字符串中的所有非 ASCII 字符替换为其等效的 ASCII
- 将utf8编码的字符串转换为本地8位编码的字符串,并将无法确定的字符替换为空白
- C++-用非ascii字符替换文件中的特殊字符
- 将字符串中的 č,š,ć 等字符替换为其 html 代码C++
- 用连字符替换字符串中的文本
- 用不同字符替换单词的代码
- 将空白字符替换为 %20 的字符串,忽略所有尾随空格
- 将部分字符*替换为较小的字符*
- 将字符串中的字符替换为某个字符串