hope someone can answer this. Here is some sample code.
namespace std {
#ifdef UNICODE
typedef wstring tstring;
typedef wstringstream tstringstream;
#define tcout std::wcout
#else
typedef string tstring;
typedef stringstream tstringstream;
#define tcout std::cout
#endif
typedef regex_iterator<std::tstring::const_iterator> TRegexIterator;
}
std::TRegexIterator::regex_type nPattern(_TEXT("-?[0-9]+(\\.[0-9]+)?((e|E)(\\+|-)[0-9]+)?"));
std::match_results<std::tstring::const_iterator> theMatches;
std::tstring test(_TEXT("-1.323E-23"));
tcout << _TEXT("Lvalue target string") << std::endl;
if (std::regex_match(test, theMatches, nPattern))
{
for (auto i= 0; i < theMatches.size(); ++i)
tcout << theMatches[i] << std::endl;
}
tcout << _TEXT("Rvalue target string") << std::endl;
if (std::regex_match(std::tstring(_TEXT("-1.323E-23")), theMatches, nPattern))
{
for (auto i= 0; i < theMatches.size(); ++i)
tcout << theMatches[i] << std::endl;
}
Ok so the problem I’m getting is that the first match result with an rvalue target string is
” 1.323E-23″ (yes, with a starting space) instead of the expected “-1.323E-23”. I’m thinking it’s because “std::match_results” stores pointers instead of data, but in that case why is the first parameter to “std::regex_match” a “const std::string&”? (this I verified from Intellisense). An “std::string&” appears more appropriate.
I’m using the VS11 beta C++ compiler.
Yes,
for example, test below code:
But, Why you believe
In my opinion “const std::string&” appears more appropriate, because ‘std::regex_match’ don’t need to change this parameter.