C++11 std::regex_replace()创建带有特定字符串问题的regexp

C++11 std::regex_replace() creating regexp with certain string issue

本文关键字:字符串 问题 regexp 创建 regex std replace C++11      更新时间:2023-10-16

我目前正在尝试使用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");

你好星球