• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

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.
0
will1383
Asked:
will1383
  • 4
  • 2
2 Solutions
 
Kim RyanIT ConsultantCommented:
$str = "Server01__NEWFIRST.2005.02.14.16.10.rpy";
# look for a string starting with any characters, then 4 digits and dot
# then 2 digits and a dot, and 2 more digits
$str =~ /^.*(\d{4})\.(\d{2})\.(\d{2})/;
# the brackets above will retain each match in special variables $1 etc
$year = $1;
$month = $2;
$day = $3;
#now assemble the string in the format u want
$new_str = "$month\-$day\-$year";
print $new_str;
0
 
Kim RyanIT ConsultantCommented:
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;
0
 
ozoCommented:
$_ = 'Server01__NEWFIRST.2005.02.14.16.10.rpy';
$date = join'-',(/\.(\d{4})\.0*(\d+)\.(\d\d)\./)[1,2,0];
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kim RyanIT ConsultantCommented:
hi will1383. hope this has helped you.
0
 
will1383Author Commented:
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.
0
 
Kim RyanIT ConsultantCommented:
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;            
0
 
will1383Author Commented:
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now