OK...

lets go explicit, the following (halting!) counterexamples use fairly short input strings and fairly simple regexes, nevertheless resulting in exploding run times...

perl -e ' for $n(10..14) { $str = "x"x($n*2+1); $t=time; $str =~ /(x+x+){$n}y/; print "\$str:$str n:$n time:",time-$t,"sec\n" }' $str:xxxxxxxxxxxxxxxxxxxxx n:10 time:0sec $str:xxxxxxxxxxxxxxxxxxxxxxx n:11 time:3sec $str:xxxxxxxxxxxxxxxxxxxxxxxxx n:12 time:9sec $str:xxxxxxxxxxxxxxxxxxxxxxxxxxx n:13 time:39sec $str:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx n:14 time:156sec

I hope it's now obvious that an empiric brute force approach can only (if ever) work with rigid restrictions!

(credits go to ratazong for finding the base principle in this example after some /msging and credits to his unwillingness to post it afterwards ... ;)

Cheers Rolf

UPDATE: improved code...

UPDATE just for fun :)

perl -e ' for $n(1..14) { $str = "x"x($n*3+1); $t=time; $str =~ /(x+x+x+){$n}y/; print "\$str:$str n:$n time:",time-$t,"sec\n" }' $str:xxxx n:1 time:0sec $str:xxxxxxx n:2 time:0sec $str:xxxxxxxxxx n:3 time:0sec $str:xxxxxxxxxxxxx n:4 time:0sec $str:xxxxxxxxxxxxxxxx n:5 time:0sec $str:xxxxxxxxxxxxxxxxxxx n:6 time:0sec $str:xxxxxxxxxxxxxxxxxxxxxx n:7 time:1sec $str:xxxxxxxxxxxxxxxxxxxxxxxxx n:8 time:9sec $str:xxxxxxxxxxxxxxxxxxxxxxxxxxxx n:9 time:73sec $str:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx n:10 time:555sec ^C

In reply to Re^4: Analysis of Regular Expressions (counterexample of slow regexes) by LanX
in thread Analysis of Regular Expressions by PetaMem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.