Boost C++ Libraries: Ticket #4123: smatches contains wrong matches, access changes content https://svn.boost.org/trac10/ticket/4123 <pre class="wiki">$ cat main.cc #include &lt;boost/regex.hpp&gt; #include &lt;iostream&gt; int main(int argc, char **argv) { boost::regex expr(argv[1]); boost::smatch matches; if (boost::regex_search(std::string(argv[2]), matches, expr)) { for (size_t i = 0; i&lt;matches.size(); ++i) std::cout &lt;&lt; "XXX " &lt;&lt; matches[i] &lt;&lt; '\n'; for (size_t i = 0; i&lt;matches.size(); ++i) std::cout &lt;&lt; "YYY " &lt;&lt; matches[i] &lt;&lt; '\n'; for (size_t i = 0; i&lt;matches.size(); ++i) std::cout &lt;&lt; "ZZZ " &lt;&lt; matches[i] &lt;&lt; '\n'; } } $ ./main 'to ([a-z]+) [A-Z]+ ([0-9]+)' 'fs to hello WORLD 123' XXX to hello WORLD 123 XXX lo WO XXX 23 YYY hello WORLD 12323 YYY WORLD YYY 23 ZZZ lo WORLD 1232323 ZZZ LD 12 ZZZ 23 </pre><p> Expected output: three times: hello 123 (or at least three times the same output) </p> <p> Perhaps I am using the API wrong, but I am inspired by this example(-output): /usr/share/doc/libboost1.38-doc/HTML/libs/regex/doc/html/boost_regex/captures.html </p> <p> I get the same results with boost 1.38. </p> <p> Using Ubuntu 9.10, x86/64, g++ 4.4.1. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4123 Trac 1.4.3 John Maddock Tue, 20 Apr 2010 17:54:43 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4123#comment:1 https://svn.boost.org/trac10/ticket/4123#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> There is a bug in your code: you are passing a <em>temporary</em> std::string object to regex_search, so after the call the match_results object contains invalidated iterators (iterators into an object that has already been destroyed) - leading to garbage output. </p> <p> HTH, John. </p> Ticket g_sauthoff@… Tue, 18 May 2010 21:17:54 GMT <link>https://svn.boost.org/trac10/ticket/4123#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4123#comment:2</guid> <description> <p> Thanks for the hint! What a stupid mistake. I posted this ticket way too fast ... </p> </description> <category>Ticket</category> </item> </channel> </rss>