I try to list all files in a directory that do not start with "Camera1", but end with ".png". For doing so, I am using a regular expression in list.files in R. To exclude "Camera1", I tried to use a negative lookahead, but it doesn't work. Where is my mistake? ;)
list.files(pathToDirectory, pattern = "^(?!Camera1).*\\.png")
I get the error: invalid 'pattern' regular expression
Thanks in advance :)
A negative lookbehind assertion asserts true if the pattern inside the lookbehind is not matched.
Lookbehind, which is used to match a phrase that is preceded by a user specified text. Positive lookbehind is syntaxed like (? <=a)something which can be used along with any regex parameter. The above phrase matches any "something" word that is preceded by an "a" word.
You probably cant perform standard negative lookaheads using grep, but usually you should be able to get equivalent behaviour using the "inverse" switch '-v'. Using that you can construct a regex for the complement of what you want to match and then pipe it through 2 greps.
A lookahead assertion has the form (?= test) and can appear anywhere in a regular expression. MATLAB® looks ahead of the current location in the text for the test condition. If MATLAB matches the test condition, it continues processing the rest of the expression to find a match.
Looks like the default engine doesn't like lookarounds, so you need to use Perl. This works:
dat <- c("Camera1.png", "Camera2.png", "hello.png", "boo")
grep("^(?!Camera1).*\\.png", dat, value=T, perl=T)
# [1] "Camera2.png" "hello.png"
But this doesn't:
grep("^(?!Camera1).*\\.png", dat, value=T)
# invalid regular expression '(?<!Camera1)\.png', reason 'Invalid regexp'
So, to do what you what you want:
grep("(?<!Camera1)\\.png", list.files(), perl=T, value=T)
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