in reply to splitting up a string based on character locations

A negative look ahead assertion ((?!...) - line 3 of the regex below) does the tricky bit:

use warnings; use strict; while (<DATA>) { chomp; if (! m{ /(\d+) # Get first bunch of digits follow +ing a / .*? Date\sTime\s # match date time string ( (?: (?! \s\d+/). )+ ) # Everything up to the space befor +e \d+/ [^/]+ / # Throw away the unwanted digits p +receeding / (\d+) # Grab last number (.*) # Grab tail for subsequent checkin +g }x # allow most white space and comme +nts ) { print "No match: $_\n"; next; } my ($first, $datetime, $last, $tail) = ($1, $2, $3, $4); if ($tail =~ /(\) | \.sta \)?) $/x) { print "Skipping $_\n"; next; } print "Extracted $first, $datetime, $last\n"; } __DATA__ 12345645/883700892.log Date Time 4 kkdjkdkd 889298383/883729083.dat 12345645/883700892.log Date Time 4 kkdjkdkd 889298383/883729083.sta (12345645/883700892.log Date Time 4 kkdjkdkd 889298383/883729083.dat) (12345645/883700892.log Date Time 4 kkdjkdkd 889298383/883729083.sta) Completely badly formed

Prints:

Extracted 883700892, 4 kkdjkdkd, 883729083 Skipping 12345645/883700892.log Date Time 4 kkdjkdkd 889298383/8837290 +83.sta Skipping (12345645/883700892.log Date Time 4 kkdjkdkd 889298383/883729 +083.dat) Skipping (12345645/883700892.log Date Time 4 kkdjkdkd 889298383/883729 +083.sta) No match: Completely badly formed

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: splitting up a string based on character locations
by batcater98 (Acolyte) on Dec 19, 2006 at 20:57 UTC

    Thanks Grandfather - What would you do with this real example.

    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:\data\18bn5489.sta

    These repeat over and over.

    Out of first line I want to exstract what is between e:\logfiles\ and .log "alliancebase" and that will change but the e:\logfiles and .log will always be there.

    I also want to exstract the Mon 13Mar06 11:00:25 and keep the skip everything from the - to the \ after data.

    And keep everything to the end.

    On second I want to skip anything that ends with a )

    On third I want to skip anything that ends with .sta

    thanks,
    Ad.

      When you learn to format your messages so that they can be read I'll take a look at your "real" problem. See Writeup Formatting Tips and read about code tags for a start. You could revise your OP while you are at it.

      You may find PMEdit helps. You can get the current version from the CPAN scripts area: http://cpan.perl.org/scripts/.


      DWIM is Perl's answer to Gödel
        Sorry, I will try to follow the rules. My Data follows, I have also put in what I am trying to exstract from each.
        First Line: I want mylocationbase, I want Mon 13Mar06 11:00:25, and I +want 18bn5489 of which all of this data will change, but the data aro +und it will stay static as these lines repeat over and over. Second Line: I want to skip per the ) at the end. Third Line: I want to skip per the .sta at the end. 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