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

Hi Monks.
A simple question for you, why isn't my reg.exp. code not matching to what I want, like:
#my $month=12; #exemplo my $month=7; # I just want to add 0, if it's a single digit date numbe +r if($month=~/(\d)/g) { $month=.0.$month; print "test - $month<br>"; }

And it's not matching right, why?
Thanks!

Replies are listed 'Best First'.
Re: Regular Expression
by davis (Vicar) on Aug 05, 2004 at 13:48 UTC
    Don't use regular expressions for this. Use printf or sprintf:
    my $month = 7; $month = sprintf "%02d", $month;

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
      Thank you all, good one!
Re: Regular Expression
by friedo (Prior) on Aug 05, 2004 at 13:49 UTC
    Your pattern, /(\d)/ just means "match a single digit character," which will match the first digit in any number, regardless of length. If you wanted to do this with a regex, you could say,

    if ($month =~ /^\d$/) { $month = '0' . $month; }

    Note that there is no need to capture the match with parentheses. This is a bit overkill for simply formating a number, though. Why not just use sprintf?

    $month = sprintf("%02d", $month);

    Update: Corrected typo.

Re: Regular Expression
by NetWallah (Canon) on Aug 05, 2004 at 15:39 UTC
    As usual, there is more than one way...

    Here is a simple, low-overhead way:

    my $month=7; $month < 10 and $month = '0' . $month; print $month;
    Update Oops .. Corrected, as per Dietz (++) below. Thanks.

        Earth first! (We'll rob the other planets later)

      NetWallah,
      $month < 10 and $month = '0' . $month;
      guess what's the result if my $month="07";
      =)
        Ack! - this was intended to be a simple case handler.

        If the expression starts getting any more complicated, I'd much prefer the "printf" of "sprintf" solution - that is a lot cleaner.

            Earth first! (We'll rob the other planets later)

        Fun :)
        How about:
        substr($month,0,1,"0$month") if ($month<10 && $month!~/^0/);
        Update: Fixed typo, as per tinita

        --
        Olivier
      $month > 9 and $month = '0' . $month;

      I think you meant $month < 10 and $month = '0' . $month; since 0 should be added only if number is less than 10.
      Anyway, very nice solution and I ++'d you.
Re: Regular Expression
by pbeckingham (Parson) on Aug 06, 2004 at 00:42 UTC

    While looking at this node, I saw

    $month=.0.$month;
    and wondered what that leading period was on the number. Is .0 equivalent to 0.0?
    my $s = 'abc'; $s = .0.$s; print $s, "\n";
    yields 0abc, but then I tried:
    my $n = .0; print $n, "\n";
    which yields 0D, and so does
    print .0, "\n";
    So the newline character is being converted to its ASCII code. Would someone explain this to me please?

    pbeckingham - typist, perishable vertebrate.
Re: Regular Expression
by chanio (Priest) on Aug 06, 2004 at 05:30 UTC
    $month=substr("0".$month,-2);

    .{\('v')/}
    _`(___)' __________________________