I have a set of file-paths, and I want to list all file-paths that do not contain x/y/z/
. The other constraint is that I cannot use print if !m{x/y/z/}
since I don't have write permissions on the script. I can provide the script with regexp patterns to include (as command-line-options), i.e., everything that matches will be printed.
This is my attempt. I am trying to match all lines that do not have x/y/z/
before them.
#!/usr/bin/perl
use warnings;
use strict;
while(<DATA>) {
print if m{(?<!x/y/z/).*};
}
__DATA__
x/y/z/a/b/x.cc
x/y/z/a/b/x.cc
x/y/z/a/b/x.cc
x/y/a/b/m.cc
x/y/a/b/m.cc
My expectation is it would print only the bottom two strings, but it prints everything. But when I change the pattern to (?<=x/y/z/).*
, it is printing only the top 3 strings:
x/y/z/a/b/x.cc
x/y/z/a/b/x.cc
x/y/z/a/b/x.cc
Why so, and what should I do to fix my regexp?
What you need is a negative lookahead. So, straight from the monastery comes the answer:
#!/usr/bin/perl
use warnings;
use strict;
while(<DATA>) {
print if m{^(?!(?s:.*)x/y/z)};
}
__DATA__
x/y/z/a/b/x.cc
x/y/z/a/b/x.cc
x/y/z/a/b/x.cc
x/y/a/b/m.cc
x/y/a/b/m.cc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With