gpt4 book ai didi

regex - 如何在 R 中引用超过\9 的捕获组?

转载 作者:行者123 更新时间:2023-12-05 00:23:46 24 4
gpt4 key购买 nike

在 R 中是否可以在正则表达式中捕获大于 9 的组?

sub("(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)", "\\1 & \\9",   
"abc-02-03-04-05-06-07-08-09")


[1] "abc & 09"

这是预期的结果,但是
sub("(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)", "\\1 & \\10",   
"abc-02-03-04-05-06-07-08-09-10")

[1] "abc & abc0"

失败,因为预期的结果是
[1] "abc & 10"

我需要这样的功能来实现以下功能,该功能最多适用于 9 种格式,但仅适用于:
x <- as.Date(c("2005-09-02", "2012-04-08"))

fmt <- "dddd, d.m.yy"

fmt <- gsub(pattern = "dddd", replacement = "\\\\1", x = fmt)
fmt <- gsub(pattern = "ddd", replacement = "\\\\2", x = fmt)
fmt <- gsub(pattern = "dd", replacement = "\\\\3", x = fmt)
fmt <- gsub(pattern = "d", replacement = "\\\\4", x = fmt)
fmt <- gsub(pattern = "mmmm", replacement = "\\\\5", x = fmt)
fmt <- gsub(pattern = "mmm", replacement = "\\\\6", x = fmt)
fmt <- gsub(pattern = "mm", replacement = "\\\\7", x = fmt)
fmt <- gsub(pattern = "m", replacement = "\\\\8", x = fmt)
fmt <- gsub(pattern = "yyyy", replacement = "\\\\9", x = fmt)
fmt <- gsub(pattern = "yy", replacement = "\\\\10", x = fmt)
fmt <- gsub(pattern = "y", replacement = "\\\\11", x = fmt)
fmt

sub("(.+)-(.+)-(.+)-0?(.+)-(.+)-(.+)-(.+)-0?(.+)-(.+)-(.+)-0?(.+)", fmt,
format(x, "%A-%a-%d-%d-%B-%b-%m-%m-%Y-%y-%y"))

最佳答案

重要的是要注意限制是九个反向引用;你得到无限的捕获。通过使用 str_match 来自 stringr (或者,更笨拙的是,来自基础 R 的 regmatches ),您始终可以重构代码以避免必须使用反向引用。

library(stringr)
(matches <- str_match(
"abc-02-03-04-05-06-07-08-09-10",
"(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)-(.+)")
)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## [1,] "abc-02-03-04-05-06-07-08-09-10" "abc" "02" "03" "04" "05" "06" "07" "08" "09" "10"
paste(matches[, 2], matches[, 11], sep = " & ")
## [1] "abc & 10"

关于regex - 如何在 R 中引用超过\9 的捕获组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27586521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com