Gossamer Forum
Home : General : Perl Programming :

reg exp problem

Quote Reply
reg exp problem
Hi guys,

I have a number that is a date (month,day,year) like so 28072002

I have been trying to use a reg ex on it whereby it swaps the month and day around like so 07282002 and then places - in between like so 07-28-2002

and then possibly removes the first two digits of the year like so 07-28-02

Unfortunately I am having no luck with doing either, jeeze reg exes are confusing Tongue

If there are any reg ex gurus with an incite I would really appreciate it.

Thanks a lot.

chmod
Quote Reply
Re: [chmod] reg exp problem In reply to
This one works,
Code:
my $date = '28072002';
(my $newdate = $date) =~ s/^(\d{2})(\d{2})(\d{2})(\d{2})$/$2-$1-$4/;
print $newdate; # prints 07-28-02
but I am sure Paul has got a more elegant version...

Ivan
-----
Iyengar Yoga Resources / GT Plugins

Last edited by:

yogi: Jul 19, 2002, 6:35 AM
Quote Reply
Re: [yogi] reg exp problem In reply to
>>This one works, <<

It will when you add the semi-colon after line 1 Cool ....I shall be right back with my attempt...need to test.

Btw... isn't \d\d shorter than \d{2} Angelic

Last edited by:

Paul: Jul 19, 2002, 6:33 AM
Quote Reply
Re: [Paul] reg exp problem In reply to
semicolon ? Angelic

yes, \d\d is shorter, and leaving out the third pair of brackets also makes it shorter....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] reg exp problem In reply to
How about this fine specimin Wink

Code:
local $";
print map { "@$_[2,3]-@$_[0,1]-@$_[6,7]" } [ (split //, 28072002) ];

Last edited by:

Paul: Jul 19, 2002, 6:42 AM
Quote Reply
Re: [Paul] reg exp problem In reply to
Show offs!

Thanks for the examples thoughSmile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [yogi] reg exp problem In reply to
Thanks guys,

I`ll be a testing tomorrow, after looking at your examples I now know why I find reg exes confusing doh!! Crazy

chmod
Quote Reply
Re: [Paul] reg exp problem In reply to
Oh heck... Blush Here's my contender

Code:
print join "-", unpack "xxA2X4A2x4A2", 28072002;
Quote Reply
Re: [Aki] reg exp problem In reply to
poop!