Boost C++ Libraries: Ticket #2143: regex_search failes (exception: Memory exhausted) https://svn.boost.org/trac10/ticket/2143 <p> Sorry for using Japanese. In this code, boost::regex_search() failes and throws Exception(Memory exhausted). I tried both VC++7.1/VC++8.0. </p> <p> I remember boost 1.33, 1.34, 1.35 gave the same result, but I don't know why. </p> <p> I think, using back-reference and relatively long input line tends to result in this kind of failure. </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;boost/regex.hpp&gt; int main() { boost::wregex re(L"(.+)[#「\\1」に傍点]"); std::wstring s = L" 女はやがて帰って来た[#「帰って来た」は底本では「帰った来た」]。" L"今度は正面が見えた。三四郎の弁当はもうしまいがけである。" L"下を向いて一生懸命に箸《はし》を突っ込んで二口三口ほおばったが、" L"女は、どうもまだ元の席へ帰らないらしい。" L"もしやと思って、ひょいと目を上げて見るとやっぱり正面に立っていた。" L"しかし三四郎が目を上げると同時に女は動きだした。" L"ただ三四郎の横を通って、自分の座へ帰るべきところを、すぐと前へ来て、" L"からだを横へ向けて、窓から首を出して、静かに外をながめだした。" L"風が強くあたって、鬢《びん》がふわふわするところが三四郎の目にはいった。" L"この時三四郎はからになった弁当の折《おり》を力いっぱいに窓からほうり出した。" L"女の窓と三四郎の窓は一軒おきの隣であった。" L"風に逆らってなげた折の蓋《ふた》が白く舞いもどったように見えた時、" L"三四郎はとんだことをしたのかと気がついて、ふと女の顔を見た。" L"顔はあいにく列車の外に出ていた。" L"けれども、女は静かに首を引っ込めて更紗《さらさ》のハンケチで" L"額のところを丁寧にふき始めた。" L"三四郎はともかくもあやまるほうが安全だと考えた。" L"「ごめんなさい」と言った。"; try { boost::wsmatch what; boost::regex_search(s, what, re); } catch (const std::exception&amp; ex) { std::cerr &lt;&lt; "Caught exception: " &lt;&lt; ex.what() &lt;&lt; std::endl; } return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2143 Trac 1.4.3 John Maddock Thu, 24 Jul 2008 11:45:38 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2143#comment:1 https://svn.boost.org/trac10/ticket/2143#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">wontfix</span> </li> </ul> <p> The problem here is that matching a back-reference is an NP-complete problem - Boost.Regex has a built in limiter that causes it to throw an exception rather than go on trying to find a match "for ever". Other engines just carry on regardless - so while they may find a match in this particular case (eventually), with a bit more text they might equally well hang the machine. In short, there ain't no good solution except to rewrite your regular expression :-( Can't help much there as I can't tell what you're trying to do from the Japanese. </p> <p> HTH, John. </p> Ticket