Negative look-ahead assertion in list.files in R

前端 未结 1 524
太阳男子
太阳男子 2021-02-13 18:26

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

1条回答
  •  逝去的感伤
    2021-02-13 18:44

    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 '(?

    So, to do what you what you want:

    grep("(?

    0 讨论(0)
提交回复
热议问题