Skip to content

Instantly share code, notes, and snippets.

@jmbarbone
Created October 7, 2021 02:53
Show Gist options
  • Save jmbarbone/75f072d17b7d96ecf1673d17ba5537c8 to your computer and use it in GitHub Desktop.
Save jmbarbone/75f072d17b7d96ecf1673d17ba5537c8 to your computer and use it in GitHub Desktop.
base alternative for string extracts
# Not as quick as stringr but only base, so...
string_extract_all <- function(x, pattern, perl = FALSE, ignore.case = FALSE) {
re <- gregexpr(pattern, x, perl = perl, ignore.case = ignore.case)
mapply(
function(xi, rei, lens) {
substring(xi, rei, rei + lens - 1L)
},
xi = x,
rei = re,
lens = lapply(re, attr, "match.length"),
SIMPLIFY = FALSE,
USE.NAMES = FALSE
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment