fishbot_v2 has my vote for best solution within the constraints. However, if you were doing this repeatedly, using index might be an interesting approach. I've coded fishbot_v2's solution into the regex sub below. I did several runs and pasted a pretty typical result.

use Benchmark qw/:all/; sub regex { return ($_[1] =~ m/^$_[0]/) ? 1 : 0; } sub index_func { return (index($_[1], $_[0]) == 0) ? 1 : 0; } cmpthese ( 500000, { 'regex match' => sub { regex('abbr', 'abbreviation') or die }, 'index match' => sub { index_func('abbr', 'abbreviation') or die } +, 'regex miss' => sub { regex('anna', 'abbreviation') and die }, 'index miss' => sub { index_func('anna', 'abbreviation') and die +}, }); __END__ Rate regex match regex miss index miss index match regex match 680272/s -- -13% -36% -38% regex miss 781250/s 15% -- -27% -29% index miss 1068376/s 57% 37% -- -3% index match 1101322/s 62% 41% 3% --

As you can see, using index for this is much faster, regardless of whether you are more likely to match or miss.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

In reply to Re: Abbreviation regex? by radiantmatrix
in thread Abbreviation regex? by bsb

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.