5

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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • Change the `\\1` to `$1`, from the doc (`?stri_replace_first_regex`): References are of the form $n, where n is the number of the capture group (their numbering starts from 1). – NicE Aug 25 '15 at 15:24

1 Answers1

5

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

NicE
  • 21,165
  • 3
  • 51
  • 68