superwombat has asked for the wisdom of the Perl Monks concerning the following question:
So... I've been working on this for a couple days now and I'm getting nowhere. I'm a bit of a perl novice, but here's the situation. I'm trying to parse a massive scalar that contains a log file. I need to build an expression to extract the last set of data, which will be marked with a header and footer. I've figured out how to get there by converting the scalar to an array, going through it line by line until I find my matches and verify that they are the last ones etc... but it winds up taking a lot of processing time to do it that way. I'm hoping that the proper regular expression will be able to find my result without having to munch through the entire file so many times.
Here's the example code I've been working with to test my regular expression.
my $string = " start: end: test code start: real 1 end: real with start: real repeating newlines and more than start: real one instance end: real of the start: real desired string end: real start: end:"; if ($string =~/(start: real)((.|\n)*(?!start: )(.|\n)*)*(end: real)/){ print "$&"; }
The goal is it should match
start: real desired string end: real
and nothing else, but right now it matches from the first "start: real" to the "last end: real".
I can see how to make the expression non-greedy, which would make it possible to capture only the first set, but I don't know how to capture only the last one.
|
|---|