in reply to Regex to find the last matching set in a long scalar

The solution with a greedy match-all .* expression preceding your regex is good and fine (and it is what I would have suggested if no one else had proposed before), but please note that if your scalar is really huge, you will probably get the following warning:
Complex regular subexpression recursion limit (32766) exceeded at ...
indicating that, perhaps, the regex is not using the regex engine in the most optimal way. It probably does not matter so much if you do it only once or just a few times, and the warning can presumably be silenced out with a pragma such as no warnings qw/regexp/; (I haven't tested), but if you're doing that many many times, then you might want to find another solution, for example a somewhat subtler regex that would not backtrack so much, by making some additional assumptions on the content of your scalar before (or possibly after) the place you want to match.

Replies are listed 'Best First'.
Re^2: Regex to find the last matching set in a long scalar
by Athanasius (Archbishop) on Nov 16, 2014 at 07:34 UTC

    If the log file is huge, it will be better to avoid the scalar variable altogether and to instead read the file in backwards, line by line. The File::ReadBackwards module on is designed for this purpose:

    #! perl use strict; use warnings; use File::ReadBackwards; my $logfile = 'log.txt'; my $header = 'start: real'; my $footer = 'end: real'; my $in_data = 0; my @lines; my $bw = File::ReadBackwards->new($logfile) or die "Cannot open file '$logfile' for reading backwards: $!"; while (my $line = $bw->readline) { chomp $line; if ($in_data) { unshift @lines, $line; last if $line eq $header; } elsif ($line eq $footer) { unshift @lines, $line; $in_data = 1; } } print join("\n", @lines), "\n";

    No regex needed. :-)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Yes, I definitely agree with you that it is usually not the best idea to slurp a huge file into a scalar (or an array), and I usually avoid that even for smaller files, at least when possible. And also that the iterating backward with the module you are mentioning is probably a better solution.