1

Using Path::Tiny. It has a method: children which could be used as:

@paths = path("/tmp")->children( qr/\.txt$/ );

If an optional qr// argument is provided, it only returns objects for child names that match the given regular expression. Only the base name is used for matching

So the

@paths = path("/tmp")->children( qr/\A(foo|bar|baz)\z/ );

will return either foo bar baz files found in the /tmp.

I need the opposite, want any file but NOT the (foo|bar|baz) ones.

The following could filter out the unwanted ones,

@paths = grep { $_->basename !~ /^(foo|bar|baz)$/ } path("/tmp")->children;

but wondering how to write the qr for it.

So, the question: How to create the qr expression for: any file but not foo|bar|baz?

I tried the negative lookahead

path(...)->children( qr/\A (?!(foo|bar|baz)) \z/x );

but, it doesn't do what I think it should do :) - e.g. my regex is wrong.

kobame
  • 5,686
  • 2
  • 28
  • 57
  • http://stackoverflow.com/q/164414/1331451 woruld work, but it's weird. You are almost right with your negative lookahead, but you need to match something after the lookahead, like three of anything: `(?!(foo|bar|baz))...` – simbabque Mar 29 '17 at 12:30

1 Answers1

4

Put the \z inside the negative look-ahead

qr/\A(?!(?:foo|bar|baz)\z)/
Borodin
  • 123,915
  • 9
  • 66
  • 138