Simple regex question

B

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?
 
G

Gunnar Hjalmarsson

Bryan said:
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
 
J

John Bokma

Bryan said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top