You really need to go read perlretut, perlre and prelreref.
Note that the code below relies on backtracking to get the first file name. The .* \\ grabs as much as it can consistent with finding a match. On the first try it will grab everything up to the last \ on the line, then fail to match the \[ later in the regex, so it backtracks to the penultimate \ (which fails too) and so on until it finds a suitable match for the entire expression.
Note too that the negative look ahead assertion is not required.
use warnings;
use strict;
while (<DATA>) {
chomp;
if (/(\) | \.sta \)?) $/x) {
print "Skipping $_\n";
next;
}
if (! m{
.* \\(\w+) # Get first file name
[^\[]* \[ [^\]]* \] \s* # skip to date time string
([^-]*) # Everything up to the hyphen \d+/
.* \\ # skip to the last file name
([^.]*) # Grab file name excluding extensi
+on
}x
) {
print "No match: $_\n";
next;
}
my ($first, $datetime, $last) = ($1, $2, $3);
print "Extracted $first, $datetime, $last\n";
}
__DATA__
e:\logfiles\mylocationbase.log [3] Mon 13Mar06 11:00:25 - (000558) Sen
+ding file d:\data\18bn5489.dat
e:\logfiles\mylocationbase.log [3] Mon 13Mar06 11:00:31 - (000558) Sen
+t file d:\data\18bn5489.dat successfully (169 Kb/sec 1049600 bytes)
e:\logfiles\mylocationbase.log [3] Mon 13Mar06 11:00:32 - (000558) Sen
+ding file d:\bnsf_data\18bn5489.sta
Prints:
Extracted mylocationbase, Mon 13Mar06 11:00:25 , 18bn5489
Skipping e:\logfiles\mylocationbase.log [3] Mon 13Mar06 11:00:31 - (00
+0558) Sent file d:\data\18bn5489.dat successfully (169 Kb/sec 1049600
+ bytes)
Skipping e:\logfiles\mylocationbase.log [3] Mon 13Mar06 11:00:32 - (00
+0558) Sending file d:\bnsf_data\18bn5489.sta
DWIM is Perl's answer to Gödel
|