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

In reply to Re: splitting up a string based on character locations by GrandFather
in thread splitting up a string based on character locations by batcater98

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.