【问题标题】:How to use back reference with stringi package?如何在 stringi 包中使用反向引用?
【发布时间】:2015-08-25 15:22:16
【问题描述】:

在 R 中,我可以使用 \\1 来引用捕获组。但是,当使用 stringi 包时,这并没有按预期工作。

library(stringi)

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

[1] "1"

预期输出:hello-you

the documentation 我找不到任何关于这个问题的信息。

【问题讨论】:

  • 从文档 (?stri_replace_first_regex) 中将 \\1 更改为 $1:引用的格式为 $n,其中 n 是捕获组的编号(它们的编号从 1 开始)。

标签: r backreference stringi


【解决方案1】:

您需要在替换字符串中使用$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2014-10-07
    • 1970-01-01
    • 2011-09-06
    • 2010-09-06
    • 2012-10-30
    • 2021-08-07
    相关资源
    最近更新 更多