2016-11-05 116 views
0

即时通讯在C++中使用正则表达式,并希望regex_search *和+符号,但它不会工作,因为它们是正则表达式符号。 (使用命名空间std)C + +搜索“正则表达式运算符”与正则表达式

string s = "15 * 12"; 
regex rx("*"); 
bool flag = regex_search(s, rx); 

terminating with uncaught exception of type std::__1::regex_error: One of *?+{ was not preceded by a valid regular expression. 

我该如何解决呢?

+1

使用'\\'转义它们 –

+0

当然,使用正则表达式执行此搜索是矫枉过正的。 –

+0

@PeteBecker是的,在这个例子中,当然,它只是一个例子;) –

回答

0

您需要使用'\'转义特殊字符。如果你在一个C++字符串常量中指定,你需要添加两个'\',如“\ *”。