Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find matching patterns from list of patterns using grepl

I used grepl to check whether a string contains any of the patterns from a set of patterns (I used '|' to separate the patterns). Reverse search didn't help. How to identify the set of patterns that match?

Additional information: This can be solved by writing a loop, but it is very time consuming as my set has > 100,000 strings. Can it be optimized?

Eg: Let the string be a <- "Hello"

pattern <- c("ll", "lo", "hl")

pattern1 <- paste(pattern, collapse="|") # "ll|lo|hl"

grepl(a, pattern=pattern1) # returns TRUE

grepl(pattern, pattern=a) # returns FALSE 'n' times - n is 3 here
like image 526
Naveen Mathew Avatar asked Feb 10 '23 12:02

Naveen Mathew


1 Answers

You are looking for str_detect from package stringr:

library(stringr)

str_detect(a, pattern)
#[1]  TRUE  TRUE FALSE

In case you have multiple strings like a = c('hello','hola','plouf') you can do:

lapply(a, function(u) pattern[str_detect(u, pattern)])
like image 75
Colonel Beauvel Avatar answered Feb 12 '23 03:02

Colonel Beauvel