Piotr Szczepanik
Piotr Szczepanik

Reputation: 381

Regex for checking date format in perl

I have a problem with creating regex for checking date which looks like this:

2017-07-12 14:41:56.784

I need to create it in perl but it's completly new for me. Any suggestions how to write it and how to use it with "IF" condition ?

Upvotes: 1

Views: 1169

Answers (2)

G. Cito
G. Cito

Reputation: 6378

You could try the module Regexp::Common::time.

use Regexp::Common qw(time);                           
my $str = "2017-07-12 14:41:56.784";                   
print "Time Gentlemen" if $str =~ m/^$RE{time}{iso}\z/ ;

The Regexp::Common modules make using regular expressions for all kinds of things much easier and potentially less error prone. It is helpful to bear in mind JWZ's infamous caveat regarding regular expressions - but using perl and Regexp::Common allows one to keep calm and carry on.

Upvotes: 3

Raps
Raps

Reputation: 1

No need to include any module, To match any datetime like this: 2017-07-12 14:41:56.784

For example "2017-07-12 14:41:56.784"

$str="2017-07-12 14:41:56.784"; 
if($str =~ /[1-9]{1}[0-9]{3}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}/)
{
    print "match found";
}

Upvotes: 0

Related Questions