How to use back reference with stringi package?

妖精的绣舞 提交于 2019-12-01 18:36:31

问题


In R I can use \\1 to reference to a capturing group. However, when using the stringi package, this doesn't work as expected.

library(stringi)

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

[1] "1"

Expected output: hello-you.

In the documentation I couldn't find anything concerning this problem.


回答1:


You need to use $1 instead of \\1 in the replacement string:

library(stringi)

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

[1] "hello-you"

From the doc, stri_*_regex uses ICU's regular expressions



来源:https://stackoverflow.com/questions/32207958/how-to-use-back-reference-with-stringi-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!