Created
October 7, 2021 02:53
-
-
Save jmbarbone/75f072d17b7d96ecf1673d17ba5537c8 to your computer and use it in GitHub Desktop.
base alternative for string extracts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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