Link to home
Start Free TrialLog in
Avatar of will1383
will1383

asked on

How do I approach this?

I am working on a small project (which will expand) in order to learn how to user Perl.  I have prior software experience so I understand all of the usual methodologies but I do not understand how to make the most efficient use of Perl's powerful features.  This is the beginning of this adventure.  I hope you can help.

Here's the start:

I have a filename that looks like the following:  Server01__NEWFIRST.2005.02.14.16.10.rpy

The date/timestamp in that name is as follows:  yyyy.mm.dd.hh.mm

From that I want to create this:

2-14-2005

Now how in the world do I perform this using the regular expressions?  I have the Perl 5 by example book in softcopy and I have been reading through the regular expression stuff but I still do not understand how to apply as there are very few examples.

Any help in educating me is greatly appreciated.  A solution is great, but I would like help in learning how the solution comes about.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just noticed you don't want the leading zero for the month

$str = "Server01__NEWFIRST.2005.02.14.16.10.rpy";
$str =~ /^.*(\d{4})\.(\d{2})\.(\d{2})/;
$year = $1;
$month = $2;
$month = sprintf("%d",$month);
$day = $3;
$new_str = "$month\-$day\-$year";
print $new_str;
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi will1383. hope this has helped you.
Avatar of will1383
will1383

ASKER

yes it has sorry.  I haven't had a chance to come back.

I'm still trying to understand how the regular expressions work.  This is a good start.

They seem quite complicated on the surface due to their lack of readability.
yes they can appear a bit cryptic. If yopu use the x option, you can embed comments in the regex to make it more redable.

$str =~ /
      ^.*      # start with any characters
      (\d{4}) # then 4 digits, capture in $1
      \.         # then literal dot, backslash delimits
      (\d{2}) # then 2 digits
      \.         # then a dot
      (\d{2}) # then 2 digits
             /x;            
ooo that's a great little option there.  Thank you for that tip.  I know I'm going to be bugging you guys quite a bit more so I can work on my regular expression education.

I appreciate it mucho.