Search code examples
c++boostboost-regex

Boost::regex_iterator constructor fails but make_regex_iterator function succeeds


std::string line;

This throws std::runtime_error what(): Memory exhausted:

regex_it =  boost::sregex_iterator(line.begin(), line.end(), re);

This works fine:

regex_it = boost::make_regex_iterator(line, re);

Does anyone know what is causing the difference in performance? The boost::regex lib is compiled on Linux in default non-recursive mode.

EDIT: Also tried

regex_it = boost::cregex_iterator(line.data(), line.data()+line.size(), re);

same problem.


Solution

  • Try working with a regex_iterator<char const*> rather than a regex_iterator<std::string::const_iterator>. (Also, the way you're calling make_regex_iterator is unnecessarily verbose by a large measure.)

    Assuming line is a std::string, try this:

    regex_it = boost::make_regex_iterator(line.c_str(), re);
    

    or this:

    regex_it = boost::cregex_iterator(line.data(), line.data() + line.size(), re);