如何在C+;+;正则表达式中将字符串与大括号{匹配 我在C++中编写正则表达式。我有2个正则表达式,在java中很好。但是这些错误会导致 one of * + was not preceded by a valid regular expression C++

如何在C+;+;正则表达式中将字符串与大括号{匹配 我在C++中编写正则表达式。我有2个正则表达式,在java中很好。但是这些错误会导致 one of * + was not preceded by a valid regular expression C++,c++,regex,c++11,C++,Regex,C++11,这些正则表达式如下: regex r1("^[\s]*{[\s]*\n"); //Space followed by '{' then followed by spaces and '\n' regex r2("^[\s]*{[\s]*\/\/.*\n") // Space followed by '{' then by '//' and '\n' 可以帮助我解决这个错误吗?或者在C++中重新编写这些正则表达式?< /p> < p>参阅: 默认情况下,正则表达式模式遵循ECMAScrip

这些正则表达式如下:

 regex r1("^[\s]*{[\s]*\n"); //Space followed by '{' then followed by spaces and '\n'
 regex r2("^[\s]*{[\s]*\/\/.*\n") // Space followed by '{' then by  '//' and '\n'
可以帮助我解决这个错误吗?或者在C++中重新编写这些正则表达式?< /p> < p>参阅:

默认情况下,正则表达式模式遵循
ECMAScript语法

国家:

字符
\
字符
说明:字符
匹配:按原样输入字符,而不解释正则表达式中的特殊含义。 除了构成上述任何特殊字符序列的字符外,任何字符都可以转义。 需要:
^
*

因此,您需要转义
{
以使代码正常工作:

std::string s("\r\n  { \r\nSome text here");
regex r1(R"(^\s*\{\s*\n)");
regex r2(R"(^\s*\{\s*//.*\n)");
std::string newtext = std::regex_replace( s, r1, "" );
std::cout << newtext << std::endl;
std::string s(“\r\n{\r\n此处有一些文本”);
正则表达式r1(R“(^\s*\{\s*\n)”;
正则表达式r2(R“(^\s*\{\s*/.*\n)”;
std::string newtext=std::regex_replace(s,r1,“”);

std::您是否需要避开反斜杠。我已经尝试了这两种方法,但仍然失败。我仍然使用原始字符串得到相同的错误:
r1(R“(^[\s]*{[\s]*\n)”)
可能会有帮助。不。它不起作用。输入字符串失败的原因是什么?预期的行为是什么?