Hi!

I've been trying to write a script to generate some useful output based on a log file, and I ran into the issue mentioned in this thread.

http://www.nntp.perl.org/group/perl.perl6.language.regex/2000/12/msg507.html

Specifically, the problem I'm trying to solve involves matching a group of log lines.

For example:

KEY blah blahblah KEY blah ah other random stuff KEY blah ha other random stuff PATTERN asdf KEY fdas PATTERN

I want to match each PATTERN, but have the match also include the most recent preceeding KEY (and everything in between). So these are the two matches I'm interested in:

KEY blah ha other random stuff PATTERN

KEY fdas PATTERN

If I do something like KEY.*PATTERN the entire contents get the match, and if I do KEY.*?PATTERN it matches everything from the first KEY to the first PATTERN. I've also tried .*KEY.+?PATTERN which just matches the very last group.

If you've got any insight it would be greatly appreciated.

Thanks.


In reply to pattern matching (greedy, non-greedy,...) by cacophony777

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.