batcater98 has asked for the wisdom of the Perl Monks concerning the following question:

I have a long string that has static characters in the string that I want to use for splitting the string up into parts. What would be the best way to break down the string based on key characters in the string.

Example: 12345645/883700892.log Date Time [4] kkdjkdkd 889298383/883729083.dat

I want the 883700892, the Date time, and the 883729083 and leave everything else out.

Also, I am looking for a easy say to skip a string if it ends in a ")" or if it ends in ".sta" or something like that.

Ideas?
Thx,
Ad.

Replies are listed 'Best First'.
Re: splitting up a string based on character locations
by jettero (Monsignor) on Dec 19, 2006 at 19:52 UTC

    The best way is split: my @a = split qr{[/.]}, $important_string

    Also see @a = grep {! m/\.sta/ } @a

    Fortunately perl is magic and doesn't require 400 lines for simple tasks like these. You can just use the built ins.

    References: grep, split

    -Paul

Re: splitting up a string based on character locations
by GrandFather (Saint) on Dec 19, 2006 at 20:09 UTC

    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

      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