A rule of thumb would not be in the official documentation. Yes, character classes are better than wildcards.

The regex you have start with .*, which will match the entire string. It will then look for the next pattern, a minus sign. Since it's at the end of the string, it will fail. It will back up on character and look for the minus sign there. Not finding it, it will back up one more, Etc. Not very efficient.

The backslash before the minus is not needed but I tend to program defensively. A backslash before any ASCII non-letter will escape it. This is in case a new meta-character is added in the future.

You may be correct in that the regex I gave may not be more efficient in that it too has backtracking. A more efficient way would be:

$module =~ s/\-[^-]*+$//;

The extra plus sign stops backtracking. This regex would scan until a minus sign, scan non-minus-signs, and look for the end of the string. If it's not at the end, the entire pattern will fail and it will have to start over. That is, it will start the pattern from the beginning but from where it currently is in the string. It will scan the string in one pass without any backtracking.


In reply to Re^3: find module name from the module archive by shawnhcorey
in thread find module name from the module archive by Lotus1

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.