1 2re_match <- function(text, pattern, perl = TRUE, ...) { 3 4 stopifnot(is.character(pattern), length(pattern) == 1, !is.na(pattern)) 5 text <- as.character(text) 6 7 match <- regexpr(pattern, text, perl = perl, ...) 8 9 start <- as.vector(match) 10 length <- attr(match, "match.length") 11 end <- start + length - 1L 12 13 matchstr <- substring(text, start, end) 14 matchstr[ start == -1 ] <- NA_character_ 15 16 empty <- data.frame(stringsAsFactors = FALSE, .text = text)[, numeric()] 17 res <- list(match = !is.na(matchstr), groups = empty) 18 19 if (!is.null(attr(match, "capture.start"))) { 20 21 gstart <- attr(match, "capture.start") 22 glength <- attr(match, "capture.length") 23 gend <- gstart + glength - 1L 24 25 groupstr <- substring(text, gstart, gend) 26 groupstr[ gstart == -1 ] <- NA_character_ 27 dim(groupstr) <- dim(gstart) 28 29 res$groups <- cbind(groupstr, res$groups, stringsAsFactors = FALSE) 30 names(res$groups) <- attr(match, "capture.names") 31 } 32 33 res 34} 35