C++ 使用正则表达式搜索"正则表达式运算符"

C++ search "regex operators" with regex

本文关键字:正则表达式 运算符 搜索 C++      更新时间:2023-10-16

我在c++中使用正则表达式,并希望regex_search *和+符号,但它不起作用,因为它们是正则表达式符号。(使用命名空间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.

我怎样才能解决这个问题?

您需要使用''转义特殊字符。如果您在c++字符串常量中指定,则需要添加两个'',例如"*"。