perl regular expression format date

Hi Everyone,

Mon 18 Jan 2010 09:52:10 AM MYT

the output is

20100118 09:52:10

how to do it in perl regular expression =~

Thanks

Hello try this,

gaurav@localhost:~$ echo 'Mon 18 Jan 2010 09:52:10 AM MYT' | perl -wln -e 'my @arr=split(/\s/,$_);my %year=("jan"=>"01","feb"=>"02","mar"=>"03");print $arr[3],$year{lc $arr[2]},$arr[1]," ",$arr[4];'
20100118 09:52:10
gaurav@localhost:~$ 

Also add to the hash list like "apr" => "04" ,"may"=>"05" etc . I have left it for you to complete the list for a wider result.

Regards,
Gaurav.

You can't do that with just an Regex, because of the required transformation of a three-letter month to a number. However, with an suitable lookup hash, it becomes almost trivial:

my %mon = ( 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7,
    'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12, );
my $str = 'Mon 18 Jan 2010 09:52:10 AM MYT';
my @dt = ( $str =~ /^.*? (\d+) (\w+) (\d{4}) (\d{2}:\d{2}:\d{2})/ );
my $date = sprintf '%4d%02d%02d %s', $dt[2], $mon{$dt[1]}, $dt[0], $dt[4];

Or, you can use a module like Date::Parse for it.

Oh, thanks :b: