Boost C++ Libraries: Ticket #3513: regex_match very slow example https://svn.boost.org/trac10/ticket/3513 <p> Hi, </p> <p> I am having an issue with a dynamic regular expresion. </p> <p> I have this source code. </p> <p> sregex tempRE = sregex::compile("<sup>(?:.*<br />r?<br />n)*var wlanPara = new Array<br />(<br />r?<br />n<br />d{0,4},<br />r?<br />n\"(?P&lt;ssid&gt;(?:<br />w+))\",<br />r?<br />n(?P&lt;channel&gt;(?:<br />d{0,4})),<br />r?<br />n<br />d{0,4},<br />r?<br />n\"[<br />w-]+\",<br />r?<br />n\"[<br />w<br />.]+\",<br />r?<br />n<br />d{0,4},<br />r?<br />n<br />d{0,4},<br />r?<br />n\"(?P&lt;signal&gt;(?:<br />d{0,4})) dB\",<br />r?<br />n<br />d{0,4},<br />d{0,4} <br />);(?:.*<br />r?<br />n)*$"); </sup></p> <p> std::string htmlText; <em> filled using text in html file attached </em></p> <p> smatch what; </p> <p> if(regex_match(htmlText, what, tempRE)) { </p> <blockquote> <p> <em>... </em></p> </blockquote> <p> } </p> <p> When the program enters to the regex_match function, the process consumes 50% of the processor and the function never returns. </p> <p> I was attach the html file that contains the text. The source code was tested using <a class="missing wiki">VisualStudio</a> 2008. </p> <p> If I use the static variant of regex, it's works perfectly.. </p> <p> sregex tempRE = bos &gt;&gt; *_ &gt;&gt; "var wlanPara = new Array(" &gt;&gt; _ln &gt;&gt; _d &gt;&gt; commonDigit &gt;&gt; ',' &gt;&gt; _ln &gt;&gt; '"' &gt;&gt; (s1= +_w) &gt;&gt; "\"," &gt;&gt; _ln &gt;&gt; (s2= commonDigit) &gt;&gt; ',' &gt;&gt; _ln &gt;&gt; commonDigit &gt;&gt; ',' &gt;&gt; _ln &gt;&gt; '"' &gt;&gt; +(_w | '-') &gt;&gt; "\"," &gt;&gt; _ln &gt;&gt; '"' &gt;&gt; +(_w | '.') &gt;&gt; "\"," &gt;&gt; _ln &gt;&gt; commonDigit &gt;&gt; ',' &gt;&gt; _ln &gt;&gt; commonDigit &gt;&gt; ',' &gt;&gt; _ln &gt;&gt; '"' &gt;&gt; (s3= commonDigit) &gt;&gt; " dB\"," &gt;&gt; _ln &gt;&gt; commonDigit &gt;&gt; ',' &gt;&gt; commonDigit &gt;&gt; " );" &gt;&gt; *_ &gt;&gt; eos; </p> <p> Thanks, </p> <p> Fernando Pelliccioni </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3513 Trac 1.4.3 Fernando Pelliccioni <fpelliccioni@…> Wed, 07 Oct 2009 17:55:22 GMT attachment set https://svn.boost.org/trac10/ticket/3513 https://svn.boost.org/trac10/ticket/3513 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">text.html</span> </li> </ul> <p> HTML file that contains the text to parse </p> Ticket Eric Niebler Fri, 30 Oct 2009 15:36:56 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/3513#comment:1 https://svn.boost.org/trac10/ticket/3513#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> Your dynamic regex is slow because you are needlessly using nested quantifiers in two places. The regex begins and ends with "(?:.*<br />r?<br />n)*". This case is explicitly called out in xpressive's docs as a common pitfall. Please read this: </p> <p> <a href="http://www.boost.org/doc/libs/1_40_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.tips_n_tricks.beware_nested_quantifiers">http://www.boost.org/doc/libs/1_40_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.tips_n_tricks.beware_nested_quantifiers</a> </p> <p> Your static regex does not use nested quantifiers. That explains the difference. </p> Ticket