Skip to content

Instantly share code, notes, and snippets.

@wush978
Created June 22, 2014 01:15
Show Gist options
  • Save wush978/1de5f8027fc0e6da682a to your computer and use it in GitHub Desktop.
Save wush978/1de5f8027fc0e6da682a to your computer and use it in GitHub Desktop.
gregexpr.extract_subpattern <- function(pattern, text, pattern.name) {
r <- gregexpr(pattern, text, perl=TRUE)
sapply(seq_along(r), function(i) {
if (r[[i]] == -1) return("")
a <- attributes(r[[i]])
stopifnot(isTRUE(all.equal(pattern.name, a$capture.names)))
substring(text[i], a$capture.start, a$capture.start + a$capture.length - 1)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment