I don't think that it's necessarily a function of the pattern. It's a function of the process.

For instance, if there's a pattern that's more preferable to use, no matter if it's the shortest, or longest pattern, it should be tested first.

In much of the work that I do, I test against the patterns that are most likely to match, and then test all of the outlying conditions that I still know how to handle.

Typically, I find that longer regexes, although they might be more time consuming to match, are a more precise fit, and so I'd qualify them as 'better' in the types of processes that I deal with.

A few metrics that I can think of that I might use would be:

There's probably dozens of other metrics ... I'd have to judge based on the process. Typically, I manually order things -- if regexA will match everything that regexB will match, but regexB won't match as much as regexA, I'd have to decide if there are different actions to be taken by the two, or if one outcome is preferable to the other.

Oh -- and a minor nitpick. /*/ matches an asterisk. /.*/ is zero or more characters (non-newlines, unless you add modifiers).


In reply to Re: Most specific pattern by jhourcle
in thread Most specific pattern by thor

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.