Opened 13 years ago
Closed 13 years ago
#3458 closed Bugs (invalid)
Xpressive Library - sub_match does not work on some occasions
Reported by: | Owned by: | Eric Niebler | |
---|---|---|---|
Milestone: | Boost 1.41.0 | Component: | xpressive |
Version: | Boost 1.40.0 | Severity: | Problem |
Keywords: | xpressive sub_match | Cc: |
Description
Decoding a time and date string. Dealing with dynamic regexes. Code does compile but suspicious output when executed. Regex does match, but submatches aren't like expected partially. Actually all string compares should return "0" but don't. For those which don't for me a "cout" of the submatch string was added.
#include <string> #include <iostream> #include <boost/xpressive/xpressive.hpp>
using namespace std; using namespace boost; using namespace boost::xpressive;
int main() {
/ Decode date and time 2009/09/14 05:30
sregex rex = sregex::compile( "(
d{4})/(
d{2})/(
d{2})(
s)(
d{2})([:])(
d{2})" ); smatch what;
if( regex_match( string("2009/09/14 05:30"), what, rex ) ) {
cout << "TAF file was generated on " << what.size() << endl; cout << what[0].str().compare( string( "2009/09/14 05:30" ) ) << endl; cout << what[1].str().compare( string( "2009" ) ) << endl; cout << what[2].str().compare( string( "09" ) ) << endl;
cout << what[3].str().compare( string( "14" ) ) << endl; cout << what[3].str() << endl;
cout << what[4].str().compare( string( " " ) ) << endl; cout << what[4].str() << endl;
cout << what[5].str().compare( string( "05" ) ) << endl; cout << what[5].str() << endl;
cout << what[6].str().compare( string( ":" ) ) << endl; cout << what[7].str().compare( string( "30" ) ) << endl;
}
return 0;
}
It's not valid to use a regex algorithm to find a pattern in a temporary string, as in:
The match_results struct ends up holding iterators into the temporary string, which has gone out of scope. See the section in the docs on match_results and iterator invalidation.