0

I have stored pattern matching functions as strings eg. string <- "grepl('a',var1)" for selecting rows of a data.table

In some cases i have to use metacharacters

dt<-data.table(var=c("a1b.c","a-b.c","a1b1c"))
dt[grepl("a.b\\.c",var),]

I don't understand what "\" especially "\." leads to in the parse function

finally

dt[eval(parse(text="grepl('a.b\\.c',var)")),] # won't work 

even so

dt[eval(parse(text="grepl('a.b[.]c',var)")),] # will work

I want to understand what parse does exactly in case the metacharcter I have to use is e.g "^".

lmo
  • 37,904
  • 9
  • 56
  • 69
  • It does work. Just add more backslashes `dt[eval(parse(text="grepl('a.b\\\\.c',var)")),] `. I'll close this as a duplicate of backslash replacement. Someone else can grill you on `evil(parse(...` – Pierre L Feb 17 '17 at 18:27
  • Thank you, great. So what happens to all those backslashes step by step? Are there certain metacharacters in the parse function? In this case "\" and "." and i have to use the backslash to keep those two characters? – Andi Kunzler Feb 17 '17 at 18:33
  • 2
    `fortunes::fortune(365)` – Rich Scriven Feb 17 '17 at 18:34

0 Answers0