Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use back reference with stringi package?

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.

like image 447
Bram Vanroy Avatar asked Aug 25 '15 15:08

Bram Vanroy


1 Answers

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

like image 120
NicE Avatar answered Oct 22 '22 08:10

NicE