Reputation: 1
I have data that contains a list of tweets like so:
"what is up", "RT @lolol wassup", "RT @joe pls help me"
I want to be able to extract the strings that start with RT @, and store it in another list. I'm currently using this:
str_extract(data, "^RT[:space:]+@[:graph:]+")
But that only extracts the "RT @name" part of the string, not the entire tweet. Can't figure out what's wrong with the regex. Thank you for the help!
Upvotes: 0
Views: 963
Reputation: 389335
str_extract
extracts the pattern you specify. To get the complete tweet when a pattern is supplied use str_subset
.
library(stringr)
result <- str_subset(data, "^RT[:space:]+@[:graph:]+")
result
#[1] "RT @lolol wassup" "RT @joe pls help me"
Upvotes: 1
Reputation: 887981
We can use grep
to extract the full string that starts (^
) with 'RT' followed by a space and @
in base R
grep('^RT @', data, value = TRUE)
#[1] "RT @lolol wassup" "RT @joe pls help me"
data <- c("what is up", "RT @lolol wassup", "RT @joe pls help me")
Upvotes: 2