[Regex] Debug Assert Failure boost::cmatch or std::copy
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
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
#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.
On 3/24/07, John Maddock
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 :-(
Well you could use std::inserter, but whether that makes things easier or just complicates an otherwise simple loop is another matter... --Michael Fawcett
participants (3)
-
John Maddock
-
Michael Fawcett
-
News Group