Also, your regular expression has some issues. Let's improve it incrementally. The value I get in $label at the current time is Mon 01/15/2001. Assuming that's correct, let's patch this up:

  1. You have two forward slashes '/' escaped. Rather than escape them, change the delimeters on the substitution for clarity:
    $label=~ s#.*\s(\d+)/(\d+)/#\1\2#;
    Right off the bat, we have a huge improvement in terms of "readability".
  2. \1 and \2 should not be in the right side of a substitution. They are used within a regular expression to identify a backreference. This implies to me that you are not using warnings. If you are, you get a message like "\1 better written as $1." Let's go ahead and change those:
    $label=~ s#.*\s(\d+)/(\d+)/#$1$2#;
    Not only does that eliminate the warning, it gets rid of a couple of escapes that might obscure the meaning to a novice programmer.
  3. And finally (you old-timers knew I was going to bring this up :-), you have a dot star '.*' in your regex. While dot star is sometimes useful (when you're slurping up the rest of a string, for example), this combination usually kills the efficiency of a regular expression. My usual comment: see Death to Dot Star! for details of this. Change the dot star to a negated character class (or a non-whitespace metacharacter '\S', in this case). If you are iterating over this expression, you may see a significant performance increase.
    $label=~ s#\S*\s(\d+)/(\d+)/#$1$2#;

We went from this:

s/.*\s(\d+)\/(\d+)\//\1\2/;
To this:
s#\S*\s(\d+)/(\d+)/#$1$2#;
It might not seem like much for such a small regex, but as they get more complicated, such small changes can make a world of difference.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid - let's improve your regex) Re: Directory by Ovid
in thread Directory by Robert

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.