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

Greetings, I have a file filled with dates in the fashion of mm/dd/yy. I have been trying to come up with a regex to manipulate the data to mm/dd/20yy. This has thrown me for quite a loop. Thanks! Eric

Replies are listed 'Best First'.
Re: exercise in regex
by dragonchild (Archbishop) on Apr 30, 2003 at 16:52 UTC
    Don't use a regex. Use Date::Manip or Date::Calc.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: exercise in regex
by pzbagel (Chaplain) on Apr 30, 2003 at 16:10 UTC

    You aren't specific if these dates are stored individually in variables or part of a larger string.

    Here is a pretty generic solution:

    s|(../../)(..)|${1}20${2}|;

    You may need to be more specific like replacing the .(dots) with digit metacharacters.

    Good luck.

      Thank you all for replying so quickly... I apologize for not supplying what I had tried; I haven't been thinking too clearly as I am finishing the last month of my paramedic school and my mind quickly turning to mush :| I will play around with the ideas here this afternoon... Thank you again, Eric
Re: exercise in regex
by perlguy (Deacon) on Apr 30, 2003 at 16:24 UTC

    This one looks for the position and substitutes with a 20. Hope this helps.

    my $date = '02/04/03'; $date =~ s#(?<=/)(?=\d{2}$)#20#; print "$date\n";
Re: exercise in regex
by Trimbach (Curate) on Apr 30, 2003 at 15:50 UTC
    Why don't you show us what you've done so far, so we can help you out?

    Gary Blackburn
    Trained Killer

Re: exercise in regex
by LordWeber (Monk) on Apr 30, 2003 at 16:00 UTC
    Re: exercise in regex
    by hardburn (Abbot) on Apr 30, 2003 at 16:03 UTC
      s{(\d+)/(\d+)/(\d+)}{$1/$2/20$3} unless m{\d+/\d+/20\d+}

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated