Other responses have explained how you can use lookbehind to satisfy the check for "not ending in a ~". A more minor point, but worth thinking about, is the fact that since the "one or more digits" can be followed by arbitrary characters, you can replace "one or more digits" with "one digit" in the spec and still retain a functionally equivalent set of constraints::

/^test\d.*(?<!~)\z/

This insight is implicit in a couple of the responses to date, but was not mentioned explicitly in either of them.

In terms of efficiency (which may not be greatly relevant to your particular problem), the main potential problem is that when the filename does end in '~' the regexp engine will do a lot of needless backtracking trying to find some other way to match. That can be avoided with a cut operator:

/^test\d(?>.*)(?<!~)\z/
but is only likely to gain anything if the filenames can be of arbitrary length: on any O/S that limits the length of filenames to something reasonably small it is unlikely to be noticeable.

Hugo


In reply to Re: matching file names using regex by hv
in thread matching file names using regex by Anonymous Monk

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.