如何在stringi包中使用反向引用?

Bra*_*roy 5 backreference r stringi

在RI中可以\\1用来引用捕获组。但是,在使用stringi软件包时,此操作无法按预期工作。

library(stringi)

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

[1] "1"
Run Code Online (Sandbox Code Playgroud)

预期输出:hello-you

文档中,我找不到与该问题有关的任何内容。

Nic*_*icE 5

您需要使用$1而不是\\1替换字符串:

library(stringi)

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

[1] "hello-you"
Run Code Online (Sandbox Code Playgroud)

文档中stri_*_regex使用ICU的正则表达式