Agrep in R - find * all * matches string (global flag)

I have a line:

string <- "I do not like green eggs and ham!" 

and pattern

 pattern <- "(egs|ham)" 

I want to know how many times pattern matches string with fuzzy matching (agrep) .

gregexpr will do this for normal compliance - I just want to know if there is a corresponding garegexpr in R or a way to emulate it without being too heavy.

( aregexec will only return the index for the first match, "eggs" and skip the "ham").

+4
source share
1 answer

You did not specify that you need the R base, so I would gladly suggest using the str_count (string, pattern) function from the Hadley Wickham stringr package.

 library(stringr) string <- "I do not like green eggs and ham!" pattern <- "(egs|ham)" str_count(string, pattern) [1] 1 

stringr is really a great R. package. Complete of all kinds of string utility.

-1
source

All Articles