Reputation: 1750
I'm using C++ Primer 5th to learn C++. The code below is copied from 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;
}
When running it, an exception was thrown :
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Press <RETURN> to close this window...
By debugging step by step, I found it was thrown while constructing the object r
from the code from 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;
}
OK.The code above is totally beyond what I can understand.That's what I have tried so far.Can anyone tell me what's gong on here? How to fix it?
UPDATE: The compiler I'm using:
gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~13.04)
Upvotes: 2
Views: 1113
Reputation: 355
You need to use minimal version of g++ 4.9 to use regex properly.(You can compile it with lower versions, but it's broken)
You can download g++ 4.9 from their side. GCC SITE
After that try:
g++49 -std=c++0x -static-libstdc++.
I try to search more about it for you.
EDIT: g++49, because of the reason, that version 4.9 can't be default after building it.
Upvotes: 2