在抛出 'std::regex_error' 的实例后调用终止 what(): 括号未关闭
terminate called after throwing an instance of 'std::regex_error' what(): Parenthesis is not closed
我会将代码中第一个g到第一个括号的字符串匹配。我不知道为什么不这样做,因为我使用了转义字符。 例如,在此字符串上:
你好测试g55_2( CNN
我打算匹配g55_2 g++ 5.4.0
#include <iostream>
#include<regex>
#include<string>
int main()
{
std::string s("g.*(");
std::regex re(s);
}
我 https://regex101.com/在这里尝试了我的正则表达式,它起作用了,但由于标题中的错误,我的 c++ 无法编译。
您在C++中逃脱了括号。正则表达式需要一个 \ 字符(需要在 C++ 中转义)和一个 ( 字符:
std::string s("g.*\(");
或者,使用原始字符串文本来避免担心C++逃避妨碍您的行为:
std::string s(R"(g.*()");
R"(...)"
中的文字文本被视为字符串。
另请注意,*
是贪婪的,但您希望它不贪婪,因此它停在第一个括号而不是最后一个括号。您可以添加一个?
以使其不贪婪:
std::string s(R"(g.*?()");
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- 在抛出 what() 的实例后调用'std::logic_error'终止:basic_string::_M_construct 空无效
- 在抛出 'std::invalid_argument' 的实例后终止调用 what(): stoi (找不到问题所在)
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 在抛出 'std::bad_weak_ptr' 的实例后终止调用 what(): bad_weak_ptr?
- 在抛出 'std::regex_error' 的实例后调用终止 what(): 括号未关闭
- 在抛出 'std::invalid_argument' 的实例后调用终止 what(): stoi 错误 in leet
- 在抛出 'std::out_of_range' 的实例后调用的 C++ 终止 what(): basic_string::substr:
- 如何修复抛出"std::logic_error"实例后调用的终止 what(): basic_string::_M_construct null 无效
- 在抛出 'std::out_of_range' what(): vector::_M_range_check 的实例后调用的终止
- 抛出 'std::logic_error' what() 的实例后调用的终止:basic_string::_S_construct null 无效
- C++抛出"std::length_error"实例后调用的终止,what(): basic_string::_M_create
- 抛出 'std::out_of_range' what() 的实例后调用的终止:basic_string::substr
- C++ 中的文件 IO 出错。在抛出 'std::length_error' 的实例后调用的终止 what(): basic_string::调整大小
- 在抛出 'std::out_of_range' what() 的实例后调用的终止:basic_string::擦除
- 如何避免错误:在抛出 what() 的实例后终止调用'std::logic_error':basic_string::_S_construct null 无效
- 如何修复此错误?在抛出 'std::bad_alloc' what() 的实例后终止调用: std::bad_alloc
- 在抛出 'std::bad_alloc' 的实例后调用终止 what(): std::bad_alloc 中止
- 在抛出 'std::bad _alloc' 的实例后调用终止 what(): std::bad_alloc 在 c++ 中