1

我想提取 Twitter @handle 前面的两个词

x <- c("this is a @handle", "My name is @handle", "this string has @more than one @handle")

执行以下操作仅提取最后一个@handle 之前的所有文本,我需要所有 @handle

(ext <- stringr::str_extract_all(x, "^.*@"))
[[1]]
[1] "this is a @"

[[2]]
[1] "My name is @"

[[3]]
[1] "this string has @more than one @"
4

1 回答 1

3

您可以使用量词{2}来指定要在字符之前提取多少个单词@。一个单词由单词字符\\w+和单词边界组成,在您的情况下是空格。我们可以使用trimws函数来删除不必要的前导和尾随空格:

library(stringr)
lapply(str_extract_all(x, "(\\w+\\s+){2}(?=@)"), trimws)

#[[1]]
#[1] "is a"

#[[2]]
#[1] "name is"

#[[3]]
#[1] "string has" "than one"
于 2016-10-16T02:56:54.790 回答