2016-11-05 97 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++字符串常量中指定,你需要添加兩個'\',如「\ *」。