Simple regex question

Discussion in 'Perl Misc' started by Bryan, Nov 27, 2006.

  1. Bryan

    Bryan Guest

    Hi,

    If I have this:
    my $v1 = "9"; #Needs to be "09"
    my $v2 = "01"; # Okay
    my $v3 = "3"; # Needs to be "03"
    my $v4 = "54"; #Okay

    I want to make sure that all strings have the same format, which is 2
    digits (or more) but that single digit values are padded with a '0'.
    This is for a database query that happens later.

    What is the regex to convert "1" to "01" and leaves "101" alone?
    Bryan, Nov 27, 2006
    #1
    1. Advertising

  2. Bryan wrote:
    > If I have this:
    > my $v1 = "9"; #Needs to be "09"
    > my $v2 = "01"; # Okay
    > my $v3 = "3"; # Needs to be "03"
    > my $v4 = "54"; #Okay
    >
    > I want to make sure that all strings have the same format, which is 2
    > digits (or more) but that single digit values are padded with a '0'.
    > This is for a database query that happens later.
    >
    > What is the regex to convert "1" to "01" and leaves "101" alone?


    You'd better use sprintf() for that.

    perldoc -f sprintf

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 27, 2006
    #2
    1. Advertising

  3. Bryan

    John Bokma Guest

    Bryan <> wrote:

    > Hi,
    >
    > If I have this:
    > my $v1 = "9"; #Needs to be "09"
    > my $v2 = "01"; # Okay
    > my $v3 = "3"; # Needs to be "03"
    > my $v4 = "54"; #Okay
    >
    > I want to make sure that all strings have the same format, which is 2
    > digits (or more) but that single digit values are padded with a '0'.
    > This is for a database query that happens later.
    >
    > What is the regex to convert "1" to "01" and leaves "101" alone?


    perldoc -f printf

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Nov 27, 2006
    #3
  4. Bryan

    zzzhc Guest

    s/^(\d)$/0$1/;

    "Bryan дµÀ£º
    "
    > Hi,
    >
    > If I have this:
    > my $v1 = "9"; #Needs to be "09"
    > my $v2 = "01"; # Okay
    > my $v3 = "3"; # Needs to be "03"
    > my $v4 = "54"; #Okay
    >
    > I want to make sure that all strings have the same format, which is 2
    > digits (or more) but that single digit values are padded with a '0'.
    > This is for a database query that happens later.
    >
    > What is the regex to convert "1" to "01" and leaves "101" alone?
    zzzhc, Nov 28, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saad Malik
    Replies:
    5
    Views:
    381
    John C. Bollinger
    May 2, 2005
  2. John Salerno

    a simple regex question

    John Salerno, Apr 1, 2006, in forum: Python
    Replies:
    6
    Views:
    308
    Paddy
    Apr 2, 2006
  3. johnny

    Simple Python REGEX Question

    johnny, May 11, 2007, in forum: Python
    Replies:
    4
    Views:
    409
    James T. Dennis
    May 12, 2007
  4. Replies:
    3
    Views:
    746
    Reedick, Andrew
    Jul 1, 2008
  5. Sam Kong
    Replies:
    8
    Views:
    116
    Csaba Henk
    Mar 25, 2005
Loading...

Share This Page