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

In reply to Re: Regular Expression from Hell by eric256
in thread Regular Expression from Hell by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.