Boost C++ Libraries: Ticket #3458: Xpressive Library - sub_match does not work on some occasions https://svn.boost.org/trac10/ticket/3458 <p> 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. </p> <p> #include &lt;string&gt; #include &lt;iostream&gt; #include &lt;boost/xpressive/xpressive.hpp&gt; </p> <p> using namespace std; using namespace boost; using namespace boost::xpressive; </p> <p> int main() { </p> <blockquote> <p> <em></em><em></em><em></em><em></em><em></em><em></em><em></em><em></em><em></em><em></em><em></em>/ <em> Decode date and time </em> 2009/09/14 05:30 <em> </em></p> </blockquote> <p> </p> <blockquote> <p> sregex rex = sregex::compile( "<sup>(<br />d<a class="report" href="https://svn.boost.org/trac10/report/4">{4}</a>)/(<br />d<a class="report" href="https://svn.boost.org/trac10/report/2">{2}</a>)/(<br />d<a class="report" href="https://svn.boost.org/trac10/report/2">{2}</a>)(<br />s)(<br />d<a class="report" href="https://svn.boost.org/trac10/report/2">{2}</a>)([:])(<br />d<a class="report" href="https://svn.boost.org/trac10/report/2">{2}</a>)" ); smatch what; </sup></p> </blockquote> <p> </p> <blockquote> <p> if( regex_match( string("2009/09/14 05:30"), what, rex ) ) { </p> <blockquote> <p> cout &lt;&lt; "TAF file was generated on " &lt;&lt; what.size() &lt;&lt; endl; cout &lt;&lt; what<a class="missing changeset" title="No changeset 0 in the repository">[0]</a>.str().compare( string( "2009/09/14 05:30" ) ) &lt;&lt; endl; cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/1" title="Import core sources for SVNmanger 0.38 ">[1]</a>.str().compare( string( "2009" ) ) &lt;&lt; endl; cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/2" title="Add Boost Disclaimer">[2]</a>.str().compare( string( "09" ) ) &lt;&lt; endl; </p> </blockquote> </blockquote> <p> </p> <blockquote> <blockquote> <p> cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/3" title="Tweak disclaimer text">[3]</a>.str().compare( string( "14" ) ) &lt;&lt; endl; cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/3" title="Tweak disclaimer text">[3]</a>.str() &lt;&lt; endl; </p> </blockquote> </blockquote> <p> </p> <blockquote> <blockquote> <p> cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/4" title="Tweak disclaimer formatting, again">[4]</a>.str().compare( string( " " ) ) &lt;&lt; endl; cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/4" title="Tweak disclaimer formatting, again">[4]</a>.str() &lt;&lt; endl; </p> </blockquote> </blockquote> <p> </p> <blockquote> <blockquote> <p> cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/5" title="Boost customization">[5]</a>.str().compare( string( "05" ) ) &lt;&lt; endl; cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/5" title="Boost customization">[5]</a>.str() &lt;&lt; endl; </p> </blockquote> </blockquote> <p> </p> <blockquote> <blockquote> <p> cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/6" title="New repository initialized by cvs2svn.">[6]</a>.str().compare( string( ":" ) ) &lt;&lt; endl; cout &lt;&lt; what<a class="changeset" href="https://svn.boost.org/trac10/changeset/7" title="Initial content for next-gen Boost website. ">[7]</a>.str().compare( string( "30" ) ) &lt;&lt; endl; </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> return 0; </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3458 Trac 1.4.3 Eric Niebler Thu, 17 Sep 2009 03:55:47 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/3458#comment:1 https://svn.boost.org/trac10/ticket/3458#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> It's not valid to use a regex algorithm to find a pattern in a temporary string, as in: </p> <blockquote> <p> regex_match( string("2009/09/14 05:30"), what, rex ) </p> </blockquote> <p> 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. </p> Ticket