Boost logo

Boost Users :

From: John Maddock (john_at_[hidden])
Date: 2007-03-24 05:46:46


News Group wrote:
>> All,
>>
>> I am trying to copy from a boost::cmatch variable to a std::set. It
>> compiles fine but when I run it I get a:
>>
>> Debug Assertion Failure
>> xtree 245
>> map/set iterator not dereferencable
>>
>> I think it's the "match.begin()" and "match.end()" in std::copy that
>> is failing. This is a short version of what I have:
>>
>>
>> #include <boost/regex.hpp>
>> #include <algorithm>
>> #include <string>
>> #include <set>
>>
>> bool Parse( const std::string &page, std::set< std::string > &links )
>> {
>> const boost::regex expression( "123" );
>> boost::cmatch match;
>>
>> if( boost::regex_search( page.c_str(), match, expression ) )
>> {
>> std::copy( match.begin(), match.end(), links.begin() );
>> return true;
>> }
>>
>> return false;
>> }
>>
>> int main()
>> {
>> std::string test_data = "www.abc123.com/index.php";
>> std::set< std::string > links;
>>
>> Parse( test_data, links );
>> return EXIT_SUCCESS;
>> }

You are using std::set incorrectly: you can't just copy stuff into it like
that, we can't even use std::back_inserter here because std::set doesn't
have a push_back member :-(

There's another issue here as well: regex_search locates the *first*
occurence of the regex within the string, if you want to find them all then
you'll need a regex_iterator. Remember that match_results contains a
collection of *sub-expression* matches for *one match*.

So off the top of my head, how about:

bool Parse( const std::string &page, std::set< std::string > &links )
{
   const boost::regex expression( "123" );

   // Enumerate occurances of sub-expression 0 (the whole match):
   boost::sregex_token_iterator i(page.begin(), page.end(), expression), j;

   while(i != j)
   {
       links.insert((*i++).str());
   }

   return links.size() != 0;
}

HTH, John.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net