Mar 2, 2005 #1 rob51383 Programmer Joined Jun 23, 2004 Messages 134 Location US Simple question, but I have always been lost when it comes to regular expression. I need to convert a date that is given in the format "20050302" to "2005-03-02". So I need a regex that will count 4 insert a "-" then count 2 and insert another "-".
Simple question, but I have always been lost when it comes to regular expression. I need to convert a date that is given in the format "20050302" to "2005-03-02". So I need a regex that will count 4 insert a "-" then count 2 and insert another "-".
Mar 2, 2005 #2 PaulTEG Technical User Joined Sep 26, 2002 Messages 4,469 Location IE Code: my $date="20050302"; $date=substr($date,0,4)."-".substr($date,4,2)."-".substr($date,6,2); Firing up the regex engine for this would be overkill IMO --Paul cigless ... Upvote 0 Downvote
Code: my $date="20050302"; $date=substr($date,0,4)."-".substr($date,4,2)."-".substr($date,6,2); Firing up the regex engine for this would be overkill IMO --Paul cigless ...
Mar 4, 2005 #3 ishnid Programmer Joined Aug 29, 2003 Messages 1,422 Location IE TIMTOWDTI: Code: substr $date, $_, 0, '-' for ( 6, 4 ); ... or ... Code: $date = join '-', unpack "A4A2A2", $date; Upvote 0 Downvote
TIMTOWDTI: Code: substr $date, $_, 0, '-' for ( 6, 4 ); ... or ... Code: $date = join '-', unpack "A4A2A2", $date;
Mar 6, 2005 #4 icrf Programmer Joined Dec 4, 2001 Messages 1,300 Location US Just for gits and shiggles, I'll go the other route: Code: use Date::Parse; use Time::Format; $date = $time{'yyyy-mm-dd', str2time($date)}; but if ever there were overkill... ________________________________________ Andrew I work for a gift card company! Upvote 0 Downvote
Just for gits and shiggles, I'll go the other route: Code: use Date::Parse; use Time::Format; $date = $time{'yyyy-mm-dd', str2time($date)}; but if ever there were overkill... ________________________________________ Andrew I work for a gift card company!
Mar 15, 2005 #5 goBoating Programmer Joined Feb 8, 2000 Messages 1,606 Location US Code: #!perl # "20050302" to "2005-03-02". $date = '20050302'; $date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/; print "TIMTOWDTI: $date ;-)\n"; 'hope this helps If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo. Upvote 0 Downvote
Code: #!perl # "20050302" to "2005-03-02". $date = '20050302'; $date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/; print "TIMTOWDTI: $date ;-)\n"; 'hope this helps If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
Mar 16, 2005 #6 ishnid Programmer Joined Aug 29, 2003 Messages 1,422 Location IE Well, while we're still TIMTOWTDIing and the regexps have entered the fray: Code: print join '-', ( $date =~ /(\d{4})(\d{2})(\d{2})/ ); ... or ... Code: $date =~ s/(?=\d{4}$|\d{2}$)/-/g; print $date,"\n"; Isn't Perl great ... Upvote 0 Downvote
Well, while we're still TIMTOWTDIing and the regexps have entered the fray: Code: print join '-', ( $date =~ /(\d{4})(\d{2})(\d{2})/ ); ... or ... Code: $date =~ s/(?=\d{4}$|\d{2}$)/-/g; print $date,"\n"; Isn't Perl great ...