batcater98 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings - I have a simple input string and I am wanting to pull out the date from it and leave the rest behind. Knowing that the lenght of the actual date portion will change weather it has a 2 digit month or a 1 digit month, and same goes for the day. I am trying to figure out a way to pull from the reverse of the string because the end of it never changes.
Date Examples: 3/1/2006 0:00:00 7/22/2007 0:00:00 12/1/2006 0:00:00 12/22/2007 0:00:00
I want to pull just the 3/1/2006 out and trash the 0:00:00. Key the 0:00:00 is always the same. With that how do I strip the string from the back side to the front?

Replies are listed 'Best First'.
Re: Date within a string manipulation.
by FunkyMonk (Bishop) on Jul 24, 2007 at 19:39 UTC
    There's plenty of ways of doing this. Just take your pick from one below:

    $_ = "3/1/2006 0:00:00"; s/ .*//; $_ = (split)[0]; $_ = $1 if m/(.*) /; ($_) = m/(.*) /; s/ 0:00:00//;

    And many, many more...

Re: Date within a string manipulation.
by BrowserUk (Patriarch) on Jul 24, 2007 at 19:59 UTC
Re: Date within a string manipulation.
by mr_mischief (Monsignor) on Jul 24, 2007 at 19:44 UTC
    $string =~ s/\s[0:]+$//;

    ...or am I misunderstanding your intent?
Re: Date within a string manipulation.
by andreas1234567 (Vicar) on Jul 25, 2007 at 06:07 UTC
    Use a module such as DateTime, Date::Manip, Date::Parse:
    use strict; use warnings; use DateTime::Format::Strptime; my $parser = DateTime::Format::Strptime->new( pattern => '%m/%d/%Y' ); while (<DATA>) { my $dt = $parser->parse_datetime( $_ ); print $dt->ymd(); } __DATA__ 3/1/2006 0:00:00 7/22/2007 0:00:00 12/1/2006 0:00:00 12/22/2007 0:00:00
    perl -l 628560.pl 2006-03-01 2007-07-22 2006-12-01 2007-12-22
    --
    Andreas