Matt W. Matt W. -4 years ago 74
R Question

grep and gsub to find patterns and call out patterns

I'm trying to search text for keywords and then pull back the keywords that it finds.

right now I'm at / reproducible example:

text <- c("Here is some text about cats and dogs",
"Here is some text about turtles and chickens",
"Here is some text about rhinos and elephants")
pattern <- "cat|turtle"
HasPattern <- as.vector(NULL)
for(i in 1:length(text)){
if(sum(grepl(pattern, text[i], ignore.case = TRUE)>0))
HasPattern <- append(HasPattern, text[i])
}


The desired result for this output would be:

gsub(grepl(pattern, text), pattern, text) # or something
[1] cat turtle NA


I've tried --

for (i in 1:length(text)){
if(grepl(pattern, text[i]) == TRUE)
gsub(text[i], pattern, text[i])
}


sub(text, pattern, text)

gsub(grepl(pattern, text), pattern, text)


In the reproducible example:
HasPattern gives me a vector of the texts that have what I want which is great. But I would also like a vector of what pattern it found in those texts.

Answer Source

We can use str_extract

library(stringr)
str_extract(text, pattern)
#[1] "cat"    "turtle" NA      
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download