Karl Cegel Karl Cegel - 9 months ago 38
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);


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 Source

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