Boost C++ Libraries: Ticket #2188: Regex matches bad strings https://svn.boost.org/trac10/ticket/2188 <p> I wrote the following small program to test if a string is a valid ip. The regex gives a false positive for the case listed below. For comparison I've also added a perl program which does it correctly. </p> <p> #include &lt;iostream&gt; #include &lt;boost/regex.hpp&gt; </p> <p> bool regex_match(const std::string &amp;text, const std::string &amp;match) { </p> <blockquote> <p> try { </p> <blockquote> <p> const boost::regex regex(match); boost::smatch matches; return boost::regex_match(text, matches, regex); </p> </blockquote> <p> } catch(boost::regex_error &amp;e) { </p> <blockquote> <p> return false; </p> </blockquote> <p> } </p> </blockquote> <p> } </p> <p> int main() { </p> <blockquote> <p> if(regex_match("1.2.03", "<sup>(25<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-5">[0-5]</a>|2<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-4">[0-4]</a><a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>|<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-1">[0-1]</a>?<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>?<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>)(<br />.(25<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-5">[0-5]</a>|2<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-4">[0-4]</a><a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>|<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-1">[0-1]</a>?<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>?<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>))<a class="report" href="https://svn.boost.org/trac10/report/3">{3}</a>$")) { </sup></p> <blockquote> <p> std::cout &lt;&lt; "GOOD :)\n"; </p> </blockquote> <p> } else { </p> <blockquote> <p> std::cout &lt;&lt; "BAD :(\n"; </p> </blockquote> <p> } </p> </blockquote> <p> } </p> <p> The corresponding perl program does it correctly: </p> <p> if('1.2.03' =~ m/<sup>(25<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-5">[0-5]</a>|2<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-4">[0-4]</a><a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>|<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-1">[0-1]</a>?<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>?<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>)(\.(25<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-5">[0-5]</a>|2<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-4">[0-4]</a><a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>|<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-1">[0-1]</a>?<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>?<a class="source" href="https://svn.boost.org/trac10/log/?revs=0-9">[0-9]</a>))<a class="report" href="https://svn.boost.org/trac10/report/3">{3}</a>$/) { </sup></p> <blockquote> <p> print "GOOD :)\n"; </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2188 Trac 1.4.3 John Maddock Fri, 15 Aug 2008 17:24:39 GMT status, milestone changed https://svn.boost.org/trac10/ticket/2188#comment:1 https://svn.boost.org/trac10/ticket/2188#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.36.0</span> → <span class="trac-field-new">Boost 1.37.0</span> </li> </ul> <p> Confirmed as a bug, I'm looking for a fix now, </p> <p> John Maddock. </p> Ticket John Maddock Sun, 17 Aug 2008 11:17:54 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2188#comment:2 https://svn.boost.org/trac10/ticket/2188#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in revision <a class="missing ticket">#48185</a>. </p> Ticket