在特定字符前提取 2 个术语
Extract 2 terms before specific character
我想提取 Twitter @handle 前面的两个词
x <- c("this is a @handle", "My name is @handle", "this string has @more than one @handle")
执行以下操作仅提取 last @handle 之前的所有文本,我需要它用于所有 @handles
(ext <- stringr::str_extract_all(x, "^.*@"))
[[1]]
[1] "this is a @"
[[2]]
[1] "My name is @"
[[3]]
[1] "this string has @more than one @"
您可以使用量词 {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"
我想提取 Twitter @handle 前面的两个词
x <- c("this is a @handle", "My name is @handle", "this string has @more than one @handle")
执行以下操作仅提取 last @handle 之前的所有文本,我需要它用于所有 @handles
(ext <- stringr::str_extract_all(x, "^.*@"))
[[1]]
[1] "this is a @"
[[2]]
[1] "My name is @"
[[3]]
[1] "this string has @more than one @"
您可以使用量词 {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"