$str =~ /\G(##.*?)(?=(##|\Z))/gs
might be faster, but it's probably the same.

$str =~ /\G(##(?:(?!$##).)*)/gs
is something to try.

Taking advantage of the fact that '##' is at the begining of a line might help greatly.

But you don't even need a regexp. Use index, which is lightyears faster:

my $sep = '##'; my $sep_len = length($sep); my $last_pos = index($str, $sep); for (;;) { my $pos = index($str, '##', $last_pos + $sep_len); my $rec; if ($pos < 0) { $rec = substr($str, $last_pos); last if not length $rec; } else { $rec = substr($str, $last_pos, $pos-$last_pos); $last_pos = $pos; } my $rid = substr($rec, 19, 10); print "$rid: " . ($keeplist{$rid} ? 'y' : 'n') . "\n"; }
You can even avoid copying stuff into $rec:
my $sep = '##'; my $sep_len = length($sep); my $last_pos = index($str, $sep); for (;;) { my $pos = index($str, '##', $last_pos + $sep_len); my $rec_pos = $last_pos; if ($pos < 0) { last if $last_pos == length($str); $last_pos = length($str); } else { $last_pos = $pos; } my $rid = substr($str, $rec_pos+19, 10); print "$rid: " . ($keeplist{$rid} ? 'y' : 'n') . "\n"; }

Update: Added code. Untested.


In reply to Re: Multi-line Regex Performance by ikegami
in thread Multi-line Regex Performance by pboin

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.