MysticRenge
MysticRenge

Reputation: 395

Finding the position of a word in string

text = "the nurse was very helpful indeed"

I want to search for "very" and then extract "very" and one word after "very"

the output should be : very helpful

I used :

word(text, start = 4, end= 5 sep = fixed(" ")) 

The is helpful for extracting but I am unable to get the value dynamically for position of the word in the string which is "4" in this case

Upvotes: 2

Views: 2588

Answers (1)

akrun
akrun

Reputation: 887971

We can use the str_extract from stringr

library(stringr)
str_extract(text, "very\\s+\\w+")

Or with regexpr/regmatches from base R

regmatches(text, regexpr("very\\s+\\w+", text))

Upvotes: 3

Related Questions