in reply to regex: extract multiple number of date patterns from certain lines

I don't know what this first date is or whether you need it. I called it $stamp. I think this does what you want. If you don't need $stamp, then just assign it to undef.
#!/usr/bin/perl -w use strict; while (<DATA>) { next if (!/dates processed/); my ($stamp, @dates) = ($_ =~ /(\d+-\d+-\d+)/g); # or my (undef, @dates) = ($_ =~ /(\d+-\d+-\d+)/g); # of course if that is what you want then change # the following line too! print "stamp=$stamp, dates are: @dates","\n"; } #prints...... #stamp=2009-02-19, dates are: 2009-01-31 2009-01-29 2009-01-30 #stamp=2009-02-18, dates are: 2009-02-16 2009-02-17 #stamp=2009-02-19, dates are: 2009-02-18 __DATA__ 2009-02-19 06:03:47,713 SOMETHING WRONG: 2009-01-33, 2009-01-44, 2009- +01-33 2009-02-19 05 58 29 138 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
  • Comment on Re: regex: extract multiple number of date patterns from certain lines
  • Download Code

Replies are listed 'Best First'.
Re^2: regex: extract multiple number of date patterns from certain lines
by Random_Walk (Prior) on Mar 04, 2009 at 19:34 UTC

    Hi Marshall

    I do need the initial date, this is for of a logfile parser that captures these dates and sends them up the line to a monitoring application that then compares the timestamp date to the processed dates and raises an alarm a processed dates was too old.

    The crux of the matter is that my log file parser has one shot at each log line with a regex and the matched parts are then passed up to the next stage, I want to get as much done in the regex as possible/reasonable, partly on the principle of keeping monitoring close to the monitored and partly for pure bloody minded IT geek fun.

    sadly the main constraint of this problem is one line of regex, code is cheating!

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
      So, if I understand this correctly, you are saying that my code works, but there is some constraint that it has to be in one single regex? If that's the case, then we are into some obfuscated code problem and this is perhaps the wrong place?

      If we are talking about clarity and performance, then that's different. Fewer lines of Perl code doesn't always equal faster performance. I simplified this stuff like must match exactly 4 times, etc. This speeds up the regex engine. As far as clarity goes, I would struggle to be more clear (I'm not a guru).

      If you are interested in performance, then measure and test performance (run benchmarks).

      Counting the number of lines of source code is a relatively poor predictor of actual code performance.

      Update: Well it just took some few seconds to get a negative vote on this post. I was genuinely trying to help with the original problem. I don't understand this requirement for "one line". I think that benchmarking and testing is the right way to go. I would be happy to help in this regard.

        Hi Marshall

        There is actually a constraint that it has to be a single regex, see my answer to Ikegami at Re^2: regex: extract multiple number of date patterns from certain lines to see more of the context in which I am operating.

        As for XP, I did not vote you down but there are some grumpy monks around with itchy trigger fingers, I got -- for every single reply in this thread. Don't worry about it too much, sometimes you get a little hit but is normally tends upwards.

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!