13

I'm using grep in some projects in R (which uses a perl=TRUE flag) and for the life of me I can't figure out why R keeps throwing errors. My query is as follows:

d$SomeColumn[grep("(?ix)<VNW[^;]*;(dis|dat)> \w*<N\(", d$Right, perl=TRUE)] <- 1

However, R throws the following error:

Error: '\w' is an unrecognized escape in character string starting ""<VNW[^;]*;(dis|dat)> \w"
oguz ismail
  • 39,105
  • 12
  • 41
  • 62
Bram Vanroy
  • 24,991
  • 21
  • 120
  • 214
  • 1
    @anubhava No, but why is that necessary? Isn't it escaped yet? – Bram Vanroy Dec 17 '14 at 15:10
  • 6
    It is because regex is being entered as string in double quotes. String needs one escaping and regex engine needs another escaping. e.g. `\\w` is passed to regex engine as `\w` – anubhava Dec 17 '14 at 15:13

1 Answers1

23

You need to escape the backslashes one more time in r.

d$SomeColumn[grep("(?ix)<VNW[^;]*;(dis|dat)> \\w*<N\\(", d$Right, perl=TRUE)] <- 1

                                              |     |
Avinash Raj
  • 166,785
  • 24
  • 204
  • 249