0

I need to use a regex in combination with non-standard evaluation.

The following works fine:

library(stringr)
> str_replace("2.5", "\\.", ",") # without non-standard evaluation
[1] "2,5"

> eval(parse(text = 'str_replace("2.5", ".", ",")')) # with non-standard evaluation
[1] ",.5"

The following does not work:

> eval(parse(text = 'str_replace("2.5", "\\.", ",")'))
Error: '\.' is an unrecognized escape in character string starting ""\."

I was thinking that I need to escape the backslash itself, however, this doesn't seem to work either:

> eval(parse(text = 'str_replace("2.5", "\\\.", ",")'))
Error: '\.' is an unrecognized escape in character string starting "'str_replace("2.5", "\\\."
Annerose N
  • 477
  • 6
  • 14

1 Answers1

0

The solution is to double-escape the backslash inside the non-standard evaluation:

> eval(parse(text = 'str_replace("2.5", "\\\\.", ",")'))
[1] "2,5"
Annerose N
  • 477
  • 6
  • 14