Boost logo

Boost Users :

From: Axel F. (axelf_at_[hidden])
Date: 2002-06-18 18:08:34


> Weird, but looks more like a VC bug than a regex one - after all the
> difference between the two examples is whether the string passed to
> regex_format is a variable or a temporary, so unless I'm missing something
> it's got to be a compiler problem?

Well, I don't think, it's a compiler problem.
stringbuf::str() retuns a temporary string. This is passed on to
The real problem is, the data in matchInfo is not stored as a copy of the
processed string, but as a reference to its characters.
So, when the temoprary string is being destroyed, the result in matchInfo is
also lost.

Even the following short code failes, using a temporary string as argument
for regex_match():

#include <string>
#include <iostream>
using namespace std;

#include <boost/regex.hpp>
using namespace boost;

 reg_expression<char> exp(".*");

 cmatch match1;
 string str = "123456";
 if (regex_match(str, match1, exp))
 cout << "matchInfo=" << match1.str() << "\n";
 cout << "match1.size()=" << match1.str().size() << "\n\n";

 cmatch match2;
 regex_match(string("123456"), match2, exp);
 cout << "match2=" << match2.str() << "\n";
 cout << "match2.size()=" << match2.str().size() << "\n\n";


 return 0;

For me, it outputs:


match2=8HÁ 56


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at