Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Reg expression on day

by Anonymous Monk
on Dec 23, 2003 at 16:49 UTC ( #316653=perlquestion: print w/replies, xml ) Need Help??

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

I need to substitute a date entry if the day has a "0" as the beginning. For example if I find this entry: 12/09/2003 I need it to be: 12/9/2003 Please advise:
s/^\d{2}\/(0)\d\/\d{4}//g;

Replies are listed 'Best First'.
Re: Reg expression on day
by Thelonius (Priest) on Dec 23, 2003 at 16:59 UTC
    That would delete the date entirely. Here's one that would work:
    s!^(\d+/)0(\d/\d{4})!$1$2!;
      Thank you!
Re: Reg expression on day
by Roy Johnson (Monsignor) on Dec 23, 2003 at 17:19 UTC
    s!/0!/!

    Update:
    To remove all leading zeroes (day, month, and year):
    s/\b0//g


    The PerlMonk tr/// Advocate
Re: Reg expression on day
by flounder99 (Friar) on Dec 23, 2003 at 17:59 UTC
    If you only want to get rid of the leading zero on the day this would work:
    s!/0?(\d+)/!/$1/!
    If you want to get rid of the leading zeros on both the month and the day this would work:
    s!0?(\d+)/0?(\d+)!$1/$2!
    use strict; for ('12/09/2003', '12/10/2003', '01/01/2004') { my $date = $_; print "Before: $date\n"; $date =~ s!/0?(\d+)/!/$1/!; print "Leading zero off of day: $date\n"; $date = $_; $date =~ s!0?(\d+)/0?(\d+)!$1/$2!; print "Leading zero off of month and day: $date\n\n"; } __END__ Before: 12/09/2003 Leading zero off of day: 12/9/2003 Leading zero off of month and day: 12/9/2003 Before: 12/10/2003 Leading zero off of day: 12/10/2003 Leading zero off of month and day: 12/10/2003 Before: 01/01/2004 Leading zero off of day: 01/1/2004 Leading zero off of month and day: 1/1/2004

    --

    flounder

      you can replace
      $date =~ s!0?(\d+)/0?(\d+)!$1/$2!;
      with
      $date =~ s!0?(\d+/)!$1!g;
      for month and day.
Re: Reg expression on day
by Paulster2 (Priest) on Dec 23, 2003 at 17:55 UTC

    An alternate way of doing this might be to break out your MM/DD/YYYY into seperate chunks (ie $MM = month, $DD = day, and $YYYY = year), then take the day variable and add zero to it. Then put it all back together again. This way you could manipulate the date if you needed to.

    ($MM, $DD, $YYYY) = split(/,"$date"); $MM=$MM+0; $DD=$DD+0; $date=$MM."/".$DD."/".$YYYY;

    Please forgive me if my split syntax is incorrect. I don't have an example in front of me. Anyway, I am just providing an alternate methodry. Although if you absolutely need to use a regex or want to get better at using them, this definitely won't help you!

    Have fun!

    UPDATE: Thanks flounder99 for the correction, I thought that I was missing something!

    Paulster2

      Sure, and it cleans up a bit:

      $date = join "/", map $_+0, split m[/], $date;

      Not sure if I like the idea of stripping those zeroes though. Probably better to store the date in a saner format (Epoch seconds maybe?), then use POSIX::strftime() to generate the desired date format.
      Please forgive me if my split syntax is incorrect

      All you need is to quote your slash:

      split('/',"$date")
      I was thinking of something similar:
      s!(\d+)/(\d+)!($1+0).'/'.($2+0)!e

      --

      flounder

Re: Reg expression on day
by jbware (Chaplain) on Dec 23, 2003 at 18:40 UTC
    Here's another way to do it, and it works for leading zeros on month and day.
    s|0(\d)/|$1/|g;
      Or more simply: s!\b0!!g.

      The PerlMonk tr/// Advocate
      Again, thanks for all the input!
Re: Reg expression on day
by Art_XIV (Hermit) on Dec 23, 2003 at 20:57 UTC

    Here's a way that makes gratuitous use of map! ;)

    use strict; while (<DATA>) { chomp; my $val = join '/', map {$_ + 0} (split /\//, $_); print "$val\n"; } __DATA__ 12/09/2004 01/15/2003 03/09/2004 10/28/2002 004/00003/02003

    Isn't Perl wonderful?

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: Reg expression on day
by delirium (Chaplain) on Dec 24, 2003 at 13:21 UTC
    Since this is already getting out of hand....

    substr($_,3,2) += 0;

    This will work only if the month always has two digits.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://316653]
Approved by Thelonius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-12-03 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (28 votes). Check out past polls.

    Notices?