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

Hi There:
I tried every possible combination for this to work and I can;t get it, can someone explain what is going on here, or why it's not working.Thank you!!!
my $test = "21234"; #if($test=~/(\d+){4}/) #if($test=~/\d\d\d\d{4}/) #if($test=~/\d\d\d\d/) #if($test=~/\d\d\d\d{4}/) #if($test=~/^(\d+){4}?/) if($test=~/\d+{4}/) { print "<td height=20 class=grtxt>0$test</td>\n"; } else { print "<td height=20 class=grtxt>$test</td>\n"; }

Where is the problem, please, thank you!

Replies are listed 'Best First'.
Re: Regular Expression from Hell
by Abigail-II (Bishop) on Apr 15, 2004 at 15:32 UTC
    You aren't anchoring, that's what's wrong with the regex. But you shouldn't use a regex in the first place.
    printf "<td height=20 class=grtxt>%05d</td>\n" => $test;
    will do fine.

    Abigail

Re: Regular Expression from Hell
by simonm (Vicar) on Apr 15, 2004 at 15:31 UTC
    I think you want /\d{4}/. You can use either the + modifier or {} to indicate quantities; there's no need for both.
Re: Regular Expression from Hell
by perlmonkey (Hermit) on Apr 15, 2004 at 15:34 UTC
    I think what you are looking for is:
    if( $test =~ /^\d{4}$/ ) { ... }
    This will match:
    '^': anchor start of string
    \d{4}: exactly 4 digits
    $: anchor end of string

    if you know it is always going to be digits, then length would probably be eaiser:
    if( length($test) == 4 ) { ... }
Re: Regular Expression from Hell
by eric256 (Parson) on Apr 15, 2004 at 15:35 UTC

    Its hard to tell what you want since you didn't tell us specificaly. It appears however than you want to put the 0 on the front if there are only for digits? You could match for 5 and reverse your if statment or do the following.

    my $test = "2123"; if($test=~/^\d{4}$/) { print "<td height=20 class=grtxt>0$test</td>\n"; } else { print "<td height=20 class=grtxt>$test</td>\n"; }

    I removed the + since plus means 1 or more and your {4} means four. I then added ^ to the start and $ to the end, that means the regex will only match if it matches the whoel line. So it will only be happy if the string is 4 digits exactly. I'm still not sure this is the best solution since you repeated your HTML without cause. It would be far better to modify $test or have a second variable to hold the output string.

    my $test = "2523"; $test = "0" . $test if($test=~/^\d{4}$/); print "<td height=20 class=grtxt>$test</td>\n";

    ___________
    Eric Hodges
      Thanks very much, it worked now!!!!!!!!!
Re: Regular Expression from Hell
by pelagic (Priest) on Apr 15, 2004 at 15:32 UTC
    What's the target of that operation? Just add leading 0's with 5 digits?

    pelagic
      Is to say if a ZIP number is coming in with 4 digits only add a "0" at the begging. Else if it already has 5 digits leave it alone.
        printf '%05d', $test;

        pelagic
Re: Regular Expression from Hell
by gmpassos (Priest) on Apr 15, 2004 at 20:20 UTC
    Take a look at perlre, you are using wrong the syntax!
    \d ## means 1 digit. \d\d ## means 2 digits. \d+ ## means 1 or more digits \d{2,} ## 2 or more \d{3} ## means 3 digits.
    And what you are doing just doesn't exists as you think:
    \d+{4} ## means one or more digits and the text "{4}".
    Actually your REGEXP doesn't compile! I just don't know from where you got the idea of \d+ with {4} after it. And when you use it right you put a lot of \d before it: \d\d\d\d{4}. Note that "\d\d" already means 2 digits, and \d\d\d\d already means what you want (check if the string have only 4 digits) but you put {4} in the last, what means 7 digits!

    See perlre, and don't forget that perldoc.com is your friend, so, you can call it every time.

    Good luck.

    Graciliano M. P.
    "Creativity is the expression of the liberty".

Re: Regular Expression from Hell
by Beechbone (Friar) on Apr 16, 2004 at 21:39 UTC
    What happens if the number has just 3 digits?
    my $test = 123; while (length($test) < 5) { $test = '0' . $test; }
    or
    my $test = 123; $test = ('0' x (5-length($test))) . $test;

    Search, Ask, Know