R – n R function to find the index of regexp matches in a character vector

matchrregex

I'm looking for an R function that works like match only for regular expression matching instead of equality. In other words, if I have a variable r that is a character vector of regular expressions and a variable x which is a character vector, I want the function to return a numeric vector the same length as r that gives, for each regular expression, the index of the first element that matches that regular expression. Note that I don't want the position within the string where the regular expression matches. I want the index of the element that matches each regular expression.

Best Answer

You're just looking for grep:

x <- c("arm","foot","lefroo", "bafoobar")
r <- c("ar","^ba","$m","foo")

Get the first regex match index:

sapply(r, function(y) grep(y,x)[1])

 ar ^ba  $m foo 
  1   4  NA   2 

Get all regex match indexes:

sapply(r, function(y) grep(y,x))

$ar
[1] 1 4 # returns matches of ar in x

$`^ba`
[1] 4

$`$m`
integer(0)

$foo
[1] 2 4