I have a large and busy log file, once or twice a day a line will pop up looking something like one of these:

2009-02-02 06:12:57,500 dates processed: 2009-01-31, 2009-01-29, 2009- +01-30 2009-02-18 06:03:47,713 dates processed: 2009-02-16, 2009-02-17 2009-02-19 05:58:29,138 dates processed: 2009-02-18

I need to extract the all occurrences of the date pattern /\d{4}-\d{2}-\d{2}/ but only when the line also contains 'dates processed'. The trailing list of dates is of variable length but there will always be at least one.

Due to the log monitoring tool we have I need to grab this in a single regex. The log file is busy on a busy production server so some degree of efficiency is desired. Up until now even before optimising I have not got it to work, here are (some of) my attempts so far:

@res =$_ =~/(\d{4}-\d\d-\d\d)/g # gets them all but also from lines without 'dates processed' @res = $_ =~/(\d{4}-\d\d-\d\d).*dates processed: (\d{4}-\d\d-\d\d,? ?) +*/g # only returns first of trailing list @res = $_ =~/(\d{4}-\d\d-\d\d).*dates processed: ((\d{4}-\d\d-\d\d)*)/ # gets the right number of results but the final list all the same val +ue!

Update

@res = $_ =~/(\d{4}-\d\d-\d\d).*dates processed: ((\d{4}-\d\d-\d\d,? ? +)*) # almost gets it but now have one too many results in the tail # due to the nested braces

Update 2

highlighted that the cunning part here is a single regex is needed, a few kind souls are missing this

</Updates>

Can any of the regex gurus out there give me a hint please

Thanks,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to regex: extract multiple number of date patterns from certain lines by Random_Walk

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.