Kiwi Kiwi - 1 month ago 11
R Question

Need to extract whole sentences which begins with a specific word in R

I need to extract whole sentences which begins with a specific word in R. Below is the code which i am trying to use but not able to get the desired result. I am new to regular expression concept in R. I want to extract the sentences which begins with word 'database'.

sent <- c("database connection","connection database fail", "fail connection database","database connection is good")
m <- gregexpr('database.*', sent)
regmatches(sent, m)


Above code gives me the remaining words after word 'database'. But my desired output is:

"database connection", "database connection is good"


Thanks for your help!

Answer

With stringr

sent <- c("database connection","connection database fail", "fail connection database","database connection is good")
stringr::str_subset(sent, "^database.*")
#> [1] "database connection"         "database connection is good"

With base R :

sent <- c("database connection","connection database fail", "fail connection database","database connection is good")
grep("^database.*", sent, value = T)
#> [1] "database connection"         "database connection is good"