使用多个模式从 list & return 中包含的一些但不是所有字符串中提取并组合多个子字符串以在 R 中列出
Extract & combine multiple substrings using multiple patterns from some but not all strings contained in list & return to list in R
我想找到一种优雅且易于操作的方式来:
- 从一些但不是全部字符串中提取多个子字符串
作为列表元素包含的(每个列表元素仅包含一个长字符串)
- 用这些多个子字符串替换各自的原始长字符串
- 将每个列表元素中的子字符串折叠成 1 个字符串
- return 一个相同长度的列表,其中包含适当的替换子字符串和未修改的长字符串。
这个问题是我之前的问题 的后续问题(尽管有所不同)。请注意,我不想 运行 正则表达式模式超过 所有 列表元素,只有那些正则表达式适用的元素。
我知道 str_replace
或 sub
可以通过匹配要更改的整个字符串并 returning 捕获组捕获的文本来交付最终结果,如下所示:
library(stringr)
myList <- as.list(c("OneTwoThreeFourFive", "mnopqrstuvwxyz", "ghijklmnopqrs", "TwentyTwoFortyFourSixty"))
fileNames <- c("AB1997R.txt", "BG2000S.txt", "MN1999R.txt", "DC1997S.txt")
names(myList) <- fileNames
is1997 <- str_detect(names(myList), "1997")
regexp <- ".*(Two).*(Four).*"
myListNew2 <- myList
myListNew2[is1997] <- lapply(myList[is1997], function(i) str_replace(i, regexp, "\1££\2"))
## This does return what I want:
myListNew2
$AB1997R.txt
[1] "Two££Four"
$BG2000S.txt
[1] "mnopqrstuvwxyz"
$MN1999R.txt
[1] "ghijklmnopqrs"
$DC1997S.txt
[1] "Two££Four"
但我更愿意这样做而不必匹配整个原始文本(因为,例如,匹配很长的文本需要时间;多个正则表达式模式的复杂性以及将它们编织在一起以匹配整个字符串的难度成功地)。我想使用单独的正则表达式模式来提取子字符串,然后用这些提取物替换原始字符串。我想出了以下方法,它有效。但肯定有更简单、更好的方法! llply
?
patternA <- "Two"
patternB <- "Four"
x <- myList[is1997]
x2 <- unlist(x)
stringA <- str_extract (x2, patternA)
stringB <- str_extract (x2, patternB)
x3 <- mapply(FUN=c, stringA, stringB, SIMPLIFY=FALSE)
x4 <- lapply(x3, function(i) paste(i, collapse = "££"))
x5 <- relist(x4,x2)
myListNew1 <- replace(myList, is1997, x5)
myListNew1
$AB1997R.txt
[1] "Two££Four"
$BG2000S.txt
[1] "mnopqrstuvwxyz"
$MN1999R.txt
[1] "ghijklmnopqrs"
$DC1997S.txt
[1] "Two££Four"
可能是这样的,我扩展了您正在寻找的模式以展示它如何变得适应性强:
library(stringr)
patterns <- c("Two","Four","Three")
hits <- lapply(myList[is1997], function(x) {
out <- sapply(patterns, str_extract, string=x)
paste(out[!is.na(out)],collapse="££")
})
myList[is1997] <- hits
#[[1]]
#[1] "Two££Four££Three"
#
#[[2]]
#[1] "mnopqrstuvwxyz"
#
#[[3]]
#[1] "ghijklmnopqrs"
#
#[[4]]
#[1] "Two££Four"
提取多个匹配项并组合成字符串
library(stringi)
patterns <- 'Two|Three|Four'
hits <- stri_join_list(stri_extract_all_regex(myList[is1997],patterns),sep = '££')
myList[is1997] <- hits
我想找到一种优雅且易于操作的方式来:
- 从一些但不是全部字符串中提取多个子字符串 作为列表元素包含的(每个列表元素仅包含一个长字符串)
- 用这些多个子字符串替换各自的原始长字符串
- 将每个列表元素中的子字符串折叠成 1 个字符串
- return 一个相同长度的列表,其中包含适当的替换子字符串和未修改的长字符串。
这个问题是我之前的问题
我知道 str_replace
或 sub
可以通过匹配要更改的整个字符串并 returning 捕获组捕获的文本来交付最终结果,如下所示:
library(stringr)
myList <- as.list(c("OneTwoThreeFourFive", "mnopqrstuvwxyz", "ghijklmnopqrs", "TwentyTwoFortyFourSixty"))
fileNames <- c("AB1997R.txt", "BG2000S.txt", "MN1999R.txt", "DC1997S.txt")
names(myList) <- fileNames
is1997 <- str_detect(names(myList), "1997")
regexp <- ".*(Two).*(Four).*"
myListNew2 <- myList
myListNew2[is1997] <- lapply(myList[is1997], function(i) str_replace(i, regexp, "\1££\2"))
## This does return what I want:
myListNew2
$AB1997R.txt
[1] "Two££Four"
$BG2000S.txt
[1] "mnopqrstuvwxyz"
$MN1999R.txt
[1] "ghijklmnopqrs"
$DC1997S.txt
[1] "Two££Four"
但我更愿意这样做而不必匹配整个原始文本(因为,例如,匹配很长的文本需要时间;多个正则表达式模式的复杂性以及将它们编织在一起以匹配整个字符串的难度成功地)。我想使用单独的正则表达式模式来提取子字符串,然后用这些提取物替换原始字符串。我想出了以下方法,它有效。但肯定有更简单、更好的方法! llply
?
patternA <- "Two"
patternB <- "Four"
x <- myList[is1997]
x2 <- unlist(x)
stringA <- str_extract (x2, patternA)
stringB <- str_extract (x2, patternB)
x3 <- mapply(FUN=c, stringA, stringB, SIMPLIFY=FALSE)
x4 <- lapply(x3, function(i) paste(i, collapse = "££"))
x5 <- relist(x4,x2)
myListNew1 <- replace(myList, is1997, x5)
myListNew1
$AB1997R.txt
[1] "Two££Four"
$BG2000S.txt
[1] "mnopqrstuvwxyz"
$MN1999R.txt
[1] "ghijklmnopqrs"
$DC1997S.txt
[1] "Two££Four"
可能是这样的,我扩展了您正在寻找的模式以展示它如何变得适应性强:
library(stringr)
patterns <- c("Two","Four","Three")
hits <- lapply(myList[is1997], function(x) {
out <- sapply(patterns, str_extract, string=x)
paste(out[!is.na(out)],collapse="££")
})
myList[is1997] <- hits
#[[1]]
#[1] "Two££Four££Three"
#
#[[2]]
#[1] "mnopqrstuvwxyz"
#
#[[3]]
#[1] "ghijklmnopqrs"
#
#[[4]]
#[1] "Two££Four"
提取多个匹配项并组合成字符串
library(stringi)
patterns <- 'Two|Three|Four'
hits <- stri_join_list(stri_extract_all_regex(myList[is1997],patterns),sep = '££')
myList[is1997] <- hits