newbie13 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all , I have two stings from logfile how can we have a common regex so that its parse datetime details for further parsing ;
Calling upgrade.sql on 05-JUL-18 10.19.42.559000 PM -12 Calling apply.sql on 17.10.18 12:28:12,447849 +02:
I created on regex :
\d\d\.\d\d\.\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]
this only matches :
Calling apply.sql on 17.10.18 12:28:12,447849 +02:
I need a common regex which matches both the lines ? Thanks,

Replies are listed 'Best First'.
Re: Common regex for timedate
by 1nickt (Canon) on Nov 12, 2018 at 14:39 UTC

    "I need a common regex which matches both the lines"

    Hi, no you don't, you need a datetime parser. See DateTime::Format::Strptime or Time::Piece (latter in core Perl).

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Common regex for timedate
by hippo (Archbishop) on Nov 12, 2018 at 14:38 UTC

    This is fairly generic, you'll probably want to tighten it further but it shows the principles.

    use strict; use warnings; use Test::More; my @good = ( 'Calling upgrade.sql on 05-JUL-18 10.19.42.559000 PM -12', 'Calling apply.sql on 17.10.18 12:28:12,447849 +02:' ); my @bad = ( 'foo' ); my $re = qr/\d\d\W(?:\d\d|[A-Z]{3})\W\d\d \d\d\W\d\d\W\d\d/; plan tests => @good + @bad; for my $str (@good) { like ($str, $re, "$str matched"); } for my $str (@bad) { unlike ($str, $re, "$str not matched"); }