HiI need some help in writing a Regular expression.I am getting Date format like "Tue Apr 12 8:21:20 2009" I want to change this format to "Tue Apr 12 2009 8:21:20" using regular expressionLooking forward your helpThanks
my $time = "Tue Apr 12 8:21:20 2009";$time =~ s/(\d{1,2}:\d{1,2}:\d{1,2}) (\d{4})$/$2 $1/;print $time;
I agree with ghoti's statement about this being trivial, but I am happy to help you out.There are multiple ways you can do this with a regex in perl, here is a basic way:my $time = "Tue Apr 12 8:21:20 2009";$time =~ s/(\d{1,2}:\d{1,2}:\d{1,2}) (\d{4})$/$2 $1/;print $time;You might also want to have a look at using a perl module (e.g. Date:****) to do this. It might be a better option as it can handle multiple date formats.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime
s/(\d{1,2}:\d{2}:\d{2})\s+(\d{4})$/$2 $1/
use Date:****;use POSIX qw(strftime);my $time = str2time("Tue Apr 12 8:21:20 2009");print strftime("%a %b %e %Y %H:%M:%S", localtime($time));
Yes, teaching people to fish is a good idea. I believe you had the best intentions of helping in this matter, however regular expressions can be daunting to new users, and since it was trivial to me I didn't mind offering a solution.
My comments around using a perl module over a regular expression was regarding future-proofing the solution. Date:**** handles multiple date formats, and once it is converted to a timestamp you can use parse it back into the format you like. I agree that this will be more overhead then a regex, but it will make things easier to handle if the input or output date change in the future - especially if you are not confident with regular expresions.e.g.use Date:****;use POSIX qw(strftime);my $time = str2time("Tue Apr 12 8:21:20 2009");print strftime("%a %b %e %Y %H:%M:%S", localtime($time));