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

Hello All,

Thanks in advance.

I want a regular expression where I can retrieve the value of the following.

$str = "2006-01-30 10:11:08.507" I wanna retrieve the middle value -> '01',

Format will be the same always, but the first block(year block) and the day and time block (last block) can vary. finally the string should have only the month value.

Can anyone please help me this?

Thanks and Best Regards,

Dan.

Edited by planetscape - added code tags and rudimentary formatting

( keep:0 edit:8 reap:0 )

Replies are listed 'Best First'.
Re: Perl Exp - Retrieve value
by japhy (Canon) on Apr 11, 2006 at 21:50 UTC
    I would simply use my ($month) = $date =~ /^\d{4}-(\d{2})-/, unless you want to verify that the string is formatted properly. Update: honestly, this isn't really a job for a regex. There's no "pattern" to match, you simply want to get the 5th and 6th characters, because the string is in the same format every time. substr($date, 5, 2) accomplishes this.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Perl Exp - Retrieve value
by davidrw (Prior) on Apr 11, 2006 at 22:03 UTC
Re: Perl Exp - Retrieve value
by saintmike (Vicar) on Apr 11, 2006 at 21:51 UTC
    my $str = "2006-01-30 10:11:08.507"; if($str =~ /-(.*?)-/) { print "Month is $1\n"; }
      /-(..)-/ = person with earphones around the back?
Re: Perl Exp - Retrieve value
by ikegami (Patriarch) on Apr 11, 2006 at 21:51 UTC
    join('', (map { /(.)/g } $str)[5,6])
    but that could be simplified using substr. The exercise left to the user.
Re: Perl Exp - Retrieve value
by ambrus (Abbot) on Apr 12, 2006 at 10:24 UTC
    $str = "2006-01-30 10:11:08.507"; use Date::Manip; $month = UnixDate($str, "%m");