1. I can't understand how your first regex can match more than 32677 times agains string '0' x 1_000_000 ? And it gives correct answer 3. It has a '+' quantifier, which is {1,32677}, true?
Its an optimization. (0)+ is equivalent to 0{1,23677}, 0+ is not. There are many places where Perl's regexes engine doesn't behave like a straighforward backtracking engine - specifically to fix problems inherent to backtracking regex engines. The restriction on the depth of recursion is one of these places too.

You can see what the regexes compile into with:

$ perl -Mre=debug -e 'qr/0+|1+/; Final program: 1: BRANCH (5) 2: PLUS (9) 3: EXACT <0> (0) 5: BRANCH (FAIL) 6: PLUS (9) 7: EXACT <1> (0) 9: END (0) minlen 1
compare:
$ perl -Mre=debug -e 'qr/(0)+|(1)+/' Compiling REx "(0)+|(1)+" Final program: 1: BRANCH (12) 2: CURLYN[1] {1,32767} (23) 4: NOTHING (6) 6: EXACT <0> (0) 10: WHILEM (0) 11: NOTHING (23) 12: BRANCH (FAIL) 13: CURLYN[2] {1,32767} (23) 15: NOTHING (17) 17: EXACT <1> (0) 21: WHILEM (0) 22: NOTHING (23) 23: END (0) minlen 1 Freeing REx: "(0)+|(1)+"
Your solution doesn't cope with inputs
This entire approach - using regexes for this problem - is nonsense, and the way you write code doesn't help at all.

In reply to Re^9: How to match more than 32766 times in regex? by Anonymous Monk
in thread How to match more than 32766 times in regex? by rsFalse

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.