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
  • 1
  • 16
  • 47
  • 69
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • 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

25

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
  • 172,303
  • 28
  • 230
  • 274