我正在使用C ++ Primer 5th学习C ++。以下代码是从P729复制而来的。
#include <iostream>
#include <regex>
#include <string>
int main()
{
// find the characters ei that follow a character other than c
std::string pattern("[^c]ei");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
std::regex r(pattern);
// ~~~~~~~~~~~^~~~~~~~~~~ where the exception was thrown.
std::smatch results;
std::string test_str = "receipt freind theif receive";
if (regex_search(test_str, results, r))
std::cout << results.str() << std::endl;
return 0;
}
运行它时,抛出了一个异常:
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Press <RETURN> to close this window...
通过逐步调试,我发现它是r
从的代码构造对象时抛出的bits/regex_compiler.h
:
template<typename _InIter, typename _TraitsT>
bool
_Compiler<_InIter, _TraitsT>::
_M_bracket_expression()
{
if (_M_match_token(_ScannerT::_S_token_bracket_begin))
{
_RMatcherT __matcher(_M_match_token(_ScannerT::_S_token_line_begin),
_M_traits);
if (!_M_bracket_list(__matcher)
|| !_M_match_token(_ScannerT::_S_token_bracket_end))
__throw_regex_error(regex_constants::error_brack);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_M_stack.push(_StateSeq(_M_state_store,
_M_state_store._M_insert_matcher(__matcher)));
return true;
}
return false;
}
好的,上面的代码完全超出了我的理解,这是我到目前为止已经尝试过的,谁能告诉我这里的锣声是什么?如何解决?
更新:我正在使用的编译器:
gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~13.04)
您需要使用最低版本的g ++ 4.9才能正确使用正则表达式。(可以使用较低版本进行编译,但已损坏)
您可以从他们那边下载g ++ 4.9。GCC站点
之后,尝试:
g ++ 49 -std = c ++ 0x -static-libstdc ++。
我会尝试为您搜索更多有关它的信息。
编辑:g ++ 49,由于这个原因,版本4.9在构建之后不能被默认。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句