Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何分辨C++;将正则表达式视为纯文本/转义所有字符 < >我正在编写一个C++函数来实现字符串替换。该函数类似于: using namespace std; string Replace(const string & str, const string & strOld, const string & strNew, bool useRegex, bool caseSensitive) { regex::flag_type flag=regex::basic; if(!caseSensitive) flag |= regex::icase; if(!useRegex) // WHAT TO DO ? std::regex rx(strOld,flag); string output=regex_replace(str,rx,strNew); return output; }_C++_Regex_Std - Fatal编程技术网

如何分辨C++;将正则表达式视为纯文本/转义所有字符 < >我正在编写一个C++函数来实现字符串替换。该函数类似于: using namespace std; string Replace(const string & str, const string & strOld, const string & strNew, bool useRegex, bool caseSensitive) { regex::flag_type flag=regex::basic; if(!caseSensitive) flag |= regex::icase; if(!useRegex) // WHAT TO DO ? std::regex rx(strOld,flag); string output=regex_replace(str,rx,strNew); return output; }

如何分辨C++;将正则表达式视为纯文本/转义所有字符 < >我正在编写一个C++函数来实现字符串替换。该函数类似于: using namespace std; string Replace(const string & str, const string & strOld, const string & strNew, bool useRegex, bool caseSensitive) { regex::flag_type flag=regex::basic; if(!caseSensitive) flag |= regex::icase; if(!useRegex) // WHAT TO DO ? std::regex rx(strOld,flag); string output=regex_replace(str,rx,strNew); return output; },c++,regex,std,C++,Regex,Std,它将str中出现的所有strOld替换为strNew。我试图使用std::regex和std::regex\u replace来实现它。如果useRegex是真的,那么它工作得很好。但是,如果useRegex为false,我无法告诉他们strOld只是一个普通字符串而不是正则表达式字符串 例如,当我打电话时: string output=Replace("hello.",".","?",false,true); 它返回“???”,而我希望它是“hello?”一半的解决方案是预处理正则表达式并手

它将
str
中出现的所有
strOld
替换为
strNew
。我试图使用
std::regex
std::regex\u replace
来实现它。如果
useRegex
是真的,那么它工作得很好。但是,如果
useRegex
false
,我无法告诉他们
strOld
只是一个普通字符串而不是正则表达式字符串

例如,当我打电话时:

string output=Replace("hello.",".","?",false,true);

它返回
“???”
,而我希望它是
“hello?”

一半的解决方案是预处理正则表达式并手动转义所有元字符。如果C++11中缺少此功能,这是最好的解决方案,从评论中可以看出这一点。

不清楚问题出在哪里。相关代码是什么?它以什么方式不起作用?我猜你也不想要
const string&str
,或者你将无法对其进行任何更改…为什么不在这种情况下避免使用正则表达式,而在
useRegex=false
的情况下只使用普通的
std::string
功能(
find
/
replace
),如果我在功能上使用
std::string
find
/
replace
),则实现不区分大小写的替换有点困难。如果/
其他
,则至少额外增加一个
。如果有什么东西可以告诉
std::regex_replace
输入只是一个普通字符串而不是regex,那就太完美了。我认为这是C++11中缺少的一个特性。在Java中,有一个函数可以使使用regex的函数中的任意字符串被识别为文字字符串。听起来很有趣!我将尝试这个解决方案。