如何在C++中用“匹配实例”和“匹配整字”替换字符串
我使用此函数替换整个文件中的字符串 我需要搜索和替换区分大小写 但问题是,尽管代码中包含icase标志,但Match case在该函数中不起作用如何在C++中用“匹配实例”和“匹配整字”替换字符串,c++,C++,我使用此函数替换整个文件中的字符串 我需要搜索和替换区分大小写 但问题是,尽管代码中包含icase标志,但Match case在该函数中不起作用 int Replace() { auto from = R"DELIM(\bis\b)DELIM"; //replace only "is" not "Is" or "iS" or "IS" auto to = "was"; //replace with "was" //The file is created automatically in
int Replace() {
auto from = R"DELIM(\bis\b)DELIM"; //replace only "is" not "Is" or "iS" or "IS"
auto to = "was"; //replace with "was"
//The file is created automatically in the debug folder of the software then you
//can put all your "is" "Is" "iS" "IS" options into it In order to check if it works
for (auto filename : { "A.txt" }) {
ifstream infile{ filename }; string c { ist {infile}, ist{} }; infile.close();
ofstream outfile{ filename };
//std::regex::icase flag does not work
regex_replace(ost{outfile},begin(c),end(c),std::regex {from, std::regex::icase}, to);
}return 0;}
如何使搜索和替换过程区分大小写?首先,您必须提供一个。对于for循环,文件名不是描述问题所必需的 因为 匹配大小写,传递std::regex::icase标志 匹配整个单词,在正则表达式周围使用单词边界\b 例如:
int main()
{
std::string input = "My name is isha. Is it true?";
std::regex reg{R"DELIM(\bis\b)DELIM", std::regex::icase};
std::cout << std::regex_replace(input, reg, "was");
return 0;
}
如果您有一个构建的尝试,那么请从中创建一个。至于您的问题,有一个不区分大小写的选项。匹配词与您的正则表达式有更多关系。谢谢,它很有效。我在问题中描述整个函数的原因是,我需要它来创建一个完整的文件。我做的是regex_replaceost{outfile},begincontent,endcontent,std::regex{RDELIM\bis\bDELIM,std::regex::icase},是;只匹配整个单词,而不匹配大小写–在你的函数中,它可以工作,但在我的函数中,我也不能做什么来让它工作?@FOREXV没有MCVE,就无法提供帮助。i、 显示我可以复制粘贴、编译和运行的代码。如果您坚持使用文件进行输入,请同时放置其内容。对不起。我用所有的新问题更新了上面的问题。您还可以看到函数本身,我是如何修改它以适合您的代码的。我想说非常感谢你的帮助,我非常感激我已经改进了上面的问题,你能告诉我你是否能帮助我吗?如果不是,那么请告诉我,这样我可以改进它。非常感谢你的帮助
My name was isha. was it true?