You have to make sure that you reset pos($str) if you use this more than once otherwise it continues from the last match.

However, for reasons that I cannot explain, this method steadfastly refuses to match beyond position 21165. Ask for any match after that and (on my system) the script quietly self terminates. No segault. No panic. No message at all.

Is this a bug worthy of reporting or am I missing something here?

#! perl -w use strict; my $str = 'a' x 100000; my $char = 'a'; for my $n ( map{ $_ * 1000 + 165 } 1 .. 30 ) { pos($str)=0; $str =~ /(?:.*?$char){$n}/g; print "Pos of '$char' #$n in \$str is ", pos($str), "\n"; } __END__ C:\test>junk Pos of 'a' #1165 in $str is 1165 Pos of 'a' #2165 in $str is 2165 Pos of 'a' #3165 in $str is 3165 Pos of 'a' #4165 in $str is 4165 Pos of 'a' #5165 in $str is 5165 Pos of 'a' #6165 in $str is 6165 Pos of 'a' #7165 in $str is 7165 Pos of 'a' #8165 in $str is 8165 Pos of 'a' #9165 in $str is 9165 Pos of 'a' #10165 in $str is 10165 Pos of 'a' #11165 in $str is 11165 Pos of 'a' #12165 in $str is 12165 Pos of 'a' #13165 in $str is 13165 Pos of 'a' #14165 in $str is 14165 Pos of 'a' #15165 in $str is 15165 Pos of 'a' #16165 in $str is 16165 Pos of 'a' #17165 in $str is 17165 Pos of 'a' #18165 in $str is 18165 Pos of 'a' #19165 in $str is 19165 Pos of 'a' #20165 in $str is 20165 Pos of 'a' #21165 in $str is 21165

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^2: The nth occurrence of a character by BrowserUk
in thread The nth occurrence of a character by Anonymous Monk

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.