in reply to how to check whether a particular word exists in filename

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