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

Good Day Monks, Here is the problem that I am facing: Users are prompted to intput their account numbers which should be a 9 digits, though account numbers are being entered with less than 9 digits. By design I have the ability to "re-format" the entered account number with a single regular expression. With this stated, is there a way in a single regular expression to check the incoming account number for length (in my case 9 digits) and if it is less than that number (e.g., 9 digits) "pad" the beginning with zeros??? Any input will be appreciated!!! THANKS!!!
  • Comment on adding characters via a regular expression

Replies are listed 'Best First'.
Re: adding characters via a regular expression
by Abigail-II (Bishop) on Apr 21, 2004 at 16:48 UTC
    Uhm, you only have to account for the possibility of numbers less than 9 characters being entered? Noone enters 10 digits? Noone enters letters or spaces? If the only possible error is to not have enough digits, no regex is required. Just do:
    $number = sprintf "%09d" => $number;

    Abigail

Re: adding characters via a regular expression
by davido (Cardinal) on Apr 21, 2004 at 16:54 UTC
    There are several ways.

    Here's an explicit way using length and treating the 'number' like a string:

    use strict; use warnings; my $number = 123; my $pad = 9 - length $number; $number = '0' x $pad . $number; print "$number\n";

    The sprintf approach is tidier:

    use strict; use warnings; my $number = 123; $number = sprintf "%09u", $number; print "$number\n";

    Update: When only a RE will do, here's a regex approach with the s/// operator. It's not as clean as the sprintf approach, but you may have your reasons for using it:

    $number = 123; $number =~ s/(\d{0,9})$/'0' x (9 - length $1) . $1/e; print "$number\n";

    Update 2: It's been said that it can't be done with a m// regex, and I suppose for a pure regular expression implementation that's true. But I couldn't just leave it at that, so here's a m// regex approach that works by taking liberties with what is being bound to the m// operator:

    my $number = 123; print "$number\n" if ($number) = ( '0' x 9 . $number ) =~ m/(\d{9})$/;


    Dave

Re: adding characters via a regular expression
by haoess (Curate) on Apr 21, 2004 at 17:00 UTC

    Assumung users are entering letters, whitespace and other stuff, and you want only the first numbers (maximal 9), use

    $number =~ s/.*?(\d{1,9}).*/sprintf "%09d", "$1"/e;

    This does not work if users enters no digit. The best way to handle this is to perform not just a single check:

    my $number = <STDIN>; if( $number !~ /\d/ ) { print "no number"; } else { # clean up and pad input }

    -- Frank

Re: adding characters via a regular expression
by BrowserUk (Patriarch) on Apr 21, 2004 at 17:44 UTC
    my $number = 1; ($number) = "000000000$number" =~ m[(.{9})$]; print $number; 000000001

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: adding characters via a regular expression
by diotalevi (Canon) on Apr 21, 2004 at 16:47 UTC
    No, not if you are only given a regexp. You could do this if you were given access to the right side of a s/// operation.