Created
May 28, 2013 05:22
-
-
Save wush978/5660649 to your computer and use it in GitHub Desktop.
extract the subpattern of the perl-based regular expression
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
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