Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative look-ahead assertion in list.files in R

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 :)

like image 1000
PeteChro Avatar asked May 29 '15 17:05

PeteChro


People also ask

What is a negative Lookbehind?

A negative lookbehind assertion asserts true if the pattern inside the lookbehind is not matched.

What is Lookbehind in regex?

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.

Does grep support negative lookahead?

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.

What is lookahead assertion?

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.


1 Answers

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)
like image 180
BrodieG Avatar answered Oct 28 '22 17:10

BrodieG