R/str_extract.R

Defines functions str_extract

Documented in str_extract

#' Extract strings from a vector of strings
#'
#' Using only base regex, return a matching string based on the posssibilites
#' entered in extract. Was intended to find a matching string when several
#' possibilites exist. For example, when searching str_extract(c("Case_Two",
#' "Case_One"), "One|Two"), the function will return the found regex in the
#' order they were found in the vector of strings, i.e. c("Two", "One").
#'
#' @param string character string or vector containing expressions to look for
#'
#' @param extract Regular expression to look for in string
#'
#' @export

str_extract <- function(string, extract){
  str.extract <- substring(
    string,
    regexpr(extract, string),
    regexpr(extract, string) + attr(regexpr(extract, string), "match.length") - 1
  )
  return(str.extract)
}
cfbeuchel/CarlHelpR documentation built on Dec. 24, 2019, 8:08 p.m.