in reply to Multi-line Regex Performance
$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:
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; 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"; }
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.
|
|---|