using boost.regex on submatches.

Hi, I have a problem using boost.regex to parse a message. The message contains an unknown number of handles terminated by a tab. Each handle has four hex digits. To find the block of handles I use a regex search and then I want to use an iterator to split up the block. #include <iostream> #include <boost/regex.hpp> #include <string> int main (int argc, char *argv[]) { std::string input("HEAD123456789abcdef0\t"); try { std::cout << "creating regex" << std::endl; boost::regex reg("^HEAD(([a-fA-F0-9]{4})+)\\t"); boost::smatch sm; std::cout << "searching " << input << std::endl; boost::regex_search(input, sm, reg); boost::sregex_iterator end; boost::regex handles("([a-fA-F0-9]{4})"); boost::sregex_iterator it(sm[1].str().begin(), sm[1].str().end(), handles); while (it != end) { std::cout << *it++ << std::endl; } } catch (...) { std::cout << "Exception" << std::endl; } } I get a segmentation fault if I run this - sometimes with a memory exhausted notice. If I replace the iterator line boost::sregex_iterator it(sm[1].str().begin(), sm[1].str().end(), handles); with const string& hs(sm[1].str()); boost::sregex_iterator it(hs.begin(), hs.end(), handles); then everything seems to work. What's going on here? Is there a simpler way of achieving this? thanks dan

AMDG Dan Smithers wrote:
boost::sregex_iterator it(sm[1].str().begin(), sm[1].str().end(), handles);
str() returns a std::string by *value* Thus, the two iterators point into different string objects.
I get a segmentation fault if I run this - sometimes with a memory exhausted notice.
If I replace the iterator line
boost::sregex_iterator it(sm[1].str().begin(), sm[1].str().end(), handles);
with
const string& hs(sm[1].str()); boost::sregex_iterator it(hs.begin(), hs.end(), handles);
then everything seems to work.
What's going on here? Is there a simpler way of achieving this?
Now you only get one string object. In Christ, Steven Watanabe
participants (2)
-
Dan Smithers
-
Steven Watanabe