/\b(K|S)\d{2}?$expression\b/;

That will match a string like ".NO.K74apmd" because it is simply looking for a word boundary before the K or S. You should anchor it to the beginning of the string instead.

/^(K|S)\d{2}?$expression\b/;

Also, that \b at the end of the pattern probably isn't really what you want. It might match something like "K74apmd.orig" for example. You probably mean to anchor that to the end of the string.

/^(K|S)\d{2}?$expression$/;

Oh, and a couple other things too... Although it won't really result in a noticeable performance hit in this case, as a matter of style you should probably avoid alternation when a character class will do. Finally, that ? is telling your {2} to be non-greedy, but a non-greedy absolute quantifier doesn't make much sense. It's like saying, "match exactly 2 but as few as possible."

Those changes leave us with:

/^[KS]\d{2}$expression$/;
-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Getting a Regex Not to Match Again by sauoq
in thread Getting a Regex Not to Match Again by dru145

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.