C++11 std::regex_replace()创建带有特定字符串问题的regexp
C++11 std::regex_replace() creating regexp with certain string issue
我目前正在尝试使用C++11库编写一个类函数,该函数将用另一个单词替换字符串中出现的每个单词。
void replace(std::string subject, std::string pattern, std::string replacement)
{
std::regex exp(pattern);
std::cout << std::regex_replace(subject, exp, replacement);
}
replace(std::string("Hello world"),std::string("world"),std::string("planet"));
不返回任何内容。
我想问题出在正则表达式上,但我不知道,也找不到如何使用ECMAScript或中的任何其他工具使正则表达式与某个字符串匹配。
有人知道如何解决这个问题吗?
为了完整性:正如Xaqq和Captain Obvlious在评论中所说,gcc还不支持regex。
请参阅此gcc-libstdc++状态页以获取参考。
在MSVC 2012上,您的函数可以与所需的输出一起工作。
replace("Hello world", "^(.*)world(.*)$", "$1planet$2");
你好星球
相关文章:
- setlocale的C++土耳其字符串问题
- C++字符串问题-如何访问字符串元素
- 提升,从字符串问题中获得时间
- 字符串问题中的系统( "command" ) 字符串
- 从文件和字符串问题中读取
- 将C++函数转换为 C#(字符串问题)
- 有关字符串C++问题
- Visual Studio和Xcode之间的Chaiscript字符串问题
- 回文字符串问题:为什么我必须放+1而不是-1才能让这个代码工作
- Dll导入Marshal C++字符串问题
- 崩溃:字符串问题
- 正在寻找另一种解决二进制字符串问题的尝试
- C++ Win32 是字符串问题
- 迭代器字符串问题
- Python 3:Python/C API字符串问题
- 带有空格的C++字符串问题
- 有些字符串问题
- C++11 std::regex_replace()创建带有特定字符串问题的regexp
- Turbo C++的字符串问题
- 棘手的子字符串问题