如何在C++中将字符串替换为"Match case"和"Match whole word"
how to replace strings with "Match case" and "Match whole word" in C++
>我使用这个函数来替换整个文件中的字符串。
我需要搜索和替换区分大小写但问题是"匹配大小写"在此函数中不起作用,尽管代码中包含 icase 标志
int Replace() {
auto from = R"DELIM(bisb)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;}
如何使搜索和替换过程区分大小写?
首先,您必须出示MCVE。您的 for 循环,文件名不是描述您的问题所必需的。
为
匹配案例,传递std::regex::icase
标志
匹配整个单词,在正则表达式周围使用b
单词边界。
例:
int main()
{
std::string input = "My name is isha. Is it true?";
std::regex reg{R"DELIM(bisb)DELIM", std::regex::icase};
std::cout << std::regex_replace(input, reg, "was");
return 0;
}
输出:
My name was isha. was it true?
相关文章:
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- 模板函数参数到模板函数的"candidate template ignored: could not match ..."
- 开放CV垫"Sizes of input arguments do not match"的问题
- 如何'Invalid conversion'和'no match for operator'更正这些 c++ 错误
- 为什么当我没有变量或其他任何命名的东西时"no match for 'operator==' "错误?
- switch-case语句如何在Arduino/C++中工作
- 如何修复以下错误"no match for call to '(std::tr1::shared_ptr<_iobuf*>) (FILE*&)'"
- 为什么我收到以下错误:"no match for 'operator=='"?(带模板的嵌套类)
- 为什么会给出"multiple test case"错误?
- 如何修复类函数"prototype does not match"和"cadidate is"错误
- 在命名空间内的类中使用带有运算符重载的字符串流时"no match for ‘operator>>’"
- MSVC:带函数的"error C2244: unable to match function definition to an existing declaration"是指专用模板类的类型别名
- 不使用系统Protobuf库时的"Protobuf compiler version doesn't match library version 3.6.1"
- 为什么我总是"error: expected unqualified-id before 'case'"?
- 一个'case'在简单的'switch'语句中不起作用
- 提升::访客操作员过载的变体"Error: no match for call to [...]"
- 如何修复C++中的"no match for operator+"?
- 如何在C++中将字符串替换为"Match case"和"Match whole word"