简体   繁体   English

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

[英]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. 我在C ++中使用正则表达式,并且想要regex_search *和+符号,但是它不起作用,因为它们是正则表达式符号。 (using namespace std) (使用命名空间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? 我该如何解决?

You need to escape the special characters using '\\'. 您需要使用“ \\”转义特殊字符。 If you specify that in a c++ string constant you need to add two '\\' something like "\\*". 如果在c ++字符串常量中指定该常量,则需要添加两个“ \\”,例如“ \\ *”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM