G'day learner@perl,

Firstly, it looks like your initial problems have been pointed out be Happy-the-monk. Just adding a little more detail:

To match filenames starting with CHECK and ending with .txt, this would be fine:

/^CHECK.*[.]txt$/

To exclude those with A1 before the .txt, you can insert what's known as a zero-width negative look-behind assertion which looks like (?<!pattern). The name's quite a mouthful but don't be put off by that: it's just a name. Take a look at Look-Around Assertions in perlre - Extended Patterns for a more detailed description (as well as similar positve and look-ahead assertions).

So, we want to assert that the pattern matches if we look-behind .txt and don't find (negative) A1. We write that like (?<!A1) and place it between /^CHECK.* and [.]txt$/ where it effectively takes up no space (zero-width). That gives:

/^CHECK.*(?<!A1)[.]txt$/

You haven't made it clear whether there must be any characters between CHECK and .txt; if so, change zero or more non-newline characters (.*) to one or more non-newline characters (.+).

Here's my test:

$ perl -Mstrict -Mwarnings -E ' my @test_filenames = qw{ CHECK_CH.ABC12_A1.txt CHECK_CH.ABC12.txt HECK_CH.ABC12_A1.txt HECK_CH.ABC12.txt CHECK_CH.A1.txt CHECK_CH..txt CHECK.txt CHECKA1.txt CHECK.ABC12.txt }; say "*** May be nothing between CHECK & .txt ***"; for (@test_filenames) { next unless /^CHECK.*(?<!A1)[.]txt$/; say; } say "*** Must be something between CHECK & .txt ***"; for (@test_filenames) { next unless /^CHECK.+(?<!A1)[.]txt$/; say; } ' *** May be nothing between CHECK & .txt *** CHECK_CH.ABC12.txt CHECK_CH..txt CHECK.txt CHECK.ABC12.txt *** Must be something between CHECK & .txt *** CHECK_CH.ABC12.txt CHECK_CH..txt CHECK.ABC12.txt

-- Ken


In reply to Re: how to check whether a particular word exists in filename by kcott
in thread how to check whether a particular word exists in filename by learner@perl

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.