Skip to content

Instantly share code, notes, and snippets.

@wush978
Created May 28, 2013 05:22
Show Gist options
  • Save wush978/5660649 to your computer and use it in GitHub Desktop.
Save wush978/5660649 to your computer and use it in GitHub Desktop.
extract the subpattern of the perl-based regular expression
extract_subpattern <- function(text, pattern) {
gregexpr.result <- gregexpr(pattern=pattern, text=text, perl=TRUE)
retval <- list()
for(i in 1:length(gregexpr.result)) {
result <- gregexpr.result[[i]]
retval[[i]] <- list()
for(name in attr(result, "capture.names")) {
start <- attr(result, "capture.start")[,name]
len <- attr(result, "capture.length")[,name]
retval[[i]][[name]] <- substr(text[i], start=start, stop = start + len - 1)
}
}
retval
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment