我想在“帮助”一词之前和之后抓住2-3个单词
我有一段文字如下:
这就是我所做的
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的 vector ,给出了关键字前后的单词数
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/