How about using negative lookahead? This way, you check to see whether what follows 'request(' actually contains the BADSTRING, and fails if that is the case.

use strict; $/ = ""; # Set up for slurp mode my $data = <DATA>; # Grab the entire file to a string. my %matches = $data =~ /^request\( # start of record (?!BADSTRING) # Fails if BADSTRING is present ([^\n]+) # Captures $blah1 \n # Match the newline [^,]+, # Match anything up to the comma ([^\n]+) # Capture $blah2. Everything from /mgx; # the comma to the next \n foreach ( keys %matches ) { print "$_ : $matches{$_}\n"; } __DATA__ request(goodstring START_TIME,some goodness request(BADSTRING START_TIME,some bad stuff request(randomstring START_TIME,more goodness request(whatever you are

This gives:

goodstring : some goodness randomstring : more goodness

Thanks to pzbagel for his excellent sample data :)

This uses several neat tricks. /g repeats the match until the string is empty. /m allows embedded newlines in the string to match. Calling the regex in list context and assigning it to a hash gives us a nice little summary to dump afterwards.

Disadvantages to this approach is of course the amount of memory used by $data and %matches. Malformed data may break /g, and of course the relative illegibility of the regex may be a problem. If your datasamples aren't too big, it might work, though.

Hope this helps, and I welcome (and appreciate) any criticism on my regex-programming style.

pernod
--
Mischief. Mayhem. Soap.


In reply to Re: grab only if pattern matches previous line by pernod
in thread grab only if pattern matches previous line by nkpgmartin

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.