1

I want to extract the two words preceding a Twitter @handle

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

Doing the following extracts all the text preceding the last @handle only, I need it for all @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 @"
JohnCoene
  • 2,107
  • 1
  • 14
  • 31

1 Answers1

3

You can use a quantifier {2} to specify how many words you want to extract before the character @. A word consists of word characters \\w+ and a word boundary, which in your case would be spaces. And we can use trimws function to remove unnecessary leading and trailing spaces:

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"
Psidom
  • 209,562
  • 33
  • 339
  • 356