2015-08-25 50 views
2

在R中,我可以使用\\1來引用捕獲組。但是,使用stringi程序包時,這不會按預期方式工作。如何在R中使用帶有stringi包的反向引用

library(stringi) 

fileName <- "hello-you.lst" 
(fileName <- stri_replace_first_regex(fileName, "(.*)\\.lst$", "\\1")) 

[1] "1" 

預計產量:hello-you

the documentation我找不到任何有關此問題的信息。

+0

更改'\\ 1'到'$ 1',從DOC( '?stri_replace_first_regex'):引用的形式爲$ n,其中n是捕獲組的編號(它們的編號從1開始)。 – NicE

回答

3

您需要在替換字符串中使用$1而不是\\1

library(stringi) 

fileName <- "hello-you.lst" 
fileName <- stri_replace_first_regex(fileName, "(.*)\\.lst$", "$1") 

[1] "hello-you" 

docstri_*_regex使用ICU's regular expressions