0

我需要结合非标准评估使用正则表达式。

以下工作正常:

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"

以下不起作用:

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

我在想我需要转义反斜杠本身,但是,这似乎也不起作用:

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

1 回答 1

0

解决方案是在非标准评估中双重转义反斜杠:

> eval(parse(text = 'str_replace("2.5", "\\\\.", ",")'))
[1] "2,5"
于 2019-10-07T11:57:24.903 回答