gpt4 book ai didi

regex - regmatches 从 gregexpr 返回多个匹配项

转载 作者:行者123 更新时间:2023-12-02 07:23:22 24 4
gpt4 key购买 nike

我想抓取“help”这个词前后的2-3个词

我有一段文字如下:

....features and lots of greenery to help soothe the nerves...blah blah...cozy up in their plush blankets to help relax the nerves

这是我做的

x <- paste("(\\S+\\s+|^)(\\S+\\s+|)(\\S+\\s+|)", treatSym[i], ".?(\\s+\\S+|)(\\s+\\S+|$)(\\s+\\S+|$)", sep="")

matching <- gregexpr(x,text)

regmatches(text, matching, invert = FALSE)

我收到这个错误是因为我猜 length(matching) = 2。但是当只有 1 个匹配时它工作得很好。

Error in regmatches(text, matching, invert = FALSE) : 
‘x’ and ‘m’ must have the same length

有没有更好的办法把关键词前后2-3个词调出来?

最佳答案

n 是一个长度为 2 的向量,给出关键字前后的单词数

n <- c(2, 2)
x <- "....features and lots of greenery to help soothe the nerves...blah blah...cozy up in their plush blankets to help relax the nerves"

pat <- sprintf('(?:[a-z]+ ){%s}help(?: [a-z]+){%s}', n[1], n[2])
m <- gregexpr(pat, x, perl = TRUE)
regmatches(x, m)[[1]]
# [1] "greenery to help soothe the" "blankets to help relax the"

作为函数

f <- function(string, keyword, n = c(2,2)) {
# pat <- sprintf('(?:[a-z]+ ){%s}%s(?: [a-z]+){%s}', n[1], keyword, n[2])
pat <- sprintf('(?:[a-z]+ ){0,%s}%s(?: [a-z]+){0,%s}', n[1], keyword, n[2])
m <- gregexpr(pat, string, perl = TRUE)
regmatches(string, m)[[1]]
}

f(x, 'help', c(1, 2))
# [1] "to help soothe the" "to help relax the"

关于regex - regmatches 从 gregexpr 返回多个匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37199262/

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