Karl Cegel Karl Cegel - 27 days ago 10
C++ Question

C++ search "regex operators" with regex

Im using regex in C++ and want regex_search * and + symbols, but it wont work, cause they are regex symbols.
(using namespace std)

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


and

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


How can I fix that?

Answer

You need to escape the special characters using '\'. If you specify that in a c++ string constant you need to add two '\' something like "\*".

Comments